Update Dialog to be controlled
This commit is contained in:
parent
24c2ee950f
commit
cdb85120de
1 changed files with 15 additions and 18 deletions
|
|
@ -1,39 +1,36 @@
|
||||||
import React from 'react'
|
import React, { PropsWithChildren, useEffect, useState } from 'react'
|
||||||
import * as DialogPrimitive from '@radix-ui/react-dialog'
|
import * as DialogPrimitive from '@radix-ui/react-dialog'
|
||||||
import { useLockedBody } from 'usehooks-ts'
|
import { useLockedBody } from 'usehooks-ts'
|
||||||
|
|
||||||
import './index.scss'
|
import './index.scss'
|
||||||
|
|
||||||
interface Props
|
interface Props extends DialogPrimitive.DialogProps {}
|
||||||
extends React.DetailedHTMLProps<
|
|
||||||
React.DialogHTMLAttributes<HTMLDivElement>,
|
|
||||||
HTMLDivElement
|
|
||||||
> {
|
|
||||||
open: boolean
|
|
||||||
onOpenChange: (open: boolean) => void
|
|
||||||
}
|
|
||||||
|
|
||||||
export const Dialog = React.forwardRef<HTMLDivElement, Props>(function dialog(
|
export const Dialog = ({ children, ...props }: PropsWithChildren<Props>) => {
|
||||||
{ children, ...props },
|
|
||||||
forwardedRef
|
|
||||||
) {
|
|
||||||
const [locked, setLocked] = useLockedBody(false, 'root')
|
const [locked, setLocked] = useLockedBody(false, 'root')
|
||||||
|
const [open, setOpen] = useState(false)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (props.open != undefined) {
|
||||||
|
toggleLocked(props.open)
|
||||||
|
setOpen(props.open)
|
||||||
|
}
|
||||||
|
}, [props.open])
|
||||||
|
|
||||||
function toggleLocked(open: boolean) {
|
function toggleLocked(open: boolean) {
|
||||||
setLocked(open)
|
setLocked(open)
|
||||||
}
|
}
|
||||||
|
|
||||||
function onOpenChange(open: boolean) {
|
function handleOpenChange(open: boolean) {
|
||||||
toggleLocked(open)
|
if (props.onOpenChange) props.onOpenChange(open)
|
||||||
props.onOpenChange(open)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DialogPrimitive.Root open={props.open} onOpenChange={onOpenChange}>
|
<DialogPrimitive.Root open={props.open} onOpenChange={handleOpenChange}>
|
||||||
{children}
|
{children}
|
||||||
</DialogPrimitive.Root>
|
</DialogPrimitive.Root>
|
||||||
)
|
)
|
||||||
})
|
}
|
||||||
|
|
||||||
export const DialogTitle = DialogPrimitive.Title
|
export const DialogTitle = DialogPrimitive.Title
|
||||||
export const DialogTrigger = DialogPrimitive.Trigger
|
export const DialogTrigger = DialogPrimitive.Trigger
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue