@spectrum-web-components/shared 0.14.2 → 0.14.3

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/README.md CHANGED
@@ -23,6 +23,10 @@ import {
23
23
  } from '@spectrum-web-components/shared';
24
24
  ```
25
25
 
26
+ ### getDeepElementFromPoint
27
+
28
+ The `getDeepElementFromPoint` method allows you to obtain the deepest possible element at a given coordinates on the current page. The method will step into any available `shadowRoot`s until it reaches the first element with no `shadowRoot` or no children available at the given coordinates.
29
+
26
30
  ### Focusable
27
31
 
28
32
  The `Focusable` subclass of `LitElement` adds some helpers method and lifecycle coverage in order to support passing focus to a container element inside of a custom element. The Focusable base class handles tabindex setting into shadowed elements automatically and is based heavily on the [aybolit delegate-focus-mixin](https://github.com/web-padawan/aybolit/blob/master/packages/core/src/mixins/delegate-focus-mixin.js).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spectrum-web-components/shared",
3
- "version": "0.14.2",
3
+ "version": "0.14.3",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -42,12 +42,13 @@
42
42
  "lit-html"
43
43
  ],
44
44
  "dependencies": {
45
- "@spectrum-web-components/base": "^0.5.7",
45
+ "@lit-labs/observers": "^1.0.1",
46
+ "@spectrum-web-components/base": "^0.5.8",
46
47
  "focus-visible": "^5.1.0",
47
48
  "tslib": "^2.0.0"
48
49
  },
49
50
  "types": "./src/index.d.ts",
50
51
  "customElements": "custom-elements.json",
51
52
  "sideEffects": false,
52
- "gitHead": "275ee61b152ac3eed0cd1603d8eab2aec90876a0"
53
+ "gitHead": "e5cd4a0db2201b8126eb9133ac3c24172d086359"
53
54
  }
@@ -0,0 +1 @@
1
+ export declare const getDeepElementFromPoint: (x: number, y: number) => Element | null;
@@ -0,0 +1,23 @@
1
+ /*
2
+ Copyright 2022 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+ export const getDeepElementFromPoint = (x, y) => {
13
+ let target = document.elementFromPoint(x, y);
14
+ while (target === null || target === void 0 ? void 0 : target.shadowRoot) {
15
+ const innerTarget = target.shadowRoot.elementFromPoint(x, y);
16
+ if (!innerTarget || innerTarget === target) {
17
+ break;
18
+ }
19
+ target = innerTarget;
20
+ }
21
+ return target;
22
+ };
23
+ //# sourceMappingURL=get-deep-element-from-point.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-deep-element-from-point.js","sourceRoot":"","sources":["get-deep-element-from-point.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACnC,CAAS,EACT,CAAS,EACK,EAAE;IAChB,IAAI,MAAM,GAAG,QAAQ,CAAC,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC7C,OAAO,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,UAAU,EAAE;QACvB,MAAM,WAAW,GACb,MAAM,CAAC,UAGV,CAAC,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACzB,IAAI,CAAC,WAAW,IAAI,WAAW,KAAK,MAAM,EAAE;YACxC,MAAM;SACT;QACD,MAAM,GAAG,WAAW,CAAC;KACxB;IACD,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC","sourcesContent":["/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nexport const getDeepElementFromPoint = (\n x: number,\n y: number\n): Element | null => {\n let target = document.elementFromPoint(x, y);\n while (target?.shadowRoot) {\n const innerTarget = (\n target.shadowRoot as unknown as {\n elementFromPoint: (x: number, y: number) => Element | null;\n }\n ).elementFromPoint(x, y);\n if (!innerTarget || innerTarget === target) {\n break;\n }\n target = innerTarget;\n }\n return target;\n};\n"]}
@@ -1,5 +1,4 @@
1
- const slotElementObserver = Symbol('slotElementObserver');
2
- const startObserving = Symbol('startObserving');
1
+ import { MutationController } from '@lit-labs/observers/mutation_controller.js';
3
2
  const slotContentIsPresent = Symbol('slotContentIsPresent');
4
3
  export function ObserveSlotPresence(constructor, lightDomSelector) {
5
4
  var _a;
@@ -7,15 +6,31 @@ export function ObserveSlotPresence(constructor, lightDomSelector) {
7
6
  ? lightDomSelector
8
7
  : [lightDomSelector];
9
8
  class SlotPresenceObservingElement extends constructor {
10
- constructor() {
11
- super(...arguments);
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ constructor(...args) {
11
+ super(args);
12
12
  this[_a] = new Map();
13
13
  this.managePresenceObservedSlot = () => {
14
+ let changes = false;
14
15
  lightDomSelectors.forEach((selector) => {
16
+ const nextValue = !!this.querySelector(selector);
17
+ const previousValue = this[slotContentIsPresent].get(selector) || false;
18
+ changes = changes || previousValue !== nextValue;
15
19
  this[slotContentIsPresent].set(selector, !!this.querySelector(selector));
16
20
  });
17
- this.requestUpdate();
21
+ if (changes)
22
+ this.requestUpdate();
18
23
  };
24
+ new MutationController(this, {
25
+ config: {
26
+ childList: true,
27
+ subtree: true,
28
+ },
29
+ callback: () => {
30
+ this.managePresenceObservedSlot();
31
+ },
32
+ });
33
+ this.managePresenceObservedSlot();
19
34
  }
20
35
  /**
21
36
  * @private
@@ -33,25 +48,10 @@ export function ObserveSlotPresence(constructor, lightDomSelector) {
33
48
  if (this[slotContentIsPresent].has(selector)) {
34
49
  return this[slotContentIsPresent].get(selector) || false;
35
50
  }
36
- throw new Error(`The provided selector \`\` is not being observed.`);
37
- }
38
- [(_a = slotContentIsPresent, startObserving)]() {
39
- const config = { childList: true, subtree: true };
40
- if (!this[slotElementObserver]) {
41
- this[slotElementObserver] = new MutationObserver(this.managePresenceObservedSlot);
42
- }
43
- this[slotElementObserver].observe(this, config);
44
- this.managePresenceObservedSlot();
45
- }
46
- connectedCallback() {
47
- super.connectedCallback();
48
- this[startObserving]();
49
- }
50
- disconnectedCallback() {
51
- this[slotElementObserver].disconnect();
52
- super.disconnectedCallback();
51
+ throw new Error(`The provided selector \`${selector}\` is not being observed.`);
53
52
  }
