PPolaris ComponentsConnect an agent

Keyboard key

Renders a single key as a physical keycap, combined with InlineStack to document a keyboard shortcut such as Ctrl + S.

Preview

Code

Example.jsx
import { Card, BlockStack, InlineStack, Text, KeyboardKey, Divider } from '@shopify/polaris';

const SHORTCUTS = [
  { id: 'save', keys: ['Ctrl', 'S'], description: 'Save the current draft' },
  { id: 'search', keys: ['Ctrl', 'K'], description: 'Open search' },
  { id: 'new', keys: ['Ctrl', 'Shift', 'N'], description: 'Create a new product' },
  { id: 'close', keys: ['Esc'], description: 'Close the modal' },
];

const Combination = ({ keys, size }) => (
  <InlineStack gap="100" blockAlign="center" wrap={false}>
    {keys.map((key, index) => (
      <InlineStack key={key} gap="100" blockAlign="center" wrap={false}>
        {index > 0 && (
          <Text as="span" tone="subdued">
            +
          </Text>
        )}
        <KeyboardKey size={size}>{key}</KeyboardKey>
      </InlineStack>
    ))}
  </InlineStack>
);

export const Example = () => (
  <Card>
    <BlockStack gap="500">
      <BlockStack gap="300">
        <Text as="h2" variant="headingSm" tone="subdued">
          Keyboard shortcuts
        </Text>
        <BlockStack gap="300">
          {SHORTCUTS.map((shortcut) => (
            <InlineStack key={shortcut.id} align="space-between" blockAlign="center" gap="400">
              <Text as="span">{shortcut.description}</Text>
              <Combination keys={shortcut.keys} />
            </InlineStack>
          ))}
        </BlockStack>
      </BlockStack>

      <Divider />

      <BlockStack gap="300">
        <Text as="h2" variant="headingSm" tone="subdued">
          Small size
        </Text>
        <InlineStack gap="200" blockAlign="center">
          <Text as="span" variant="bodySm">
            Press
          </Text>
          <Combination keys={['Ctrl', 'S']} size="small" />
          <Text as="span" variant="bodySm">
            to save without leaving the page.
          </Text>
        </InlineStack>
      </BlockStack>
    </BlockStack>
  </Card>
);