Skip to content

Commit

Permalink
feat(KeybindingHint): Convert to CSS modules behind feature flag (#5326)
Browse files Browse the repository at this point in the history
* feat(KeybindingHint): Convert to CSS modules behind feature flag

* add className prop
  • Loading branch information
hussam-i-am authored Nov 27, 2024
1 parent 0aa7474 commit 1d79cc5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 23 deletions.
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

0 comments on commit 1d79cc5

Please sign in to comment.