Skip to content

Commit

Permalink
enable click to toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
huntabyte committed Feb 9, 2025
1 parent b1f4160 commit 73fb68a
Showing 1 changed file with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ class NavigationMenuTriggerState {
context: NavigationMenuProviderState;
itemContext: NavigationMenuItemState;
listContext: NavigationMenuListState;
hasPointerMoveOpened = $state(false);
wasClickClose = $state(false);
hasPointerMoveOpened = box(false);
wasClickClose = false;
open = $derived.by(
() => this.itemContext.opts.value.current === this.context.opts.value.current
);
Expand All @@ -369,6 +369,7 @@ class NavigationMenuTriggerState {
list: NavigationMenuListState;
}
) {
this.hasPointerMoveOpened = boxAutoReset(false, 300);
this.context = context.provider;
this.itemContext = context.item;
this.listContext = context.list;
Expand Down Expand Up @@ -409,22 +410,28 @@ class NavigationMenuTriggerState {
this.opts.disabled.current ||
this.wasClickClose ||
this.itemContext.wasEscapeClose ||
this.hasPointerMoveOpened
this.hasPointerMoveOpened.current
) {
return;
}
this.context.onTriggerEnter(this.itemContext.opts.value.current);
this.hasPointerMoveOpened = true;
this.hasPointerMoveOpened.current = true;
});

onpointerleave = whenMouse(() => {
if (this.opts.disabled.current) return;
this.context.onTriggerLeave();
this.hasPointerMoveOpened = false;
this.hasPointerMoveOpened.current = false;
});

onclick = (_: BitsMouseEvent<HTMLButtonElement>) => {
this.context.onItemSelect(this.itemContext.opts.value.current);
// if opened via pointer move, we prevent the click event
if (this.hasPointerMoveOpened.current) return;
if (this.open) {
this.context.onItemSelect("");
} else {
this.context.onItemSelect(this.itemContext.opts.value.current);
}
this.wasClickClose = this.open;
};

Expand Down

0 comments on commit 73fb68a

Please sign in to comment.