import { createSignal, onCleanup, Show, splitProps } from "solid-js"; import type { Component, JSX } from "solid-js"; type TooltipPosition = "top" | "right" | "bottom" | "left"; type TooltipProps = { content: string | JSX.Element; position?: TooltipPosition; delay?: number; children: JSX.Element; class?: string; }; const positionStyles: Record = { top: { tooltip: "bottom-full left-1/2 -translate-x-1/2 mb-2", arrow: "top-full left-1/2 -translate-x-1/2 border-t-gray-800 border-x-transparent border-b-transparent", }, bottom: { tooltip: "top-full left-1/2 -translate-x-1/2 mt-2", arrow: "bottom-full left-1/2 -translate-x-1/2 border-b-gray-800 border-x-transparent border-t-transparent", }, left: { tooltip: "right-full top-1/2 -translate-y-1/2 mr-2", arrow: "left-full top-1/2 -translate-y-1/2 border-l-gray-800 border-y-transparent border-r-transparent", }, right: { tooltip: "left-full top-1/2 -translate-y-1/2 ml-2", arrow: "right-full top-1/2 -translate-y-1/2 border-r-gray-800 border-y-transparent border-l-transparent", }, }; export const Tooltip: Component = (props) => { const [local, _others] = splitProps(props, ["content", "position", "delay", "children", "class"]); const position = () => local.position ?? "top"; const delay = () => local.delay ?? 200; const [visible, setVisible] = createSignal(false); let timeoutId: number | undefined; const show = () => { timeoutId = window.setTimeout(() => setVisible(true), delay()); }; const hide = () => { if (timeoutId) clearTimeout(timeoutId); setVisible(false); }; onCleanup(() => { if (timeoutId) clearTimeout(timeoutId); }); return ( {local.children} {local.content} ); }; export const KeyboardHint: Component<{ keys: string[]; children: JSX.Element }> = (props) => { const keysDisplay = () => props.keys.map((key) => ( {key} )); return {keysDisplay()}}>{props.children}; };