Skip to content

Commit fd57e7e

Browse files
fix(popover): clean up popover a bit
1 parent 915b3db commit fd57e7e

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

packages/kit-headless/src/components/combobox/combobox-popover.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ export const ComboboxPopover = component$(
1515
/* REMEMBER, whenever an option is selected or onMouseDown$ the listbox is closed, and the popover should sync to that */
1616
useTask$(async ({ track }) => {
1717
track(() => context.isListboxOpenSig.value);
18-
track(() => context.popoverRef.value);
1918

20-
console.log('POPOVER REF:', context.popoverRef.value);
19+
console.log('LISTBOX OPEN?: ', context.isListboxOpenSig.value);
2120

2221
if (context.isListboxOpenSig.value) {
2322
await initPopover$();

packages/kit-headless/src/components/popover/popover-impl.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,13 @@ export const PopoverImpl = component$<PopoverImplProps>((props) => {
5353
/** have we rendered on the client yet? 0: no, 1: force, 2: yes */
5454
const hasRenderedOnClientSig = useSignal(isServer ? 0 : 2);
5555
const teleportSig = useSignal(false);
56-
const isToggledSig = useSignal(false);
5756

5857
// This forces a re-render on each popover instance when the signal changes
5958
if (hasRenderedOnClientSig.value === 1) {
6059
// Now run the task again after we force-rendered the contex
6160
setTimeout(() => (teleportSig.value = true), 0);
6261
}
6362

64-
useTask$(({ track }) => {
65-
track(() => isToggledSig.value);
66-
67-
if (props.popoverRef) {
68-
props.popoverRef.value = popoverRef.value;
69-
}
70-
});
71-
7263
useTask$(async ({ track, cleanup }) => {
7364
track(() => teleportSig.value);
7465

@@ -104,7 +95,10 @@ export const PopoverImpl = component$<PopoverImplProps>((props) => {
10495
popover={props.manual || props.popover === 'manual' ? 'manual' : 'auto'}
10596
ref={popoverRef}
10697
onToggle$={(e) => {
107-
isToggledSig.value = true;
98+
if (props.popoverRef) {
99+
props.popoverRef.value = popoverRef.value;
100+
}
101+
108102
if (!popoverRef.value) return;
109103

110104
const popover = popoverRef.value;

packages/kit-headless/src/components/popover/popover-trigger.tsx

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ type PopoverTriggerProps = {
1010

1111
export function usePopover(popovertarget: string) {
1212
const hasPolyfillLoadedSig = useSignal<boolean>(false);
13-
const didInteractSig = useSignal<boolean>(false);
1413
const isSupportedSig = useSignal<boolean>(false);
1514

15+
const didInteractSig = useSignal<boolean>(false);
16+
const popoverSig = useSignal<HTMLElement | null>(null);
17+
18+
const hookExecutedSig = useSignal<boolean>(false);
19+
1620
const loadPolyfill$ = $(async () => {
1721
await import('@oddbird/popover-polyfill');
1822
document.dispatchEvent(new CustomEvent('poppolyload'));
@@ -26,7 +30,6 @@ export function usePopover(popovertarget: string) {
2630
'popover' in HTMLElement.prototype;
2731

2832
isSupportedSig.value = isSupported;
29-
console.log('INSIDE HERE: ', isSupportedSig.value);
3033

3134
if (!hasPolyfillLoadedSig.value && !isSupported) {
3235
await loadPolyfill$();
@@ -38,18 +41,26 @@ export function usePopover(popovertarget: string) {
3841
useTask$(({ track }) => {
3942
track(() => didInteractSig.value);
4043

41-
if (isBrowser && isSupportedSig.value) {
42-
const popover = document.getElementById(popovertarget);
44+
if (!isBrowser) return;
4345

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;
4554

46-
console.log('inside interact task: ', popover);
47-
console.log('popover id: ', popovertarget);
55+
if (!popover) return;
4856

4957
if (popover && popover.hasAttribute('popover')) {
5058
popover.showPopover();
5159
}
5260
}
61+
62+
console.log('HOOK EXECUTED');
63+
hookExecutedSig.value = true;
5364
});
5465

5566
// event is created after teleported properly
@@ -58,7 +69,7 @@ export function usePopover(popovertarget: string) {
5869
$(() => {
5970
if (!didInteractSig.value) return;
6071

61-
const popover = document.querySelector(`#${popovertarget}`);
72+
const popover = popoverSig.value;
6273

6374
if (!popover) return;
6475

@@ -72,12 +83,12 @@ export function usePopover(popovertarget: string) {
7283
}),
7384
);
7485

75-
return { initPopover$, isSupportedSig };
86+
return { initPopover$ };
7687
}
7788

7889
export const PopoverTrigger = component$<PopoverTriggerProps>(
7990
({ popovertarget, ...rest }: PopoverTriggerProps) => {
80-
const { initPopover$, isSupportedSig } = usePopover(popovertarget);
91+
const { initPopover$ } = usePopover(popovertarget);
8192

8293
return (
8394
<button
@@ -87,7 +98,6 @@ export const PopoverTrigger = component$<PopoverTriggerProps>(
8798
onClick$={[
8899
rest.onClick$,
89100
$(() => {
90-
console.log('isSupported: ', isSupportedSig.value);
91101
initPopover$();
92102
}),
93103
]}

0 commit comments

Comments
 (0)