54
53
  }
54
+ _a = slotContentIsPresent;
55
55
  return SlotPresenceObservingElement;
56
56
  }
57
57
  //# sourceMappingURL=observe-slot-presence.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"observe-slot-presence.js","sourceRoot":"","sources":["observe-slot-presence.ts"],"names":[],"mappings":"AAYA,MAAM,mBAAmB,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC;AAC1D,MAAM,cAAc,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAChD,MAAM,oBAAoB,GAAG,MAAM,CAAC,sBAAsB,CAAC,CAAC;AAc5D,MAAM,UAAU,mBAAmB,CAC/B,WAAc,EACd,gBAAmC;;IAEnC,MAAM,iBAAiB,GAAG,KAAK,CAAC,OAAO,CAAC,gBAAgB,CAAC;QACrD,CAAC,CAAC,gBAAgB;QAClB,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC;IACzB,MAAM,4BACF,SAAQ,WAAW;QADvB;;YAqBY,QAAsB,GAAyB,IAAI,GAAG,EAAE,CAAC;YAW1D,+BAA0B,GAAG,GAAS,EAAE;gBAC3C,iBAAiB,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;oBACnC,IAAI,CAAC,oBAAoB,CA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;QAC3B,CAAC;QAEe,oBAAoB;YAChC,IAAI,CAAC,mBAAmB,CAAC,CAAC,UAAU,EAAE,CAAC;YACvC,KAAK,CAAC,oBAAoB,EAAE,CAAC;QACjC,CAAC;KACJ;IACD,OAAO,4BAA4B,CAAC;AACxC,CAAC","sourcesContent":["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport { ReactiveElement } from '@spectrum-web-components/base';\n\nconst slotElementObserver = Symbol('slotElementObserver');\nconst startObserving = Symbol('startObserving');\nconst slotContentIsPresent = Symbol('slotContentIsPresent');\n\ntype Constructor<T = Record<string, unknown>> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new (...args: any[]): T;\n prototype: T;\n};\n\nexport interface SlotPresenceObservingInterface {\n slotContentIsPresent: boolean;\n getSlotContentPresence(selector: string): boolean;\n managePresenceObservedSlot(): void;\n}\n\nexport function ObserveSlotPresence<T extends Constructor<ReactiveElement>>(\n constructor: T,\n lightDomSelector: string | string[]\n): T & Constructor<SlotPresenceObservingInterface> {\n const lightDomSelectors = Array.isArray(lightDomSelector)\n ? lightDomSelector\n : [lightDomSelector];\n class SlotPresenceObservingElement\n extends constructor\n implements SlotPresenceObservingInterface\n {\n private [slotElementObserver]!: MutationObserver;\n\n /**\n * @private\n */\n public get slotContentIsPresent(): boolean {\n if (lightDomSelectors.length === 1) {\n return (\n this[slotContentIsPresent].get(lightDomSelectors[0]) ||\n false\n );\n } else {\n throw new Error(\n 'Multiple selectors provided to `ObserveSlotPresence` use `getSlotContentPresence(selector: string)` instead.'\n );\n }\n }\n private [slotContentIsPresent]: Map<string, boolean> = new Map();\n\n public getSlotContentPresence(selector: string): boolean {\n if (this[slotContentIsPresent].has(selector)) {\n return this[slotContentIsPresent].get(selector) || false;\n }\n throw new Error(\n `The provided selector \\`\\` is not being observed.`\n );\n }\n\n public managePresenceObservedSlot = (): void => {\n lightDomSelectors.forEach((selector) => {\n this[slotContentIsPresent].set(\n selector,\n !!this.querySelector(selector)\n );\n });\n this.requestUpdate();\n };\n\n private [startObserving](): void {\n const config = { childList: true, subtree: true };\n if (!this[slotElementObserver]) {\n this[slotElementObserver] = new MutationObserver(\n this.managePresenceObservedSlot\n );\n }\n this[slotElementObserver].observe(this, config);\n this.managePresenceObservedSlot();\n }\n\n public override connectedCallback(): void {\n super.connectedCallback();\n this[startObserving]();\n }\n\n public override disconnectedCallback(): void {\n this[slotElementObserver].disconnect();\n super.disconnectedCallback();\n }\n }\n return SlotPresenceObservingElement;\n}\n"]}
