diff --git a/src/components/Tooltip/Tooltip.stories.tsx b/src/components/Tooltip/Tooltip.stories.tsx index 213db536..9be3d3c3 100644 --- a/src/components/Tooltip/Tooltip.stories.tsx +++ b/src/components/Tooltip/Tooltip.stories.tsx @@ -70,13 +70,11 @@ export const Minimal: Story = { }, args: { children: 'Minimal', + hasArrow: false, placement: 'bottom', }, - argTypes: { - hasArrow: { control: false }, - }, render: (args) => ( - +
{args.children}
@@ -96,12 +94,10 @@ export const Highlight: Story = { args: { children: 'Highlight', hasArrow: true, - }, - argTypes: { - placement: { control: false }, + placement: 'bottom', }, render: (args) => ( - +
{args.children}
diff --git a/src/components/Tooltip/Tooltip.tsx b/src/components/Tooltip/Tooltip.tsx index 2a131857..adc06d19 100644 --- a/src/components/Tooltip/Tooltip.tsx +++ b/src/components/Tooltip/Tooltip.tsx @@ -7,22 +7,21 @@ import TooltipContent from './TooltipContent'; import TooltipTrigger from './TooltipTrigger'; const INIT_POSITION = { top: 0, left: 0 }; -const MINIMAL_TOOLTIP_PADDING = 8; -export type Placement = 'top' | 'bottom'; +export interface TooltipShape { + hasArrow?: boolean; + placement?: 'top' | 'bottom'; +} -interface TooltipContextProps { +interface TooltipContextProps extends TooltipShape { tooltipRef: Ref; isVisible: boolean; - hasArrow?: boolean; - placement?: Placement; position: typeof INIT_POSITION; onOpenTooltip: () => void; onCloseTooltip: () => void; } -interface TooltipProps - extends Pick {} +interface TooltipProps extends TooltipShape {} export const TooltipContext = createContext(null); @@ -41,16 +40,9 @@ const TooltipRoot = ({ return; } - const { top, left } = getPosition(tooltipRef.current, placement); + const { top, left } = getPosition(tooltipRef.current, hasArrow, placement); - if (!hasArrow && placement === 'top') { - setPosition({ - top: top - MINIMAL_TOOLTIP_PADDING, - left, - }); - } else { - setPosition({ top, left }); - } + setPosition({ top, left }); }, [isVisible, hasArrow, placement]); const handleTooltipOpen = () => { @@ -67,6 +59,7 @@ const TooltipRoot = ({ tooltipRef, isVisible, hasArrow, + placement, position, onOpenTooltip: handleTooltipOpen, onCloseTooltip: handleTooltipClose, diff --git a/src/components/Tooltip/TooltipContent.tsx b/src/components/Tooltip/TooltipContent.tsx index 05e359e4..8ffdeedd 100644 --- a/src/components/Tooltip/TooltipContent.tsx +++ b/src/components/Tooltip/TooltipContent.tsx @@ -1,20 +1,29 @@ import type { PropsWithChildren } from 'react'; import { assignInlineVars } from '@vanilla-extract/dynamic'; +import clsx from 'clsx'; import { useTooltipContext } from '@/src/hooks/useTooltipContext'; import * as styles from './style.css'; import TooltipPortal from './TooltipPortal'; +const ARROW_STYLE = { + top: styles.bottomArrow, + bottom: styles.topArrow, +}; + const TooltipContent = ({ children }: PropsWithChildren) => { - const { isVisible, hasArrow, position } = useTooltipContext(); + const { isVisible, hasArrow, placement, position } = useTooltipContext(); return ( <> {isVisible && (
{ const { x, y, width, height } = triggerElement.getBoundingClientRect(); const { scrollX, scrollY } = window; + const margin = hasArrow ? TOOLTIP_MARGIN.HIGHLIGHT : TOOLTIP_MARGIN.MINIMAL; const left = x + scrollX + width / 2; switch (placement) { case 'top': - return { top: y + scrollY - height, left }; + return { top: y + scrollY - height - margin, left }; case 'bottom': return { top: y + scrollY + height, left }; default: