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

Fade-out transition when closing the tooltip #1106

Merged
merged 3 commits into from
Nov 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/docs/examples/styling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,26 @@ In summary, if you do it correctly you can use CSS specificity instead of `!impo

:::

### Customizing opening/closing animation

By default, the tooltip has a fade-in/fade-out transition when opening/closing, with a delay of 150ms for both.
If you wish to change the delay for any of them, override the following CSS variables:

:::caution

Do not set `--rt-transition-closing-delay` to `0`. Doing so will result in the tooltip component being stuck (but not visible) on the DOM. This isn't itself a problem, but may lead to performance issues.

Set to `1ms` or a similar value if you want to disable the fade-out transition when closing.

:::

```css
:root {
--rt-transition-show-delay: 0.15s;
--rt-transition-closing-delay: 0.15s;
}
```

### Disabling ReactTooltip CSS

ReactTooltip works seamlessly by automatically injecting CSS into your application. To disable this functionality, use the tooltip prop `disableStyleInjection`.
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,7 @@ For advanced styling, check [the examples](./examples/styling.mdx).
--rt-color-warning: #f0ad4e;
--rt-color-info: #337ab7;
--rt-opacity: 0.9;
--rt-transition-show-delay: 0.15s;
--rt-transition-closing-delay: 0.15s;
}
```
46 changes: 15 additions & 31 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,6 @@ const Tooltip = ({
}
}, [])

useEffect(() => {
if (!show) {
/**
* this fixes weird behavior when switching between two anchor elements very quickly
* remove the timeout and switch quickly between two adjancent anchor elements to see it
*
* in practice, this means the tooltip is not immediately removed from the DOM on hide
*/
const timeout = setTimeout(() => {
setRendered(false)
}, 150)
return () => {
clearTimeout(timeout)
}
}
return () => null
}, [show])

const handleShow = (value: boolean) => {
if (!mounted.current) {
return
Expand Down Expand Up @@ -657,13 +639,21 @@ const Tooltip = ({
styles[variant],
className,
`react-tooltip__place-${actualPlacement}`,
{
'react-tooltip__show': canShow,
[coreStyles['show']]: canShow,
[coreStyles['fixed']]: positionStrategy === 'fixed',
[coreStyles['clickable']]: clickable,
},
coreStyles[canShow ? 'show' : 'closing'],
canShow ? 'react-tooltip__show' : 'react-tooltip__closing',
positionStrategy === 'fixed' && coreStyles['fixed'],
clickable && coreStyles['clickable'],
)}
onTransitionEnd={(event: TransitionEvent) => {
/**
* @warning if `--rt-transition-closing-delay` is set to 0,
* the tooltip will be stuck (but not visible) on the DOM
*/
if (show || event.propertyName !== 'opacity') {
return
}
setRendered(false)
}}
Comment on lines +647 to +656
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an obvious alternative to this? Thought about doing a check on the --rt-transition-closing-delay variable to setup a timeout for setRendered(false) (as it was being done before, see code removed above), but it felt very hacky.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree, we can find a better solution for this on V6

style={{
...externalStyles,
...inlineStyles,
Expand All @@ -678,13 +668,7 @@ const Tooltip = ({
coreStyles['arrow'],
styles['arrow'],
classNameArrow,
{
/**
* changed from dash `no-arrow` to camelcase because of:
* https://github.com/indooorsman/esbuild-css-modules-plugin/issues/42
*/
[coreStyles['noArrow']]: noArrow,
},
noArrow && coreStyles['noArrow'],
)}
style={{
...inlineArrowStyles,
Expand Down
11 changes: 7 additions & 4 deletions src/components/Tooltip/core-styles.module.css
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
.tooltip {
visibility: hidden;
position: absolute;
top: 0;
left: 0;
pointer-events: none;
opacity: 0;
transition: opacity 0.3s ease-out;
will-change: opacity, visibility;
will-change: opacity;
}

.fixed {
Expand All @@ -27,8 +25,13 @@
}

.show {
visibility: visible;
opacity: var(--rt-opacity);
transition: opacity var(--rt-transition-show-delay) ease-out;
}

.closing {
opacity: 0;
transition: opacity var(--rt-transition-closing-delay) ease-in;
}

/** end - core styles **/
2 changes: 2 additions & 0 deletions src/tokens.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
--rt-color-warning: #f0ad4e;
--rt-color-info: #337ab7;
--rt-opacity: 0.9;
--rt-transition-show-delay: 0.15s;
--rt-transition-closing-delay: 0.15s;
Comment on lines +9 to +10
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After testing for some time, 0.3s started feeling waaaay too long. Still open for suggestions though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}