Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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/iconbutton-keybinding-hint-array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

`IconButton`: `keybindingHint` now accepts `string[]` in addition to `string`. Multiple hints are rendered joined with "or".
4 changes: 4 additions & 0 deletions packages/react/src/Button/IconButton.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export const KeybindingHintOnDescription = () => (

export const KeybindingHint = () => <IconButton icon={BoldIcon} aria-label="Bold" keybindingHint="Mod+B" />

export const MultipleKeybindingHints = () => (
<IconButton icon={BoldIcon} aria-label="Bold" keybindingHint={['Mod+B', 'Control+B']} />
)

export const LongDelayedTooltip = () => (
// Ideal for cases where we don't want to show the tooltip immediately — for example, when the user is just passing over the element.
<Tooltip text="This is a tooltip with 1200ms delay" delay="long">
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/Button/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export type IconButtonProps = ButtonA11yProps & {
tooltipDirection?: TooltipDirection
/** @deprecated Use `keybindingHint` instead. */
keyshortcuts?: string
Comment thread
iansan5653 marked this conversation as resolved.
keybindingHint?: string
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct type in iconbutton.docs.json as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 464c69d. Updated keybindingHint type in IconButton.docs.json from "string" to "string | string[]" and updated the description to mention the array form.

keybindingHint?: string | string[]
} & Omit<ButtonBaseProps, 'aria-label' | 'aria-labelledby'>

// adopted from React.AnchorHTMLAttributes
Expand Down
26 changes: 21 additions & 5 deletions packages/react/src/TooltipV2/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type TooltipProps = React.PropsWithChildren<{
direction?: TooltipDirection
text: string
type?: 'label' | 'description'
keybindingHint?: KeybindingHintProps['keys']
keybindingHint?: KeybindingHintProps['keys'] | Array<KeybindingHintProps['keys']>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct type on tooltip.docs.json as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 464c69d. Updated keybindingHint type in Tooltip.docs.json from "string" to "string | string[]" and updated the description accordingly.

/**
* Delay in milliseconds before showing the tooltip
* @default short (50ms)
Expand Down Expand Up @@ -273,6 +273,15 @@ export const Tooltip: ForwardRefExoticComponent<
const isMacOS = useIsMacOS()
const hasAriaLabel = 'aria-label' in rest

// Normalize keybindingHint to an array for uniform rendering, treating empty arrays as undefined
const keybindingHints = keybindingHint
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think it would be simpler to allow empty arrays and just check for keybindingHint.length in the later conditionals

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d0bf446. keybindingHints now normalizes to [] for falsy/empty-array inputs, and the later conditionals use .length === 0 / .length > 0 checks.

? Array.isArray(keybindingHint)
? keybindingHint.length > 0
? keybindingHint
: undefined
: [keybindingHint]
: undefined

return (
<TooltipContext.Provider value={value}>
<>
Expand Down Expand Up @@ -353,9 +362,9 @@ export const Tooltip: ForwardRefExoticComponent<
onMouseEnter={openTooltip}
onMouseLeave={closeTooltip}
// If there is an aria-label prop, always assign the ID to the parent so the accessible label can be overridden
id={hasAriaLabel || !keybindingHint ? tooltipId : undefined}
id={hasAriaLabel || !keybindingHints ? tooltipId : undefined}
>
{keybindingHint ? (
{keybindingHints ? (
<>
<span id={hasAriaLabel ? undefined : tooltipId}>
{text}
Expand All @@ -364,10 +373,17 @@ export const Tooltip: ForwardRefExoticComponent<
and renders full key names as `VisuallyHidden` text. Due to the browser bug this causes the label text
to duplicate the symbols and key names. To work around this, we exclude the hint from being part of the
label and instead render the plain keybinding description string. */}
<VisuallyHidden>({getAccessibleKeybindingHintString(keybindingHint, isMacOS)})</VisuallyHidden>
<VisuallyHidden>
({keybindingHints.map(hint => getAccessibleKeybindingHintString(hint, isMacOS)).join(' or ')})
</VisuallyHidden>
</span>
<span className={clsx(classes.KeybindingHintContainer, text && classes.HasTextBefore)} aria-hidden>
<KeybindingHint keys={keybindingHint} format="condensed" variant="onEmphasis" size="small" />
{keybindingHints.map((hint, i) => (
<React.Fragment key={`${i}-${hint}`}>
{i > 0 && ' or '}
<KeybindingHint keys={hint} format="condensed" variant="onEmphasis" size="small" />
</React.Fragment>
))}
</span>
</>
) : (
Expand Down
17 changes: 17 additions & 0 deletions packages/react/src/TooltipV2/__tests__/Tooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@ describe('Tooltip', () => {
)
expect(getByRole('button', {name: 'Overridden label'})).toBeInTheDocument()
})
it('includes multiple keybinding hints joined with "or" in the label text', () => {
const {getByRole} = HTMLRender(<TooltipComponent type="label" keybindingHint={['Control+K', 'Control+Shift+K']} />)
expect(getByRole('button', {name: 'Tooltip text (control k or control shift k)'})).toBeInTheDocument()
})
it('renders multiple keybinding hints when an array is provided', () => {
const {getAllByTestId, container} = HTMLRender(
<TooltipComponent keybindingHint={['Control+K', 'Control+Shift+K']} />,
)
expect(getAllByTestId('keybinding-hint')).toHaveLength(2)
// Verify the "or" separator is rendered between keybinding hints
const hintContainer = container.querySelector('[aria-hidden="true"] [aria-hidden="true"]')
Comment thread
iansan5653 marked this conversation as resolved.
expect(hintContainer?.textContent).toContain(' or ')
})
it('treats an empty array keybindingHint as if no hint was provided', () => {
const {queryByTestId} = HTMLRender(<TooltipComponent keybindingHint={[]} />)
expect(queryByTestId('keybinding-hint')).not.toBeInTheDocument()
})

it('should append tooltip id to existing aria-describedby value on the trigger element', () => {
const {getByRole, getByText} = HTMLRender(<TooltipComponentWithExistingDescription />)
Expand Down
Loading