Tansy UI is a React component kit by OpenKhalix, named after the tansy
flower ("golden buttons"). It has two surfaces:
- A landing page built entirely from the kit's own components, with
five featured parts, a palette-switching playground form, and links
into Storybook
- A Storybook workshop with Tansy branding, a Welcome page, palette
toolbar and accessibility checks
Includes 15 components with colocated tokens and stories, seven brand
palettes, a palette-aware logo, a Docker/nginx image, a scripted
cPanel deploy, and a generator that keeps the landing page catalogue
in sync with Storybook.
278 lines
9.0 KiB
TypeScript
278 lines
9.0 KiB
TypeScript
import {
|
|
isValidElement,
|
|
useCallback,
|
|
useEffect,
|
|
useRef,
|
|
useState,
|
|
forwardRef,
|
|
type CSSProperties,
|
|
type ReactNode,
|
|
type Ref,
|
|
} from 'react'
|
|
import { createPortal } from 'react-dom'
|
|
import { AlertCircle, type LucideIcon } from 'lucide-react'
|
|
import { cn } from '@/lib/cn'
|
|
import {
|
|
ARROW_POSITION_COLORS,
|
|
BASE_ARROW_CLASSES,
|
|
BASE_ARROW_CLASSES_OUTER,
|
|
TOOLTIP_COLORS,
|
|
TOOLTIP_SIZES,
|
|
WHITE_BORDER_MAP,
|
|
type TooltipPosition,
|
|
type TooltipSize,
|
|
type TooltipTheme,
|
|
} from './tooltip.tokens'
|
|
|
|
/**
|
|
* Portal-rendered tooltip with viewport clamping.
|
|
*
|
|
* Ported from the original `Tooltip` component.
|
|
* Every form control in the source app renders one of these for its help text,
|
|
* so it is ported first as a shared dependency.
|
|
*
|
|
* String content gets a themed bubble with an arrow. React-node content gets a
|
|
* light panel sized to fit its content (a wider arrow sits behind it) and
|
|
* stays open while the pointer is over it, so interactive menus work.
|
|
*/
|
|
|
|
const MAX_WIDTH = 280
|
|
|
|
function directionOf(position: TooltipPosition): 't' | 'b' | 'l' | 'r' {
|
|
if (position.startsWith('top')) return 't'
|
|
if (position.startsWith('bottom')) return 'b'
|
|
if (position.startsWith('left')) return 'l'
|
|
return 'r'
|
|
}
|
|
|
|
export type TooltipProps = {
|
|
/** String renders a themed bubble; a React node renders an interactive panel. */
|
|
content: ReactNode
|
|
position?: TooltipPosition
|
|
children?: ReactNode
|
|
size?: TooltipSize
|
|
theme?: TooltipTheme
|
|
rounded?: boolean
|
|
arrow?: boolean
|
|
shadow?: boolean
|
|
/** Hover delay in ms before the tooltip appears. */
|
|
delay?: number
|
|
className?: string
|
|
triggerClassName?: string
|
|
iconClassName?: string
|
|
/** Overrides the default `AlertCircle` trigger icon. */
|
|
icon?: LucideIcon
|
|
}
|
|
|
|
export const Tooltip = forwardRef(function Tooltip(
|
|
{
|
|
content,
|
|
position = 'top',
|
|
children,
|
|
size = 'md',
|
|
theme = 'dark',
|
|
rounded = true,
|
|
arrow = true,
|
|
shadow = false,
|
|
delay = 0,
|
|
className = '',
|
|
triggerClassName = '',
|
|
iconClassName = 'text-orange-500 cursor-pointer hover:text-orange-600 transition-colors',
|
|
icon,
|
|
}: TooltipProps,
|
|
ref: Ref<HTMLDivElement>,
|
|
) {
|
|
const [isHovered, setIsHovered] = useState(false)
|
|
const [showTooltip, setShowTooltip] = useState(false)
|
|
const [coords, setCoords] = useState({ x: 0, y: 0, width: 0, height: 0 })
|
|
|
|
const triggerRef = useRef<HTMLDivElement>(null)
|
|
const closeTimer = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
const lastRectRef = useRef<DOMRect | null>(null)
|
|
|
|
useEffect(() => {
|
|
const node = triggerRef.current
|
|
if (typeof ref === 'function') ref(node)
|
|
else if (ref) ref.current = node
|
|
}, [ref])
|
|
|
|
const isStringContent = typeof content === 'string' && content.trim().length > 0
|
|
const isNodeContent = isValidElement(content)
|
|
const shouldShow = isStringContent || isNodeContent
|
|
const isShortText = typeof content === 'string' && content.length <= 33
|
|
const ResolvedIcon: LucideIcon = icon ?? AlertCircle
|
|
|
|
const updatePosition = useCallback(() => {
|
|
const node = triggerRef.current
|
|
if (!node) return
|
|
|
|
const rect = node.getBoundingClientRect()
|
|
const last = lastRectRef.current
|
|
// Skip a re-measure when nothing moved — otherwise hovering over a static
|
|
// trigger would keep resetting coords on every mouse event.
|
|
if (
|
|
last &&
|
|
Math.abs(last.top - rect.top) < 0.5 &&
|
|
Math.abs(last.left - rect.left) < 0.5
|
|
) {
|
|
return
|
|
}
|
|
lastRectRef.current = rect
|
|
setCoords({ x: rect.left, y: rect.top, width: rect.width, height: rect.height })
|
|
}, [])
|
|
|
|
const handleMouseEnter = () => {
|
|
updatePosition()
|
|
setIsHovered(true)
|
|
if (closeTimer.current) {
|
|
clearTimeout(closeTimer.current)
|
|
closeTimer.current = null
|
|
}
|
|
}
|
|
|
|
const handleMouseLeave = () => {
|
|
if (isNodeContent) {
|
|
// Grace period so the pointer can travel from trigger to panel.
|
|
closeTimer.current = setTimeout(() => setIsHovered(false), 200)
|
|
} else {
|
|
setIsHovered(false)
|
|
}
|
|
}
|
|
|
|
const handleMenuMouseEnter = () => {
|
|
if (closeTimer.current) {
|
|
clearTimeout(closeTimer.current)
|
|
closeTimer.current = null
|
|
}
|
|
}
|
|
|
|
const handleMenuMouseLeave = () => {
|
|
closeTimer.current = setTimeout(() => {
|
|
setIsHovered(false)
|
|
setShowTooltip(false)
|
|
}, 200)
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (isHovered) {
|
|
const timer = setTimeout(() => setShowTooltip(true), delay)
|
|
return () => clearTimeout(timer)
|
|
}
|
|
setShowTooltip(false)
|
|
}, [isHovered, delay])
|
|
|
|
useEffect(() => {
|
|
if (!showTooltip) return
|
|
const reposition = () => updatePosition()
|
|
window.addEventListener('scroll', reposition, true)
|
|
window.addEventListener('resize', reposition)
|
|
return () => {
|
|
window.removeEventListener('scroll', reposition, true)
|
|
window.removeEventListener('resize', reposition)
|
|
}
|
|
}, [showTooltip, updatePosition])
|
|
|
|
const direction = directionOf(position)
|
|
const arrowColorClass = isNodeContent
|
|
? WHITE_BORDER_MAP[direction]
|
|
: ARROW_POSITION_COLORS[position]?.[theme] ?? ''
|
|
|
|
const positionStyle = (): CSSProperties => {
|
|
const { x, y, width, height } = coords
|
|
const viewportWidth = typeof window !== 'undefined' ? window.innerWidth : 0
|
|
const viewportHeight = typeof window !== 'undefined' ? window.innerHeight : 0
|
|
const clampX = (v: number) => (viewportWidth ? Math.min(Math.max(v, 8), viewportWidth - 8) : v)
|
|
const clampY = (v: number) => (viewportHeight ? Math.min(Math.max(v, 8), viewportHeight - 8) : v)
|
|
const base: CSSProperties = { position: 'fixed', zIndex: 9999, maxWidth: `${MAX_WIDTH}px` }
|
|
|
|
switch (position) {
|
|
case 'top':
|
|
return { ...base, top: clampY(y - 8), left: clampX(x + width / 2), transform: 'translateX(-50%) translateY(-100%)' }
|
|
case 'top-start':
|
|
return { ...base, top: clampY(y - 8), left: clampX(x), transform: 'translateY(-100%)' }
|
|
case 'top-end':
|
|
return { ...base, top: clampY(y - 8), left: clampX(x + width), transform: 'translateX(-100%) translateY(-100%)' }
|
|
case 'bottom':
|
|
return { ...base, top: clampY(y + height + 8), left: clampX(x + width / 2), transform: 'translateX(-50%)' }
|
|
case 'bottom-start':
|
|
return { ...base, top: clampY(y + height + 8), left: clampX(x) }
|
|
case 'bottom-end':
|
|
return { ...base, top: clampY(y + height + 8), left: clampX(x + width), transform: 'translateX(-100%)' }
|
|
case 'left':
|
|
return { ...base, top: clampY(y + height / 2), left: clampX(x - 8), transform: 'translateY(-50%) translateX(-100%)' }
|
|
case 'left-start':
|
|
return { ...base, top: clampY(y), left: clampX(x - 8), transform: 'translateX(-100%)' }
|
|
case 'left-end':
|
|
return { ...base, top: clampY(y + height), left: clampX(x - 8), transform: 'translateY(-100%) translateX(-100%)' }
|
|
case 'right':
|
|
return { ...base, top: clampY(y + height / 2), left: clampX(x + width + 8), transform: 'translateY(-50%)' }
|
|
case 'right-start':
|
|
return { ...base, top: clampY(y), left: clampX(x + width + 8) }
|
|
case 'right-end':
|
|
return { ...base, top: clampY(y + height), left: clampX(x + width + 8), transform: 'translateY(-100%)' }
|
|
default:
|
|
return { ...base, top: clampY(y), left: clampX(x) }
|
|
}
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div
|
|
ref={triggerRef}
|
|
className={cn('relative inline-block', triggerClassName)}
|
|
onMouseEnter={handleMouseEnter}
|
|
onMouseLeave={handleMouseLeave}
|
|
>
|
|
{children ?? <ResolvedIcon size={16} className={cn(iconClassName)} />}
|
|
</div>
|
|
|
|
{showTooltip &&
|
|
shouldShow &&
|
|
createPortal(
|
|
<div
|
|
role="tooltip"
|
|
onMouseEnter={isNodeContent ? handleMenuMouseEnter : undefined}
|
|
onMouseLeave={isNodeContent ? handleMenuMouseLeave : undefined}
|
|
style={positionStyle()}
|
|
className={cn(
|
|
TOOLTIP_SIZES[size] ?? TOOLTIP_SIZES.md,
|
|
isStringContent
|
|
? (TOOLTIP_COLORS[theme] ?? TOOLTIP_COLORS.dark)
|
|
: 'bg-white text-slate-700 border border-slate-200',
|
|
shadow && 'shadow-lg',
|
|
rounded ? 'rounded-lg' : 'rounded',
|
|
className,
|
|
)}
|
|
>
|
|
<div
|
|
className={cn(
|
|
'block text-start max-h-[250px] align-top overflow-auto',
|
|
isShortText && isStringContent ? 'max-w-[210px] w-auto whitespace-nowrap' : '',
|
|
!isStringContent ? 'min-w-[210px]' : '',
|
|
)}
|
|
>
|
|
{content}
|
|
</div>
|
|
|
|
{arrow && (
|
|
<>
|
|
{isNodeContent && (
|
|
<div
|
|
className={cn(
|
|
BASE_ARROW_CLASSES_OUTER[position],
|
|
ARROW_POSITION_COLORS[position]?.[theme] ?? '',
|
|
)}
|
|
/>
|
|
)}
|
|
<div className={cn(BASE_ARROW_CLASSES[position], arrowColorClass)} />
|
|
</>
|
|
)}
|
|
</div>,
|
|
document.body,
|
|
)}
|
|
</>
|
|
)
|
|
})
|
|
|
|
export default Tooltip
|