-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show VideoPlayer tooltips when associated control receives focus (#872)
* show VideoTooltip when the parent is focused (or contains focus) and allow dismissal with Esc key * remove duplicate labels * reduce scope of mousemove listener * add changeset * add interaction test to test tooltip focus visibility * update snapshots
- Loading branch information
1 parent
bbdf7f2
commit 872bdcf
Showing
10 changed files
with
100 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'@primer/react-brand': patch | ||
--- | ||
|
||
`VideoPlayer` tooltips now show when the associated control receives focus. |
This file contains 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 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 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 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
Binary file added
BIN
+22.9 KB
.../Visual-Comparison-VideoPlayer-VideoPlayer-Tooltip-Visible-On-Focus-1-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 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 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
63 changes: 53 additions & 10 deletions
63
packages/react/src/VideoPlayer/components/VideoTooltip/VideoTooltip.tsx
This file contains 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,17 +1,60 @@ | ||
import React, {type HTMLAttributes} from 'react' | ||
import React, {useEffect, useRef, useState, type HTMLAttributes} from 'react' | ||
import clsx from 'clsx' | ||
|
||
import {Text} from '../../../Text' | ||
import styles from '../../VideoPlayer.module.css' | ||
|
||
type VideoTooltipProps = HTMLAttributes<HTMLDivElement> | ||
|
||
export const VideoTooltip = ({children, className, ...rest}: VideoTooltipProps) => ( | ||
<div className={clsx(styles.VideoPlayer__tooltip, className)} {...rest}> | ||
<span className={styles.VideoPlayer__tooltipContent}> | ||
<Text className={styles.VideoPlayer__tooltipText} weight="medium"> | ||
{children} | ||
</Text> | ||
</span> | ||
</div> | ||
) | ||
export const VideoTooltip = ({children, className, ...rest}: VideoTooltipProps) => { | ||
const tooltipRef = useRef<HTMLDivElement>(null) | ||
const [hasFocus, setHasFocus] = useState(false) | ||
|
||
useEffect(() => { | ||
const tooltip = tooltipRef.current | ||
const parent = tooltip?.parentElement | ||
|
||
if (!tooltip || !parent) { | ||
return | ||
} | ||
|
||
const handleKeyDown = (e: KeyboardEvent) => { | ||
if (e.key === 'Escape') { | ||
setHasFocus(false) | ||
} | ||
} | ||
|
||
const checkFocus = () => { | ||
const isFocused = parent === document.activeElement || parent.contains(document.activeElement) | ||
setHasFocus(isFocused) | ||
} | ||
|
||
parent.addEventListener('focus', checkFocus) | ||
parent.addEventListener('blur', checkFocus) | ||
parent.addEventListener('focusin', checkFocus) | ||
parent.addEventListener('focusout', checkFocus) | ||
window.addEventListener('keydown', handleKeyDown) | ||
|
||
return () => { | ||
parent.removeEventListener('focus', checkFocus) | ||
parent.removeEventListener('blur', checkFocus) | ||
parent.removeEventListener('focusin', checkFocus) | ||
parent.removeEventListener('focusout', checkFocus) | ||
window.removeEventListener('keydown', handleKeyDown) | ||
} | ||
}, [tooltipRef]) | ||
|
||
return ( | ||
<div | ||
className={clsx(styles.VideoPlayer__tooltip, hasFocus && styles['VideoPlayer__tooltip-visible'], className)} | ||
ref={tooltipRef} | ||
{...rest} | ||
> | ||
<span className={styles.VideoPlayer__tooltipContent}> | ||
<Text className={styles.VideoPlayer__tooltipText} weight="medium"> | ||
{children} | ||
</Text> | ||
</span> | ||
</div> | ||
) | ||
} |