Skip to content

Commit b2545db

Browse files
feat: new render prop
1 parent 13542f6 commit b2545db

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useEffect, useState, useRef } from 'react'
22
import classNames from 'classnames'
33
import debounce from 'utils/debounce'
4-
import { TooltipContent } from 'components/TooltipContent'
54
import { useTooltip } from 'components/TooltipProvider'
65
import useIsomorphicLayoutEffect from 'utils/use-isomorphic-layout-effect'
76
import { computeTooltipPosition } from '../../utils/compute-positions'
@@ -22,7 +21,6 @@ const Tooltip = ({
2221
positionStrategy = 'absolute',
2322
middlewares,
2423
wrapper: WrapperElement,
25-
children = null,
2624
delayShow = 0,
2725
delayHide = 0,
2826
float = false,
@@ -35,7 +33,6 @@ const Tooltip = ({
3533
afterHide,
3634
// props handled by controller
3735
content,
38-
html,
3936
isOpen,
4037
setIsOpen,
4138
activeAnchor,
@@ -469,7 +466,7 @@ const Tooltip = ({
469466
}
470467
setActualPlacement(computedStylesData.place as PlacesType)
471468
})
472-
}, [show, activeAnchor, content, html, place, offset, positionStrategy, position])
469+
}, [show, activeAnchor, content, place, offset, positionStrategy, position])
473470

474471
useEffect(() => {
475472
const anchorById = document.querySelector<HTMLElement>(`[id='${anchorId}']`)
@@ -512,8 +509,7 @@ const Tooltip = ({
512509
}
513510
}, [id, anchorSelect])
514511

515-
const hasContentOrChildren = Boolean(html || content || children)
516-
const canShow = hasContentOrChildren && show && Object.keys(inlineStyles).length > 0
512+
const canShow = content && show && Object.keys(inlineStyles).length > 0
517513

518514
return rendered ? (
519515
<WrapperElement
@@ -534,11 +530,7 @@ const Tooltip = ({
534530
style={{ ...externalStyles, ...inlineStyles }}
535531
ref={tooltipRef}
536532
>
537-
{/**
538-
* content priority: html > content > children
539-
* children should be last so that it can be used as the "default" content
540-
*/}
541-
{(html && <TooltipContent content={html} />) || content || children}
533+
{content}
542534
<WrapperElement
543535
className={classNames('react-tooltip-arrow', styles['arrow'], classNameArrow, {
544536
/**

src/components/Tooltip/TooltipTypes.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ export interface IPosition {
3939
export interface ITooltip {
4040
className?: string
4141
classNameArrow?: string
42-
content?: string
43-
html?: string
42+
content?: ChildrenType
4443
place?: PlacesType
4544
offset?: number
4645
id?: string
@@ -52,7 +51,6 @@ export interface ITooltip {
5251
anchorId?: string
5352
anchorSelect?: string
5453
wrapper: WrapperType
55-
children?: ChildrenType
5654
events?: EventsType[]
5755
positionStrategy?: PositionStrategy
5856
middlewares?: Middleware[]

src/components/TooltipController/TooltipController.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import type {
88
WrapperType,
99
DataAttribute,
1010
ITooltip,
11+
ChildrenType,
1112
} from 'components/Tooltip/TooltipTypes'
1213
import { useTooltip } from 'components/TooltipProvider'
14+
import { TooltipContent } from 'components/TooltipContent'
1315
import type { ITooltipController } from './TooltipControllerTypes'
1416

1517
const TooltipController = ({
@@ -18,6 +20,7 @@ const TooltipController = ({
1820
anchorSelect,
1921
content,
2022
html,
23+
render,
2124
className,
2225
classNameArrow,
2326
variant = 'dark',
@@ -196,14 +199,27 @@ const TooltipController = ({
196199
}
197200
}, [anchorRefs, providerActiveAnchor, activeAnchor, anchorId, anchorSelect])
198201

202+
/**
203+
* content priority: children < renderContent or content < html
204+
* children should be lower priority so that it can be used as the "default" content
205+
*/
206+
let renderedContent: ChildrenType = children
207+
if (render) {
208+
renderedContent = render({ content: tooltipContent ?? null, activeAnchor })
209+
} else if (tooltipContent) {
210+
renderedContent = tooltipContent
211+
}
212+
if (tooltipHtml) {
213+
renderedContent = <TooltipContent content={tooltipHtml} />
214+
}
215+
199216
const props: ITooltip = {
200217
id,
201218
anchorId,
202219
anchorSelect,
203220
className,
204221
classNameArrow,
205-
content: tooltipContent,
206-
html: tooltipHtml,
222+
content: renderedContent,
207223
place: tooltipPlace,
208224
variant: tooltipVariant,
209225
offset: tooltipOffset,
@@ -227,7 +243,7 @@ const TooltipController = ({
227243
setActiveAnchor: (anchor: HTMLElement | null) => setActiveAnchor(anchor),
228244
}
229245

230-
return children ? <Tooltip {...props}>{children}</Tooltip> : <Tooltip {...props} />
246+
return <Tooltip {...props} />
231247
}
232248

233249
export default TooltipController

src/components/TooltipController/TooltipControllerTypes.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ export interface ITooltipController {
1515
className?: string
1616
classNameArrow?: string
1717
content?: string
18+
/**
19+
* @deprecated Use `children` or `render` instead
20+
*/
1821
html?: string
22+
render?: (render: { content: string | null; activeAnchor: HTMLElement | null }) => ChildrenType
1923
place?: PlacesType
2024
offset?: number
2125
id?: string

0 commit comments

Comments
 (0)