Skip to content

Commit 211df3d

Browse files
feat: holdPositionOnRender
1 parent 1c41e79 commit 211df3d

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

src/App.tsx

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable jsx-a11y/click-events-have-key-events */
33
import { TooltipController as Tooltip } from 'components/TooltipController'
44
import { IPosition } from 'components/Tooltip/TooltipTypes.d'
5-
import React, { useState } from 'react'
5+
import React, { useMemo, useState } from 'react'
66
import { inline, offset } from '@floating-ui/dom'
77
import styles from './styles.module.css'
88

@@ -23,6 +23,26 @@ function App() {
2323
setAnchorId(target.id)
2424
}
2525

26+
const [which, setWhich] = useState<'1' | '2'>('1')
27+
const [show, setShow] = useState(true)
28+
29+
const button1 = useMemo(
30+
() => (
31+
<button data-tooltip-id="hold" data-tooltip-content="button 1" className="anchor-button">
32+
button 1
33+
</button>
34+
),
35+
[],
36+
)
37+
const button2 = useMemo(
38+
() => (
39+
<button data-tooltip-id="hold" data-tooltip-content="button 2" className="anchor-button">
40+
button 2
41+
</button>
42+
),
43+
[],
44+
)
45+
2646
return (
2747
<main className={styles['main']}>
2848
<button
@@ -93,6 +113,25 @@ function App() {
93113
Tooltip content
94114
</Tooltip>
95115
</section>
116+
<section style={{ marginTop: '100px', marginBottom: '100px' }}>
117+
<div style={{ height: '21px' }}>
118+
{show && <div>{which === '1' ? button1 : button2}</div>}
119+
</div>
120+
<Tooltip id="hold" isOpen holdPositionOnRender>
121+
Hello world!
122+
</Tooltip>
123+
<button
124+
onClick={() => {
125+
setShow(false)
126+
setTimeout(() => {
127+
setShow(true)
128+
}, 1000)
129+
setWhich((w) => (w === '2' ? '1' : '2'))
130+
}}
131+
>
132+
switch
133+
</button>
134+
</section>
96135
<div style={{ display: 'flex', gap: '12px', flexDirection: 'row' }}>
97136
<div>
98137
<div

src/components/Tooltip/Tooltip.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const Tooltip = ({
4242
content,
4343
contentWrapperRef,
4444
isOpen,
45+
holdPositionOnRender,
4546
setIsOpen,
4647
activeAnchor,
4748
setActiveAnchor,
@@ -379,7 +380,7 @@ const Tooltip = ({
379380
let updateTooltipCleanup: null | (() => void) = null
380381
if (closeOnResize) {
381382
window.addEventListener('resize', handleScrollResize)
382-
} else if (activeAnchor && tooltipRef.current) {
383+
} else if (activeAnchor && tooltipRef.current && !holdPositionOnRender) {
383384
updateTooltipCleanup = autoUpdate(
384385
activeAnchor as HTMLElement,
385386
tooltipRef.current as HTMLElement,
@@ -639,7 +640,7 @@ const Tooltip = ({
639640

640641
const canShow = !hidden && content && show && Object.keys(inlineStyles).length > 0
641642

642-
return rendered ? (
643+
return rendered || (wasShowing.current && holdPositionOnRender) ? (
643644
<WrapperElement
644645
id={id}
645646
role="tooltip"

src/components/Tooltip/TooltipTypes.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export interface ITooltip {
8484
style?: CSSProperties
8585
position?: IPosition
8686
isOpen?: boolean
87+
holdPositionOnRender?: boolean
8788
setIsOpen?: (value: boolean) => void
8889
afterShow?: () => void
8990
afterHide?: () => void

src/components/TooltipController/TooltipController.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const TooltipController = ({
4545
position,
4646
isOpen,
4747
disableStyleInjection = false,
48+
holdPositionOnRender,
4849
border,
4950
opacity,
5051
arrowColor,
@@ -333,6 +334,7 @@ const TooltipController = ({
333334
style,
334335
position,
335336
isOpen,
337+
holdPositionOnRender,
336338
border,
337339
opacity,
338340
arrowColor,

src/components/TooltipController/TooltipControllerTypes.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export interface ITooltipController {
6161
position?: IPosition
6262
isOpen?: boolean
6363
disableStyleInjection?: boolean | 'core'
64+
holdPositionOnRender?: boolean
6465
/**
6566
* @description see https://developer.mozilla.org/en-US/docs/Web/CSS/border.
6667
*

0 commit comments

Comments
 (0)