Skip to content

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

TypeScript

fix(core): correct rest-spread precedence on Kbd ARIA props (#3983)

Baraa Hamodi0c9b3ba2635c
diff --git a/.changeset/kbd-spread-precedence.md b/.changeset/kbd-spread-precedence.md
new file mode 100644
index 0000000000..ae9b46e225
--- /dev/null
+++ b/.changeset/kbd-spread-precedence.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Kbd: pass-through props no longer clobber the computed role and spoken accessible name (rest-spread precedence corrected, mirroring Avatar).
+@bhamodi
diff --git a/packages/core/src/Kbd/Kbd.test.tsx b/packages/core/src/Kbd/Kbd.test.tsx
index 48a811b5ba..65fc7e0725 100644
--- a/packages/core/src/Kbd/Kbd.test.tsx
+++ b/packages/core/src/Kbd/Kbd.test.tsx
@@ -119,4 +119,30 @@ describe('Kbd', () => {
expect(screen.getByText('⇧')).toBeInTheDocument();
expect(screen.getByText('+')).toBeInTheDocument();
});
+
+ it('keeps the computed role and aria-label when unrelated rest props are spread', () => {
+ render(<Kbd keys="mod+k" data-testid="kbd" id="shortcut" />);
+ const el = screen.getByTestId('kbd');
+ // Pass-through attributes land on the wrapper...
+ expect(el).toHaveAttribute('id', 'shortcut');
+ // ...without disturbing the computed accessibility contract.
+ expect(el).toHaveAttribute('role', 'img');
+ expect(el).toHaveAttribute('aria-label', 'Control + K');
+ });
+
+ it('computed role and aria-label win over consumer-passed overrides', () => {
+ // Contract props the component computes must not be clobbered by
+ // pass-through attributes: {...rest} is spread before role/aria-label.
+ render(
+ <Kbd
+ keys="mod+k"
+ role="presentation"
+ aria-label="custom"
+ data-testid="kbd"
+ />,
+ );
+ const el = screen.getByTestId('kbd');
+ expect(el).toHaveAttribute('role', 'img');
+ expect(el).toHaveAttribute('aria-label', 'Control + K');
+ });
});
diff --git a/packages/core/src/Kbd/Kbd.tsx b/packages/core/src/Kbd/Kbd.tsx
index 929e88a5a4..292fb9c6e3 100644
--- a/packages/core/src/Kbd/Kbd.tsx
+++ b/packages/core/src/Kbd/Kbd.tsx
@@ -188,10 +188,10 @@ export function Kbd({keys, ref, xstyle, className, style, ...rest}: KbdProps) {
return (
<span
+ {...rest}
ref={ref}
role="img"
aria-label={accessibleName}
- {...rest}
{...mergeProps(
themeProps('kbd'),
stylex.props(styles.wrapper, xstyle),