Skip to content
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

feat(tooltip): accessibility improvements for tooltip #1463

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 40 additions & 8 deletions components/tooltip/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const TOOLTIP_OFFSET = 4

const popperStyle = resolve`
z-index: ${layers.applicationTop};
pointer-events: none;

// Hide popper when reference component is obscured (https://popper.js.org/docs/v2/modifiers/hide/)
div[data-popper-reference-hidden="true"] {
visibility: hidden;
Expand Down Expand Up @@ -66,33 +64,63 @@ const Tooltip = ({
}, closeDelay)
}

useEffect(
() => () => {
const onFocus = () => {
clearTimeout(closeTimerRef.current)

openTimerRef.current = setTimeout(() => {
setOpen(true)
}, openDelay)
}

const onBlur = () => {
clearTimeout(openTimerRef.current)

closeTimerRef.current = setTimeout(() => {
setOpen(false)
}, closeDelay)
}

const handleKeyDown = (event) => {
if (event.key === 'Escape') {
closeTimerRef.current = setTimeout(() => {
setOpen(false)
}, closeDelay)
}
}

useEffect(() => {
document.addEventListener('keydown', handleKeyDown)

return () => {
clearTimeout(openTimerRef.current)
clearTimeout(closeTimerRef.current)
},
[]
)
document.removeEventListener('keydown', handleKeyDown)
}
}, [])

return (
<>
{typeof children === 'function' ? (
children({
onMouseOver: onMouseOver,
onMouseOut: onMouseOut,
onFocus: { onFocus },
onBlur: { onBlur },
ref: popperReference,
})
) : (
<span
onMouseOver={onMouseOver}
onMouseOut={onMouseOut}
onFocus={onFocus}
onBlur={onBlur}
ref={popperReference}
aria-describedby={open ? 'tooltipContenDhis2Ui' : ''}
data-test={`${dataTest}-reference`}
>
{children}
</span>
)}

{open && (
<Portal>
<Popper
Expand All @@ -103,7 +131,11 @@ const Tooltip = ({
>
<div
className={className}
id="tooltipContenDhis2Ui"
onMouseOver={onMouseOver}
onMouseOut={onMouseOut}
data-test={`${dataTest}-content`}
role="tooltip"
>
{content}
</div>
Expand Down
56 changes: 56 additions & 0 deletions components/tooltip/src/tooltip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,59 @@ describe('it handles custom open and close delays', () => {
expect(document.querySelector(tooltipContentSelector)).toBe(null)
})
})

describe('Keyboard interaction', () => {
it('opens on keyboard focus and closes on blur', () => {
wrapper = mount(<Tooltip content={tooltipContent}>Hi hi!</Tooltip>)
wrapper.simulate('focus')

expect(document.querySelector(tooltipContentSelector)).toBe(null)

act(() => {
jest.advanceTimersByTime(200)
})

const res = document.querySelector(tooltipContentSelector)
expect(res).not.toBe(null)
expect(res.textContent).toBe(tooltipContent)

wrapper.simulate('blur')
act(() => {
jest.advanceTimersByTime(200)
})
expect(document.querySelector(tooltipContentSelector)).toBe(null)

act(() => {
jest.runAllTimers()
})
})

it('closes when escape key is pressed', () => {
wrapper = mount(<Tooltip content={tooltipContent}>Hi hi!</Tooltip>)

// open tooltip
wrapper.simulate('mouseover')

act(() => {
jest.advanceTimersByTime(200)
})

// verify tooltip is open
expect(document.querySelector(tooltipContentSelector)).not.toBe(null)

//Press the Escape key
act(() => {
document.dispatchEvent(
alaa-yahia marked this conversation as resolved.
Show resolved Hide resolved
new KeyboardEvent('keydown', { key: 'Escape' })
)
})

// wait for close delay
act(() => {
jest.advanceTimersByTime(200)
})

// expect tooltip to be closed
expect(document.querySelector(tooltipContentSelector)).toBe(null)
})
})
Loading