diff --git a/.changeset/rude-panthers-cough.md b/.changeset/rude-panthers-cough.md new file mode 100644 index 000000000..2881dc920 --- /dev/null +++ b/.changeset/rude-panthers-cough.md @@ -0,0 +1,5 @@ +--- +"bits-ui": patch +--- + +fix: bug where menu `onChange` functions were being called before change diff --git a/src/lib/bits/context-menu/components/ContextMenuCheckboxItem.svelte b/src/lib/bits/context-menu/components/ContextMenuCheckboxItem.svelte index 2d6c1359a..3cbea2b9e 100644 --- a/src/lib/bits/context-menu/components/ContextMenuCheckboxItem.svelte +++ b/src/lib/bits/context-menu/components/ContextMenuCheckboxItem.svelte @@ -18,8 +18,10 @@ disabled, defaultChecked: checked, onCheckedChange: ({ next }) => { - checked = next; - onCheckedChange?.(next); + if (checked !== next) { + onCheckedChange?.(next); + checked = next; + } return next; } }); diff --git a/src/lib/bits/context-menu/components/ContextMenuRadioGroup.svelte b/src/lib/bits/context-menu/components/ContextMenuRadioGroup.svelte index ff5dbc3cc..15accf94b 100644 --- a/src/lib/bits/context-menu/components/ContextMenuRadioGroup.svelte +++ b/src/lib/bits/context-menu/components/ContextMenuRadioGroup.svelte @@ -14,7 +14,7 @@ } = setRadioGroupCtx({ defaultValue: value, onValueChange: ({ next }) => { - if (next) { + if (next && value !== next) { onValueChange?.(next); value = next; } diff --git a/src/lib/bits/dropdown-menu/components/DropdownMenuCheckboxItem.svelte b/src/lib/bits/dropdown-menu/components/DropdownMenuCheckboxItem.svelte index 2f6d89bc4..054536d19 100644 --- a/src/lib/bits/dropdown-menu/components/DropdownMenuCheckboxItem.svelte +++ b/src/lib/bits/dropdown-menu/components/DropdownMenuCheckboxItem.svelte @@ -19,8 +19,10 @@ disabled, defaultChecked: checked, onCheckedChange: ({ next }) => { - onCheckedChange?.(next); - checked = next; + if (checked !== next) { + onCheckedChange?.(next); + checked = next; + } return next; } }); diff --git a/src/lib/bits/dropdown-menu/components/DropdownMenuRadioGroup.svelte b/src/lib/bits/dropdown-menu/components/DropdownMenuRadioGroup.svelte index ff5dbc3cc..15accf94b 100644 --- a/src/lib/bits/dropdown-menu/components/DropdownMenuRadioGroup.svelte +++ b/src/lib/bits/dropdown-menu/components/DropdownMenuRadioGroup.svelte @@ -14,7 +14,7 @@ } = setRadioGroupCtx({ defaultValue: value, onValueChange: ({ next }) => { - if (next) { + if (next && value !== next) { onValueChange?.(next); value = next; } diff --git a/src/lib/bits/menubar/components/MenubarCheckboxItem.svelte b/src/lib/bits/menubar/components/MenubarCheckboxItem.svelte index 8f11b69d3..1fb390c09 100644 --- a/src/lib/bits/menubar/components/MenubarCheckboxItem.svelte +++ b/src/lib/bits/menubar/components/MenubarCheckboxItem.svelte @@ -19,8 +19,10 @@ disabled, defaultChecked: checked, onCheckedChange: ({ next }) => { - onCheckedChange?.(next); - checked = next; + if (checked !== next) { + onCheckedChange?.(next); + checked = next; + } return next; } }); diff --git a/src/lib/bits/menubar/components/MenubarRadioGroup.svelte b/src/lib/bits/menubar/components/MenubarRadioGroup.svelte index 9787b406b..493883fc6 100644 --- a/src/lib/bits/menubar/components/MenubarRadioGroup.svelte +++ b/src/lib/bits/menubar/components/MenubarRadioGroup.svelte @@ -14,9 +14,9 @@ } = setRadioGroupCtx({ defaultValue: value, onValueChange: ({ next }) => { - if (next) { - value = next; + if (next && next !== value) { onValueChange?.(next); + value = next; } return next; }