Files
gruperly/apps/web/src/components/ui/tooltip.tsx
Jose Selesan 69275c37b4 chore: update comments and documentation for clarity and consistency across various files
- Translated comments from Spanish to English in vite.config.ts, docker-compose.yml, attendance-risk.ts, analytics.ts, attendance.ts, attendees.ts, enums.ts, groups.ts, and onboarding.ts.
- Improved descriptions for better understanding of code functionality and purpose.
- Ensured uniformity in terminology and phrasing throughout the codebase.
2026-10-01 16:06:54 -03:00

69 lines
2.2 KiB
TypeScript

import { type ReactNode, useId, useState } from 'react'
import { cn } from '../../lib/utils'
export type TooltipProps = {
/** Explanatory text shown on hover, keyboard focus or tap. */
content: ReactNode
/** Trigger content. It sits inside a <button> to stay accessible. */
children: ReactNode
className?: string
/** Alternate positioning for the bubble (default: below, anchored right on sm+). */
tooltipClassName?: string
}
/**
* Accessible tooltip with no dependencies: hover (mouse), focus (keyboard) and
* tap (touch, via focus). It closes on Escape, when the pointer leaves or when
* it loses focus. The bubble is anchored below the trigger so it doesn't cover
* the content.
*/
export function Tooltip({ content, children, className, tooltipClassName }: TooltipProps) {
const [open, setOpen] = useState(false)
const id = useId()
const show = () => setOpen(true)
const hide = () => setOpen(false)
return (
<span className="relative inline-flex">
<button
type="button"
aria-describedby={open ? id : undefined}
// Hover only applies to mouse pointers: on touch there is no
// "persistent hover" and tap is handled with focus + click.
onPointerEnter={(e) => {
if (e.pointerType === 'mouse') show()
}}
onPointerLeave={(e) => {
if (e.pointerType === 'mouse') hide()
}}
onFocus={show}
onBlur={hide}
onClick={show}
onKeyDown={(e) => {
if (e.key === 'Escape') hide()
}}
className={cn(
'inline-flex cursor-help rounded-full focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent',
className,
)}
>
{children}
</button>
{open ? (
<span
id={id}
role="tooltip"
className={cn(
'absolute top-full left-0 z-50 mt-1.5 w-max max-w-[min(16rem,calc(100vw_-_2rem))] rounded-lg border border-border bg-surface px-2.5 py-2 text-xs leading-snug text-foreground shadow-sm animate-fade-in sm:left-auto sm:right-0',
tooltipClassName,
)}
>
{content}
</span>
) : null}
</span>
)
}