How to close a dialog? #1030
-
Change TypeAddition Proposed ChangesHi, I think, this function is present in this libary, but I am too stupid to figure out how to close a dialog. I think, it would be a good idea to add such an example to the docs, because the dialog example there also does not close when you press the "save" button. Looking forward to your help :) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hi, Coincidentally I was experimenting with this today and found the same. It appears that the Dialog does include the ability to close itself without needing to add extra props (such as binding open). It adds a cross icon in the top-right. If you click that, it closes. However if you compare it to the underlying component on https://www.bits-ui.com/docs/components/dialog that has a
... and ...
Those being its "Save" and "X" buttons. So ... that's clearly how it "should" be done. Interestingly the shadcn-svelte version has opted to not expose that Assuming it's not, I tried adding it myself ... and it appears to work. I simply modified the import { Dialog as DialogPrimitive } from 'bits-ui';
const Root = DialogPrimitive.Root;
const Trigger = DialogPrimitive.Trigger;
const Close = DialogPrimitive.Close; // ADDED
import Title from './dialog-title.svelte';
import Portal from './dialog-portal.svelte';
import Footer from './dialog-footer.svelte';
import Header from './dialog-header.svelte';
import Overlay from './dialog-overlay.svelte';
import Content from './dialog-content.svelte';
import Description from './dialog-description.svelte';
export {
Root,
Title,
Portal,
Footer,
Header,
Trigger,
Overlay,
Content,
Description,
Close, // ADDED
//
Root as Dialog,
Title as DialogTitle,
Portal as DialogPortal,
Footer as DialogFooter,
Header as DialogHeader,
Trigger as DialogTrigger,
Overlay as DialogOverlay,
Content as DialogContent,
Description as DialogDescription,
Close as DialogClose // ADDED
}; Now in my code I can add a custom cancel button to the Dialog footer e.g:
|
Beta Was this translation helpful? Give feedback.
-
I created this (when using the default close (x) provided):
|
Beta Was this translation helpful? Give feedback.
-
Its much easier than this. Just use the underlying bits ui primitive
|
Beta Was this translation helpful? Give feedback.
Hi,
Coincidentally I was experimenting with this today and found the same.
It appears that the Dialog does include the ability to close itself without needing to add extra props (such as binding open). It adds a cross icon in the top-right. If you click that, it closes.
However if you compare it to the underlying component on https://www.bits-ui.com/docs/components/dialog that has a
<Dialog.Close />
. For example you can see its example has::