Not to re-mount Dialog/Drawer content #41
-
|
Is it possible not to re-mount Dialog/Drawer content on every open/close? In current implementation:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
Funny, this was the behavior corvu had for a few months before I decided to change it because it went against people's expectations. Sadly, this is very tricky to implement in userland and I haven't found a clean way to do this yet. A simple solution whould be something like this, but this comes with issues: import Dialog from '@corvu/dialog'
import { createMemo, type Accessor, type JSX } from 'solid-js'
function CachedDialogContent() {
const cachedContent = createCached(() => <input />)
return (
<Dialog>
<Dialog.Trigger>
Open
</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Content>
{cachedContent()()}
</Dialog.Content>
</Dialog.Portal>
</Dialog>
)
}
const createCached = (fn: () => JSX.Element) => {
let result: Accessor<JSX.Element>
let called = false
return () => {
if (called) {
return result
} else {
called = true
return (result = createMemo(fn))
}
}
}
export default CachedDialogContentIt resolves the content once the dialog renders for the first time and keeps the html element cached and reuses them when opened again. If I ever find a good solution for this I'd love to provide an optional wrapper component that enables memoizing detachable content like this. But all solutions I've tried up until now either don't fully work or felt weird. And I'm hesitant to add this to the Dialog itself as I'd like to avoid increasing the bundle size if it's not a necessary feature or one that benefits the majority of the users. |
Beta Was this translation helpful? Give feedback.
-
|
Okay follow up: |
Beta Was this translation helpful? Give feedback.
Okay follow up:
This issue catched me and I found a solution! 🥳
I released a small utility which allows users to easily persist their unmounting components: solid-persistent
I added an example in the dialog documentation on how to use it: https://corvu.dev/docs/primitives/dialog/#persistent-content