Skip to content

Add positionStrategy prop #841

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 2 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 32 additions & 30 deletions docs/docs/options.mdx

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const Tooltip = ({
place = 'top',
offset = 10,
events = ['hover'],
positionStrategy = 'absolute',
wrapper: WrapperElement = 'div',
children = null,
delayShow = 0,
Expand Down Expand Up @@ -148,6 +149,7 @@ const Tooltip = ({
elementReference,
tooltipReference: tooltipRef.current as HTMLElement,
tooltipArrowReference: tooltipArrowRef.current as HTMLElement,
strategy: positionStrategy,
}).then((computedStylesData) => {
if (Object.keys(computedStylesData.tooltipStyles).length) {
setInlineStyles(computedStylesData.tooltipStyles)
Expand All @@ -170,6 +172,7 @@ const Tooltip = ({
role="tooltip"
className={classNames(styles['tooltip'], styles[variant], className, {
[styles['show']]: isOpen || show,
[styles['fixed']]: positionStrategy === 'fixed',
})}
style={inlineStyles}
ref={tooltipRef as React.RefObject<HTMLDivElement>}
Expand Down
3 changes: 3 additions & 0 deletions src/components/Tooltip/TooltipTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type ChildrenType = Element | ElementType | ReactNode

export type EventsType = 'hover' | 'click'

export type PositionStrategy = 'absolute' | 'fixed'

export interface ITooltip {
className?: string
classNameArrow?: string
Expand All @@ -24,6 +26,7 @@ export interface ITooltip {
wrapper?: WrapperType
children?: ChildrenType
events?: EventsType[]
positionStrategy?: PositionStrategy
delayShow?: number
delayHide?: number
isOpen?: boolean
Expand Down
4 changes: 4 additions & 0 deletions src/components/Tooltip/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
will-change: opacity, visibility;
}

.fixed {
position: fixed;
}

.arrow {
position: absolute;
background: var(--rt-color-dark);
Expand Down
10 changes: 9 additions & 1 deletion src/components/TooltipController/TooltipController.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useEffect, useState } from 'react'
import { Tooltip } from 'components/Tooltip'
import type {
import {
EventsType,
PositionStrategy,
PlacesType,
VariantType,
WrapperType,
Expand All @@ -22,6 +23,7 @@ const TooltipController = ({
wrapper = 'div',
children = null,
events = ['hover'],
positionStrategy = 'absolute',
delayShow = 0,
delayHide = 0,
getContent,
Expand All @@ -36,6 +38,8 @@ const TooltipController = ({
const [tooltipDelayHide, setTooltipDelayHide] = useState(delayHide)
const [tooltipWrapper, setTooltipWrapper] = useState<WrapperType>(wrapper)
const [tooltipEvents, setTooltipEvents] = useState<EventsType[]>(events)
const [tooltipPositionStrategy, setTooltipPositionStrategy] =
useState<PositionStrategy>(positionStrategy)
const [isHtmlContent, setIsHtmlContent] = useState<boolean>(Boolean(html))

const getDataAttributesFromAnchorElement = (elementReference: HTMLElement) => {
Expand Down Expand Up @@ -89,6 +93,9 @@ const TooltipController = ({
const parsedEvents = value.split(' ')
setTooltipEvents(parsedEvents as EventsType[])
},
positionStrategy: (value: PositionStrategy) => {
setTooltipPositionStrategy(value)
},
'delay-show': (value: number) => {
setTooltipDelayShow(Number(value))
},
Expand Down Expand Up @@ -181,6 +188,7 @@ const TooltipController = ({
offset: tooltipOffset,
wrapper: tooltipWrapper,
events: tooltipEvents,
positionStrategy: tooltipPositionStrategy,
delayShow: tooltipDelayShow,
delayHide: tooltipDelayHide,
isOpen,
Expand Down
2 changes: 2 additions & 0 deletions src/components/TooltipController/TooltipControllerTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
WrapperType,
ChildrenType,
EventsType,
PositionStrategy,
} from 'components/Tooltip/TooltipTypes'

export interface ITooltipController {
Expand All @@ -19,6 +20,7 @@ export interface ITooltipController {
wrapper?: WrapperType
children?: ChildrenType
events?: EventsType[]
positionStrategy?: PositionStrategy
delayShow?: number
delayHide?: number
getContent?: function
Expand Down
1 change: 1 addition & 0 deletions src/utils/compute-positions-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface IComputePositions {
tooltipArrowReference?: Element | HTMLElement | null
place?: 'top' | 'right' | 'bottom' | 'left'
offset?: number
strategy?: 'absolute' | 'fixed'
}
4 changes: 3 additions & 1 deletion src/utils/compute-positions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const computeToolTipPosition = async ({
tooltipArrowReference = null,
place = 'top',
offset: offsetValue = 10,
strategy = 'absolute',
}: IComputePositions) => {
if (!elementReference) {
// elementReference can be null or undefined and we will not compute the position
Expand All @@ -23,9 +24,9 @@ export const computeToolTipPosition = async ({

if (tooltipArrowReference) {
middleware.push(arrow({ element: tooltipArrowReference as HTMLElement }))

return computePosition(elementReference as HTMLElement, tooltipReference as HTMLElement, {
placement: place,
strategy,
middleware,
}).then(({ x, y, placement, middlewareData }) => {
const styles = { left: `${x}px`, top: `${y}px` }
Expand Down Expand Up @@ -55,6 +56,7 @@ export const computeToolTipPosition = async ({

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