• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adobe / spectrum-web-components / 12802187570

16 Jan 2025 04:36AM UTC coverage: 98.206% (-0.003%) from 98.209%
12802187570

Pull #5019

github

web-flow
Merge 012b0d7d4 into cee6892e5
Pull Request #5019: chore: fix update-spectrum-css script

5151 of 5424 branches covered (94.97%)

Branch coverage included in aggregate %.

33006 of 33430 relevant lines covered (98.73%)

380.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/tools/shared/src/observe-slot-presence.ts
1
/*
64✔
2
Copyright 2020 Adobe. All rights reserved.
64✔
3
This file is licensed to you under the Apache License, Version 2.0 (the "License");
64✔
4
you may not use this file except in compliance with the License. You may obtain a copy
64✔
5
of the License at http://www.apache.org/licenses/LICENSE-2.0
64✔
6
Unless required by applicable law or agreed to in writing, software distributed under
64✔
7
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
64✔
8
OF ANY KIND, either express or implied. See the License for the specific language
64✔
9
governing permissions and limitations under the License.
64✔
10
*/
64✔
11
import { ReactiveElement } from '@spectrum-web-components/base';
64✔
12
import { MutationController } from '@lit-labs/observers/mutation-controller.js';
64✔
13

64✔
14
const slotContentIsPresent = Symbol('slotContentIsPresent');
64✔
15

64✔
16
type Constructor<T = Record<string, unknown>> = {
64✔
17
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
64✔
18
    new (...args: any[]): T;
64✔
19
    prototype: T;
64✔
20
};
64✔
21

64✔
22
export interface SlotPresenceObservingInterface {
64✔
23
    slotContentIsPresent: boolean;
64✔
24
    getSlotContentPresence(selector: string): boolean;
64✔
25
    managePresenceObservedSlot(): void;
64✔
26
}
64✔
27

64✔
28
export function ObserveSlotPresence<T extends Constructor<ReactiveElement>>(
64✔
29
    constructor: T,
85✔
30
    lightDomSelector: string | string[]
85✔
31
): T & Constructor<SlotPresenceObservingInterface> {
85✔
32
    const lightDomSelectors = Array.isArray(lightDomSelector)
85✔
33
        ? lightDomSelector
23✔
34
        : [lightDomSelector];
62✔
35
    class SlotPresenceObservingElement
85✔
36
        extends constructor
85✔
37
        implements SlotPresenceObservingInterface
85✔
38
    {
85✔
39
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
85✔
40
        constructor(...args: any[]) {
85✔
41
            super(args);
5,542✔
42

5,542✔
43
            new MutationController(this, {
5,542✔
44
                config: {
5,542✔
45
                    childList: true,
5,542✔
46
                    subtree: true,
5,542✔
47
                },
5,542✔
48
                callback: () => {
5,542✔
49
                    this.managePresenceObservedSlot();
5,654✔
50
                },
5,654✔
51
            });
5,542✔
52

5,542✔
53
            this.managePresenceObservedSlot();
5,542✔
54
        }
5,542✔
55

85✔
56
        /**
85✔
57
         *  @private
85✔
58
         */
85✔
59
        public get slotContentIsPresent(): boolean {
85✔
60
            if (lightDomSelectors.length === 1) {
2,656✔
61
                return (
2,656✔
62
                    this[slotContentIsPresent].get(lightDomSelectors[0]) ||
2,656✔
63
                    false
2,612✔
64
                );
2,656✔
65
            } else {
2,656✔
66
                throw new Error(
×
67
                    'Multiple selectors provided to `ObserveSlotPresence` use `getSlotContentPresence(selector: string)` instead.'
×
68
                );
×
69
            }
×
70
        }
2,656✔
71
        private [slotContentIsPresent]: Map<string, boolean> = new Map();
85✔
72

85✔
73
        public getSlotContentPresence(selector: string): boolean {
85✔
74
            if (this[slotContentIsPresent].has(selector)) {
919✔
75
                return this[slotContentIsPresent].get(selector) || false;
919✔
76
            }
919✔
77
            throw new Error(
×
78
                `The provided selector \`${selector}\` is not being observed.`
×
79
            );
×
80
        }
919✔
81

85✔
82
        public managePresenceObservedSlot = (): void => {
85✔
83
            let changes = false;
11,212✔
84
            lightDomSelectors.forEach((selector) => {
11,212✔
85
                const nextValue = !!this.querySelector(`:scope > ${selector}`);
12,184✔
86
                const previousValue =
12,184✔
87
                    this[slotContentIsPresent].get(selector) || false;
12,184✔
88
                changes = changes || previousValue !== nextValue;
12,184✔
89
                this[slotContentIsPresent].set(
12,184✔
90
                    selector,
12,184✔
91
                    !!this.querySelector(`:scope > ${selector}`)
12,184✔
92
                );
12,184✔
93
            });
11,212✔
94
            if (changes) {
11,212✔
95
                this.updateComplete.then(() => {
305✔
96
                    this.requestUpdate();
305✔
97
                });
305✔
98
            }
305✔
99
        };
11,212✔
100
    }
85✔
101
    return SlotPresenceObservingElement;
85✔
102
}
85✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc