Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: various pointer/click issues #794

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-moons-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": patch
---

fix: various pointer/click issues
14 changes: 2 additions & 12 deletions packages/bits-ui/src/lib/bits/checkbox/checkbox.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,11 @@ class CheckboxRootState {
}
};

#onpointerdown = (e: PointerEvent) => {
#onclick = () => {
if (this.disabled.current) return;
if (e.pointerType === "touch" || e.button !== 0) return e.preventDefault();
this.#toggle();
};

#onpointerup = (e: PointerEvent) => {
if (this.disabled.current) return;
if (e.pointerType === "touch") {
e.preventDefault();
this.#toggle();
}
};

props = $derived.by(
() =>
({
Expand All @@ -87,8 +78,7 @@ class CheckboxRootState {
"data-state": getCheckboxDataState(this.checked.current),
[CHECKBOX_ROOT_ATTR]: "",
//
onpointerdown: this.#onpointerdown,
onpointerup: this.#onpointerup,
onclick: this.#onclick,
onkeydown: this.#onkeydown,
}) as const
);
Expand Down
4 changes: 4 additions & 0 deletions packages/bits-ui/src/lib/bits/dialog/dialog.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ class DialogTriggerState {
#onpointerdown = (e: PointerEvent) => {
if (this.#disabled.current) return;
if (e.pointerType === "touch") return e.preventDefault();
if (e.button > 0) return;
// by default, it will attempt to focus this trigger on pointerdown
// since this also opens the dialog we want to prevent that behavior
e.preventDefault();

this.#root.handleOpen();
};

Expand Down Expand Up @@ -159,6 +161,7 @@ class DialogCloseState {
#onpointerdown = (e: PointerEvent) => {
if (this.#disabled.current) return;
if (e.pointerType === "touch") return e.preventDefault();
if (e.button > 0) return;
this.#root.handleClose();
};

Expand Down Expand Up @@ -392,6 +395,7 @@ class AlertDialogCancelState {
#onpointerdown = (e: PointerEvent) => {
if (this.#disabled.current) return;
if (e.pointerType === "touch") return e.preventDefault();
if (e.button > 0) return;
this.#root.handleClose();
};

Expand Down
14 changes: 2 additions & 12 deletions packages/bits-ui/src/lib/bits/switch/switch.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,8 @@ class SwitchRootState {
this.#toggle();
};

#onpointerup = (e: PointerEvent) => {
#onclick = (e: PointerEvent) => {
if (this.disabled.current) return;
if (e.pointerType === "touch") {
e.preventDefault();
this.#toggle();
}
};

#onpointerdown = (e: PointerEvent) => {
if (this.disabled.current) return;
if (e.pointerType === "touch") return e.preventDefault();
this.#toggle();
};

Expand All @@ -92,8 +83,7 @@ class SwitchRootState {
"aria-required": getAriaRequired(this.required.current),
[ROOT_ATTR]: "",
//
onpointerdown: this.#onpointerdown,
onpointerup: this.#onpointerup,
onclick: this.#onclick,
onkeydown: this.#onkeydown,
}) as const
);
Expand Down
16 changes: 3 additions & 13 deletions packages/bits-ui/src/lib/bits/toggle-group/toggle-group.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,9 @@ class ToggleGroupItemState {
this.#root.toggleItem(this.#value.current, this.#id.current);
};

#onpointerdown = (e: PointerEvent) => {
#onclick = () => {
if (this.#isDisabled) return;
if (e.pointerType === "touch" || e.button !== 0) return e.preventDefault();
this.toggleItem();
};

#onpointerup = (e: PointerEvent) => {
if (this.#isDisabled) return;
if (e.pointerType === "touch") {
e.preventDefault();
this.toggleItem();
}
this.#root.toggleItem(this.#value.current, this.#id.current);
};

#onkeydown = (e: KeyboardEvent) => {
Expand Down Expand Up @@ -233,8 +224,7 @@ class ToggleGroupItemState {
disabled: getDisabled(this.#isDisabled),
[ITEM_ATTR]: "",
//
onpointerdown: this.#onpointerdown,
onpointerup: this.#onpointerup,
onclick: this.#onclick,
onkeydown: this.#onkeydown,
}) as const
);
Expand Down
22 changes: 2 additions & 20 deletions packages/bits-ui/src/lib/bits/toggle/toggle.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,8 @@ class ToggleRootState {
}
};

#onpointerdown = (e: PointerEvent) => {
#onclick = () => {
if (this.#disabled.current) return;
if (e.pointerType === "touch" || e.button !== 0) return e.preventDefault();
this.#togglePressed();
};

#onpointerup = (e: PointerEvent) => {
if (this.#disabled.current) return;
if (e.pointerType === "touch") {
e.preventDefault();
this.#togglePressed();
}
};

#onkeydown = (e: KeyboardEvent) => {
if (this.#disabled.current) return;
if (![kbd.ENTER, kbd.SPACE].includes(e.key)) return;
e.preventDefault();
this.#togglePressed();
};

Expand All @@ -68,9 +52,7 @@ class ToggleRootState {
"aria-pressed": getAriaPressed(this.pressed.current),
"data-state": getToggleDataState(this.pressed.current),
disabled: getDisabled(this.#disabled.current),
onpointerdown: this.#onpointerdown,
onpointerup: this.#onpointerup,
onkeydown: this.#onkeydown,
onclick: this.#onclick,
}) as const
);
}
Expand Down