Skip to content

closeOnScroll and closeOnResize props #1040

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 4 commits into from
Jun 14, 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
2 changes: 2 additions & 0 deletions docs/docs/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ import { Tooltip } from 'react-tooltip';
| `noArrow` | `boolean` | no | `false` | `true` `false` | Tooltip arrow will not be shown |
| `clickable` | `boolean` | no | `false` | `true` `false` | Allow interaction with elements inside the tooltip. Useful when using buttons and inputs |
| `closeOnEsc` | `boolean` | no | `false` | `true` `false` | Pressing escape key will close the tooltip |
| `closeOnScroll` | `boolean` | no | `false` | `true` `false` | Scrolling anywhere on the window will close the tooltip |
| `closeOnEsc` | `boolean` | no | `false` | `true` `false` | Resizing the window will close the tooltip |
| `style` | `CSSProperties` | no | | a React inline style | Add inline styles directly to the tooltip |
| `position` | `{ x: number; y: number }` | no | | any `number` value for both `x` and `y` | Override the tooltip position on the DOM |
| `isOpen` | `boolean` | no | 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
2 changes: 2 additions & 0 deletions docs/docs/upgrade-guide/changelog-v4-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ If you run into any problems with the tooltip not updating after changes are mad
- [x] `float` - `boolean` - used to achieve V4's `effect="float"`
- [x] `hidden` - `boolean` - when set, the tooltip will not show
- [x] `render` - `function` - can be used to render dynamic content based on the active anchor element (check [the examples](../examples/render.mdx) for more details)
- [x] `closeOnScroll` - `boolean` - when set, the tooltip will close when scrolling anywhere on the window
- [x] `closeOnResize` - `boolean` - when set, the tooltip will close when resizing the window

## `V4` props available in `V5`

Expand Down
14 changes: 14 additions & 0 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const Tooltip = ({
noArrow = false,
clickable = false,
closeOnEsc = false,
closeOnScroll = false,
closeOnResize = false,
style: externalStyles,
position,
afterShow,
Expand Down Expand Up @@ -300,6 +302,12 @@ const Tooltip = ({
elementRefs.add({ current: anchorById })
}

if (closeOnScroll) {
window.addEventListener('scroll', debouncedHandleHideTooltip)
}
if (closeOnResize) {
window.addEventListener('resize', debouncedHandleHideTooltip)
}
if (closeOnEsc) {
window.addEventListener('keydown', handleEsc)
}
Expand Down Expand Up @@ -344,6 +352,12 @@ const Tooltip = ({
})

return () => {
if (closeOnScroll) {
window.removeEventListener('scroll', debouncedHandleHideTooltip)
}
if (closeOnResize) {
window.removeEventListener('resize', debouncedHandleHideTooltip)
}
if (shouldOpenOnClick) {
window.removeEventListener('click', handleClickOutsideAnchors)
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/Tooltip/TooltipTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export interface ITooltip {
noArrow?: boolean
clickable?: boolean
closeOnEsc?: boolean
closeOnScroll?: boolean
closeOnResize?: boolean
style?: CSSProperties
position?: IPosition
isOpen?: boolean
Expand Down
4 changes: 4 additions & 0 deletions src/components/TooltipController/TooltipController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const TooltipController = ({
noArrow = false,
clickable = false,
closeOnEsc = false,
closeOnScroll = false,
closeOnResize = false,
style,
position,
isOpen,
Expand Down Expand Up @@ -276,6 +278,8 @@ const TooltipController = ({
noArrow,
clickable,
closeOnEsc,
closeOnScroll,
closeOnResize,
style,
position,
isOpen,
Expand Down
11 changes: 11 additions & 0 deletions src/components/TooltipController/TooltipControllerTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ export interface ITooltipController {
hidden?: boolean
noArrow?: boolean
clickable?: boolean
/**
* @todo refactor to `hideOnEsc` for naming consistency
*/
closeOnEsc?: boolean
/**
* @todo refactor to `hideOnScroll` for naming consistency
*/
closeOnScroll?: boolean
/**
* @todo refactor to `hideOnResize` for naming consistency
*/
closeOnResize?: boolean
style?: CSSProperties
position?: IPosition
isOpen?: boolean
Expand Down