@@ -10,9 +10,13 @@ type PopoverTriggerProps = {
10
10
11
11
export function usePopover ( popovertarget : string ) {
12
12
const hasPolyfillLoadedSig = useSignal < boolean > ( false ) ;
13
- const didInteractSig = useSignal < boolean > ( false ) ;
14
13
const isSupportedSig = useSignal < boolean > ( false ) ;
15
14
15
+ const didInteractSig = useSignal < boolean > ( false ) ;
16
+ const popoverSig = useSignal < HTMLElement | null > ( null ) ;
17
+
18
+ const hookExecutedSig = useSignal < boolean > ( false ) ;
19
+
16
20
const loadPolyfill$ = $ ( async ( ) => {
17
21
await import ( '@oddbird/popover-polyfill' ) ;
18
22
document . dispatchEvent ( new CustomEvent ( 'poppolyload' ) ) ;
@@ -26,7 +30,6 @@ export function usePopover(popovertarget: string) {
26
30
'popover' in HTMLElement . prototype ;
27
31
28
32
isSupportedSig . value = isSupported ;
29
- console . log ( 'INSIDE HERE: ' , isSupportedSig . value ) ;
30
33
31
34
if ( ! hasPolyfillLoadedSig . value && ! isSupported ) {
32
35
await loadPolyfill$ ( ) ;
@@ -38,18 +41,26 @@ export function usePopover(popovertarget: string) {
38
41
useTask$ ( ( { track } ) => {
39
42
track ( ( ) => didInteractSig . value ) ;
40
43
41
- if ( isBrowser && isSupportedSig . value ) {
42
- const popover = document . getElementById ( popovertarget ) ;
44
+ if ( ! isBrowser ) return ;
43
45
44
- if ( ! popover ) return ;
46
+ // get popover
47
+ if ( ! popoverSig . value ) {
48
+ popoverSig . value = document . getElementById ( popovertarget ) ;
49
+ }
50
+
51
+ // so it only runs once on click for supported browsers
52
+ if ( isSupportedSig . value ) {
53
+ const popover = popoverSig . value ;
45
54
46
- console . log ( 'inside interact task: ' , popover ) ;
47
- console . log ( 'popover id: ' , popovertarget ) ;
55
+ if ( ! popover ) return ;
48
56
49
57
if ( popover && popover . hasAttribute ( 'popover' ) ) {
50
58
popover . showPopover ( ) ;
51
59
}
52
60
}
61
+
62
+ console . log ( 'HOOK EXECUTED' ) ;
63
+ hookExecutedSig . value = true ;
53
64
} ) ;
54
65
55
66
// event is created after teleported properly
@@ -58,7 +69,7 @@ export function usePopover(popovertarget: string) {
58
69
$ ( ( ) => {
59
70
if ( ! didInteractSig . value ) return ;
60
71
61
- const popover = document . querySelector ( `# ${ popovertarget } ` ) ;
72
+ const popover = popoverSig . value ;
62
73
63
74
if ( ! popover ) return ;
64
75
@@ -72,12 +83,12 @@ export function usePopover(popovertarget: string) {
72
83
} ) ,
73
84
) ;
74
85
75
- return { initPopover$, isSupportedSig } ;
86
+ return { initPopover$ } ;
76
87
}
77
88
78
89
export const PopoverTrigger = component$ < PopoverTriggerProps > (
79
90
( { popovertarget, ...rest } : PopoverTriggerProps ) => {
80
- const { initPopover$, isSupportedSig } = usePopover ( popovertarget ) ;
91
+ const { initPopover$ } = usePopover ( popovertarget ) ;
81
92
82
93
return (
83
94
< button
@@ -87,7 +98,6 @@ export const PopoverTrigger = component$<PopoverTriggerProps>(
87
98
onClick$ = { [
88
99
rest . onClick$ ,
89
100
$ ( ( ) => {
90
- console . log ( 'isSupported: ' , isSupportedSig . value ) ;
91
101
initPopover$ ( ) ;
92
102
} ) ,
93
103
] }
0 commit comments