Dialogs appear above the page and require the user's immediate attention. Use them for confirmations, forms,
or focused tasks that interrupt the main flow.
This is a standard dialog. You can put any content you want in here!
CloseOpen Dialog
<cs-dialoglabel="Dialog"id="dialog-overview">
This is a standard dialog. You can put any content you want in here!
<cs-buttonslot="footer"variant="brand"data-dialog="close">Close</cs-button></cs-dialog><cs-buttonappearance="filled">Open Dialog</cs-button><scripttype="module">const dialog = document.querySelector('#dialog-overview');const openButton = dialog.nextElementSibling;
openButton.addEventListener('click',()=>(dialog.open =true));</script>
Examples
Without a Header
Headers are enabled by default. To render a dialog without a header, add the without-header attribute.
Look ma, no header! Sometimes you just need a clean, simple dialog.
CloseOpen Dialog
<cs-dialoglabel="Dialog"without-headerclass="dialog-without-header">
Look ma, no header! Sometimes you just need a clean, simple dialog.
<cs-buttonslot="footer"variant="brand"data-dialog="close">Close</cs-button></cs-dialog><cs-buttonappearance="filled">Open Dialog</cs-button><scripttype="module">const dialog = document.querySelector('.dialog-without-header');const openButton = dialog.nextElementSibling;
openButton.addEventListener('click',()=>(dialog.open =true));</script>
Footer
Footers can be used to display titles and more. Use the footer slot to add a footer to the dialog.
Check out the footer below — it's a great place for actions and buttons.
CloseOpen Dialog
<cs-dialoglabel="Dialog"class="dialog-footer">
Check out the footer below — it's a great place for actions and buttons.
<cs-buttonslot="footer"variant="brand"data-dialog="close">Close</cs-button></cs-dialog><cs-buttonappearance="filled">Open Dialog</cs-button><scripttype="module">const dialog = document.querySelector('.dialog-footer');const openButton = dialog.nextElementSibling;
openButton.addEventListener('click',()=>(dialog.open =true));</script>
Opening & Closing Declaratively
You can open and close dialogs with JavaScript by toggling the open attribute, but you can also do it declaratively. Add the data-dialog="open id" to any button on the page, where id is the ID of the dialog you want to open.
This dialog was opened declaratively — no JavaScript required!
CloseOpen Dialog
<cs-dialoglabel="Dialog"id="dialog-opening">
This dialog was opened declaratively — no JavaScript required!
<cs-buttonslot="footer"variant="brand"data-dialog="close">Close</cs-button></cs-dialog><cs-buttonappearance="filled"data-dialog="open dialog-opening">Open Dialog</cs-button>
Similarly, you can add data-dialog="close" to a button inside of a dialog to tell it to close.
Click the button in the footer to close this dialog declaratively.
CloseOpen Dialog
<cs-dialoglabel="Dialog"id="dialog-dismiss">
Click the button in the footer to close this dialog declaratively.
<cs-buttonslot="footer"variant="brand"data-dialog="close">Close</cs-button></cs-dialog><cs-buttonappearance="filled"data-dialog="open dialog-dismiss">Open Dialog</cs-button>
Width
Use the --width custom property to set the dialog’s width.
This dialog is wider than the default — handy when you need more room for content.
CloseOpen Dialog
<cs-dialoglabel="Dialog"class="dialog-width"style="--width: 50vw;">
This dialog is wider than the default — handy when you need more room for content.
<cs-buttonslot="footer"variant="brand"data-dialog="close">Close</cs-button></cs-dialog><cs-buttonappearance="filled">Open Dialog</cs-button><scripttype="module">const dialog = document.querySelector('.dialog-width');const openButton = dialog.nextElementSibling;
openButton.addEventListener('click',()=>(dialog.open =true));</script>
Scrolling
By design, a dialog’s height will never exceed that of the viewport. As such, dialogs will not scroll with the page ensuring the header and footer are always accessible to the user.
Scroll down and give it a try!
CloseOpen Dialog
<cs-dialoglabel="Dialog"class="dialog-scrolling"><divstyle="height: 150vh;border: dashed 2px var(--cs-color-surface-border);padding: 0 1rem;"><pclass="cs-cluster cs-gap-2xs">Scroll down and give it a try! <cs-iconname="arrow_downward"></cs-icon></p></div><cs-buttonslot="footer"variant="brand"data-dialog="close">Close</cs-button></cs-dialog><cs-buttonappearance="filled">Open Dialog</cs-button><scripttype="module">const dialog = document.querySelector('.dialog-scrolling');const openButton = dialog.nextElementSibling;
openButton.addEventListener('click',()=>(dialog.open =true));</script>
Header Actions
The header shows a functional close button by default. You can use the header-actions slot to add additional buttons if needed.
You can add custom actions to the header, like the icon button up there!
CloseOpen Dialog
<cs-dialoglabel="Dialog"class="dialog-header-actions"><cs-buttonclass="new-window"slot="header-actions"appearance="plain"><cs-iconname="open_in_new"label="Open in new window"></cs-icon></cs-button>
You can add custom actions to the header, like the icon button up there!
<cs-buttonslot="footer"variant="brand"data-dialog="close">Close</cs-button></cs-dialog><cs-buttonappearance="filled">Open Dialog</cs-button><scripttype="module">const dialog = document.querySelector('.dialog-header-actions');const openButton = dialog.nextElementSibling;const newWindowButton = dialog.querySelector('.new-window');
openButton.addEventListener('click',()=>(dialog.open =true));
newWindowButton.addEventListener('click',()=> window.open(location.href));</script>
Light Dismissal
If you want the dialog to close when the user clicks on the overlay, add the light-dismiss attribute.
This dialog will close when you click on the overlay.
CloseOpen Dialog
<cs-dialoglabel="Dialog"light-dismissclass="dialog-light-dismiss">
This dialog will close when you click on the overlay.
<cs-buttonslot="footer"variant="brand"data-dialog="close">Close</cs-button></cs-dialog><cs-buttonappearance="filled">Open Dialog</cs-button><scripttype="module">const dialog = document.querySelector('.dialog-light-dismiss');const openButton = dialog.nextElementSibling;
openButton.addEventListener('click',()=>(dialog.open =true));</script>
Preventing the Dialog from Closing
By default, dialogs will close when the user clicks the close button or presses the Escape key. In most cases, the default behavior is the best behavior in terms of UX. However, there are situations where this may be undesirable, such as when data loss will occur.
To keep the dialog open in such cases, you can cancel the cs-hide event. When canceled, the dialog will remain open and pulse briefly to draw the user’s attention to it.
You can use event.detail.source to determine which element triggered the request to close. This example prevents the dialog from closing unless a specific button is clicked.
This dialog will only close when you click the button below.
Only this button will close itOpen Dialog
<cs-dialoglabel="Dialog"class="dialog-deny-close">
This dialog will only close when you click the button below.
<cs-buttonslot="footer"variant="brand"data-dialog="close">Only this button will close it</cs-button></cs-dialog><cs-buttonappearance="filled">Open Dialog</cs-button><scripttype="module">const dialog = document.querySelector('.dialog-deny-close');const openButton = dialog.nextElementSibling;const closeButton = dialog.querySelector('cs-button[slot="footer"]');
openButton.addEventListener('click',()=>(dialog.open =true));// Prevent the dialog from closing unless the close button was clicked
dialog.addEventListener('cs-hide',event=>{if(event.detail.source !== closeButton){
event.preventDefault();}});</script>
Initial Focus
To give focus to a specific element when the dialog opens, use the autofocus attribute.
CloseOpen Dialog
<cs-dialoglabel="Dialog"class="dialog-focus"><cs-inputautofocusplaceholder="I will have focus when the dialog is opened"></cs-input><cs-buttonslot="footer"variant="brand"data-dialog="close">Close</cs-button></cs-dialog><cs-buttonappearance="filled">Open Dialog</cs-button><scripttype="module">const dialog = document.querySelector('.dialog-focus');const input = dialog.querySelector('cs-input');const openButton = dialog.nextElementSibling;
openButton.addEventListener('click',()=>(dialog.open =true));</script>
API
Importing
If you're using the autoloader or a hosted project, components load on demand — no manual import
needed. To cherry-pick this component, use one of the following snippets.
The dialog's label as displayed in the header. You should always include a relevant label, as it is required for proper accessibility. If you need to display HTML, use the label slot instead.
string
''
Yes
lightDismiss
light-dismiss
When enabled, the dialog will be closed when the user clicks outside of it.
boolean
false
—
open
open
Indicates whether or not the dialog is open. Toggle this attribute to show and hide the dialog.
boolean
false
Yes
ssrFooter
ssr-footer
Only required for SSR. Set to true if you're slotting in a footer element so the server-rendered markup includes the footer before the component hydrates on the client.
boolean
false
—
withoutHeader
without-header
Disables the header. This will also remove the default close button.
Emitted after the dialog closes and all animations are complete.
cs-after-show
Emitted after the dialog opens and all animations are complete.
cs-hide
Emitted when the dialog is requested to close. Calling event.preventDefault() will prevent the dialog from closing. You can inspect event.detail.source to see which element caused the dialog to close. If the source is the dialog element itself, the user has pressed Escape or the dialog has been closed programmatically. Avoid using this unless closing the dialog will result in destructive behavior such as data loss.