Skip to content

Commit

Permalink
fix: polyfill old versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmcgrath13 committed Feb 25, 2025
1 parent 389dfb5 commit f2115e8
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ export function isCustomProps<T>(
const TRIANGLE_SIZE = 5
const DEFAULT_POSITION = 'top'

// useId was introduced in React 18 - polyfill for older versions
const genId = (): string => {
if (React.useId) {
return React.useId();
} else {
return `${Math.floor(Math.random() * 900000) + 100000}`
}
}

export function Tooltip(props: DefaultTooltipProps): ReactElement
export function Tooltip<T>(props: CustomTooltipProps<T>): ReactElement
export function Tooltip<
Expand All @@ -49,7 +58,7 @@ export function Tooltip<
}: DefaultTooltipProps | CustomTooltipProps<FCProps>): ReactElement {
const triggerElementRef = useRef<HTMLElement & HTMLButtonElement>(null)
const tooltipBodyRef = useRef<HTMLElement>(null)
const tooltipID = `tooltip-${useId()}`
const tooltipID = useRef(`tooltip-${genId()}`)

const [isVisible, setVisible] = useState(false)
const [isShown, setIsShown] = useState(false)
Expand Down

0 comments on commit f2115e8

Please sign in to comment.