-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: show full value on hover for truncated single-line fields #10273
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
+195
−52
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
edf1868
feat: show full value on hover for truncated single-line fields
fiosman dcd165e
chore: clean up
fiosman d3e460e
feat: show full value on hover for truncated single-line fields
fiosman 09aad61
feat: show full value on hover for truncated single-line fields
fiosman 972b19d
feat: show full value on hover for truncated single-line fields
fiosman d18c9b6
feat: show full value on hover for truncated single-line fields
fiosman 72dbe66
chore: address copilot feedback
fiosman 5d0c62b
chore: more copilot feedback
fiosman 35b7750
feat: handle cases to render tooltip when there is a combination of n…
fiosman d0db86c
feat: show raw nunjucks template string if a template is invalid
fiosman 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import classnames from 'classnames'; | ||
| import React, { type CSSProperties, type ReactNode } from 'react'; | ||
| import React, { type CSSProperties, type ReactNode, useEffect } from 'react'; | ||
| import { mergeProps, OverlayContainer, useOverlayPosition, useTooltip, useTooltipTrigger } from 'react-aria'; | ||
| import { createPortal } from 'react-dom'; | ||
| import { useTooltipTriggerState } from 'react-stately'; | ||
|
|
@@ -14,37 +14,102 @@ interface Props { | |
| wide?: boolean; | ||
| style?: CSSProperties; | ||
| onClick?: () => void; | ||
| // Anchors the tooltip to the mouse position instead of the trigger element's bounding | ||
| // box. Useful when the trigger is much wider/taller than its actual visible content | ||
| // (e.g. a text field that fills a whole table cell) - anchoring to the trigger box would | ||
| // place the tooltip relative to the cell, not to whatever the user is actually hovering. | ||
| followCursor?: boolean; | ||
| // When provided, called on hover to decide whether the tooltip should open at all. Use this | ||
| // for fields whose content is clipped/truncated, so the tooltip only appears when there's | ||
| // more content than what's visible. | ||
| shouldShow?: () => boolean; | ||
| } | ||
|
|
||
| export const Tooltip = (props: Props) => { | ||
| const { children, message, className, wide, selectable, delay = 400, position, style } = props; | ||
| const { children, message, className, wide, selectable, delay = 400, position, style, followCursor, shouldShow } = props; | ||
| const triggerRef = React.useRef<HTMLDivElement>(null); | ||
| const overlayRef = React.useRef<HTMLDivElement>(null); | ||
| const dwellTimeout = React.useRef<ReturnType<typeof setTimeout> | null>(null); | ||
| const cursorPosRef = React.useRef({ x: 0, y: 0 }); | ||
|
|
||
| const state = useTooltipTriggerState({ delay }); | ||
| const trigger = useTooltipTrigger(props, state, triggerRef); | ||
| // react-stately shares a single "warmup" timer across every tooltip on the page: once one | ||
| // tooltip has shown, any other tooltip opens almost instantly for the next 500ms instead of | ||
| // waiting out `delay`. Quickly sweeping the mouse across many trigger elements (e.g. table | ||
| // rows) then makes each one flash open and closed in turn. `trigger: 'focus'` tells | ||
| // useTooltipTrigger to ignore hover entirely (keyboard/focus accessibility is unaffected), | ||
| // and we open/close on hover ourselves below with a plain dwell timer, so showing a tooltip | ||
| // always waits the full `delay` no matter what any other tooltip just did. | ||
| const trigger = useTooltipTrigger({ ...props, trigger: 'focus' }, state, triggerRef); | ||
| const tooltip = useTooltip(trigger.tooltipProps, state); | ||
|
|
||
| const { overlayProps: positionProps } = useOverlayPosition({ | ||
| targetRef: triggerRef, | ||
| overlayRef, | ||
| placement: position, | ||
| offset: 5, | ||
| isOpen: state.isOpen, | ||
| isOpen: state.isOpen && !followCursor, | ||
| }); | ||
|
|
||
| const clearDwellTimeout = () => { | ||
| if (dwellTimeout.current) { | ||
| clearTimeout(dwellTimeout.current); | ||
| dwellTimeout.current = null; | ||
| } | ||
| }; | ||
|
|
||
| const handleMouseEnter = (e: React.MouseEvent) => { | ||
| clearDwellTimeout(); | ||
| if (followCursor) { | ||
| cursorPosRef.current = { x: e.clientX, y: e.clientY }; | ||
| } | ||
| dwellTimeout.current = setTimeout(() => { | ||
| dwellTimeout.current = null; | ||
| // Re-check right before opening: over the dwell the pointer may have moved to a spot | ||
| // (e.g. onto a nunjucks tag with its own tooltip) where the tooltip shouldn't show. | ||
| if (shouldShow && !shouldShow()) { | ||
| return; | ||
| } | ||
| state.open(true); | ||
| }, delay); | ||
| }; | ||
|
|
||
| const handleMouseMove = (e: React.MouseEvent) => { | ||
| if (followCursor) { | ||
| cursorPosRef.current = { x: e.clientX, y: e.clientY }; | ||
| } | ||
| // The pointer can move to a spot where the tooltip should no longer show (e.g. from plain | ||
| // text onto a nunjucks tag with its own tooltip) after it's already open. Re-check and close | ||
| // so the two tooltips don't double up. | ||
| if (state.isOpen && shouldShow && !shouldShow()) { | ||
| state.close(true); | ||
| } | ||
| }; | ||
|
|
||
| const handleMouseLeave = () => { | ||
| clearDwellTimeout(); | ||
| state.close(true); | ||
| }; | ||
|
fiosman marked this conversation as resolved.
|
||
|
|
||
| useEffect(() => clearDwellTimeout, []); | ||
|
fiosman marked this conversation as resolved.
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. clever |
||
|
|
||
| const tooltipClasses = classnames(className, 'tooltip'); | ||
| const bubbleClasses = classnames('tooltip__bubble theme--tooltip', { | ||
| 'tooltip__bubble--visible': state.isOpen, | ||
| 'tooltip__bubble--wide': wide, | ||
| selectable, | ||
| }); | ||
|
|
||
| const cursorStyle: CSSProperties = followCursor | ||
| ? { position: 'fixed', top: cursorPosRef.current.y + 16, left: cursorPosRef.current.x + 12, margin: 0 } | ||
| : {}; | ||
|
|
||
| const overlayContent = message ? ( | ||
| <div | ||
| ref={overlayRef} | ||
| onClick={e => e.stopPropagation()} | ||
| {...mergeProps(tooltip.tooltipProps, positionProps)} | ||
| {...mergeProps(tooltip.tooltipProps, followCursor ? {} : positionProps)} | ||
| style={followCursor ? cursorStyle : undefined} | ||
| className={bubbleClasses} | ||
| > | ||
| {message} | ||
|
|
@@ -60,6 +125,9 @@ export const Tooltip = (props: Props) => { | |
| className={tooltipClasses} | ||
| style={{ position: 'relative', ...style }} | ||
| {...trigger.triggerProps} | ||
| onMouseEnter={handleMouseEnter} | ||
| onMouseMove={handleMouseMove} | ||
| onMouseLeave={handleMouseLeave} | ||
| onClick={props.onClick} | ||
| > | ||
| {children} | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.