@patternfly/pfe-core 2.3.0 → 2.4.0

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.
@@ -1,120 +1,122 @@
1
- var _ScrollSpyController_instances, _ScrollSpyController_tagNames, _ScrollSpyController_activeAttribute, _ScrollSpyController_io, _ScrollSpyController_passedLinks, _ScrollSpyController_force, _ScrollSpyController_intersected, _ScrollSpyController_root, _ScrollSpyController_rootMargin, _ScrollSpyController_threshold, _ScrollSpyController_rootNode, _ScrollSpyController_getHash, _ScrollSpyController_linkChildren_get, _ScrollSpyController_initIo, _ScrollSpyController_markPassed, _ScrollSpyController_setActive, _ScrollSpyController_nextIntersection, _ScrollSpyController_onIo;
2
- import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
3
1
  export class ScrollSpyController {
2
+ #tagNames;
3
+ #activeAttribute;
4
+ #io;
5
+ /** Which link's targets have already scrolled past? */
6
+ #passedLinks = new Set();
7
+ /** Ignore intersections? */
8
+ #force = false;
9
+ /** Has the intersection observer found an element? */
10
+ #intersected = false;
11
+ #root;
12
+ #rootMargin;
13
+ #threshold;
14
+ #rootNode;
15
+ #getHash;
16
+ get #linkChildren() {
17
+ return Array.from(this.host.querySelectorAll(this.#tagNames.join(',')))
18
+ .filter(this.#getHash);
19
+ }
4
20
  get root() {
5
- return __classPrivateFieldGet(this, _ScrollSpyController_root, "f");
21
+ return this.#root;
6
22
  }
7
23
  set root(v) {
8
- __classPrivateFieldSet(this, _ScrollSpyController_root, v, "f");
9
- __classPrivateFieldGet(this, _ScrollSpyController_io, "f")?.disconnect();
10
- __classPrivateFieldGet(this, _ScrollSpyController_instances, "m", _ScrollSpyController_initIo).call(this);
24
+ this.#root = v;
25
+ this.#io?.disconnect();
26
+ this.#initIo();
11
27
  }
12
28
  get rootMargin() {
13
- return __classPrivateFieldGet(this, _ScrollSpyController_rootMargin, "f");
29
+ return this.#rootMargin;
14
30
  }
15
31
  set rootMargin(v) {
16
- __classPrivateFieldSet(this, _ScrollSpyController_rootMargin, v, "f");
17
- __classPrivateFieldGet(this, _ScrollSpyController_io, "f")?.disconnect();
18
- __classPrivateFieldGet(this, _ScrollSpyController_instances, "m", _ScrollSpyController_initIo).call(this);
32
+ this.#rootMargin = v;
33
+ this.#io?.disconnect();
34
+ this.#initIo();
19
35
  }
20
36
  get threshold() {
21
- return __classPrivateFieldGet(this, _ScrollSpyController_threshold, "f");
37
+ return this.#threshold;
22
38
  }
23
39
  set threshold(v) {
24
- __classPrivateFieldSet(this, _ScrollSpyController_threshold, v, "f");
25
- __classPrivateFieldGet(this, _ScrollSpyController_io, "f")?.disconnect();
26
- __classPrivateFieldGet(this, _ScrollSpyController_instances, "m", _ScrollSpyController_initIo).call(this);
40
+ this.#threshold = v;
41
+ this.#io?.disconnect();
42
+ this.#initIo();
27
43
  }
28
44
  constructor(host, options) {
29
- _ScrollSpyController_instances.add(this);
30
45
  this.host = host;
31
- _ScrollSpyController_tagNames.set(this, void 0);
32
- _ScrollSpyController_activeAttribute.set(this, void 0);
33
- _ScrollSpyController_io.set(this, void 0);
34
- /** Which link's targets have already scrolled past? */
35
- _ScrollSpyController_passedLinks.set(this, new Set());
36
- /** Ignore intersections? */
37
- _ScrollSpyController_force.set(this, false);
38
- /** Has the intersection observer found an element? */
39
- _ScrollSpyController_intersected.set(this, false);
40
- _ScrollSpyController_root.set(this, void 0);
41
- _ScrollSpyController_rootMargin.set(this, void 0);
42
- _ScrollSpyController_threshold.set(this, void 0);
43
- _ScrollSpyController_rootNode.set(this, void 0);
44
- _ScrollSpyController_getHash.set(this, void 0);
45
46
  host.addController(this);
46
- __classPrivateFieldSet(this, _ScrollSpyController_tagNames, options.tagNames, "f");
47
- __classPrivateFieldSet(this, _ScrollSpyController_root, options.root, "f");
48
- __classPrivateFieldSet(this, _ScrollSpyController_rootMargin, options.rootMargin, "f");
49
- __classPrivateFieldSet(this, _ScrollSpyController_activeAttribute, options.activeAttribute ?? 'active', "f");
50
- __classPrivateFieldSet(this, _ScrollSpyController_threshold, options.threshold ?? 0.85, "f");
51
- __classPrivateFieldSet(this, _ScrollSpyController_rootNode, options.rootNode ?? host.getRootNode(), "f");
52
- __classPrivateFieldSet(this, _ScrollSpyController_getHash, options?.getHash ?? ((el) => el.getAttribute('href')), "f");
47
+ this.#tagNames = options.tagNames;
48
+ this.#root = options.root;
49
+ this.#rootMargin = options.rootMargin;
50
+ this.#activeAttribute = options.activeAttribute ?? 'active';
51
+ this.#threshold = options.threshold ?? 0.85;
52
+ this.#rootNode = options.rootNode ?? host.getRootNode();
53
+ this.#getHash = options?.getHash ?? ((el) => el.getAttribute('href'));
53
54
  }
54
55
  hostConnected() {
55
- __classPrivateFieldGet(this, _ScrollSpyController_instances, "m", _ScrollSpyController_initIo).call(this);
56
+ this.#initIo();
56
57
  }
57
- /** Explicitly set the active item */
58
- async setActive(link) {
59
- __classPrivateFieldSet(this, _ScrollSpyController_force, true, "f");
60
- __classPrivateFieldGet(this, _ScrollSpyController_instances, "m", _ScrollSpyController_setActive).call(this, link);
61
- let sawActive = false;
62
- for (const child of __classPrivateFieldGet(this, _ScrollSpyController_instances, "a", _ScrollSpyController_linkChildren_get)) {
63
- __classPrivateFieldGet(this, _ScrollSpyController_instances, "m", _ScrollSpyController_markPassed).call(this, child, !sawActive);
64
- if (child === link) {
65
- sawActive = true;
66
- }
58
+ #initIo() {
59
+ const rootNode = this.#rootNode;
60
+ if (rootNode instanceof Document || rootNode instanceof ShadowRoot) {
61
+ const { rootMargin, threshold, root } = this;
62
+ this.#io = new IntersectionObserver(r => this.#onIo(r), { root, rootMargin, threshold });
63
+ this.#linkChildren
64
+ .map(x => this.#getHash(x))
65
+ .filter((x) => !!x)
66
+ .map(x => rootNode.getElementById(x.replace('#', '')))
67
+ .filter((x) => !!x)
68
+ .forEach(target => this.#io?.observe(target));
67
69
  }
68
- await __classPrivateFieldGet(this, _ScrollSpyController_instances, "m", _ScrollSpyController_nextIntersection).call(this);
69
- __classPrivateFieldSet(this, _ScrollSpyController_force, false, "f");
70
- }
71
- }
72
- _ScrollSpyController_tagNames = new WeakMap(), _ScrollSpyController_activeAttribute = new WeakMap(), _ScrollSpyController_io = new WeakMap(), _ScrollSpyController_passedLinks = new WeakMap(), _ScrollSpyController_force = new WeakMap(), _ScrollSpyController_intersected = new WeakMap(), _ScrollSpyController_root = new WeakMap(), _ScrollSpyController_rootMargin = new WeakMap(), _ScrollSpyController_threshold = new WeakMap(), _ScrollSpyController_rootNode = new WeakMap(), _ScrollSpyController_getHash = new WeakMap(), _ScrollSpyController_instances = new WeakSet(), _ScrollSpyController_linkChildren_get = function _ScrollSpyController_linkChildren_get() {
73
- return Array.from(this.host.querySelectorAll(__classPrivateFieldGet(this, _ScrollSpyController_tagNames, "f").join(',')))
74
- .filter(__classPrivateFieldGet(this, _ScrollSpyController_getHash, "f"));
75
- }, _ScrollSpyController_initIo = function _ScrollSpyController_initIo() {
76
- const rootNode = __classPrivateFieldGet(this, _ScrollSpyController_rootNode, "f");
77
- if (rootNode instanceof Document || rootNode instanceof ShadowRoot) {
78
- const { rootMargin, threshold, root } = this;
79
- __classPrivateFieldSet(this, _ScrollSpyController_io, new IntersectionObserver(r => __classPrivateFieldGet(this, _ScrollSpyController_instances, "m", _ScrollSpyController_onIo).call(this, r), { root, rootMargin, threshold }), "f");
80
- __classPrivateFieldGet(this, _ScrollSpyController_instances, "a", _ScrollSpyController_linkChildren_get)
81
- .map(x => __classPrivateFieldGet(this, _ScrollSpyController_getHash, "f").call(this, x))
82
- .filter((x) => !!x)
83
- .map(x => rootNode.getElementById(x.replace('#', '')))
84
- .filter((x) => !!x)
85
- .forEach(target => __classPrivateFieldGet(this, _ScrollSpyController_io, "f")?.observe(target));
86
70
  }
87
- }, _ScrollSpyController_markPassed = function _ScrollSpyController_markPassed(link, force) {
88
- if (force) {
89
- __classPrivateFieldGet(this, _ScrollSpyController_passedLinks, "f").add(link);
71
+ #markPassed(link, force) {
72
+ if (force) {
73
+ this.#passedLinks.add(link);
74
+ }
75
+ else {
76
+ this.#passedLinks.delete(link);
77
+ }
90
78
  }
91
- else {
92
- __classPrivateFieldGet(this, _ScrollSpyController_passedLinks, "f").delete(link);
79
+ #setActive(link) {
80
+ for (const child of this.#linkChildren) {
81
+ child.toggleAttribute(this.#activeAttribute, child === link);
82
+ }
93
83
  }
94
- }, _ScrollSpyController_setActive = function _ScrollSpyController_setActive(link) {
95
- for (const child of __classPrivateFieldGet(this, _ScrollSpyController_instances, "a", _ScrollSpyController_linkChildren_get)) {
96
- child.toggleAttribute(__classPrivateFieldGet(this, _ScrollSpyController_activeAttribute, "f"), child === link);
84
+ async #nextIntersection() {
85
+ this.#intersected = false;
86
+ // safeguard the loop
87
+ setTimeout(() => this.#intersected = false, 3000);
88
+ while (!this.#intersected) {
89
+ await new Promise(requestAnimationFrame);
90
+ }
97
91
  }
98
- }, _ScrollSpyController_nextIntersection = async function _ScrollSpyController_nextIntersection() {
99
- __classPrivateFieldSet(this, _ScrollSpyController_intersected, false, "f");
100
- // safeguard the loop
101
- setTimeout(() => __classPrivateFieldSet(this, _ScrollSpyController_intersected, false, "f"), 3000);
102
- while (!__classPrivateFieldGet(this, _ScrollSpyController_intersected, "f")) {
103
- await new Promise(requestAnimationFrame);
92
+ async #onIo(entries) {
93
+ if (!this.#force) {
94
+ for (const { target, boundingClientRect, intersectionRect } of entries) {
95
+ const selector = `:is(${this.#tagNames.join(',')})[href="#${target.id}"]`;
96
+ const link = this.host.querySelector(selector);
97
+ if (link) {
98
+ this.#markPassed(link, boundingClientRect.top < intersectionRect.top);
99
+ }
100
+ }
101
+ const link = [...this.#passedLinks];
102
+ const last = link.at(-1);
103
+ this.#setActive(last ?? this.#linkChildren.at(0));
104
+ }
105
+ this.#intersected = true;
104
106
  }
105
- }, _ScrollSpyController_onIo = async function _ScrollSpyController_onIo(entries) {
106
- if (!__classPrivateFieldGet(this, _ScrollSpyController_force, "f")) {
107
- for (const { target, boundingClientRect, intersectionRect } of entries) {
108
- const selector = `:is(${__classPrivateFieldGet(this, _ScrollSpyController_tagNames, "f").join(',')})[href="#${target.id}"]`;
109
- const link = this.host.querySelector(selector);
110
- if (link) {
111
- __classPrivateFieldGet(this, _ScrollSpyController_instances, "m", _ScrollSpyController_markPassed).call(this, link, boundingClientRect.top < intersectionRect.top);
107
+ /** Explicitly set the active item */
108
+ async setActive(link) {
109
+ this.#force = true;
110
+ this.#setActive(link);
111
+ let sawActive = false;
112
+ for (const child of this.#linkChildren) {
113
+ this.#markPassed(child, !sawActive);
114
+ if (child === link) {
115
+ sawActive = true;
112
116
  }
113
117
  }
114
- const link = [...__classPrivateFieldGet(this, _ScrollSpyController_passedLinks, "f")];
115
- const last = link.at(-1);
116
- __classPrivateFieldGet(this, _ScrollSpyController_instances, "m", _ScrollSpyController_setActive).call(this, last ?? __classPrivateFieldGet(this, _ScrollSpyController_instances, "a", _ScrollSpyController_linkChildren_get).at(0));
118
+ await this.#nextIntersection();
119
+ this.#force = false;
117
120
  }
118
- __classPrivateFieldSet(this, _ScrollSpyController_intersected, true, "f");
119
- };
121
+ }
120
122
  //# sourceMappingURL=scroll-spy-controller.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"scroll-spy-controller.js","sourceRoot":"","sources":["scroll-spy-controller.ts"],"names":[],"mappings":";;AA2BA,MAAM,OAAO,mBAAmB;IA2B9B,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,iCAAM,CAAC;IACpB,CAAC;IAED,IAAI,IAAI,CAAC,CAAC;QACR,uBAAA,IAAI,6BAAS,CAAC,MAAA,CAAC;QACf,uBAAA,IAAI,+BAAI,EAAE,UAAU,EAAE,CAAC;QACvB,uBAAA,IAAI,mEAAQ,MAAZ,IAAI,CAAU,CAAC;IACjB,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,uBAAA,IAAI,uCAAY,CAAC;IAC1B,CAAC;IAED,IAAI,UAAU,CAAC,CAAC;QACd,uBAAA,IAAI,mCAAe,CAAC,MAAA,CAAC;QACrB,uBAAA,I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,uBAAA,IAAI,kCAAO,EAAE;QAChB,KAAK,MAAM,EAAE,MAAM,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,IAAI,OAAO,EAAE;YACtE,MAAM,QAAQ,GAAG,OAAO,uBAAA,IAAI,qCAAU,CAAC,IAAI,CAAC,GAAG,CAAC,YAAY,MAAM,CAAC,EAAE,IAAI,CAAC;YAC1E,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;YAC/C,IAAI,IAAI,EAAE;gBACR,uBAAA,IAAI,uEAAY,MAAhB,IAAI,EAAa,IAAI,EAAE,kBAAkB,CAAC,GAAG,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC;aACvE;SACF;QACD,MAAM,IAAI,GAAG,CAAC,GAAG,uBAAA,IAAI,wCAAa,CAAC,CAAC;QACpC,MAAM,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACzB,uBAAA,IAAI,sEAAW,MAAf,IAAI,EAAY,IAAI,IAAI,uBAAA,IAAI,6EAAc,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;KACnD;IACD,uBAAA,IAAI,oCAAgB,IAAI,MAAA,CAAC;AAC3B,CAAC","sourcesContent":["import type { ReactiveController, ReactiveControllerHost } from 'lit';\n\nexport interface ScrollSpyControllerOptions extends IntersectionObserverInit {\n /**\n * Tag names of legal link children.\n * Legal children must have an `href` property/attribute pair, like `<a>`.\n */\n tagNames: string[];\n\n /**\n * Attribute to set on the active link element.\n * @default 'active'\n */\n activeAttribute?: string;\n\n /**\n * The root node to query content for\n * @default the host's root node\n */\n rootNode?: Node;\n /**\n * function to call on link children to get their URL hash (i.e. id to scroll to)\n * @default el => el.getAttribute('href');\n */\n getHash?: (el: Element) => string | null;\n}\n\nexport class ScrollSpyController implements ReactiveController {\n #tagNames: string[];\n #activeAttribute: string;\n\n #io?: IntersectionObserver;\n\n /** Which link's targets have already scrolled past? */\n #passedLinks = new Set<Element>();\n\n /** Ignore intersections? */\n #force = false;\n\n /** Has the intersection observer found an element? */\n #intersected = false;\n\n #root: ScrollSpyControllerOptions['root'];\n #rootMargin?: string;\n #threshold: number | number[];\n\n #rootNode: Node;\n #getHash: (el: Element) => string | null;\n\n get #linkChildren(): Element[] {\n return Array.from(this.host.querySelectorAll(this.#tagNames.join(',')))\n .filter(this.#getHash);\n }\n\n get root() {\n return this.#root;\n }\n\n set root(v) {\n this.#root = v;\n this.#io?.disconnect();\n this.#initIo();\n }\n\n get rootMargin() {\n return this.#rootMargin;\n }\n\n set rootMargin(v) {\n this.#rootMargin = v;\n this.#io?.disconnect();\n this.#initIo();\n }\n\n get threshold() {\n return this.#threshold;\n }\n\n set threshold(v) {\n this.#threshold = v;\n this.#io?.disconnect();\n this.#initIo();\n }\n\n constructor(\n private host: ReactiveControllerHost & HTMLElement,\n options: ScrollSpyControllerOptions,\n ) {\n host.addController(this);\n this.#tagNames = options.tagNames;\n this.#root = options.root;\n this.#rootMargin = options.rootMargin;\n this.#activeAttribute = options.activeAttribute ?? 'active';\n this.#threshold = options.threshold ?? 0.85;\n this.#rootNode = options.rootNode ?? host.getRootNode();\n this.#getHash = options?.getHash ?? ((el: Element) => el.getAttribute('href'));\n }\n\n hostConnected() {\n this.#initIo();\n }\n\n #initIo() {\n const rootNode = this.#rootNode;\n if (rootNode instanceof Document || rootNode instanceof ShadowRoot) {\n const { rootMargin, threshold, root } = this;\n this.#io = new IntersectionObserver(r => this.#onIo(r), { root, rootMargin, threshold });\n this.#linkChildren\n .map(x => this.#getHash(x))\n .filter((x): x is string => !!x)\n .map(x => rootNode.getElementById(x.replace('#', '')))\n .filter((x): x is HTMLElement => !!x)\n .forEach(target => this.#io?.observe(target));\n }\n }\n\n #markPassed(link: Element, force: boolean) {\n if (force) {\n this.#passedLinks.add(link);\n } else {\n this.#passedLinks.delete(link);\n }\n }\n\n #setActive(link?: EventTarget | null) {\n for (const child of this.#linkChildren) {\n child.toggleAttribute(this.#activeAttribute, child === link);\n }\n }\n\n async #nextIntersection() {\n this.#intersected = false;\n // safeguard the loop\n setTimeout(() => this.#intersected = false, 3000);\n while (!this.#intersected) {\n await new Promise(requestAnimationFrame);\n }\n }\n\n async #onIo(entries: IntersectionObserverEntry[]) {\n if (!this.#force) {\n for (const { target, boundingClientRect, intersectionRect } of entries) {\n const selector = `:is(${this.#tagNames.join(',')})[href=\"#${target.id}\"]`;\n const link = this.host.querySelector(selector);\n if (link) {\n this.#markPassed(link, boundingClientRect.top < intersectionRect.top);\n }\n }\n const link = [...this.#passedLinks];\n const last = link.at(-1);\n this.#setActive(last ?? this.#linkChildren.at(0));\n }\n this.#intersected = true;\n }\n\n /** Explicitly set the active item */\n public async setActive(link: EventTarget | null) {\n this.#force = true;\n this.#setActive(link);\n let sawActive = false;\n for (const child of this.#linkChildren) {\n this.#markPassed(child, !sawActive);\n if (child === link) {\n sawActive = true;\n }\n }\n await this.#nextIntersection();\n this.#force = false;\n }\n}\n"]}
1
+ {"version":3,"file":"scroll-spy-controller.js","sourceRoot":"","sources":["scroll-spy-controller.ts"],"names":[],"mappings":"AA2BA,MAAM,OAAO,mBAAmB;IAC9B,SAAS,CAAW;IACpB,gBAAgB,CAAS;IAEzB,GAAG,CAAwB;IAE3B,uDAAuD;IACvD,YAAY,GAAG,IAAI,GAAG,EAAW,CAAC;IAElC,4BAA4B;IAC5B,MAAM,GAAG,KAAK,CAAC;IAEf,sDAAsD;IACtD,YAAY,GAAG,KAAK,CAAC;IAErB,KAAK,CAAqC;IAC1C,WAAW,CAAU;IACrB,UAAU,CAAoB;IAE9B,SAAS,CAAO;IAChB,QAAQ,CAAiC;IAEzC,IAAI,aAAa;QACf,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;aACpE,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC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AC,CAAC;SACnD;QACD,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;IAC3B,CAAC;IAED,qCAAqC;IAC9B,KAAK,CAAC,SAAS,CAAC,IAAwB;QAC7C,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QACtB,IAAI,SAAS,GAAG,KAAK,CAAC;QACtB,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,aAAa,EAAE;YACtC,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC,SAAS,CAAC,CAAC;YACpC,IAAI,KAAK,KAAK,IAAI,EAAE;gBAClB,SAAS,GAAG,IAAI,CAAC;aAClB;SACF;QACD,MAAM,IAAI,CAAC,iBAAiB,EAAE,CAAC;QAC/B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;CACF","sourcesContent":["import type { ReactiveController, ReactiveControllerHost } from 'lit';\n\nexport interface ScrollSpyControllerOptions extends IntersectionObserverInit {\n /**\n * Tag names of legal link children.\n * Legal children must have an `href` property/attribute pair, like `<a>`.\n */\n tagNames: string[];\n\n /**\n * Attribute to set on the active link element.\n * @default 'active'\n */\n activeAttribute?: string;\n\n /**\n * The root node to query content for\n * @default the host's root node\n */\n rootNode?: Node;\n /**\n * function to call on link children to get their URL hash (i.e. id to scroll to)\n * @default el => el.getAttribute('href');\n */\n getHash?: (el: Element) => string | null;\n}\n\nexport class ScrollSpyController implements ReactiveController {\n #tagNames: string[];\n #activeAttribute: string;\n\n #io?: IntersectionObserver;\n\n /** Which link's targets have already scrolled past? */\n #passedLinks = new Set<Element>();\n\n /** Ignore intersections? */\n #force = false;\n\n /** Has the intersection observer found an element? */\n #intersected = false;\n\n #root: ScrollSpyControllerOptions['root'];\n #rootMargin?: string;\n #threshold: number | number[];\n\n #rootNode: Node;\n #getHash: (el: Element) => string | null;\n\n get #linkChildren(): Element[] {\n return Array.from(this.host.querySelectorAll(this.#tagNames.join(',')))\n .filter(this.#getHash);\n }\n\n get root() {\n return this.#root;\n }\n\n set root(v) {\n this.#root = v;\n this.#io?.disconnect();\n this.#initIo();\n }\n\n get rootMargin() {\n return this.#rootMargin;\n }\n\n set rootMargin(v) {\n this.#rootMargin = v;\n this.#io?.disconnect();\n this.#initIo();\n }\n\n get threshold() {\n return this.#threshold;\n }\n\n set threshold(v) {\n this.#threshold = v;\n this.#io?.disconnect();\n this.#initIo();\n }\n\n constructor(\n private host: ReactiveControllerHost & HTMLElement,\n options: ScrollSpyControllerOptions,\n ) {\n host.addController(this);\n this.#tagNames = options.tagNames;\n this.#root = options.root;\n this.#rootMargin = options.rootMargin;\n this.#activeAttribute = options.activeAttribute ?? 'active';\n this.#threshold = options.threshold ?? 0.85;\n this.#rootNode = options.rootNode ?? host.getRootNode();\n this.#getHash = options?.getHash ?? ((el: Element) => el.getAttribute('href'));\n }\n\n hostConnected() {\n this.#initIo();\n }\n\n #initIo() {\n const rootNode = this.#rootNode;\n if (rootNode instanceof Document || rootNode instanceof ShadowRoot) {\n const { rootMargin, threshold, root } = this;\n this.#io = new IntersectionObserver(r => this.#onIo(r), { root, rootMargin, threshold });\n this.#linkChildren\n .map(x => this.#getHash(x))\n .filter((x): x is string => !!x)\n .map(x => rootNode.getElementById(x.replace('#', '')))\n .filter((x): x is HTMLElement => !!x)\n .forEach(target => this.#io?.observe(target));\n }\n }\n\n #markPassed(link: Element, force: boolean) {\n if (force) {\n this.#passedLinks.add(link);\n } else {\n this.#passedLinks.delete(link);\n }\n }\n\n #setActive(link?: EventTarget | null) {\n for (const child of this.#linkChildren) {\n child.toggleAttribute(this.#activeAttribute, child === link);\n }\n }\n\n async #nextIntersection() {\n this.#intersected = false;\n // safeguard the loop\n setTimeout(() => this.#intersected = false, 3000);\n while (!this.#intersected) {\n await new Promise(requestAnimationFrame);\n }\n }\n\n async #onIo(entries: IntersectionObserverEntry[]) {\n if (!this.#force) {\n for (const { target, boundingClientRect, intersectionRect } of entries) {\n const selector = `:is(${this.#tagNames.join(',')})[href=\"#${target.id}\"]`;\n const link = this.host.querySelector(selector);\n if (link) {\n this.#markPassed(link, boundingClientRect.top < intersectionRect.top);\n }\n }\n const link = [...this.#passedLinks];\n const last = link.at(-1);\n this.#setActive(last ?? this.#linkChildren.at(0));\n }\n this.#intersected = true;\n }\n\n /** Explicitly set the active item */\n public async setActive(link: EventTarget | null) {\n this.#force = true;\n this.#setActive(link);\n let sawActive = false;\n for (const child of this.#linkChildren) {\n this.#markPassed(child, !sawActive);\n if (child === link) {\n sawActive = true;\n }\n }\n await this.#nextIntersection();\n this.#force = false;\n }\n}\n"]}
@@ -11,6 +11,7 @@ function isObjectConfigSpread(config) {
11
11
  const isSlot = (n) => (child) => n === SlotController.anonymous ? !child.hasAttribute('slot')
12
12
  : child.getAttribute('slot') === n;
13
13
  class SlotController {
14
+ static { this.anonymous = Symbol('anonymous slot'); }
14
15
  constructor(host, ...config) {
15
16
  this.host = host;
16
17
  this.nodes = new Map();
@@ -132,7 +133,6 @@ class SlotController {
132
133
  this.host.requestUpdate();
133
134
  }
134
135
  }
135
- SlotController.anonymous = Symbol('anonymous slot');
136
136
  __decorate([
137
137
  bound
138
138
  ], SlotController.prototype, "onSlotChange", null);
@@ -1 +1 @@
1
- {"version":3,"file":"slot-controller.js","sourceRoot":"","sources":["slot-controller.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAgCrC,SAAS,oBAAoB,CAAC,MAA2C;IACvE,OAAO,MAAM,CAAC,MAAM,KAAK,CAAC,IAAI,OAAO,MAAM,CAAC,CAAC,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;AACpF,CAAC;AAED;;;GAGG;AACH,MAAM,MAAM,GACV,CAA8B,CAA2C,EAAE,EAAE,CAC3E,CAAC,KAAc,EAAc,EAAE,CAC3B,CAA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CAAC;QACtF,MAAM,UAAU,GAAG,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,IAAI,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;QAC3E,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;IACxC,CAAC;IAED;;OAEG;IACY,IAAI;QACjB,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;QACnB,kDAAkD;QAClD,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACtC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACxD,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;IAC5B,CAAC;;AA3Ia,wBAAS,GAAG,MAAM,CAAC,gBAAgB,CAAC,AAA3B,CAA4B;AA8FpC;IAAd,KAAK;kDAIL;AAEoB;IAApB,KAAK;gDAaL;AAOc;IAAd,KAAK;8CAQL;AAKc;IAAd,KAAK;0CAML;SA5IU,cAAc","sourcesContent":["import type { ReactiveController, ReactiveElement } from 'lit';\n\nimport { bound } from '../decorators/bound.js';\nimport { Logger } from './logger.js';\n\ninterface AnonymousSlot {\n hasContent: boolean;\n elements: Element[];\n slot: HTMLSlotElement | null;\n}\n\ninterface NamedSlot extends AnonymousSlot {\n name: string;\n initialized: true;\n}\n\nexport type Slot = NamedSlot | AnonymousSlot;\n\nexport interface SlotsConfig {\n slots: (string | null)[];\n /**\n * Object mapping new slot name keys to deprecated slot name values\n * @example `pf-modal--header` is deprecated in favour of `header`\n * ```js\n * new SlotController(this, {\n * slots: ['header'],\n * deprecations: {\n * 'header': 'pf-modal--header'\n * }\n * })\n * ```\n */\n deprecations?: Record<string, string>;\n}\n\nfunction isObjectConfigSpread(config: ([SlotsConfig] | (string | null)[])): config is [SlotsConfig] {\n return config.length === 1 && typeof config[0] === 'object' && config[0] !== null;\n}\n\n/**\n * If it's a named slot, return its children,\n * for the default slot, look for direct children not assigned to a slot\n */\nconst isSlot =\n <T extends Element = Element>(n: string | typeof SlotController.anonymous) =>\n (child: Element): child is T =>\n n === SlotController.anonymous ? !child.hasAttribute('slot')\n : child.getAttribute('slot') === n;\n\nexport class SlotController implements ReactiveController {\n public static anonymous = Symbol('anonymous slot');\n\n private nodes = new Map<string | typeof SlotController.anonymous, Slot>();\n\n private logger: Logger;\n\n private firstUpdated = false;\n\n private mo = new MutationObserver(this.onMutation);\n\n private slotNames: (string | null)[];\n\n private deprecations: Record<string, string> = {};\n\n constructor(public host: ReactiveElement, ...config: ([SlotsConfig] | (string | null)[])) {\n this.logger = new Logger(this.host);\n\n if (isObjectConfigSpread(config)) {\n const [{ slots, deprecations }] = config;\n this.slotNames = slots;\n this.deprecations = deprecations ?? {};\n } else if (config.length >= 1) {\n this.slotNames = config;\n this.deprecations = {};\n } else {\n this.slotNames = [null];\n }\n\n\n host.addController(this);\n }\n\n hostConnected() {\n this.host.addEventListener('slotchange', this.onSlotChange as EventListener);\n this.firstUpdated = false;\n this.mo.observe(this.host, { childList: true });\n this.init();\n }\n\n hostUpdated() {\n if (!this.firstUpdated) {\n this.slotNames.forEach(this.initSlot);\n this.firstUpdated = true;\n }\n }\n\n hostDisconnected() {\n this.mo.disconnect();\n }\n\n /**\n * Returns a boolean statement of whether or not any of those slots exists in the light DOM.\n *\n * @param {String|Array} name The slot name.\n * @example this.hasSlotted(\"header\");\n */\n hasSlotted(...names: string[]): boolean {\n if (!names.length) {\n this.logger.warn(`Please provide at least one slot name for which to search.`);\n return false;\n } else {\n return names.some(x =>\n this.nodes.get(x)?.hasContent ?? false);\n }\n }\n\n /**\n * Given a slot name or slot names, returns elements assigned to the requested slots as an array.\n * If no value is provided, it returns all children not assigned to a slot (without a slot attribute).\n *\n * @example Get header-slotted elements\n * ```js\n * this.getSlotted('header')\n * ```\n *\n * @example Get header- and footer-slotted elements\n * ```js\n * this.getSlotted('header', 'footer')\n * ```\n *\n * @example Get default-slotted elements\n * ```js\n * this.getSlotted();\n * ```\n */\n getSlotted<T extends Element = Element>(...slotNames: string[]): T[] {\n if (!slotNames.length) {\n return (this.nodes.get(SlotController.anonymous)?.elements ?? []) as T[];\n } else {\n return slotNames.flatMap(slotName =>\n this.nodes.get(slotName)?.elements ?? []) as T[];\n }\n }\n\n @bound private onSlotChange(event: Event & { target: HTMLSlotElement }) {\n const slotName = event.target.name;\n this.initSlot(slotName);\n this.host.requestUpdate();\n }\n\n @bound private async onMutation(records: MutationRecord[]) {\n const changed = [];\n for (const { addedNodes, removedNodes } of records) {\n for (const node of [...addedNodes, ...removedNodes]) {\n if (node instanceof HTMLElement && node.slot) {\n this.initSlot(node.slot);\n changed.push(node.slot);\n }\n }\n }\n if (changed.length) {\n this.host.requestUpdate();\n }\n }\n\n private getChildrenForSlot<T extends Element = Element>(name: string | typeof SlotController.anonymous): T[] {\n const children = Array.from(this.host.children) as T[];\n return children.filter(isSlot(name));\n }\n\n @bound private initSlot(slotName: string | null) {\n const name = slotName || SlotController.anonymous;\n const elements = this.nodes.get(name)?.slot?.assignedElements?.() ?? this.getChildrenForSlot(name);\n const selector = slotName ? `slot[name=\"${slotName}\"]` : 'slot:not([name])';\n const slot = this.host.shadowRoot?.querySelector?.<HTMLSlotElement>(selector) ?? null;\n const hasContent = !!elements.length;\n this.nodes.set(name, { elements, name: slotName ?? '', hasContent, slot });\n this.logger.log(slotName, hasContent);\n }\n\n /**\n * Maps the defined slots into an object that is easier to query\n */\n @bound private init() {\n this.nodes.clear();\n // Loop over the properties provided by the schema\n this.slotNames.forEach(this.initSlot);\n Object.values(this.deprecations).forEach(this.initSlot);\n this.host.requestUpdate();\n }\n}\n"]}
1
+ {"version":3,"file":"slot-controller.js","sourceRoot":"","sources":["slot-controller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, ReactiveElement } from 'lit';\n\nimport { bound } from '../decorators/bound.js';\nimport { Logger } from './logger.js';\n\ninterface AnonymousSlot {\n hasContent: boolean;\n elements: Element[];\n slot: HTMLSlotElement | null;\n}\n\ninterface NamedSlot extends AnonymousSlot {\n name: string;\n initialized: true;\n}\n\nexport type Slot = NamedSlot | AnonymousSlot;\n\nexport interface SlotsConfig {\n slots: (string | null)[];\n /**\n * Object mapping new slot name keys to deprecated slot name values\n * @example `pf-modal--header` is deprecated in favour of `header`\n * ```js\n * new SlotController(this, {\n * slots: ['header'],\n * deprecations: {\n * 'header': 'pf-modal--header'\n * }\n * })\n * ```\n */\n deprecations?: Record<string, string>;\n}\n\nfunction isObjectConfigSpread(config: ([SlotsConfig] | (string | null)[])): config is [SlotsConfig] {\n return config.length === 1 && typeof config[0] === 'object' && config[0] !== null;\n}\n\n/**\n * If it's a named slot, return its children,\n * for the default slot, look for direct children not assigned to a slot\n */\nconst isSlot =\n <T extends Element = Element>(n: string | typeof SlotController.anonymous) =>\n (child: Element): child is T =>\n n === SlotController.anonymous ? !child.hasAttribute('slot')\n : child.getAttribute('slot') === n;\n\nexport class SlotController implements ReactiveController {\n public static anonymous = Symbol('anonymous slot');\n\n private nodes = new Map<string | typeof SlotController.anonymous, Slot>();\n\n private logger: Logger;\n\n private firstUpdated = false;\n\n private mo = new MutationObserver(this.onMutation);\n\n private slotNames: (string | null)[];\n\n private deprecations: Record<string, string> = {};\n\n constructor(public host: ReactiveElement, ...config: ([SlotsConfig] | (string | null)[])) {\n this.logger = new Logger(this.host);\n\n if (isObjectConfigSpread(config)) {\n const [{ slots, deprecations }] = config;\n this.slotNames = slots;\n this.deprecations = deprecations ?? {};\n } else if (config.length >= 1) {\n this.slotNames = config;\n this.deprecations = {};\n } else {\n this.slotNames = [null];\n }\n\n\n host.addController(this);\n }\n\n hostConnected() {\n this.host.addEventListener('slotchange', this.onSlotChange as EventListener);\n this.firstUpdated = false;\n this.mo.observe(this.host, { childList: true });\n this.init();\n }\n\n hostUpdated() {\n if (!this.firstUpdated) {\n this.slotNames.forEach(this.initSlot);\n this.firstUpdated = true;\n }\n }\n\n hostDisconnected() {\n this.mo.disconnect();\n }\n\n /**\n * Returns a boolean statement of whether or not any of those slots exists in the light DOM.\n *\n * @param {String|Array} name The slot name.\n * @example this.hasSlotted(\"header\");\n */\n hasSlotted(...names: string[]): boolean {\n if (!names.length) {\n this.logger.warn(`Please provide at least one slot name for which to search.`);\n return false;\n } else {\n return names.some(x =>\n this.nodes.get(x)?.hasContent ?? false);\n }\n }\n\n /**\n * Given a slot name or slot names, returns elements assigned to the requested slots as an array.\n * If no value is provided, it returns all children not assigned to a slot (without a slot attribute).\n *\n * @example Get header-slotted elements\n * ```js\n * this.getSlotted('header')\n * ```\n *\n * @example Get header- and footer-slotted elements\n * ```js\n * this.getSlotted('header', 'footer')\n * ```\n *\n * @example Get default-slotted elements\n * ```js\n * this.getSlotted();\n * ```\n */\n getSlotted<T extends Element = Element>(...slotNames: string[]): T[] {\n if (!slotNames.length) {\n return (this.nodes.get(SlotController.anonymous)?.elements ?? []) as T[];\n } else {\n return slotNames.flatMap(slotName =>\n this.nodes.get(slotName)?.elements ?? []) as T[];\n }\n }\n\n @bound private onSlotChange(event: Event & { target: HTMLSlotElement }) {\n const slotName = event.target.name;\n this.initSlot(slotName);\n this.host.requestUpdate();\n }\n\n @bound private async onMutation(records: MutationRecord[]) {\n const changed = [];\n for (const { addedNodes, removedNodes } of records) {\n for (const node of [...addedNodes, ...removedNodes]) {\n if (node instanceof HTMLElement && node.slot) {\n this.initSlot(node.slot);\n changed.push(node.slot);\n }\n }\n }\n if (changed.length) {\n this.host.requestUpdate();\n }\n }\n\n private getChildrenForSlot<T extends Element = Element>(name: string | typeof SlotController.anonymous): T[] {\n const children = Array.from(this.host.children) as T[];\n return children.filter(isSlot(name));\n }\n\n @bound private initSlot(slotName: string | null) {\n const name = slotName || SlotController.anonymous;\n const elements = this.nodes.get(name)?.slot?.assignedElements?.() ?? this.getChildrenForSlot(name);\n const selector = slotName ? `slot[name=\"${slotName}\"]` : 'slot:not([name])';\n const slot = this.host.shadowRoot?.querySelector?.<HTMLSlotElement>(selector) ?? null;\n const hasContent = !!elements.length;\n this.nodes.set(name, { elements, name: slotName ?? '', hasContent, slot });\n this.logger.log(slotName, hasContent);\n }\n\n /**\n * Maps the defined slots into an object that is easier to query\n */\n @bound private init() {\n this.nodes.clear();\n // Loop over the properties provided by the schema\n this.slotNames.forEach(this.initSlot);\n Object.values(this.deprecations).forEach(this.initSlot);\n this.host.requestUpdate();\n }\n}\n"]}
@@ -0,0 +1,23 @@
1
+ import type { ReactiveController, ReactiveControllerHost } from 'lit';
2
+ export type DateTimeFormat = 'full' | 'long' | 'medium' | 'short';
3
+ export interface TimestampOptions {
4
+ dateFormat?: DateTimeFormat;
5
+ timeFormat?: DateTimeFormat;
6
+ customFormat?: Intl.DateTimeFormatOptions;
7
+ displaySuffix: string;
8
+ locale: Intl.LocalesArgument;
9
+ relative: boolean;
10
+ utc: boolean;
11
+ hour12: boolean;
12
+ }
13
+ export declare class TimestampController implements ReactiveController {
14
+ #private;
15
+ get localeString(): string;
16
+ get date(): Date;
17
+ set date(string: Date);
18
+ get isoString(): string;
19
+ get time(): string;
20
+ constructor(host: ReactiveControllerHost, options?: Partial<TimestampOptions>);
21
+ hostConnected?(): void;
22
+ set(prop: PropertyKey, value: unknown): void;
23
+ }
@@ -0,0 +1,108 @@
1
+ const defaults = {
2
+ dateFormat: undefined,
3
+ timeFormat: undefined,
4
+ customFormat: undefined,
5
+ displaySuffix: '',
6
+ locale: undefined,
7
+ relative: false,
8
+ utc: false,
9
+ hour12: false,
10
+ };
11
+ export class TimestampController {
12
+ static #isTimestampOptionKey(prop) {
13
+ return prop in defaults;
14
+ }
15
+ #date = new Date();
16
+ #options = {};
17
+ #host;
18
+ get localeString() {
19
+ return this.#date.toLocaleString(this.#options.locale);
20
+ }
21
+ get date() {
22
+ return this.#date;
23
+ }
24
+ set date(string) {
25
+ this.#date = new Date(string);
26
+ }
27
+ get isoString() {
28
+ return this.#date.toISOString();
29
+ }
30
+ get time() {
31
+ if (this.#options.relative) {
32
+ return this.#getTimeRelative();
33
+ }
34
+ else {
35
+ let { displaySuffix, locale } = this.#options;
36
+ if (this.#options.utc) {
37
+ displaySuffix ||= 'UTC';
38
+ }
39
+ const localeString = this.#date.toLocaleString(locale, this.#options.customFormat ?? {
40
+ hour12: this.#options.hour12,
41
+ timeStyle: this.#options.timeFormat,
42
+ dateStyle: this.#options.dateFormat,
43
+ ...this.#options.utc && { timeZone: 'UTC' },
44
+ });
45
+ return `${localeString} ${displaySuffix ?? ''}`.trim();
46
+ }
47
+ }
48
+ constructor(host, options) {
49
+ this.#host = host;
50
+ host.addController(this);
51
+ for (const [name, value] of Object.entries(this.#options)) {
52
+ // @ts-expect-error: seems typescript compiler isn't up to the task here
53
+ this.#options[name] = options?.[name] ?? value;
54
+ }
55
+ }
56
+ /**
57
+ * Based off of Github Relative Time
58
+ * https://github.com/github/time-elements/blob/master/src/relative-time.js
59
+ */
60
+ #getTimeRelative() {
61
+ const date = this.#date;
62
+ const { locale } = this.#options;
63
+ const rtf = new Intl.RelativeTimeFormat(locale, { localeMatcher: 'best fit', numeric: 'auto', style: 'long' });
64
+ const ms = date.getTime() - Date.now();
65
+ const tense = ms > 0 ? 1 : -1;
66
+ let qty = 0;
67
+ let units;
68
+ const s = Math.round(Math.abs(ms) / 1000);
69
+ const min = Math.round(s / 60);
70
+ const h = Math.round(min / 60);
71
+ const d = Math.round(h / 24);
72
+ const m = Math.round(d / 30);
73
+ const y = Math.round(m / 12);
74
+ if (m >= 12) {
75
+ qty = y;
76
+ units = 'year';
77
+ }
78
+ else if (d >= 30) {
79
+ qty = m;
80
+ units = 'month';
81
+ }
82
+ else if (h >= 24) {
83
+ qty = d;
84
+ units = 'day';
85
+ }
86
+ else if (min >= 45) {
87
+ qty = h;
88
+ units = 'hour';
89
+ }
90
+ else if (s >= 45) {
91
+ qty = min;
92
+ units = 'minute';
93
+ }
94
+ else if (s >= 10) {
95
+ qty = s;
96
+ units = 'second';
97
+ }
98
+ return typeof (units) !== 'undefined' ? rtf.format(tense * qty, units) : 'just now';
99
+ }
100
+ set(prop, value) {
101
+ if (TimestampController.#isTimestampOptionKey(prop)) {
102
+ // @ts-expect-error: seems typescript compiler isn't up to the task here
103
+ this.#options[prop] = value;
104
+ this.#host.requestUpdate();
105
+ }
106
+ }
107
+ }
108
+ //# sourceMappingURL=timestamp-controller.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timestamp-controller.js","sourceRoot":"","sources":["timestamp-controller.ts"],"names":[],"mappings":"AAeA,MAAM,QAAQ,GAAG;IACf,UAAU,EAAE,SAAS;IACrB,UAAU,EAAE,SAAS;IACrB,YAAY,EAAE,SAAS;IACvB,aAAa,EAAE,EAAE;IACjB,MAAM,EAAE,SAAS;IACjB,QAAQ,EAAE,KAAK;IACf,GAAG,EAAE,KAAK;IACV,MAAM,EAAE,KAAK;CACL,CAAC;AAEX,MAAM,OAAO,mBAAmB;IAC9B,MAAM,CAAC,qBAAqB,CAAC,IAAiB;QAC5C,OAAO,IAAI,IAAI,QAAQ,CAAC;IAC1B,CAAC;IAED,KAAK,GAAG,IAAI,IAAI,EAAE,CAAC;IAEnB,QAAQ,GAAqB,EAAsB,CAAC;IAEpD,KAAK,CAAyB;IAE9B,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IACzD,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAAI,IAAI,CAAC,MAAM;QACb,IAAI,CAAC,KAAK,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC;IAClC,CAAC;IAED,IAAI,IAAI;QACN,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AAE;YACpB,GAAG,GAAG,CAAC,CAAC;YACR,KAAK,GAAG,MAAM,CAAC;SAChB;aAAM,IAAI,CAAC,IAAI,EAAE,EAAE;YAClB,GAAG,GAAG,GAAG,CAAC;YACV,KAAK,GAAG,QAAQ,CAAC;SAClB;aAAM,IAAI,CAAC,IAAI,EAAE,EAAE;YAClB,GAAG,GAAG,CAAC,CAAC;YACR,KAAK,GAAG,QAAQ,CAAC;SAClB;QAED,OAAO,OAAO,CAAC,KAAK,CAAC,KAAK,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,GAAG,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC;IACtF,CAAC;IAED,GAAG,CAAC,IAAiB,EAAE,KAAc;QACnC,IAAI,mBAAmB,CAAC,qBAAqB,CAAC,IAAI,CAAC,EAAE;YACnD,wEAAwE;YACxE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;YAC5B,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,CAAC;SAC5B;IACH,CAAC;CACF","sourcesContent":["import type { ReactiveController, ReactiveControllerHost } from 'lit';\n\nexport type DateTimeFormat = 'full' | 'long' | 'medium' | 'short';\n\nexport interface TimestampOptions {\n dateFormat?: DateTimeFormat;\n timeFormat?: DateTimeFormat;\n customFormat?: Intl.DateTimeFormatOptions;\n displaySuffix: string;\n locale: Intl.LocalesArgument;\n relative: boolean;\n utc: boolean;\n hour12: boolean;\n}\n\nconst defaults = {\n dateFormat: undefined,\n timeFormat: undefined,\n customFormat: undefined,\n displaySuffix: '',\n locale: undefined,\n relative: false,\n utc: false,\n hour12: false,\n} as const;\n\nexport class TimestampController implements ReactiveController {\n static #isTimestampOptionKey(prop: PropertyKey): prop is keyof TimestampOptions {\n return prop in defaults;\n }\n\n #date = new Date();\n\n #options: TimestampOptions = {} as TimestampOptions;\n\n #host: ReactiveControllerHost;\n\n get localeString() {\n return this.#date.toLocaleString(this.#options.locale);\n }\n\n get date() {\n return this.#date;\n }\n\n set date(string) {\n this.#date = new Date(string);\n }\n\n get isoString() {\n return this.#date.toISOString();\n }\n\n get time() {\n if (this.#options.relative) {\n return this.#getTimeRelative();\n } else {\n let { displaySuffix, locale } = this.#options;\n if (this.#options.utc) {\n displaySuffix ||= 'UTC';\n }\n const localeString = this.#date.toLocaleString(locale, this.#options.customFormat ?? {\n hour12: this.#options.hour12,\n timeStyle: this.#options.timeFormat,\n dateStyle: this.#options.dateFormat,\n ...this.#options.utc && { timeZone: 'UTC' },\n });\n\n return `${localeString} ${displaySuffix ?? ''}`.trim();\n }\n }\n\n constructor(host: ReactiveControllerHost, options?: Partial<TimestampOptions>) {\n this.#host = host;\n host.addController(this);\n for (const [name, value] of Object.entries(this.#options)) {\n // @ts-expect-error: seems typescript compiler isn't up to the task here\n this.#options[name] = options?.[name] ?? value;\n }\n }\n\n hostConnected?(): void\n\n /**\n * Based off of Github Relative Time\n * https://github.com/github/time-elements/blob/master/src/relative-time.js\n */\n #getTimeRelative() {\n const date = this.#date;\n const { locale } = this.#options;\n const rtf = new Intl.RelativeTimeFormat(locale as string, { localeMatcher: 'best fit', numeric: 'auto', style: 'long' });\n const ms: number = date.getTime() - Date.now();\n const tense = ms > 0 ? 1 : -1;\n let qty = 0;\n let units: Intl.RelativeTimeFormatUnit | undefined;\n const s = Math.round(Math.abs(ms) / 1000);\n const min = Math.round(s / 60);\n const h = Math.round(min / 60);\n const d = Math.round(h / 24);\n const m = Math.round(d / 30);\n const y = Math.round(m / 12);\n if (m >= 12) {\n qty = y;\n units = 'year';\n } else if (d >= 30) {\n qty = m;\n units = 'month';\n } else if (h >= 24) {\n qty = d;\n units = 'day';\n } else if (min >= 45) {\n qty = h;\n units = 'hour';\n } else if (s >= 45) {\n qty = min;\n units = 'minute';\n } else if (s >= 10) {\n qty = s;\n units = 'second';\n }\n\n return typeof (units) !== 'undefined' ? rtf.format(tense * qty, units) : 'just now';\n }\n\n set(prop: PropertyKey, value: unknown) {\n if (TimestampController.#isTimestampOptionKey(prop)) {\n // @ts-expect-error: seems typescript compiler isn't up to the task here\n this.#options[prop] = value;\n this.#host.requestUpdate();\n }\n }\n}\n"]}