Skip to content

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

TypeScript

fix(Kbd): add platform detection for mod key (#722)

Cindy Zhang975cc92b5b26
diff --git a/packages/core/src/Kbd/XDSKbd.test.tsx b/packages/core/src/Kbd/XDSKbd.test.tsx
index 586ac5df7b..b0d421b1a8 100644
--- a/packages/core/src/Kbd/XDSKbd.test.tsx
+++ b/packages/core/src/Kbd/XDSKbd.test.tsx
@@ -2,6 +2,9 @@
* @file XDSKbd.test.tsx
* @input Uses React Testing Library, XDSKbd
* @output Tests for XDSKbd component
+ *
+ * Note: Tests run in jsdom which reports a non-Mac platform,
+ * so `mod` resolves to "Ctrl" rather than "⌘".
*/
import {render, screen} from '@testing-library/react';
@@ -16,11 +19,17 @@ describe('XDSKbd', () => {
it('renders multiple keys separated by +', () => {
render(<XDSKbd keys="mod+k" />);
- // mod should render as ⌘
- expect(screen.getByText('\u2318')).toBeInTheDocument();
+ // In jsdom (non-Mac), mod renders as "Ctrl"
+ expect(screen.getByText('Ctrl')).toBeInTheDocument();
expect(screen.getByText('K')).toBeInTheDocument();
});
+ it('renders mod as Ctrl on non-Mac platforms', () => {
+ // jsdom is a non-Mac environment, so mod → Ctrl
+ render(<XDSKbd keys="mod" />);
+ expect(screen.getByText('Ctrl')).toBeInTheDocument();
+ });
+
it('maps modifier keys to symbols', () => {
render(<XDSKbd keys="ctrl+alt+shift+k" />);
expect(screen.getByText('\u2303')).toBeInTheDocument(); // ⌃
@@ -52,7 +61,8 @@ describe('XDSKbd', () => {
it('handles whitespace around keys', () => {
render(<XDSKbd keys="mod + k" />);
- expect(screen.getByText('\u2318')).toBeInTheDocument();
+ // In jsdom (non-Mac), mod renders as "Ctrl"
+ expect(screen.getByText('Ctrl')).toBeInTheDocument();
expect(screen.getByText('K')).toBeInTheDocument();
});
diff --git a/packages/core/src/Kbd/XDSKbd.tsx b/packages/core/src/Kbd/XDSKbd.tsx
index f275396013..3e9570ff52 100644
--- a/packages/core/src/Kbd/XDSKbd.tsx
+++ b/packages/core/src/Kbd/XDSKbd.tsx
@@ -50,11 +50,27 @@ const styles = stylex.create({
},
});
+/**
+ * Lazy platform detection — SSR-safe, cached after first call.
+ * Returns true on macOS, iPhone, iPad, iPod; false elsewhere (including SSR).
+ */
+let _isMac: boolean | null = null;
+function isMac(): boolean {
+ if (_isMac === null) {
+ _isMac =
+ typeof navigator !== 'undefined' &&
+ /Mac|iPhone|iPad|iPod/.test(
+ navigator.platform ?? navigator.userAgent ?? '',
+ );
+ }
+ return _isMac;
+}
+
/**
* Map of modifier key names to display symbols.
+ * Note: `mod` is not in this map — it resolves dynamically via `getKeyDisplay`.
*/
const KEY_DISPLAY: Record<string, string> = {
- mod: '\u2318', // ⌘
ctrl: '\u2303', // ⌃
alt: '\u2325', // ⌥
shift: '\u21E7', // ⇧
@@ -68,6 +84,18 @@ const KEY_DISPLAY: Record<string, string> = {
right: '\u2192',
};
+/**
+ * Resolves a key name to its display string. Handles the platform-aware
+ * `mod` key (⌘ on macOS, Ctrl on other platforms) and falls back to
+ * KEY_DISPLAY or uppercased key name.
+ */
+function getKeyDisplay(key: string): string {
+ if (key === 'mod') {
+ return isMac() ? '\u2318' : 'Ctrl';
+ }
+ return KEY_DISPLAY[key] ?? key.toUpperCase();
+}
+
export interface XDSKbdProps {
/**
* Keyboard shortcut string. Use "+" to separate keys.
@@ -130,7 +158,7 @@ export function XDSKbd({keys, xstyle, className, style}: XDSKbdProps) {
aria-hidden="true">
{parts.map((key, i) => (
<kbd key={i} {...stylex.props(styles.kbd)}>
- {KEY_DISPLAY[key] ?? key.toUpperCase()}
+ {getKeyDisplay(key)}
</kbd>
))}
</span>