Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-keybinding-hint-mod-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

KeybindingHint: Sort Mod alongside Command on Apple platforms and Control elsewhere in displayed shortcuts and accessible descriptions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions packages/react/src/KeybindingHint/KeybindingHint.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {page} from 'vitest/browser'

import {KeybindingHint, getAccessibleKeybindingHintString} from '../KeybindingHint'
import {implementsClassName} from '../utils/testing'
import {PlatformOverrideProvider, type Platform} from './platform'

describe('KeybindingHint', () => {
implementsClassName(props => <KeybindingHint keys="Control" {...props} />)
Expand Down Expand Up @@ -50,6 +51,27 @@ describe('KeybindingHint', () => {
expect(names).toEqual(namesInOrder)
})

it.each<{
platform: Platform
expected: string[]
}>([
{platform: 'apple', expected: ['Control', 'Command', 'Command', 'Option', 'Shift', 'K']},
{platform: 'windows', expected: ['Control', 'Control', 'Windows', 'Alt', 'Shift', 'K']},
{platform: 'other', expected: ['Control', 'Control', 'Meta', 'Alt', 'Shift', 'K']},
])('sorts Mod like its resolved modifier on $platform', ({platform, expected}) => {
render(
<PlatformOverrideProvider value={platform}>
<KeybindingHint format="full" keys="Meta+Shift+mOd+Alt+Control+K" />
</PlatformOverrideProvider>,
)
const names = page
.getByText(/^(Command|Control|Windows|Meta|Alt|Option|Shift|K)$/)
.elements()
.filter(el => el.hasAttribute('aria-hidden'))
.map(el => el.textContent)
expect(names).toEqual(expected)
})

it('capitalizes other keys', async () => {
render(<KeybindingHint format="condensed" keys="control+a" />)
for (const key of ['⌃', 'A']) await expect.element(page.getByText(key, {exact: true})).toBeInTheDocument()
Expand Down Expand Up @@ -103,6 +125,17 @@ describe('getAccessibleKeybindingHintString', () => {
it('sorts modifiers in standard order', () =>
expect(getAccessibleKeybindingHintString('Alt+Shift+Command+%', false)).toBe('alt shift command percent'))

it.each<{
platform: Platform
expected: string
}>([
{platform: 'apple', expected: 'control command command option shift k then command shift p'},
{platform: 'windows', expected: 'control control Windows alt shift k then control shift p'},
{platform: 'other', expected: 'control control meta alt shift k then control shift p'},
])('sorts Mod in accessible sequences on $platform', ({platform, expected}) => {
expect(getAccessibleKeybindingHintString('Meta+Shift+mOd+Alt+Control+K Shift+Mod+P', platform)).toBe(expected)
})

it('joins chords in a sequence with "then"', () =>
expect(getAccessibleKeybindingHintString('Alt+9 x y', false)).toBe('alt 9 then x then y'))

Expand Down
48 changes: 26 additions & 22 deletions packages/react/src/KeybindingHint/components/Chord.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@ import {Key} from './Key'
import {clsx} from 'clsx'
import classes from './Chord.module.css'
import {splitChord} from './utils'
import {usePlatform} from '../platform'

export const Chord = ({keys, format = 'condensed', variant = 'normal', size = 'normal'}: KeybindingHintProps) => (
<Text
data-kbd-chord
className={clsx(classes.Chord, {
[classes.ChordNormal]: variant === 'normal',
[classes.ChordOnEmphasis]: variant === 'onEmphasis',
[classes.ChordOnPrimary]: variant === 'onPrimary',
[classes.ChordSmall]: size === 'small',
})}
>
{splitChord(keys).map((k, i) => (
<Fragment key={i}>
{i > 0 && format === 'full' ? (
<span aria-hidden> + </span> // hiding the plus sign helps screen readers be more concise
) : (
' ' // space is nonvisual due to flex layout but critical for labelling / screen readers
)}
export const Chord = ({keys, format = 'condensed', variant = 'normal', size = 'normal'}: KeybindingHintProps) => {
const platform = usePlatform()
return (
<Text
data-kbd-chord
className={clsx(classes.Chord, {
[classes.ChordNormal]: variant === 'normal',
[classes.ChordOnEmphasis]: variant === 'onEmphasis',
[classes.ChordOnPrimary]: variant === 'onPrimary',
[classes.ChordSmall]: size === 'small',
})}
>
{splitChord(keys, platform).map((k, i) => (
<Fragment key={i}>
{i > 0 && format === 'full' ? (
<span aria-hidden> + </span> // hiding the plus sign helps screen readers be more concise
) : (
' ' // space is nonvisual due to flex layout but critical for labelling / screen readers
)}

<Key name={k} format={format} />
</Fragment>
))}
</Text>
)
<Key name={k} format={format} />
</Fragment>
))}
</Text>
)
}
13 changes: 7 additions & 6 deletions packages/react/src/KeybindingHint/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ const keySortPriorities: Partial<Record<string, number>> = {
function: 6,
}

const keySortPriority = (priority: string) => keySortPriorities[priority] ?? Infinity

const compareLowercaseKeys = (a: string, b: string) => keySortPriority(a) - keySortPriority(b)
const keySortPriority = (key: string, platform: Platform) => {
const resolvedKey = key === 'mod' ? (platform === 'apple' ? 'meta' : 'control') : key
return keySortPriorities[resolvedKey] ?? Infinity
}

/** Split and sort the chord keys in standard order. */
export const splitChord = (chord: string) =>
export const splitChord = (chord: string, platform: Platform) =>
chord
.split('+')
.map(k => k.toLowerCase())
.sort(compareLowercaseKeys)
.sort((a, b) => keySortPriority(a, platform) - keySortPriority(b, platform))

const splitSequence = (sequence: string) => sequence.split(' ')

/** Plain string version of `Chord` for use in `aria` string attributes. */
export const accessibleChordString = (chord: string, platform: Platform) =>
splitChord(chord)
splitChord(chord, platform)
.map(key => accessibleKeyName(key, platform))
.join(' ')

Expand Down
1 change: 1 addition & 0 deletions packages/react/src/KeybindingHint/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface KeybindingHintProps {
* represents "a then b". To represent the " " key, use "Space".
*
* The fake key name "Mod" can be used to represent "Command" on macOS and "Control" on other platforms.
* It is sorted alongside the corresponding platform modifier.
*
* See https://github.com/github/hotkey for format details.
*/
Expand Down
Loading