forked from Foxy/foxy-elements
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponsive.ts
41 lines (33 loc) · 1.26 KB
/
responsive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Constructor, LitElement } from 'lit-element';
import debounce from 'lodash-es/debounce';
const breakpoints = Object.entries({ sm: 640, md: 768, lg: 1024, xl: 1280 });
export const ResponsiveMixin = <TBase extends Constructor<LitElement>>(
BaseElement: TBase
): TBase => {
return class ResponsiveElement extends BaseElement {
private __removeBreakpoints?: () => void;
private __handleResize = debounce((entries: ResizeObserverEntry[]) => {
entries.forEach(({ contentRect, target }) => {
breakpoints.forEach(([name, minWidth]) => {
if (contentRect.width >= minWidth) {
if (!target.hasAttribute(name)) target.setAttribute(name, '');
} else {
if (target.hasAttribute(name)) target.removeAttribute(name);
}
});
});
}, 16);
connectedCallback(): void {
super.connectedCallback();
const observer = new ResizeObserver(this.__handleResize);
observer.observe(this);
this.__removeBreakpoints = () => observer.disconnect();
}
disconnectedCallback(): void {
super.disconnectedCallback();
this.__removeBreakpoints?.();
this.__handleResize.cancel();
}
};
};