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

bryanmylee / svelte-popperjs / 7051771841

30 Nov 2023 08:14PM UTC coverage: 100.0%. Remained the same
7051771841

Pull #29

github

web-flow
Merge b7d92fa12 into 128959b65
Pull Request #29: build(deps-dev): bump @adobe/css-tools from 4.0.1 to 4.3.2

19 of 19 branches covered (100.0%)

Branch coverage included in aggregate %.

100 of 100 relevant lines covered (100.0%)

5.8 hits per line

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

100.0
/src/index.ts
1
import type { Readable } from 'svelte/store';
1✔
2
import {
1✔
3
        createPopper,
1✔
4
        Instance,
1✔
5
        OptionsGeneric,
1✔
6
        Modifier,
1✔
7
        type VirtualElement,
1✔
8
} from '@popperjs/core';
1✔
9
import { onDestroy } from 'svelte';
1✔
10
export type { VirtualElement } from '@popperjs/core';
1✔
11

1✔
12
export type PopperOptions<TModifier> =
1✔
13
        | Partial<OptionsGeneric<TModifier>>
1✔
14
        | undefined;
1✔
15

1✔
16
export type ReferenceAction = (
1✔
17
        node: Element | VirtualElement | Readable<VirtualElement>
1✔
18
) => {
1✔
19
        destroy?(): void;
1✔
20
};
1✔
21

1✔
22
export type ContentAction<TModifier> = (
1✔
23
        node: HTMLElement,
1✔
24
        popperOptions?: PopperOptions<TModifier>
1✔
25
) => {
1✔
26
        update(popperOptions: PopperOptions<TModifier>): void;
1✔
27
        destroy(): void;
1✔
28
};
1✔
29

1✔
30
export function createPopperActions<
1✔
31
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
11✔
32
        TModifier extends Partial<Modifier<any, any>>
11✔
33
>(
11✔
34
        initOptions?: PopperOptions<TModifier>
11✔
35
): [ReferenceAction, ContentAction<TModifier>, () => Instance | null] {
11✔
36
        let popperInstance: Instance | null = null;
11✔
37
        let referenceNode: VirtualElement | Element | undefined;
11✔
38
        let contentNode: HTMLElement | undefined;
11✔
39
        let options: PopperOptions<TModifier> | undefined = initOptions;
11✔
40

11✔
41
        const initPopper = () => {
11✔
42
                if (referenceNode !== undefined && contentNode !== undefined) {
22✔
43
                        popperInstance = createPopper(referenceNode, contentNode, options);
11✔
44
                }
11✔
45
        };
22✔
46

11✔
47
        const deinitPopper = () => {
11✔
48
                if (popperInstance !== null) {
6✔
49
                        popperInstance.destroy();
5✔
50
                        popperInstance = null;
5✔
51
                }
5✔
52
        };
6✔
53

11✔
54
        const referenceAction: ReferenceAction = (node) => {
11✔
55
                if ('subscribe' in node) {
11✔
56
                        setupVirtualElementObserver(node);
1✔
57
                        return {};
1✔
58
                } else {
10✔
59
                        referenceNode = node;
10✔
60
                        initPopper();
10✔
61
                        return {
10✔
62
                                destroy() {
10✔
63
                                        deinitPopper();
2✔
64
                                },
2✔
65
                        };
10✔
66
                }
10✔
67
        };
11✔
68

11✔
69
        const setupVirtualElementObserver = (node: Readable<VirtualElement>) => {
11✔
70
                const unsubscribe = node.subscribe(($node) => {
1✔
71
                        if (referenceNode === undefined) {
2✔
72
                                referenceNode = $node;
1✔
73
                                initPopper();
1✔
74
                        } else {
1✔
75
                                // Preserve the reference to the virtual element.
1✔
76
                                Object.assign(referenceNode, $node);
1✔
77
                                popperInstance?.update();
1✔
78
                        }
1✔
79
                });
2✔
80
                onDestroy(unsubscribe);
1✔
81
        };
1✔
82

11✔
83
        const contentAction: ContentAction<TModifier> = (node, contentOptions?) => {
11✔
84
                contentNode = node;
11✔
85
                options = { ...initOptions, ...contentOptions };
11✔
86
                initPopper();
11✔
87
                return {
11✔
88
                        update(newContentOptions: PopperOptions<TModifier>) {
11✔
89
                                options = { ...initOptions, ...newContentOptions };
2✔
90
                                popperInstance?.setOptions(options);
2✔
91
                        },
2✔
92
                        destroy() {
11✔
93
                                deinitPopper();
4✔
94
                        },
4✔
95
                };
11✔
96
        };
11✔
97

11✔
98
        return [referenceAction, contentAction, () => popperInstance];
11✔
99
}
11✔
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