Skip to content

Commit 129f3f0

Browse files
authored
Merge pull request #841 from gabrieljablonski/add-position-strategy-prop
Add `positionStrategy` prop
2 parents 95c65bd + a48923d commit 129f3f0

File tree

8 files changed

+56
-31
lines changed

8 files changed

+56
-31
lines changed

docs/docs/options.mdx

Lines changed: 32 additions & 30 deletions
Large diffs are not rendered by default.

src/components/Tooltip/Tooltip.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const Tooltip = ({
1616
place = 'top',
1717
offset = 10,
1818
events = ['hover'],
19+
positionStrategy = 'absolute',
1920
wrapper: WrapperElement = 'div',
2021
children = null,
2122
delayShow = 0,
@@ -148,6 +149,7 @@ const Tooltip = ({
148149
elementReference,
149150
tooltipReference: tooltipRef.current as HTMLElement,
150151
tooltipArrowReference: tooltipArrowRef.current as HTMLElement,
152+
strategy: positionStrategy,
151153
}).then((computedStylesData) => {
152154
if (Object.keys(computedStylesData.tooltipStyles).length) {
153155
setInlineStyles(computedStylesData.tooltipStyles)
@@ -170,6 +172,7 @@ const Tooltip = ({
170172
role="tooltip"
171173
className={classNames(styles['tooltip'], styles[variant], className, {
172174
[styles['show']]: isOpen || show,
175+
[styles['fixed']]: positionStrategy === 'fixed',
173176
})}
174177
style={inlineStyles}
175178
ref={tooltipRef as React.RefObject<HTMLDivElement>}

src/components/Tooltip/TooltipTypes.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export type ChildrenType = Element | ElementType | ReactNode
1010

1111
export type EventsType = 'hover' | 'click'
1212

13+
export type PositionStrategy = 'absolute' | 'fixed'
14+
1315
export interface ITooltip {
1416
className?: string
1517
classNameArrow?: string
@@ -24,6 +26,7 @@ export interface ITooltip {
2426
wrapper?: WrapperType
2527
children?: ChildrenType
2628
events?: EventsType[]
29+
positionStrategy?: PositionStrategy
2730
delayShow?: number
2831
delayHide?: number
2932
isOpen?: boolean

src/components/Tooltip/styles.module.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
will-change: opacity, visibility;
1414
}
1515

16+
.fixed {
17+
position: fixed;
18+
}
19+
1620
.arrow {
1721
position: absolute;
1822
background: var(--rt-color-dark);

src/components/TooltipController/TooltipController.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useEffect, useState } from 'react'
22
import { Tooltip } from 'components/Tooltip'
33
import type {
44
EventsType,
5+
PositionStrategy,
56
PlacesType,
67
VariantType,
78
WrapperType,
@@ -22,6 +23,7 @@ const TooltipController = ({
2223
wrapper = 'div',
2324
children = null,
2425
events = ['hover'],
26+
positionStrategy = 'absolute',
2527
delayShow = 0,
2628
delayHide = 0,
2729
getContent,
@@ -36,6 +38,8 @@ const TooltipController = ({
3638
const [tooltipDelayHide, setTooltipDelayHide] = useState(delayHide)
3739
const [tooltipWrapper, setTooltipWrapper] = useState<WrapperType>(wrapper)
3840
const [tooltipEvents, setTooltipEvents] = useState<EventsType[]>(events)
41+
const [tooltipPositionStrategy, setTooltipPositionStrategy] =
42+
useState<PositionStrategy>(positionStrategy)
3943
const [isHtmlContent, setIsHtmlContent] = useState<boolean>(Boolean(html))
4044

4145
const getDataAttributesFromAnchorElement = (elementReference: HTMLElement) => {
@@ -89,6 +93,9 @@ const TooltipController = ({
8993
const parsedEvents = value.split(' ')
9094
setTooltipEvents(parsedEvents as EventsType[])
9195
},
96+
positionStrategy: (value: PositionStrategy) => {
97+
setTooltipPositionStrategy(value)
98+
},
9299
'delay-show': (value: number) => {
93100
setTooltipDelayShow(Number(value))
94101
},
@@ -181,6 +188,7 @@ const TooltipController = ({
181188
offset: tooltipOffset,
182189
wrapper: tooltipWrapper,
183190
events: tooltipEvents,
191+
positionStrategy: tooltipPositionStrategy,
184192
delayShow: tooltipDelayShow,
185193
delayHide: tooltipDelayHide,
186194
isOpen,

src/components/TooltipController/TooltipControllerTypes.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
WrapperType,
55
ChildrenType,
66
EventsType,
7+
PositionStrategy,
78
} from 'components/Tooltip/TooltipTypes'
89

910
export interface ITooltipController {
@@ -19,6 +20,7 @@ export interface ITooltipController {
1920
wrapper?: WrapperType
2021
children?: ChildrenType
2122
events?: EventsType[]
23+
positionStrategy?: PositionStrategy
2224
delayShow?: number
2325
delayHide?: number
2426
getContent?: function

src/utils/compute-positions-types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export interface IComputePositions {
44
tooltipArrowReference?: Element | HTMLElement | null
55
place?: 'top' | 'right' | 'bottom' | 'left'
66
offset?: number
7+
strategy?: 'absolute' | 'fixed'
78
}

src/utils/compute-positions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const computeToolTipPosition = async ({
77
tooltipArrowReference = null,
88
place = 'top',
99
offset: offsetValue = 10,
10+
strategy = 'absolute',
1011
}: IComputePositions) => {
1112
if (!elementReference) {
1213
// elementReference can be null or undefined and we will not compute the position
@@ -23,9 +24,9 @@ export const computeToolTipPosition = async ({
2324

2425
if (tooltipArrowReference) {
2526
middleware.push(arrow({ element: tooltipArrowReference as HTMLElement }))
26-
2727
return computePosition(elementReference as HTMLElement, tooltipReference as HTMLElement, {
2828
placement: place,
29+
strategy,
2930
middleware,
3031
}).then(({ x, y, placement, middlewareData }) => {
3132
const styles = { left: `${x}px`, top: `${y}px` }
@@ -55,6 +56,7 @@ export const computeToolTipPosition = async ({
5556

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

0 commit comments

Comments
 (0)