Skip to content

Commit 8d2f001

Browse files
committed
feat: add border options (with arrow)
1 parent a2ba43a commit 8d2f001

File tree

7 files changed

+22
-2
lines changed

7 files changed

+22
-2
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const Tooltip = ({
4343
setIsOpen,
4444
activeAnchor,
4545
setActiveAnchor,
46+
border,
4647
}: ITooltip) => {
4748
const tooltipRef = useRef<HTMLElement>(null)
4849
const tooltipArrowRef = useRef<HTMLElement>(null)
@@ -236,6 +237,7 @@ const Tooltip = ({
236237
tooltipArrowReference: tooltipArrowRef.current,
237238
strategy: positionStrategy,
238239
middlewares,
240+
border,
239241
}).then((computedStylesData) => {
240242
if (Object.keys(computedStylesData.tooltipStyles).length) {
241243
setInlineStyles(computedStylesData.tooltipStyles)
@@ -502,6 +504,7 @@ const Tooltip = ({
502504
tooltipArrowReference: tooltipArrowRef.current,
503505
strategy: positionStrategy,
504506
middlewares,
507+
border,
505508
}).then((computedStylesData) => {
506509
if (!mounted.current) {
507510
// invalidate computed positions after remount

src/components/Tooltip/TooltipTypes.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,5 @@ export interface ITooltip {
7777
afterHide?: () => void
7878
activeAnchor: HTMLElement | null
7979
setActiveAnchor: (anchor: HTMLElement | null) => void
80+
border?: string | null
8081
}

src/components/TooltipController/TooltipController.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const TooltipController = ({
4444
style,
4545
position,
4646
isOpen,
47+
border,
4748
setIsOpen,
4849
afterShow,
4950
afterHide,
@@ -283,6 +284,7 @@ const TooltipController = ({
283284
style,
284285
position,
285286
isOpen,
287+
border,
286288
setIsOpen,
287289
afterShow,
288290
afterHide,

src/components/TooltipController/TooltipControllerTypes.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface ITooltipController {
6060
style?: CSSProperties
6161
position?: IPosition
6262
isOpen?: boolean
63+
border?: string | null
6364
setIsOpen?: (value: boolean) => void
6465
afterShow?: () => void
6566
afterHide?: () => void

src/test/utils.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe('compute positions', () => {
6363
top: '',
6464
},
6565
tooltipStyles: {
66+
border: null,
6667
left: '5px',
6768
top: '-10px',
6869
},

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export interface IComputePositions {
88
offset?: number
99
strategy?: 'absolute' | 'fixed'
1010
middlewares?: Middleware[]
11+
border?: string | null
1112
}

src/utils/compute-positions.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const computeTooltipPosition = async ({
99
offset: offsetValue = 10,
1010
strategy = 'absolute',
1111
middlewares = [offset(Number(offsetValue)), flip(), shift({ padding: 5 })],
12+
border = null,
1213
}: IComputePositions) => {
1314
if (!elementReference) {
1415
// elementReference can be null or undefined and we will not compute the position
@@ -31,7 +32,7 @@ export const computeTooltipPosition = async ({
3132
strategy,
3233
middleware,
3334
}).then(({ x, y, placement, middlewareData }) => {
34-
const styles = { left: `${x}px`, top: `${y}px` }
35+
const styles = { left: `${x}px`, top: `${y}px`, border }
3536

3637
const { x: arrowX, y: arrowY } = middlewareData.arrow ?? { x: 0, y: 0 }
3738

@@ -43,12 +44,22 @@ export const computeTooltipPosition = async ({
4344
left: 'right',
4445
}[placement.split('-')[0]] ?? 'bottom'
4546

47+
const borderSide =
48+
border &&
49+
{
50+
top: { borderBottom: border, borderRight: border },
51+
right: { borderBottom: border, borderLeft: border },
52+
bottom: { borderTop: border, borderLeft: border },
53+
left: { borderTop: border, borderRight: border },
54+
}[placement.split('-')[0]]
55+
4656
const arrowStyle = {
4757
left: arrowX != null ? `${arrowX}px` : '',
4858
top: arrowY != null ? `${arrowY}px` : '',
4959
right: '',
5060
bottom: '',
51-
[staticSide]: '-4px',
61+
...{ ...borderSide },
62+
[staticSide]: '-5px',
5263
}
5364

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

0 commit comments

Comments
 (0)