fix(design-system): singleton prompt (#11352)
**What** - add a flag to disable rendering multiple prompts on a page **Why** - pressing "r" when a prompt is already open would stack additional prompts --- CLOSES SUP-802 Co-authored-by: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com>
This commit is contained in:
@@ -1,22 +1,29 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
|
import { useRef } from "react"
|
||||||
import { createRoot } from "react-dom/client"
|
import { createRoot } from "react-dom/client"
|
||||||
import { RenderPrompt, RenderPromptProps } from "./render-prompt"
|
import { RenderPrompt, RenderPromptProps } from "./render-prompt"
|
||||||
|
|
||||||
type UsePromptProps = Omit<RenderPromptProps, "onConfirm" | "onCancel" | "open">
|
type UsePromptProps = Omit<RenderPromptProps, "onConfirm" | "onCancel" | "open">
|
||||||
|
|
||||||
const usePrompt = () => {
|
const usePrompt = () => {
|
||||||
const prompt = async (props: UsePromptProps): Promise<boolean> => {
|
const currentPromptPromise = useRef<Promise<boolean> | null>(null)
|
||||||
return new Promise((resolve) => {
|
|
||||||
let open = true
|
|
||||||
|
|
||||||
|
const prompt = async (props: UsePromptProps): Promise<boolean> => {
|
||||||
|
if (currentPromptPromise.current) {
|
||||||
|
return currentPromptPromise.current
|
||||||
|
}
|
||||||
|
|
||||||
|
const promptPromise = new Promise<boolean>((resolve) => {
|
||||||
|
let open = true
|
||||||
const mountRoot = createRoot(document.createElement("div"))
|
const mountRoot = createRoot(document.createElement("div"))
|
||||||
|
|
||||||
const onCancel = () => {
|
const onCancel = () => {
|
||||||
open = false
|
open = false
|
||||||
mountRoot.unmount()
|
mountRoot.unmount()
|
||||||
resolve(false)
|
resolve(false)
|
||||||
|
currentPromptPromise.current = null
|
||||||
|
|
||||||
// TEMP FIX for Radix issue with dropdowns persisting pointer-events: none on body after closing
|
// TEMP FIX for Radix issue with dropdowns persisting pointer-events: none on body after closing
|
||||||
document.body.style.pointerEvents = "auto"
|
document.body.style.pointerEvents = "auto"
|
||||||
@@ -26,6 +33,7 @@ const usePrompt = () => {
|
|||||||
open = false
|
open = false
|
||||||
resolve(true)
|
resolve(true)
|
||||||
mountRoot.unmount()
|
mountRoot.unmount()
|
||||||
|
currentPromptPromise.current = null
|
||||||
|
|
||||||
// TEMP FIX for Radix issue with dropdowns persisting pointer-events: none on body after closing
|
// TEMP FIX for Radix issue with dropdowns persisting pointer-events: none on body after closing
|
||||||
document.body.style.pointerEvents = "auto"
|
document.body.style.pointerEvents = "auto"
|
||||||
@@ -44,6 +52,9 @@ const usePrompt = () => {
|
|||||||
|
|
||||||
render()
|
render()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
currentPromptPromise.current = promptPromise
|
||||||
|
return promptPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
return prompt
|
return prompt
|
||||||
|
|||||||
Reference in New Issue
Block a user