Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): ToggleButton keeps its reflected pressed state over a caller's (#9159)

Parinitha0464850d03c
diff --git a/.changeset/toggle-button-reflected-pressed-state.md b/.changeset/toggle-button-reflected-pressed-state.md
new file mode 100644
index 0000000000..ac1bce26d5
--- /dev/null
+++ b/.changeset/toggle-button-reflected-pressed-state.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A caller's `aria-pressed` or `data-is-pressed` no longer replaces the pressed state `ToggleButton` reflects (`architecture:public-component-api` INV5, `ToggleButton` FR12). Before, caller props were spread last, so passing either attribute made assistive technology and theme rules see a state that did not match `isPressed`. `ToggleButton` now owns `aria-pressed` and the reflected `data-is-pressed` and `data-elevation`. Other caller `data-*` attributes still reach the root.
+
+@parinith-web
diff --git a/packages/core/src/ToggleButton/ToggleButton.test.tsx b/packages/core/src/ToggleButton/ToggleButton.test.tsx
index 48af552dfb..f3c54951a5 100644
--- a/packages/core/src/ToggleButton/ToggleButton.test.tsx
+++ b/packages/core/src/ToggleButton/ToggleButton.test.tsx
@@ -652,6 +652,60 @@ describe('ToggleButtonGroup (single)', () => {
});
});
+describe('ToggleButton caller attributes', () => {
+ // Spread so the pressed-state attributes are passed the way a wrapper that
+ // forwards arbitrary DOM props would pass them.
+ const callerProps: Record<string, string> = {
+ 'data-is-pressed': 'bogus',
+ 'aria-pressed': 'mixed',
+ 'data-elevation': 'high',
+ 'data-testid-extra': 'kept',
+ };
+
+ it('keeps the reflected pressed state when a caller passes its own', () => {
+ render(
+ <>
+ <ToggleButton
+ label="Unpressed"
+ isPressed={false}
+ onPressedChange={() => {}}
+ {...callerProps}
+ />
+ <ToggleButton
+ label="Pressed"
+ isPressed
+ onPressedChange={() => {}}
+ {...callerProps}
+ />
+ </>,
+ );
+ const unpressed = screen.getByRole('button', {name: 'Unpressed'});
+ const pressed = screen.getByRole('button', {name: 'Pressed'});
+ expect(unpressed).toHaveAttribute('data-is-pressed', 'false');
+ expect(unpressed).toHaveAttribute('aria-pressed', 'false');
+ expect(pressed).toHaveAttribute('data-is-pressed', 'true');
+ expect(pressed).toHaveAttribute('aria-pressed', 'true');
+ // The reflected elevation (the default, none) wins over a caller's too.
+ expect(unpressed).toHaveAttribute('data-elevation', 'none');
+ expect(pressed).toHaveAttribute('data-elevation', 'none');
+ });
+
+ it('still forwards other caller data attributes to the root', () => {
+ render(
+ <ToggleButton
+ label="Favorite"
+ isPressed={false}
+ onPressedChange={() => {}}
+ {...callerProps}
+ />,
+ );
+ expect(screen.getByRole('button', {name: 'Favorite'})).toHaveAttribute(
+ 'data-testid-extra',
+ 'kept',
+ );
+ });
+});
+
// =============================================================================
// ToggleButtonGroup — Multiple mode
// =============================================================================
diff --git a/packages/core/src/ToggleButton/ToggleButton.tsx b/packages/core/src/ToggleButton/ToggleButton.tsx
index 0c2716114a..2aab8e39c6 100644
--- a/packages/core/src/ToggleButton/ToggleButton.tsx
+++ b/packages/core/src/ToggleButton/ToggleButton.tsx
@@ -29,7 +29,7 @@ import {Button, type ButtonSize} from '../Button';
import type {Elevation} from '../utils/types';
import {useToggleButtonGroup} from './ToggleButtonGroup';
import type {BaseProps} from '../BaseProps';
-import {themeProps} from '../utils/themeProps';
+import {themeDataAttributes, themeProps} from '../utils/themeProps';
import {mergeProps} from '../utils/mergeProps';
// =============================================================================
@@ -352,7 +352,6 @@ export function ToggleButton({
isLoading={isLoading}
isInterruptible
isIconOnly={isIconOnly}
- aria-pressed={isPressed}
icon={resolvedIcon}
tooltip={tooltip}
{...mergeProps(
@@ -366,7 +365,14 @@ export function ToggleButton({
style={style}
onClick={handleClick}
clickAction={clickAction}
- {...props}>
+ {...props}
+ // The pressed state this component owns wins over a caller's
+ // same-named attributes (public-component-api INV5).
+ aria-pressed={isPressed}
+ {...themeDataAttributes({
+ isPressed: isPressed ? 'true' : 'false',
+ elevation,
+ })}>
{labelContent}
</Button>
);