Replies: 2 comments 4 replies
-
I ended up doing like this, so I don't have to worry about resetting dialog states, mounting & unmounting will do the job. (Still wondering if this is a good way.): function App() {
const [data, mutateData] = useDataFromAPI()
const dialogToggle = useToggle()
return (
<div className="dialog-trigger-attachable" style={{ position: 'relative' }}>
<button className="real-dialog-trigger" onClick={dialogToggle.on}>
Open dialog
</button>
{dialogToggle.isOn && (
<MyDialog
data={data}
setIsOpen={dialogToggle.set}
saveChanges={mutateData}
/>
)}
</div>
)
}
type DialogProps = {
data: string;
saveChanges: (data: string) => void;
setIsOpen: (isOpen: boolean) => void;
}
function MyDialog(props: DialogProps) {
const [input, setInput] = useState(props.data)
return (
// The open state will always be true
// because this dialog will unmount if dialogToggle.isOn is set to false
<Dialog.Root open={true} setIsOpen={props.setIsOpen}>
{/* The trigger is only used as the positioning anchor for content */}
<Dialog.Trigger asChild>
<div style={{ position: 'absolute', inset: 0, pointerEvents: 'none' }} />
</Dialog.Trigger>
<Styled.Content>
<input
value={input}
onChange={(event) => setInput(event.target.value)}
/>
<Dialog.Close onClick={() => props.saveChanges(input)}>
Save
</Dialog.Close>
<Dialog.Close>Cancel</Dialog.Close>
</Styled.Content>
</Dialog.Root>
)
} |
Beta Was this translation helpful? Give feedback.
4 replies
-
Also interested to know if there is a recommended pattern for this. I would like to use a Trigger, but found it messy to reset state on close. I chose to enable unmounting by wrapping the Dialog. I can pass a trigger to this wrapper component but then I'm not using the Radix trigger. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Let's say I have a dialog like this:
Should I reset the dialog states like above, or:
Beta Was this translation helpful? Give feedback.
All reactions