dialog.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {
  2. Dialog as Kobalte,
  3. DialogRootProps,
  4. DialogTitleProps,
  5. DialogCloseButtonProps,
  6. DialogDescriptionProps,
  7. } from "@kobalte/core/dialog"
  8. import { ComponentProps, type JSX, onCleanup, Show, splitProps } from "solid-js"
  9. import { IconButton } from "./icon-button"
  10. export interface DialogProps extends DialogRootProps {
  11. trigger?: JSX.Element
  12. class?: ComponentProps<"div">["class"]
  13. classList?: ComponentProps<"div">["classList"]
  14. }
  15. export function DialogRoot(props: DialogProps) {
  16. let trigger!: HTMLElement
  17. const [local, others] = splitProps(props, ["trigger", "class", "classList", "children"])
  18. const resetTabIndex = () => {
  19. trigger.tabIndex = 0
  20. }
  21. const handleTriggerFocus = (e: FocusEvent & { currentTarget: HTMLElement | null }) => {
  22. const firstChild = e.currentTarget?.firstElementChild as HTMLElement
  23. if (!firstChild) return
  24. firstChild.focus()
  25. trigger.tabIndex = -1
  26. firstChild.addEventListener("focusout", resetTabIndex)
  27. onCleanup(() => {
  28. firstChild.removeEventListener("focusout", resetTabIndex)
  29. })
  30. }
  31. return (
  32. <Kobalte {...others}>
  33. <Show when={props.trigger}>
  34. <Kobalte.Trigger ref={trigger} data-component="dialog-trigger" onFocusIn={handleTriggerFocus}>
  35. {props.trigger}
  36. </Kobalte.Trigger>
  37. </Show>
  38. <Kobalte.Portal>
  39. <Kobalte.Overlay data-component="dialog-overlay" />
  40. <div data-component="dialog">
  41. <div data-slot="dialog-container">
  42. <Kobalte.Content
  43. data-slot="dialog-content"
  44. classList={{
  45. ...(local.classList ?? {}),
  46. [local.class ?? ""]: !!local.class,
  47. }}
  48. >
  49. {local.children}
  50. </Kobalte.Content>
  51. </div>
  52. </div>
  53. </Kobalte.Portal>
  54. </Kobalte>
  55. )
  56. }
  57. function DialogHeader(props: ComponentProps<"div">) {
  58. return <div data-slot="dialog-header" {...props} />
  59. }
  60. function DialogBody(props: ComponentProps<"div">) {
  61. return <div data-slot="dialog-body" {...props} />
  62. }
  63. function DialogTitle(props: DialogTitleProps & ComponentProps<"h2">) {
  64. return <Kobalte.Title data-slot="dialog-title" {...props} />
  65. }
  66. function DialogDescription(props: DialogDescriptionProps & ComponentProps<"p">) {
  67. return <Kobalte.Description data-slot="dialog-description" {...props} />
  68. }
  69. function DialogCloseButton(props: DialogCloseButtonProps & ComponentProps<"button">) {
  70. return <Kobalte.CloseButton data-slot="dialog-close-button" as={IconButton} icon="close" variant="ghost" {...props} />
  71. }
  72. export const Dialog = Object.assign(DialogRoot, {
  73. Header: DialogHeader,
  74. Title: DialogTitle,
  75. Description: DialogDescription,
  76. CloseButton: DialogCloseButton,
  77. Body: DialogBody,
  78. })