Skip to content
Navigation Menu
{{ message }}
-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Meta: add tooltipped and addToolTip helpers
#9449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8cb4c0c
Initial plan
Copilot 602dc76
feat: add addToolTip helper and refactor existing tooltip usages
Copilot 985fa42
fix: use isConnected guard in addToolTip to avoid unexpected DOM inse…
Copilot bc3d299
docs: clarify aria-labelledby behavior in addToolTip JSDoc; fix JSX s…
Copilot 3c2a560
fix: remove leading space from renderShortcut, add explicit separator…
Copilot 63a0882
refactor: split into tooltipped() for JSX and addToolTip() for existi…
Copilot 944b74b
refactor: use tooltipped inline in all three features
Copilot 89d0f76
refactor: flip tooltipped/addToolTip params to (content, element)
Copilot a6a7e58
Improve docs
fregante c0c0aaf
style hotkeys
SunsetTechuila 3973cdc
Apply suggestions from code review
SunsetTechuila 6b8c72c
foramt
SunsetTechuila 1fc248f
move styles
SunsetTechuila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* Copied from: https://github.com/primer/react/blob/dfed7ca73532922ec0526dd85afcf7ae471c566e/packages/react/src/KeybindingHint/KeybindingHint.module.css */ | ||
| .rgh-shortcut { | ||
| position: relative; | ||
| padding: 0; | ||
| margin-left: var(--base-size-6, 0.375rem); | ||
| overflow: visible; | ||
| /* eslint-disable-next-line css/font-family-fallbacks */ | ||
| font-family: inherit; | ||
| font-size: inherit; | ||
| line-height: unset; | ||
| color: inherit; | ||
| white-space: nowrap; | ||
| vertical-align: baseline; | ||
| background: none; | ||
| border: none; | ||
| box-shadow: none; | ||
| } | ||
|
|
||
| /* Adapted from: https://github.com/primer/react/blob/dfed7ca73532922ec0526dd85afcf7ae471c566e/packages/react/src/KeybindingHint/components/Chord.module.css */ | ||
| .rgh-shortcut-chord { | ||
| display: inline-flex; | ||
| border: var(--borderWidth-thin, 0.0625rem) solid; | ||
| border-radius: var(--borderRadius-small, 0.1875rem); | ||
| border-color: #0000; | ||
| font-weight: var(--base-text-weight-normal, 400); | ||
| font-size: 11px; | ||
| gap: 0.5ch; | ||
| box-shadow: none; | ||
| vertical-align: baseline; | ||
| overflow: hidden; | ||
| justify-content: center; | ||
| padding: var(--base-size-2, 0.125rem); | ||
| line-height: var(--base-size-8, 0.5rem); | ||
| min-width: var(--base-size-16, 1rem); | ||
| color: var(--fgColor-onEmphasis, fuchsia); | ||
| background: var(--counter-bgColor-emphasis, fuchsia); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import './tooltip.css'; | ||
|
|
||
| import React from 'dom-chef'; | ||
|
|
||
| import {upperCaseFirst} from '../github-helpers/index.js'; | ||
|
|
||
| export type TooltipOptions = { | ||
| label: string; | ||
| shortcut?: string; | ||
| direction?: 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw'; | ||
| type?: 'label' | 'description'; | ||
| }; | ||
|
|
||
| function renderShortcut(shortcut: string): JSX.Element { | ||
| return ( | ||
| <kbd className='rgh-shortcut'> | ||
| {shortcut.split(' ').map((key, index) => ( | ||
| <> | ||
| {index > 0 && ' '} | ||
| <span className='rgh-shortcut-chord' data-kbd-chord='true'> | ||
| {upperCaseFirst(key)} | ||
| </span> | ||
| </> | ||
| ))} | ||
| </kbd> | ||
| ); | ||
| } | ||
|
|
||
| function createTooltipFor(element: Element, content: string | TooltipOptions): HTMLElement { | ||
| const options: TooltipOptions = typeof content === 'string' | ||
| ? {label: content} | ||
| : content; | ||
|
|
||
| // Ensure the element has an ID for the `for` attribute to link to | ||
| element.id ||= crypto.randomUUID(); | ||
|
|
||
| const tooltipId = crypto.randomUUID(); | ||
| element.setAttribute('aria-labelledby', tooltipId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure we always want that, but ok for now |
||
|
|
||
| return ( | ||
| <tool-tip | ||
| id={tooltipId} | ||
| className="sr-only position-absolute" | ||
| for={element.id} | ||
| popover="manual" | ||
| data-direction={options.direction ?? 's'} | ||
| data-type={options.type ?? 'label'} | ||
| aria-hidden="true" | ||
| role="tooltip" | ||
| > | ||
| {options.label} | ||
| {options.shortcut && renderShortcut(options.shortcut)} | ||
| </tool-tip> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| Generates a tooltip for the received element. You should use this when generating elements via JSX | ||
|
|
||
| @example return <div>{tooltipped('Does something', <button type="button">...</button>)}</div>; | ||
| */ | ||
| export function tooltipped( | ||
| content: string | TooltipOptions, | ||
| element: Element, | ||
| ): [Element, HTMLElement] { | ||
|
fregante marked this conversation as resolved.
|
||
| return [element, createTooltipFor(element, content)]; | ||
| } | ||
|
|
||
| /** | ||
| Attaches a tooltip to an existing element. Don't use this with JSX. | ||
|
|
||
| @example addToolTip('Does something', $('.some-existing-button')) | ||
| */ | ||
| export default function addToolTip( | ||
| content: string | TooltipOptions, | ||
| element: Element, | ||
| ): void { | ||
|
fregante marked this conversation as resolved.
|
||
| if (!element.parentNode) { | ||
| throw new Error('Element has no parent. Use `tooltipped` instead for elements not yet attached to a parent.'); | ||
| } | ||
|
|
||
| element.after(createTooltipFor(element, content)); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
You can’t perform that action at this time.

Uh oh!
There was an error while loading. Please reload this page.