An open source design system that's fully customizable and agent ready
TypeScript
fix(ToggleButton): a caller onClick runs first and no longer replaces the toggle request (#7656)
Cindy Zhang09492662b1c4
diff --git a/.changeset/toggle-button-caller-onclick.md b/.changeset/toggle-button-caller-onclick.mdnew file mode 100644index 0000000000..bf371ef4cc--- /dev/null+++ b/.changeset/toggle-button-caller-onclick.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `ToggleButton` keeps toggling when it also receives an `onClick`, for example from a wrapper that renders it as its trigger. That `onClick` used to replace the toggle's own click handling, so `onPressedChange` never ran and a `ToggleButtonGroup` member never asked its group to toggle (`pressedChangeAction` still ran). The caller's `onClick` now runs first and the toggle request follows. On a standalone `ToggleButton`, calling `preventDefault()` in that `onClick` cancels the request: neither `onPressedChange` nor `pressedChangeAction` runs.++@cixzhangdiff --git a/packages/core/src/ToggleButton/ToggleButton.test.tsx b/packages/core/src/ToggleButton/ToggleButton.test.tsxindex 5e4d1135ea..1d44395276 100644--- a/packages/core/src/ToggleButton/ToggleButton.test.tsx+++ b/packages/core/src/ToggleButton/ToggleButton.test.tsx@@ -497,6 +497,78 @@ describe('ToggleButton', () => {expect(button).not.toHaveAttribute('aria-busy', 'true');await act(async () => {});});++ // family:buttons FR5: a caller onClick on a standalone ToggleButton runs+ // before onPressedChange and pressedChangeAction, never replaces the toggle+ // request, and cancels it only by calling preventDefault() itself.+ describe('with a caller onClick', () => {+ it('runs the caller onClick first, then requests the next state and runs the Action', async () => {+ const calls: string[] = [];+ render(+ <ToggleButton+ label="Compact"+ isPressed={false}+ onClick={() => calls.push('onClick')}+ onPressedChange={next => calls.push(`onPressedChange:${next}`)}+ pressedChangeAction={next => {+ calls.push(`pressedChangeAction:${next}`);+ }}+ />,+ );++ fireEvent.click(screen.getByRole('button', {name: 'Compact'}));+ await act(async () => {});++ expect(calls).toEqual([+ 'onClick',+ 'onPressedChange:true',+ 'pressedChangeAction:true',+ ]);+ });++ it('requests no next state when the caller onClick calls preventDefault', async () => {+ const onPressedChange = vi.fn();+ const pressedChangeAction = vi.fn();+ render(+ <ToggleButton+ label="Compact"+ isPressed={false}+ onClick={event => event.preventDefault()}+ onPressedChange={onPressedChange}+ pressedChangeAction={pressedChangeAction}+ />,+ );++ const button = screen.getByRole('button', {name: 'Compact'});+ fireEvent.click(button);+ await act(async () => {});++ expect(onPressedChange).not.toHaveBeenCalled();+ expect(pressedChangeAction).not.toHaveBeenCalled();+ expect(button).toHaveAttribute('aria-pressed', 'false');+ });++ it('still requests the next state when the click was already prevented before the caller onClick ran', () => {+ const onPressedChange = vi.fn();+ render(+ <div onClickCapture={event => event.preventDefault()}>+ <ToggleButton+ label="Compact"+ isPressed={false}+ onClick={() => {}}+ onPressedChange={onPressedChange}+ />+ </div>,+ );++ fireEvent.click(screen.getByRole('button', {name: 'Compact'}));++ expect(onPressedChange).toHaveBeenCalledExactlyOnceWith(+ true,+ expect.anything(),+ );+ });+ });});// =============================================================================@@ -721,6 +793,29 @@ describe.each(['single', 'multiple'] as const)(expect(onPressedChange).not.toHaveBeenCalled();expect(pressedChangeAction).not.toHaveBeenCalled();});++ // family:buttons FR11: a member's selection request comes from the group,+ // and a caller onClick on the member must not replace it.+ it('still asks the group to toggle a member that receives an onClick', async () => {+ const onChange = vi.fn();+ const onClick = vi.fn();+ const groupProps =+ type === 'single'+ ? {type, value: null, onChange}+ : {type, value: [], onChange};+ render(+ <ToggleButtonGroup {...groupProps} label="View">+ <ToggleButton value="grid" label="Grid" onClick={onClick} />+ </ToggleButtonGroup>,+ );++ await userEvent.setup().click(screen.getByRole('button', {name: 'Grid'}));++ expect(onClick).toHaveBeenCalledTimes(1);+ expect(onChange).toHaveBeenCalledExactlyOnceWith(+ type === 'single' ? 'grid' : ['grid'],+ );+ });},);diff --git a/packages/core/src/ToggleButton/ToggleButton.tsx b/packages/core/src/ToggleButton/ToggleButton.tsxindex a46c47c940..0c2716114a 100644--- a/packages/core/src/ToggleButton/ToggleButton.tsx+++ b/packages/core/src/ToggleButton/ToggleButton.tsx@@ -9,7 +9,8 @@* @position Thin wrapper over Button; adds controlled toggle pattern** ToggleButton wraps Button with `isPressed` and adds:- * - `onPressedChange` from internal `onClick`; `pressedChangeAction` via Button+ * - `onPressedChange` from internal `onClick`, after any caller `onClick`;+ * `pressedChangeAction` via Button* - `pressedIcon` for outline-to-filled icon swap* - Font weight shift on press with width reservation to prevent layout shift* - Group integration via ToggleButtonGroupContext@@ -258,6 +259,7 @@ export function ToggleButton({xstyle,className,style,+ onClick: onClickProp,...props}: ToggleButtonProps): ReactNode {const group = useToggleButtonGroup();@@ -287,11 +289,20 @@ export function ToggleButton({if (isDisabled) {return;}+ // A caller onClick (a trigger wrapper often supplies one) runs first and+ // never replaces the toggle's own request (family:buttons FR5, FR11).+ const wasPrevented = event.defaultPrevented;+ onClickProp?.(event);if (group && value != null) {group.toggle(value);event.preventDefault();return;}+ // A standalone toggle is cancelled only by the caller's own+ // preventDefault(); Button then skips pressedChangeAction too.+ if (!wasPrevented && event.defaultPrevented) {+ return;+ }onPressedChangeProp?.(nextPressed, event);};