hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { createSignal } from "solid-js"; |
| 2 | import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; |
| 3 | import { IconInfo } from "@/components/ui/icons"; |
| 4 | import { cn } from "@/lib/cn"; |
| 5 | import { useT } from "@/stores/preferences-context"; |
| 6 | |
| 7 | /** |
| 8 | * A circled "i" that reveals an explanatory note — for a line that explains a |
| 9 | * column or a table and would otherwise sit on screen all the time. |
| 10 | * |
| 11 | * Hover and keyboard focus open it like a tooltip; a click or a tap pins it |
| 12 | * open (a tooltip alone never opens on touch), and a second click, Escape, a |
| 13 | * tap elsewhere or moving focus away closes it again. |
| 14 | */ |
| 15 | export function InfoTip(props: { |
| 16 | text: string; |
| 17 | /** Accessible name of the button; defaults to "Bilgi" / "More info". */ |
| 18 | label?: string; |
| 19 | class?: string; |
| 20 | }) { |
| 21 | const t = useT(); |
| 22 | let trigger: HTMLButtonElement | undefined; |
| 23 | const [hoverOpen, setHoverOpen] = createSignal(false); |
| 24 | const [pinned, setPinned] = createSignal(false); |
| 25 | return ( |
| 26 | <Tooltip |
| 27 | open={hoverOpen() || pinned()} |
| 28 | onOpenChange={setHoverOpen} |
| 29 | openDelay={150} |
| 30 | closeDelay={100} |
| 31 | placement="top" |
| 32 | gutter={6} |
| 33 | > |
| 34 | <TooltipTrigger |
| 35 | ref={trigger} |
| 36 | type="button" |
| 37 | aria-label={props.label ?? t("common.info")} |
| 38 | data-info-tip="" |
| 39 | class={cn( |
| 40 | "inline-flex h-5 w-5 shrink-0 cursor-help items-center justify-center rounded-full text-muted-foreground outline-hidden transition-colors", |
| 41 | "hover:text-foreground focus-visible:text-foreground focus-visible:ring-2 focus-visible:ring-ring", |
| 42 | // A bigger hit area on touch without changing the header's height. |
| 43 | "touch:relative touch:after:absolute touch:after:-inset-2.5 touch:after:content-['']", |
| 44 | pinned() && "text-foreground", |
| 45 | props.class, |
| 46 | )} |
| 47 | onClick={(event: MouseEvent) => { |
| 48 | // Never reach a sortable header or a clickable row around it. |
| 49 | event.stopPropagation(); |
| 50 | setPinned((value) => !value); |
| 51 | }} |
| 52 | onKeyDown={(event: KeyboardEvent) => { |
| 53 | if (event.key === "Enter" || event.key === " ") event.stopPropagation(); |
| 54 | }} |
| 55 | onBlur={() => setPinned(false)} |
| 56 | > |
| 57 | <IconInfo class="h-3.5 w-3.5" /> |
| 58 | </TooltipTrigger> |
| 59 | <TooltipContent |
| 60 | class="max-w-72 text-left text-xs leading-5 font-normal normal-case tracking-normal whitespace-normal" |
| 61 | onEscapeKeyDown={() => setPinned(false)} |
| 62 | onPointerDownOutside={(event) => { |
| 63 | // A tap on the icon itself is its own toggle, not "outside". |
| 64 | const target = event.detail.originalEvent.target as Node | null; |
| 65 | if (trigger && target && trigger.contains(target)) return; |
| 66 | setPinned(false); |
| 67 | }} |
| 68 | > |
| 69 | {props.text} |
| 70 | </TooltipContent> |
| 71 | </Tooltip> |
| 72 | ); |
| 73 | } |