Default
package showcase
import (
"github.com/templui/templui/components/button"
"github.com/templui/templui/components/dialog"
"github.com/templui/templui/components/form"
"github.com/templui/templui/components/input"
)
templ DialogDefault() {
@dialog.Dialog() {
@dialog.Trigger() {
@button.Button(button.Props{
Variant: button.VariantOutline,
}) {
Open Dialog
}
}
@dialog.Content(dialog.ContentProps{
Class: "max-w-md",
}) {
@dialog.Header() {
@dialog.Title() {
Edit Profile
}
@dialog.Description() {
Make changes to your profile here. Click save when you're done.
}
}
<div class="space-y-4">
<div class="space-y-2">
@form.Label(form.LabelProps{For: "name"}) {
Name
}
@input.Input(input.Props{
ID: "name",
Placeholder: "Enter your name",
Value: "John Doe",
})
</div>
<div class="space-y-2">
@form.Label(form.LabelProps{For: "username"}) {
Username
}
@input.Input(input.Props{
ID: "username",
Placeholder: "Enter your username",
Value: "@johndoe",
})
</div>
</div>
@dialog.Footer() {
@dialog.Close() {
@button.Button(button.Props{
Variant: button.VariantOutline,
}) {
Cancel
}
}
@button.Button() {
Save changes
}
}
}
}
}
Installation
templui add dialogLoad the script once in your layout:
<head>
@dialog.Script()
</head>import "github.com/templui/templui/components/dialog"Load the script once in your layout:
<head>
@dialog.Script()
</head>Examples
External Trigger
package showcase
import (
"github.com/templui/templui/components/button"
"github.com/templui/templui/components/dialog"
)
templ DialogExternalTrigger() {
<div class="space-y-4">
// External trigger button
@dialog.Trigger(dialog.TriggerProps{
For: "external-dialog",
}) {
@button.Button(button.Props{
Variant: button.VariantOutline,
}) {
Open External Dialog
}
}
// Dialog defined separately
@dialog.Dialog(dialog.Props{
ID: "external-dialog",
}) {
@dialog.Content(dialog.ContentProps{
Class: "max-w-md",
}) {
@dialog.Header() {
@dialog.Title() {
External Dialog
}
@dialog.Description() {
This dialog can be triggered from anywhere on the page.
}
}
@dialog.Footer() {
@dialog.Close() {
@button.Button() {
Close
}
}
}
}
}
</div>
}
API Reference
Dialog
Main dialog wrapper component. Provides context for child components.
| Name | Type | Default |
|---|---|---|
Optional HTML id for the dialog root. Useful for external targeting. | | |
Additional CSS classes to apply to the dialog container | | |
Additional HTML attributes to apply to the dialog container | | |
Whether to disable closing the dialog by clicking outside | | |
Whether to disable closing the dialog with the ESC key | | |
Whether the dialog should be open initially | | |
Trigger
Element that triggers the dialog to open. Usually used inside Dialog, or externally by targeting a dialog root ID with For.
| Name | Type | Default |
|---|---|---|
Unique identifier for the trigger element | | |
Additional CSS classes to apply to the trigger | | |
Additional HTML attributes to apply to the trigger | | |
Optional dialog root id for external triggers | | |
Content
Native dialog content rendered inside Dialog.
| Name | Type | Default |
|---|---|---|
Additional CSS classes to apply to the content | | |
Additional HTML attributes to apply to the content | | |
Whether to hide the default close button | | |
Header
Header section of the dialog.
| Name | Type | Default |
|---|---|---|
Unique identifier for the header element | | |
Additional CSS classes to apply to the header | | |
Additional HTML attributes to apply to the header | | |
Title
Dialog title component. Should be used inside Header.
| Name | Type | Default |
|---|---|---|
Unique identifier for the title element | | |
Additional CSS classes to apply to the title | | |
Additional HTML attributes to apply to the title | | |
Description
Dialog description component. Should be used inside Header.
| Name | Type | Default |
|---|---|---|
Unique identifier for the description element | | |
Additional CSS classes to apply to the description | | |
Additional HTML attributes to apply to the description | | |
Body
Body/content section of the dialog.
| Name | Type | Default |
|---|---|---|
Unique identifier for the body element | | |
Additional CSS classes to apply to the body | | |
Additional HTML attributes to apply to the body | | |
Close
Element that closes the dialog.
| Name | Type | Default |
|---|---|---|
Unique identifier for the close element | | |
Additional CSS classes to apply to the close element | | |
Additional HTML attributes to apply to the close element | | |
Optional dialog root id to close (defaults to closest dialog if empty) | | |
JavaScript API
// Open a dialog programmatically
window.tui.dialog.open("dialog-root-id");
// Close a dialog
window.tui.dialog.close("dialog-root-id");
// Toggle dialog state
window.tui.dialog.toggle("dialog-root-id");
// Check if dialog is open (returns true/false)
const isOpen = window.tui.dialog.isOpen("dialog-root-id");