dialog.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {
  2. createContext,
  3. createEffect,
  4. createRoot,
  5. createSignal,
  6. getOwner,
  7. onCleanup,
  8. type Owner,
  9. type ParentProps,
  10. runWithOwner,
  11. useContext,
  12. type JSX,
  13. startTransition,
  14. } from "solid-js"
  15. import { Dialog as Kobalte } from "@kobalte/core/dialog"
  16. import { makeEventListener } from "@solid-primitives/event-listener"
  17. type DialogElement = () => JSX.Element
  18. type Active = {
  19. id: string
  20. node: JSX.Element
  21. dispose: () => void
  22. owner: Owner
  23. onClose?: () => void
  24. setClosing: (closing: boolean) => void
  25. }
  26. const Context = createContext<ReturnType<typeof init>>()
  27. function init() {
  28. const [active, setActive] = createSignal<Active | undefined>()
  29. const timer = { current: undefined as ReturnType<typeof setTimeout> | undefined }
  30. const lock = { value: false }
  31. onCleanup(() => {
  32. if (timer.current === undefined) return
  33. clearTimeout(timer.current)
  34. timer.current = undefined
  35. })
  36. const close = () => {
  37. const current = active()
  38. if (!current || lock.value) return
  39. lock.value = true
  40. current.onClose?.()
  41. current.setClosing(true)
  42. const id = current.id
  43. if (timer.current !== undefined) {
  44. clearTimeout(timer.current)
  45. timer.current = undefined
  46. }
  47. timer.current = setTimeout(() => {
  48. timer.current = undefined
  49. current.dispose()
  50. if (active()?.id === id) setActive(undefined)
  51. lock.value = false
  52. }, 100)
  53. }
  54. createEffect(() => {
  55. if (!active()) return
  56. const onKeyDown = (event: KeyboardEvent) => {
  57. if (event.key !== "Escape") return
  58. close()
  59. event.preventDefault()
  60. event.stopPropagation()
  61. }
  62. makeEventListener(window, "keydown", onKeyDown, { capture: true })
  63. })
  64. const show = (element: DialogElement, owner: Owner, onClose?: () => void) => {
  65. // Immediately dispose any existing dialog when showing a new one
  66. const current = active()
  67. if (current) {
  68. current.dispose()
  69. setActive(undefined)
  70. }
  71. if (timer.current !== undefined) {
  72. clearTimeout(timer.current)
  73. timer.current = undefined
  74. }
  75. lock.value = false
  76. const id = Math.random().toString(36).slice(2)
  77. let dispose: (() => void) | undefined
  78. let setClosing: ((closing: boolean) => void) | undefined
  79. const node = runWithOwner(owner, () =>
  80. createRoot((d: () => void) => {
  81. dispose = d
  82. const [closing, setClosingSignal] = createSignal(false)
  83. setClosing = setClosingSignal
  84. return (
  85. <Kobalte
  86. modal
  87. open={!closing()}
  88. onOpenChange={(open: boolean) => {
  89. if (open) return
  90. close()
  91. }}
  92. >
  93. <Kobalte.Portal>
  94. <Kobalte.Overlay data-component="dialog-overlay" onClick={close} />
  95. {element()}
  96. </Kobalte.Portal>
  97. </Kobalte>
  98. )
  99. }),
  100. )
  101. if (!dispose || !setClosing) return
  102. setActive({ id, node, dispose, owner, onClose, setClosing })
  103. }
  104. return {
  105. get active() {
  106. return active()
  107. },
  108. close,
  109. show,
  110. }
  111. }
  112. export function DialogProvider(props: ParentProps) {
  113. const ctx = init()
  114. return (
  115. <Context.Provider value={ctx}>
  116. {props.children}
  117. <div data-component="dialog-stack">{ctx.active?.node}</div>
  118. </Context.Provider>
  119. )
  120. }
  121. export function useDialog() {
  122. const ctx = useContext(Context)
  123. const owner = getOwner()
  124. if (!owner) {
  125. throw new Error("useDialog must be used within a DialogProvider")
  126. }
  127. if (!ctx) {
  128. throw new Error("useDialog must be used within a DialogProvider")
  129. }
  130. return {
  131. get active() {
  132. return ctx.active
  133. },
  134. show(element: DialogElement, onClose?: () => void) {
  135. const base = ctx.active?.owner ?? owner
  136. return startTransition(() => ctx.show(element, base, onClose))
  137. },
  138. close() {
  139. ctx.close()
  140. },
  141. }
  142. }