Skip to content

Expose tooltip placement using classes #970

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

Merged
merged 4 commits into from
Mar 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useTooltip } from 'components/TooltipProvider'
import useIsomorphicLayoutEffect from 'utils/use-isomorphic-layout-effect'
import { computeTooltipPosition } from '../../utils/compute-positions'
import styles from './styles.module.css'
import type { IPosition, ITooltip } from './TooltipTypes'
import type { IPosition, ITooltip, PlacesType } from './TooltipTypes'

const Tooltip = ({
// props
Expand Down Expand Up @@ -45,6 +45,7 @@ const Tooltip = ({
const tooltipArrowRef = useRef<HTMLElement>(null)
const tooltipShowDelayTimerRef = useRef<NodeJS.Timeout | null>(null)
const tooltipHideDelayTimerRef = useRef<NodeJS.Timeout | null>(null)
const [actualPlacement, setActualPlacement] = useState(place)
const [inlineStyles, setInlineStyles] = useState({})
const [inlineArrowStyles, setInlineArrowStyles] = useState({})
const [show, setShow] = useState(false)
Expand Down Expand Up @@ -228,6 +229,7 @@ const Tooltip = ({
if (Object.keys(computedStylesData.tooltipArrowStyles).length) {
setInlineArrowStyles(computedStylesData.tooltipArrowStyles)
}
setActualPlacement(computedStylesData.place as PlacesType)
})
}

Expand Down Expand Up @@ -465,6 +467,7 @@ const Tooltip = ({
if (Object.keys(computedStylesData.tooltipArrowStyles).length) {
setInlineArrowStyles(computedStylesData.tooltipArrowStyles)
}
setActualPlacement(computedStylesData.place as PlacesType)
})
}, [show, activeAnchor, content, html, place, offset, positionStrategy, position])

Expand Down Expand Up @@ -516,11 +519,18 @@ const Tooltip = ({
<WrapperElement
id={id}
role="tooltip"
className={classNames('react-tooltip', styles['tooltip'], styles[variant], className, {
[styles['show']]: canShow,
[styles['fixed']]: positionStrategy === 'fixed',
[styles['clickable']]: clickable,
})}
className={classNames(
'react-tooltip',
styles['tooltip'],
styles[variant],
className,
`react-tooltip__place-${actualPlacement}`,
{
[styles['show']]: canShow,
[styles['fixed']]: positionStrategy === 'fixed',
[styles['clickable']]: clickable,
},
)}
style={{ ...externalStyles, ...inlineStyles }}
ref={tooltipRef}
>
Expand Down
4 changes: 2 additions & 2 deletions src/test/__snapshots__/tooltip-attributes.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports[`tooltip attributes basic tooltip 1`] = `
Lorem Ipsum
</span>
<div
class="react-tooltip"
class="react-tooltip react-tooltip__place-top"
role="tooltip"
>
Hello World!
Expand All @@ -30,7 +30,7 @@ exports[`tooltip attributes tooltip with place 1`] = `
Lorem Ipsum
</span>
<div
class="react-tooltip"
class="react-tooltip react-tooltip__place-right"
role="tooltip"
>
Hello World!
Expand Down
14 changes: 7 additions & 7 deletions src/test/__snapshots__/tooltip-props.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports[`tooltip props basic tooltip 1`] = `
Lorem Ipsum
</span>
<div
class="react-tooltip"
class="react-tooltip react-tooltip__place-top"
role="tooltip"
>
Hello World!
Expand All @@ -27,7 +27,7 @@ exports[`tooltip props clickable tooltip 1`] = `
Lorem Ipsum
</span>
<div
class="react-tooltip undefined"
class="react-tooltip react-tooltip__place-top undefined"
Copy link
Member Author

Choose a reason for hiding this comment

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

when using the clickable prop, we get an undefined classname when testing. this doesn't happen outside of testing.

any ideas @danielbarion?

Copy link
Member

Choose a reason for hiding this comment

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

that's weird, I'll take a look when I have some available time, but this is not a blocker for this PR as this is an extra-class

role="tooltip"
style="left: 5px; top: -10px;"
>
Expand All @@ -50,7 +50,7 @@ exports[`tooltip props tooltip with custom position 1`] = `
Lorem Ipsum
</span>
<div
class="react-tooltip"
class="react-tooltip react-tooltip__place-top"
role="tooltip"
>
Hello World!
Expand Down Expand Up @@ -79,7 +79,7 @@ exports[`tooltip props tooltip with delay show 1`] = `
Lorem Ipsum
</span>
<div
class="react-tooltip"
class="react-tooltip react-tooltip__place-top"
role="tooltip"
>
Hello World!
Expand All @@ -98,7 +98,7 @@ exports[`tooltip props tooltip with float 1`] = `
Lorem Ipsum
</span>
<div
class="react-tooltip"
class="react-tooltip react-tooltip__place-top"
role="tooltip"
>
Hello World!
Expand All @@ -117,7 +117,7 @@ exports[`tooltip props tooltip with html 1`] = `
Lorem Ipsum
</span>
<div
class="react-tooltip"
class="react-tooltip react-tooltip__place-top"
role="tooltip"
>
<span>
Expand All @@ -141,7 +141,7 @@ exports[`tooltip props tooltip with place 1`] = `
Lorem Ipsum
</span>
<div
class="react-tooltip"
class="react-tooltip react-tooltip__place-right"
role="tooltip"
>
Hello World!
Expand Down
6 changes: 4 additions & 2 deletions src/test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('compute positions', () => {
tooltipArrowReference: null,
})

expect(value).toEqual({ tooltipStyles: {}, tooltipArrowStyles: {} })
expect(value).toEqual({ tooltipStyles: {}, tooltipArrowStyles: {}, place: 'top' })
})

test('empty tooltip reference element', async () => {
Expand All @@ -23,7 +23,7 @@ describe('compute positions', () => {
tooltipArrowReference: null,
})

expect(value).toEqual({ tooltipStyles: {}, tooltipArrowStyles: {} })
expect(value).toEqual({ tooltipStyles: {}, tooltipArrowStyles: {}, place: 'top' })
})

test('empty tooltip arrow reference element', async () => {
Expand All @@ -41,6 +41,7 @@ describe('compute positions', () => {
left: '5px',
top: '10px',
},
place: 'bottom',
})
})

Expand All @@ -65,6 +66,7 @@ describe('compute positions', () => {
left: '5px',
top: '-10px',
},
place: 'top',
})
})
})
Expand Down
10 changes: 5 additions & 5 deletions src/utils/compute-positions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export const computeTooltipPosition = async ({
// elementReference can be null or undefined and we will not compute the position
// eslint-disable-next-line no-console
// console.error('The reference element for tooltip was not defined: ', elementReference)
return { tooltipStyles: {}, tooltipArrowStyles: {} }
return { tooltipStyles: {}, tooltipArrowStyles: {}, place }
}

if (tooltipReference === null) {
return { tooltipStyles: {}, tooltipArrowStyles: {} }
return { tooltipStyles: {}, tooltipArrowStyles: {}, place }
}

const middleware = middlewares
Expand Down Expand Up @@ -51,17 +51,17 @@ export const computeTooltipPosition = async ({
[staticSide]: '-4px',
}

return { tooltipStyles: styles, tooltipArrowStyles: arrowStyle }
return { tooltipStyles: styles, tooltipArrowStyles: arrowStyle, place: placement }
})
}

return computePosition(elementReference as HTMLElement, tooltipReference as HTMLElement, {
placement: 'bottom',
strategy,
middleware,
}).then(({ x, y }) => {
}).then(({ x, y, placement }) => {
const styles = { left: `${x}px`, top: `${y}px` }

return { tooltipStyles: styles, tooltipArrowStyles: {} }
return { tooltipStyles: styles, tooltipArrowStyles: {}, place: placement }
})
}