Skip to content

Commit 670c0e8

Browse files
gabrieljablonskiGabriel Jablonski
authored andcommitted
refactor: improve content/html interaction
1 parent b03247b commit 670c0e8

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const Tooltip = ({
3030
afterShow,
3131
afterHide,
3232
// props handled by controller
33-
isHtmlContent = false,
3433
content,
34+
html,
3535
isOpen,
3636
setIsOpen,
3737
}: ITooltip) => {
@@ -315,7 +315,18 @@ const Tooltip = ({
315315
return () => {
316316
mounted = false
317317
}
318-
}, [show, isOpen, anchorId, activeAnchor, content, place, offset, positionStrategy, position])
318+
}, [
319+
show,
320+
isOpen,
321+
anchorId,
322+
activeAnchor,
323+
content,
324+
html,
325+
place,
326+
offset,
327+
positionStrategy,
328+
position,
329+
])
319330

320331
useEffect(() => {
321332
return () => {
@@ -328,7 +339,7 @@ const Tooltip = ({
328339
}
329340
}, [])
330341

331-
const hasContentOrChildren = Boolean(content || children)
342+
const hasContentOrChildren = Boolean(html || content || children)
332343

333344
return (
334345
<WrapperElement
@@ -342,7 +353,8 @@ const Tooltip = ({
342353
style={{ ...externalStyles, ...inlineStyles }}
343354
ref={tooltipRef}
344355
>
345-
{children || (isHtmlContent ? <TooltipContent content={content as string} /> : content)}
356+
{/* content priority: children > html > content */}
357+
{children || (html && <TooltipContent content={html} />) || content}
346358
<div
347359
className={classNames('react-tooltip-arrow', styles['arrow'], classNameArrow, {
348360
[styles['no-arrow']]: noArrow,

src/components/Tooltip/TooltipTypes.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export interface ITooltip {
4040
id?: string
4141
variant?: VariantType
4242
anchorId?: string
43-
isHtmlContent?: boolean
4443
wrapper?: WrapperType
4544
children?: ChildrenType
4645
events?: EventsType[]

src/components/TooltipController/TooltipController.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const TooltipController = ({
3838
afterShow,
3939
afterHide,
4040
}: ITooltipController) => {
41-
const [tooltipContent, setTooltipContent] = useState(content || html)
41+
const [tooltipContent, setTooltipContent] = useState(content)
42+
const [tooltipHtml, setTooltipHtml] = useState(html)
4243
const [tooltipPlace, setTooltipPlace] = useState(place)
4344
const [tooltipVariant, setTooltipVariant] = useState(variant)
4445
const [tooltipOffset, setTooltipOffset] = useState(offset)
@@ -48,7 +49,6 @@ const TooltipController = ({
4849
const [tooltipWrapper, setTooltipWrapper] = useState<WrapperType>(wrapper)
4950
const [tooltipEvents, setTooltipEvents] = useState(events)
5051
const [tooltipPositionStrategy, setTooltipPositionStrategy] = useState(positionStrategy)
51-
const [isHtmlContent, setIsHtmlContent] = useState(Boolean(html))
5252
const { anchorRefs, activeAnchor } = useTooltip()(id)
5353

5454
const getDataAttributesFromAnchorElement = (elementReference: HTMLElement) => {
@@ -74,8 +74,7 @@ const TooltipController = ({
7474
setTooltipContent(value ?? content)
7575
},
7676
html: (value) => {
77-
setIsHtmlContent(Boolean(value ?? html))
78-
setTooltipContent(value ?? html ?? content)
77+
setTooltipHtml(value ?? html)
7978
},
8079
variant: (value) => {
8180
setTooltipVariant((value as VariantType) ?? variant)
@@ -112,14 +111,12 @@ const TooltipController = ({
112111
}
113112

114113
useEffect(() => {
115-
setIsHtmlContent(false)
116114
setTooltipContent(content)
117-
if (html) {
118-
// html will take precedence
119-
setIsHtmlContent(true)
120-
setTooltipContent(html)
121-
}
122-
}, [content, html])
115+
}, [content])
116+
117+
useEffect(() => {
118+
setTooltipHtml(html)
119+
}, [html])
123120

124121
useEffect(() => {
125122
const elementRefs = new Set(anchorRefs)
@@ -176,7 +173,7 @@ const TooltipController = ({
176173
className,
177174
classNameArrow,
178175
content: tooltipContent,
179-
isHtmlContent,
176+
html: tooltipHtml,
180177
place: tooltipPlace,
181178
variant: tooltipVariant,
182179
offset: tooltipOffset,

0 commit comments

Comments
 (0)