Skip to content

Commit 1d79cc5

Browse files
authored
feat(KeybindingHint): Convert to CSS modules behind feature flag (#5326)
* feat(KeybindingHint): Convert to CSS modules behind feature flag * add className prop
1 parent 0aa7474 commit 1d79cc5

File tree

5 files changed

+66
-23
lines changed

5 files changed

+66
-23
lines changed

.changeset/gentle-apes-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": minor
3+
---
4+
5+
Convert KeybindingHint to CSS modules behind feature flag
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.KeybindingHint {
2+
position: relative;
3+
padding: 0;
4+
overflow: visible;
5+
font-family: inherit;
6+
font-size: inherit;
7+
/* stylelint-disable-next-line primer/typography */
8+
line-height: unset;
9+
color: inherit;
10+
white-space: nowrap;
11+
vertical-align: baseline;
12+
background: none;
13+
border: none;
14+
box-shadow: none;
15+
}

packages/react/src/KeybindingHint/KeybindingHint.tsx

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,48 @@ import {memo} from 'react'
33
import Text from '../Text'
44
import type {KeybindingHintProps} from './props'
55
import {accessibleSequenceString, Sequence} from './components/Sequence'
6+
import {useFeatureFlag} from '../FeatureFlags'
7+
8+
import classes from './KeybindingHint.module.css'
9+
import {clsx} from 'clsx'
610

711
/** `kbd` element with style resets. */
8-
const Kbd = ({children}: {children: ReactNode}) => (
9-
<Text
10-
as={'kbd' as 'span'}
11-
sx={{
12-
color: 'inherit',
13-
fontFamily: 'inherit',
14-
fontSize: 'inherit',
15-
border: 'none',
16-
background: 'none',
17-
boxShadow: 'none',
18-
p: 0,
19-
lineHeight: 'unset',
20-
position: 'relative',
21-
overflow: 'visible',
22-
verticalAlign: 'baseline',
23-
textWrap: 'nowrap',
24-
}}
25-
>
26-
{children}
27-
</Text>
28-
)
12+
const Kbd = ({children, className}: {children: ReactNode; className?: string}) => {
13+
const enabled = useFeatureFlag('primer_react_css_modules_team')
14+
15+
return (
16+
<Text
17+
as={'kbd' as 'span'}
18+
className={clsx(className, enabled && classes.KeybindingHint)}
19+
data-testid="keybinding-hint"
20+
sx={
21+
enabled
22+
? undefined
23+
: {
24+
color: 'inherit',
25+
fontFamily: 'inherit',
26+
fontSize: 'inherit',
27+
border: 'none',
28+
background: 'none',
29+
boxShadow: 'none',
30+
p: 0,
31+
lineHeight: 'unset',
32+
position: 'relative',
33+
overflow: 'visible',
34+
verticalAlign: 'baseline',
35+
textWrap: 'nowrap',
36+
}
37+
}
38+
>
39+
{children}
40+
</Text>
41+
)
42+
}
2943

3044
/** Indicates the presence of an available keybinding. */
3145
// KeybindingHint is a good candidate for memoizing since props will rarely change
32-
export const KeybindingHint = memo((props: KeybindingHintProps) => (
33-
<Kbd>
46+
export const KeybindingHint = memo(({className, ...props}: KeybindingHintProps) => (
47+
<Kbd className={className}>
3448
<Sequence {...props} />
3549
</Kbd>
3650
))

packages/react/src/KeybindingHint/props.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ export interface KeybindingHintProps {
3131
* Control the size of the hint.
3232
*/
3333
size?: 'small' | 'normal'
34+
/**
35+
* Additional class name to apply to the hint.
36+
*/
37+
className?: string
3438
}

packages/react/src/__tests__/KeybindingHint.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ describe('KeybindingHint', () => {
7474
expect(el).toBeInTheDocument()
7575
expect(el).not.toHaveAttribute('aria-hidden')
7676
})
77+
78+
it('accepts className prop', () => {
79+
render(<KeybindingHint keys="Control" className="test-class" />)
80+
expect(screen.getByTestId('keybinding-hint')).toHaveClass('test-class')
81+
})
7782
})
7883

7984
describe('getAccessibleKeybindingHintString', () => {

0 commit comments

Comments
 (0)