1
+ {"version":3,"file":"observe-slot-presence.js","sourceRoot":"","sources":["observe-slot-presence.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,kBAAkB,EAAE,MAAM,4CAA4C,CAAC;AAEhF,MAAM,oBAAoB,GAAG,MAAM,CAAC,sBAAsB,CAAC,CAAC;AAc5D,MAAM,UAAU,mBAAmB,CAC/B,WAAc,EACd,gBAAmC;;IAEnC,MAAM,iBAAiB,GAAG,KAAK,CAAC,OAAO,CAAC,gBAAgB,CAAC;QACrD,CAAC,CAAC,gBAAgB;QAClB,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC;IACzB,MAAM,4BACF,SAAQ,WAAW;QAGnB,8DAA8D;QAC9D,YAAY,GAAG,IAAW;YACtB,KAAK,CAAC,IAAI,CAAC,CAAC;YA8BR,QAAsB,GAAyB,IAAI,GAAG,EAAE,CAAC;YAW1D,+BAA0B,GAAG,GAAS,EAAE;gBAC3C,IAAI,OAAO,GAAG,KAAK,CAAC;gBACpB,iBAAiB,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;oBACnC,MAA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QAAQ,2BAA2B,CACjE,CAAC;QACN,CAAC;KAgBJ;SAzBY,oBAAoB;IA0BjC,OAAO,4BAA4B,CAAC;AACxC,CAAC","sourcesContent":["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport { ReactiveElement } from '@spectrum-web-components/base';\nimport { MutationController } from '@lit-labs/observers/mutation_controller.js';\n\nconst slotContentIsPresent = Symbol('slotContentIsPresent');\n\ntype Constructor<T = Record<string, unknown>> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new (...args: any[]): T;\n prototype: T;\n};\n\nexport interface SlotPresenceObservingInterface {\n slotContentIsPresent: boolean;\n getSlotContentPresence(selector: string): boolean;\n managePresenceObservedSlot(): void;\n}\n\nexport function ObserveSlotPresence<T extends Constructor<ReactiveElement>>(\n constructor: T,\n lightDomSelector: string | string[]\n): T & Constructor<SlotPresenceObservingInterface> {\n const lightDomSelectors = Array.isArray(lightDomSelector)\n ? lightDomSelector\n : [lightDomSelector];\n class SlotPresenceObservingElement\n extends constructor\n implements SlotPresenceObservingInterface\n {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n constructor(...args: any[]) {\n super(args);\n\n new MutationController(this, {\n config: {\n childList: true,\n subtree: true,\n },\n callback: () => {\n this.managePresenceObservedSlot();\n },\n });\n\n this.managePresenceObservedSlot();\n }\n\n /**\n * @private\n */\n public get slotContentIsPresent(): boolean {\n if (lightDomSelectors.length === 1) {\n return (\n this[slotContentIsPresent].get(lightDomSelectors[0]) ||\n false\n );\n } else {\n throw new Error(\n 'Multiple selectors provided to `ObserveSlotPresence` use `getSlotContentPresence(selector: string)` instead.'\n );\n }\n }\n private [slotContentIsPresent]: Map<string, boolean> = new Map();\n\n public getSlotContentPresence(selector: string): boolean {\n if (this[slotContentIsPresent].has(selector)) {\n return this[slotContentIsPresent].get(selector) || false;\n }\n throw new Error(\n `The provided selector \\`${selector}\\` is not being observed.`\n );\n }\n\n public managePresenceObservedSlot = (): void => {\n let changes = false;\n lightDomSelectors.forEach((selector) => {\n const nextValue = !!this.querySelector(selector);\n const previousValue =\n this[slotContentIsPresent].get(selector) || false;\n changes = changes || previousValue !== nextValue;\n this[slotContentIsPresent].set(\n selector,\n !!this.querySelector(selector)\n );\n });\n if (changes) this.requestUpdate();\n };\n }\n return SlotPresenceObservingElement;\n}\n"]}
@@ -7,5 +7,5 @@ export interface SlotTextObservingInterface {
7
7
  slotHasContent: boolean;
8
8
  manageTextObservedSlot(): void;
9
9
  }
10
- export declare function ObserveSlotText<T extends Constructor<ReactiveElement>>(constructor: T, slotSelector?: string): T & Constructor<SlotTextObservingInterface>;
10
+ export declare function ObserveSlotText<T extends Constructor<ReactiveElement>>(constructor: T, slotName?: string): T & Constructor<SlotTextObservingInterface>;
11
11
  export {};
@@ -1,15 +1,28 @@
1
1
  import { __decorate } from "tslib";
2
2
  import { property, queryAssignedNodes, } from '@spectrum-web-components/base/src/decorators.js';
3
- const slotElementObserver = Symbol('slotElementObserver');
4
- // Fix needed for: https://github.com/lit/lit/issues/1789
3
+ import { MutationController } from '@lit-labs/observers/mutation_controller.js';
5
4
  const assignedNodesList = Symbol('assignedNodes');
6
- const startObserving = Symbol('startObserving');
7
- export function ObserveSlotText(constructor, slotSelector) {
5
+ export function ObserveSlotText(constructor, slotName) {
8
6
  var _a;
9
7
  class SlotTextObservingElement extends constructor {
10
- constructor() {
11
- super(...arguments);
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ constructor(...args) {
10
+ super(args);
12
11
  this.slotHasContent = false;
12
+ new MutationController(this, {
13
+ config: {
14
+ characterData: true,
15
+ subtree: true,
16
+ },
17
+ callback: (mutationsList) => {
18
+ for (const mutation of mutationsList) {
19
+ if (mutation.type === 'characterData') {
20
+ this.manageTextObservedSlot();
21
+ return;
22
+ }
23
+ }
24
+ },
25
+ });
13
26
  }
14
27
  manageTextObservedSlot() {
15
28
  if (!this[assignedNodesList])
@@ -22,40 +35,33 @@ export function ObserveSlotText(constructor, slotSelector) {
22
35
  });
23
36
  this.slotHasContent = assignedNodes.length > 0;
24
37
  }
25
- firstUpdated(changedProperties) {
26
- super.firstUpdated(changedProperties);
27
- this.manageTextObservedSlot();
28
- }
29
- [(_a = assignedNodesList, startObserving)]() {
30
- const config = { characterData: true, subtree: true };
31
- if (!this[slotElementObserver]) {
32
- const callback = (mutationsList) => {
33
- for (const mutation of mutationsList) {
34
- if (mutation.type === 'characterData') {
35
- this.manageTextObservedSlot();
36
- }
38
+ update(changedProperties) {
39
+ if (!this.hasUpdated) {
40
+ const { childNodes } = this;
41
+ const textNodes = [...childNodes].filter((node) => {
42
+ if (node.tagName) {
43
+ return slotName
44
+ ? node.getAttribute('slot') ===
45
+ slotName
46
+ : !node.hasAttribute('slot');
37
47
  }
38
- };
39
- this[slotElementObserver] = new MutationObserver(callback);
48
+ return node.textContent ? node.textContent.trim() : false;
49
+ });
50
+ this.slotHasContent = textNodes.length > 0;
40
51
  }
41
- this[slotElementObserver].observe(this, config);
42
- }
43
- connectedCallback() {
44
- super.connectedCallback();
45
- this[startObserving]();
52
+ super.update(changedProperties);
46
53
  }
47
- disconnectedCallback() {
48
- if (this[slotElementObserver]) {
49
- this[slotElementObserver].disconnect();
50
- }
51
- super.disconnectedCallback();
54
+ firstUpdated(changedProperties) {
55
+ super.firstUpdated(changedProperties);
56
+ this.manageTextObservedSlot();
52
57
  }
53
58
  }
59
+ _a = assignedNodesList;
54
60
  __decorate([
55
61
  property({ type: Boolean, attribute: false })
56
62
  ], SlotTextObservingElement.prototype, "slotHasContent", void 0);
57
63
  __decorate([
58
- queryAssignedNodes(slotSelector, true)
64
+ queryAssignedNodes(slotName, true)
59
65
  ], SlotTextObservingElement.prototype, _a, void 0);
60
66
  return SlotTextObservingElement;
61
67
  }
@@ -1 +1 @@
1
- {"version":3,"file":"observe-slot-text.js","sourceRoot":"","sources":["observe-slot-text.ts"],"names":[],"mappings":";AAYA,OAAO,EACH,QAAQ,EACR,kBAAkB,GACrB,MAAM,iDAAiD,CAAC;AAEzD,MAAM,mBAAmB,GAAG,MAAM,CAAC,qBAAqB,CAAC,CAAC;AAC1D,yDAAyD;AACzD,MAAM,iBAAiB,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AAClD,MAAM,cAAc,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAahD,MAAM,UAAU,eAAe,CAC3B,WAAc,EACd,YAAqB;;IAErB,MAAM,wBACF,SAAQ,WAAW;QADvB;;YAOW,mBAAc,GAAG,KAAK,CAAC;QAqDlC,CAAC;QAhDU,sBAAsB;YACzB,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC;gBAAE,OAAO;YACrC,MAAM,aAAa,GAAG,CAAC,GAAG,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,MAAM,CACrD,CAAC,IAAI,EAAE,EAAE;gBACL,IAAK,IAAoB,CAAC,OAAO,EAAE;oBAC/B,OAAO,IAAI,CAAC;iBACf;gBACD,OAAO,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;YAC9D,CAAC,CACJ,CAAC;YACF,IAAI,CAAC,cAAc,GAAG,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC;QACnD,CAAC;QAEkB,YAAY,CAC3B,iBAAiC;YAEjC,KAAK,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAC;YACtC,IAAI,CAAC,sBAAsB,EAAE,CAAC;QAClC,CAAC;QAEO,OAtBC,iBAAiB,EAsBjB,cAAc,EAAC;YACpB,MAAM,MAAM,GAAG,EAAE,aAAa,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;YACtD,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAAC,EAAE;gBAC5B,MAAM,QAAQ,GAAG,CACb,aAAoC,EAChC,EAAE;oBACN,KAAK,MAAM,QAAQ,IAAI,aAAa,EAAE;wBAClC,IAAI,QAAQ,CAAC,IAAI,KAAK,eAAe,EAAE;4BACnC,IAAI,CAAC,sBAAsB,EAAE,CAAC;yBACjC;qBACJ;gBACL,CAAC,CAAC;gBACF,IAAI,CAAC,mBAAmB,CAAC,GAAG,IAAI,gBAAgB,CAAC,QAAQ,CAAC,CAAC;aAC9D;YACD,IAAI,CAAC,mBAAmB,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACpD,CAAC;QAEe,iBAAiB;YAC7B,KAAK,CAAC,iBAAiB,EAAE,CAAC;YAC1B,IAAI,CAAC,cAAc,CAAC,EAAE,CAAC;QAC3B,CAAC;QAEe,oBAAoB;YAChC,IAAI,IAAI,CAAC,mBAAmB,CAAC,EAAE;gBAC3B,IAAI,CAAC,mBAAmB,CAAC,CAAC,UAAU,EAAE,CAAC;aAC1C;YACD,KAAK,CAAC,oBAAoB,EAAE,CAAC;QACjC,CAAC;KACJ;IArDG;QADC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;oEAChB;IAG9B;QADC,kBAAkB,CAAC,YAAY,EAAE,IAAI,CAAC;sDACe;IAmD1D,OAAO,wBAAwB,CAAC;AACpC,CAAC","sourcesContent":["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport { PropertyValues, ReactiveElement } from '@spectrum-web-components/base';\nimport {\n property,\n queryAssignedNodes,\n} from '@spectrum-web-components/base/src/decorators.js';\n\nconst slotElementObserver = Symbol('slotElementObserver');\n// Fix needed for: https://github.com/lit/lit/issues/1789\nconst assignedNodesList = Symbol('assignedNodes');\nconst startObserving = Symbol('startObserving');\n\ntype Constructor<T = Record<string, unknown>> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new (...args: any[]): T;\n prototype: T;\n};\n\nexport interface SlotTextObservingInterface {\n slotHasContent: boolean;\n manageTextObservedSlot(): void;\n}\n\nexport function ObserveSlotText<T extends Constructor<ReactiveElement>>(\n constructor: T,\n slotSelector?: string\n): T & Constructor<SlotTextObservingInterface> {\n class SlotTextObservingElement\n extends constructor\n implements SlotTextObservingInterface\n {\n private [slotElementObserver]!: MutationObserver;\n\n @property({ type: Boolean, attribute: false })\n public slotHasContent = false;\n\n @queryAssignedNodes(slotSelector, true)\n private [assignedNodesList]!: NodeListOf<HTMLElement>;\n\n public manageTextObservedSlot(): void {\n if (!this[assignedNodesList]) return;\n const assignedNodes = [...this[assignedNodesList]].filter(\n (node) => {\n if ((node as HTMLElement).tagName) {\n return true;\n }\n return node.textContent ? node.textContent.trim() : false;\n }\n );\n this.slotHasContent = assignedNodes.length > 0;\n }\n\n protected override firstUpdated(\n changedProperties: PropertyValues\n ): void {\n super.firstUpdated(changedProperties);\n this.manageTextObservedSlot();\n }\n\n private [startObserving](): void {\n const config = { characterData: true, subtree: true };\n if (!this[slotElementObserver]) {\n const callback = (\n mutationsList: Array<MutationRecord>\n ): void => {\n for (const mutation of mutationsList) {\n if (mutation.type === 'characterData') {\n this.manageTextObservedSlot();\n }\n }\n };\n this[slotElementObserver] = new MutationObserver(callback);\n }\n this[slotElementObserver].observe(this, config);\n }\n\n public override connectedCallback(): void {\n super.connectedCallback();\n this[startObserving]();\n }\n\n public override disconnectedCallback(): void {\n if (this[slotElementObserver]) {\n this[slotElementObserver].disconnect();\n }\n super.disconnectedCallback();\n }\n }\n return SlotTextObservingElement;\n}\n"]}
1
+ {"version":3,"file":"observe-slot-text.js","sourceRoot":"","sources":["observe-slot-text.ts"],"names":[],"mappings":";AAYA,OAAO,EACH,QAAQ,EACR,kBAAkB,GACrB,MAAM,iDAAiD,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,4CAA4C,CAAC;AAEhF,MAAM,iBAAiB,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AAalD,MAAM,UAAU,eAAe,CAC3B,WAAc,EACd,QAAiB;;IAEjB,MAAM,wBACF,SAAQ,WAAW;QAGnB,8DAA8D;QAC9D,YAAY,GAAG,IAAW;YACtB,KAAK,CAAC,IAAI,CAAC,CAAC;YAmBT,mBAAc,GAAG,KAAK,CAAC;YAjB1B,IAAI,kBAAkB,CAAC,IAAI,EAAE;gBACzB,MAAM,EAAE;oBACJ,aAAa,EAAE,IAAI;oBACnB,OAAO,EAAE,IAAI;iBAChB;gBACD,QAAQ,EAAE,CAAC,aAAoC,EAAE,EAAE;oBAC/C,KAAK,MAAM,QAAQ,IAAI,aAAa,EAAE;wBAClC,IAAI,QAAQ,CAAC,IAAI,KAAK,eAAe,EAAE;4BACnC,IAAI,CAAC,sBAAsB,EAAE,CAAC;4BAC9B,OAAO;yBACV;qBACJ;gBACL,CAAC;aACJ,CAAC,CAAC;QACP,CAAC;QAQM,sBAAsB;YACzB,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC;gBAAE,OAAO;YACrC,MAAM,aAAa,GAAG,CAAC,GAAG,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,MAAM,CACrD,CAAC,IAAI,EAAE,EAAE;gBACL,IAAK,IAAoB,CAAC,OAAO,EAAE;oBAC/B,OAAO,IAAI,CAAC;iBACf;gBACD,OAAO,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,EAA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, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport { PropertyValues, ReactiveElement } from '@spectrum-web-components/base';\nimport {\n property,\n queryAssignedNodes,\n} from '@spectrum-web-components/base/src/decorators.js';\nimport { MutationController } from '@lit-labs/observers/mutation_controller.js';\n\nconst assignedNodesList = Symbol('assignedNodes');\n\ntype Constructor<T = Record<string, unknown>> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new (...args: any[]): T;\n prototype: T;\n};\n\nexport interface SlotTextObservingInterface {\n slotHasContent: boolean;\n manageTextObservedSlot(): void;\n}\n\nexport function ObserveSlotText<T extends Constructor<ReactiveElement>>(\n constructor: T,\n slotName?: string\n): T & Constructor<SlotTextObservingInterface> {\n class SlotTextObservingElement\n extends constructor\n implements SlotTextObservingInterface\n {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n constructor(...args: any[]) {\n super(args);\n\n new MutationController(this, {\n config: {\n characterData: true,\n subtree: true,\n },\n callback: (mutationsList: Array<MutationRecord>) => {\n for (const mutation of mutationsList) {\n if (mutation.type === 'characterData') {\n this.manageTextObservedSlot();\n return;\n }\n }\n },\n });\n }\n\n @property({ type: Boolean, attribute: false })\n public slotHasContent = false;\n\n @queryAssignedNodes(slotName, true)\n private [assignedNodesList]!: NodeListOf<HTMLElement>;\n\n public manageTextObservedSlot(): void {\n if (!this[assignedNodesList]) return;\n const assignedNodes = [...this[assignedNodesList]].filter(\n (node) => {\n if ((node as HTMLElement).tagName) {\n return true;\n }\n return node.textContent ? node.textContent.trim() : false;\n }\n );\n this.slotHasContent = assignedNodes.length > 0;\n }\n\n protected override update(changedProperties: PropertyValues): void {\n if (!this.hasUpdated) {\n const { childNodes } = this;\n const textNodes = [...childNodes].filter((node) => {\n if ((node as HTMLElement).tagName) {\n return slotName\n ? (node as HTMLElement).getAttribute('slot') ===\n slotName\n : !(node as HTMLElement).hasAttribute('slot');\n }\n return node.textContent ? node.textContent.trim() : false;\n });\n this.slotHasContent = textNodes.length > 0;\n }\n super.update(changedProperties);\n }\n\n protected override firstUpdated(\n changedProperties: PropertyValues\n ): void {\n super.firstUpdated(changedProperties);\n this.manageTextObservedSlot();\n }\n }\n return SlotTextObservingElement;\n}\n"]}