@fluid-topics/ft-wc-utils 1.3.12 → 1.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/directives/index.d.ts +1 -0
- package/build/directives/index.js +1 -0
- package/build/directives/whenSize.d.ts +24 -0
- package/build/directives/whenSize.js +55 -0
- package/build/globals.min.js +23 -16
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./whenSize";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./whenSize";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AsyncDirective, PartInfo } from "lit/async-directive.js";
|
|
2
|
+
import { Part } from "lit";
|
|
3
|
+
export declare enum Breakpoint {
|
|
4
|
+
sm = "sm",
|
|
5
|
+
md = "md",
|
|
6
|
+
lg = "lg",
|
|
7
|
+
xl = "xl"
|
|
8
|
+
}
|
|
9
|
+
export declare class WhenSizeDirective extends AsyncDirective {
|
|
10
|
+
private size;
|
|
11
|
+
private lastArgs?;
|
|
12
|
+
constructor(partInfo: PartInfo);
|
|
13
|
+
disconnected(): void;
|
|
14
|
+
reconnected(): void;
|
|
15
|
+
private readonly onResize;
|
|
16
|
+
update(_: Part, [threshold, aboveRenderer, belowRenderer]: Parameters<WhenSizeDirective["render"]>): unknown;
|
|
17
|
+
render(thresholdOrBreakpoint: number | Breakpoint, aboveRenderer: () => unknown, maybeBelowRenderer?: () => unknown): unknown;
|
|
18
|
+
private resolveThreshold;
|
|
19
|
+
}
|
|
20
|
+
export interface WhenSizeDirectiveFn {
|
|
21
|
+
(threshold: number, aboveRenderer: () => unknown, belowRenderer?: () => unknown): unknown;
|
|
22
|
+
(breakpoint: Breakpoint, aboveRenderer: () => unknown, belowRenderer?: () => unknown): unknown;
|
|
23
|
+
}
|
|
24
|
+
export declare const whenSize: WhenSizeDirectiveFn;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { AsyncDirective, directive } from "lit/async-directive.js";
|
|
2
|
+
import { nothing } from "lit";
|
|
3
|
+
export var Breakpoint;
|
|
4
|
+
(function (Breakpoint) {
|
|
5
|
+
Breakpoint["sm"] = "sm";
|
|
6
|
+
Breakpoint["md"] = "md";
|
|
7
|
+
Breakpoint["lg"] = "lg";
|
|
8
|
+
Breakpoint["xl"] = "xl";
|
|
9
|
+
})(Breakpoint || (Breakpoint = {}));
|
|
10
|
+
const BREAKPOINT_THRESHOLDS = {
|
|
11
|
+
sm: 640,
|
|
12
|
+
md: 768,
|
|
13
|
+
lg: 1024,
|
|
14
|
+
xl: 1280,
|
|
15
|
+
};
|
|
16
|
+
export class WhenSizeDirective extends AsyncDirective {
|
|
17
|
+
constructor(partInfo) {
|
|
18
|
+
super(partInfo);
|
|
19
|
+
this.size = 0;
|
|
20
|
+
this.onResize = () => {
|
|
21
|
+
const newSize = window.innerWidth;
|
|
22
|
+
if (this.size !== newSize) {
|
|
23
|
+
this.size = newSize;
|
|
24
|
+
// Force the directive to re-render
|
|
25
|
+
if (this.lastArgs) {
|
|
26
|
+
this.setValue(this.render(...this.lastArgs));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
this.size = window.innerWidth;
|
|
31
|
+
window.addEventListener("resize", this.onResize);
|
|
32
|
+
}
|
|
33
|
+
disconnected() {
|
|
34
|
+
window.removeEventListener("resize", this.onResize);
|
|
35
|
+
}
|
|
36
|
+
reconnected() {
|
|
37
|
+
window.addEventListener("resize", this.onResize);
|
|
38
|
+
}
|
|
39
|
+
update(_, [threshold, aboveRenderer, belowRenderer]) {
|
|
40
|
+
this.lastArgs = [threshold, aboveRenderer, belowRenderer];
|
|
41
|
+
return this.render(threshold, aboveRenderer, belowRenderer);
|
|
42
|
+
}
|
|
43
|
+
render(thresholdOrBreakpoint, aboveRenderer, maybeBelowRenderer) {
|
|
44
|
+
const threshold = this.resolveThreshold(thresholdOrBreakpoint);
|
|
45
|
+
const belowRenderer = maybeBelowRenderer !== null && maybeBelowRenderer !== void 0 ? maybeBelowRenderer : (() => nothing);
|
|
46
|
+
return this.size > threshold ? aboveRenderer() : belowRenderer();
|
|
47
|
+
}
|
|
48
|
+
resolveThreshold(arg) {
|
|
49
|
+
if (typeof arg === "number") {
|
|
50
|
+
return arg;
|
|
51
|
+
}
|
|
52
|
+
return BREAKPOINT_THRESHOLDS[arg];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export const whenSize = directive(WhenSizeDirective);
|