Skip to content

Prop for closing tooltip when pressing escape key #917

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 2 commits into from
Jan 24, 2023
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
1 change: 1 addition & 0 deletions docs/docs/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import 'react-tooltip/dist/react-tooltip.css'
| float | boolean | false | `false` | `true` `false` | tooltip will follow the mouse position when it moves inside the anchor element (same as V4's `effect="float"`) |
| noArrow | boolean | false | `false` | `true` `false` | tooltip arrow will not be shown |
| clickable | boolean | false | `false` | `true` `false` | allow interaction with elements inside the tooltip. useful when using buttons and inputs |
| closeOnEsc | boolean | false | `false` | `true` `false` | pressing escape key will close the tooltip |
| style | CSSProperties | false | | any React inline style | add styles directly to the component by `style` attribute |
| position | `{ x: number; y: number }` | false | | any `number` value for both `x` and `y` | override the tooltip position on the viewport |
| isOpen | boolen | false | handled by internal state | `true` `false` | the tooltip can be controlled or uncontrolled, this attribute can be used to handle show and hide tooltip outside tooltip (can be used **without** `setIsOpen`) |
Expand Down
21 changes: 19 additions & 2 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Tooltip = ({
float = false,
noArrow = false,
clickable = false,
closeOnEsc = false,
style: externalStyles,
position,
afterShow,
Expand Down Expand Up @@ -190,6 +191,13 @@ const Tooltip = ({
handleShow(false)
}

const handleEsc = (event: KeyboardEvent) => {
if (event.key !== 'Escape') {
return
}
handleShow(false)
}

// debounce handler to prevent call twice when
// mouse enter and focus events being triggered toggether
const debouncedHandleShowTooltip = debounce(handleShowTooltip, 50)
Expand All @@ -210,6 +218,10 @@ const Tooltip = ({
return () => null
}

if (closeOnEsc) {
window.addEventListener('keydown', handleEsc)
}

const enabledEvents: { event: string; listener: (event?: Event) => void }[] = []

if (events.find((event: string) => event === 'click')) {
Expand Down Expand Up @@ -252,7 +264,12 @@ const Tooltip = ({
})

return () => {
window.removeEventListener('click', handleClickOutsideAnchor)
if (events.find((event: string) => event === 'click')) {
window.removeEventListener('click', handleClickOutsideAnchor)
}
if (closeOnEsc) {
window.removeEventListener('keydown', handleEsc)
}
if (clickable) {
tooltipRef.current?.removeEventListener('mouseenter', handleMouseEnterTooltip)
tooltipRef.current?.removeEventListener('mouseleave', handleMouseLeaveTooltip)
Expand All @@ -263,7 +280,7 @@ const Tooltip = ({
})
})
}
}, [anchorRefs, activeAnchor, anchorId, events, delayHide, delayShow])
}, [anchorRefs, activeAnchor, closeOnEsc, anchorId, events, delayHide, delayShow])

useEffect(() => {
if (position) {
Expand Down
1 change: 1 addition & 0 deletions src/components/Tooltip/TooltipTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface ITooltip {
float?: boolean
noArrow?: boolean
clickable?: boolean
closeOnEsc?: boolean
style?: CSSProperties
position?: IPosition
isOpen?: boolean
Expand Down
2 changes: 2 additions & 0 deletions src/components/TooltipController/TooltipController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const TooltipController = ({
float = false,
noArrow = false,
clickable = false,
closeOnEsc = false,
style,
position,
isOpen,
Expand Down Expand Up @@ -187,6 +188,7 @@ const TooltipController = ({
float: tooltipFloat,
noArrow,
clickable,
closeOnEsc,
style,
position,
isOpen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface ITooltipController {
float?: boolean
noArrow?: boolean
clickable?: boolean
closeOnEsc?: boolean
style?: CSSProperties
position?: IPosition
isOpen?: boolean
Expand Down