2
0

tooltip.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { Tooltip as KobalteTooltip } from "@kobalte/core/tooltip"
  2. import { children, createEffect, createSignal, splitProps } from "solid-js"
  3. import type { ComponentProps } from "solid-js"
  4. export interface TooltipProps extends ComponentProps<typeof KobalteTooltip> {
  5. value: string | (() => string)
  6. class?: string
  7. }
  8. export function Tooltip(props: TooltipProps) {
  9. const [open, setOpen] = createSignal(false)
  10. const [local, others] = splitProps(props, ["children", "class"])
  11. const c = children(() => local.children)
  12. createEffect(() => {
  13. const childElements = c()
  14. if (childElements instanceof HTMLElement) {
  15. childElements.addEventListener("focus", () => setOpen(true))
  16. childElements.addEventListener("blur", () => setOpen(false))
  17. } else if (Array.isArray(childElements)) {
  18. for (const child of childElements) {
  19. if (child instanceof HTMLElement) {
  20. child.addEventListener("focus", () => setOpen(true))
  21. child.addEventListener("blur", () => setOpen(false))
  22. }
  23. }
  24. }
  25. })
  26. return (
  27. <KobalteTooltip forceMount {...others} open={open()} onOpenChange={setOpen}>
  28. <KobalteTooltip.Trigger as={"div"} data-component="tooltip-trigger" class={local.class}>
  29. {c()}
  30. </KobalteTooltip.Trigger>
  31. <KobalteTooltip.Portal>
  32. <KobalteTooltip.Content data-component="tooltip" data-placement={props.placement}>
  33. {typeof others.value === "function" ? others.value() : others.value}
  34. {/* <KobalteTooltip.Arrow data-slot="arrow" /> */}
  35. </KobalteTooltip.Content>
  36. </KobalteTooltip.Portal>
  37. </KobalteTooltip>
  38. )
  39. }