Skip to content
Merged
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/gentle-apes-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

Convert KeybindingHint to CSS modules behind feature flag
15 changes: 15 additions & 0 deletions packages/react/src/KeybindingHint/KeybindingHint.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.KeybindingHint {
position: relative;
padding: 0;
overflow: visible;
font-family: inherit;
font-size: inherit;
/* stylelint-disable-next-line primer/typography */
line-height: unset;
color: inherit;
white-space: nowrap;
vertical-align: baseline;
background: none;
border: none;
box-shadow: none;
}
60 changes: 37 additions & 23 deletions packages/react/src/KeybindingHint/KeybindingHint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,48 @@ import {memo} from 'react'
import Text from '../Text'
import type {KeybindingHintProps} from './props'
import {accessibleSequenceString, Sequence} from './components/Sequence'
import {useFeatureFlag} from '../FeatureFlags'

import classes from './KeybindingHint.module.css'
import {clsx} from 'clsx'

/** `kbd` element with style resets. */
const Kbd = ({children}: {children: ReactNode}) => (
<Text
as={'kbd' as 'span'}
sx={{
color: 'inherit',
fontFamily: 'inherit',
fontSize: 'inherit',
border: 'none',
background: 'none',
boxShadow: 'none',
p: 0,
lineHeight: 'unset',
position: 'relative',
overflow: 'visible',
verticalAlign: 'baseline',
textWrap: 'nowrap',
}}
>
{children}
</Text>
)
const Kbd = ({children, className}: {children: ReactNode; className?: string}) => {
const enabled = useFeatureFlag('primer_react_css_modules_team')

return (
<Text
as={'kbd' as 'span'}
className={clsx(className, enabled && classes.KeybindingHint)}
data-testid="keybinding-hint"
sx={
enabled
? undefined
: {
color: 'inherit',
fontFamily: 'inherit',
fontSize: 'inherit',
border: 'none',
background: 'none',
boxShadow: 'none',
p: 0,
lineHeight: 'unset',
position: 'relative',
overflow: 'visible',
verticalAlign: 'baseline',
textWrap: 'nowrap',
}
}
>
{children}
</Text>
)
}

/** Indicates the presence of an available keybinding. */
// KeybindingHint is a good candidate for memoizing since props will rarely change
export const KeybindingHint = memo((props: KeybindingHintProps) => (
<Kbd>
export const KeybindingHint = memo(({className, ...props}: KeybindingHintProps) => (
<Kbd className={className}>
<Sequence {...props} />
</Kbd>
))
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/KeybindingHint/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ export interface KeybindingHintProps {
* Control the size of the hint.
*/
size?: 'small' | 'normal'
/**
* Additional class name to apply to the hint.
*/
className?: string
}
5 changes: 5 additions & 0 deletions packages/react/src/__tests__/KeybindingHint.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ describe('KeybindingHint', () => {
expect(el).toBeInTheDocument()
expect(el).not.toHaveAttribute('aria-hidden')
})

it('accepts className prop', () => {
render(<KeybindingHint keys="Control" className="test-class" />)
expect(screen.getByTestId('keybinding-hint')).toHaveClass('test-class')
})
})

describe('getAccessibleKeybindingHintString', () => {
Expand Down
Loading