Corex.Dialog
(Corex v0.1.0-alpha.29)
View Source
Phoenix implementation of Zag.js Dialog.
Examples
Basic Usage
With top-level slots:
<.dialog id="my-dialog" class="dialog">
<:trigger>Open Dialog</:trigger>
<:title>Dialog Title</:title>
<:description>
This is a dialog description that explains what the dialog is about.
</:description>
<:content>
<p>Dialog content goes here. You can add any content you want inside the dialog.</p>
</:content>
<:close_trigger>
<.icon name="hero-x-mark" class="icon" />
</:close_trigger>
</.dialog>With title, description, and close trigger inside content (use the same id as the dialog):
<.dialog id="my-dialog" class="dialog">
<:trigger>Open Dialog</:trigger>
<:content>
<.dialog_title id="my-dialog">Dialog Title</.dialog_title>
<.dialog_description id="my-dialog">
This is a dialog description that explains what the dialog is about.
</.dialog_description>
<p>Dialog content goes here. You can add any content you want inside the dialog.</p>
<.dialog_close_trigger id="my-dialog">
<.icon name="hero-x-mark" class="icon" />
</.dialog_close_trigger>
</:content>
</.dialog>Controlled Mode
<.dialog
id="my-dialog"
controlled
open={@dialog_open}
on_open_change="dialog_changed">
<:trigger>Open Dialog</:trigger>
<:content>
<:title>Dialog Title</:title>
<:description>Dialog description goes here.</:description>
<p>Dialog content</p>
<:close_trigger>Close</:close_trigger>
</:content>
</.dialog>def handle_event("dialog_changed", %{"open" => open}, socket) do
{:noreply, assign(socket, :dialog_open, open)}
endAPI Control
In order to use the API, you must use an id on the component
Client-side
<button phx-click={Corex.Dialog.set_open("my-dialog", true)}>
Open Dialog
</button>Server-side
def handle_event("open_dialog", _, socket) do
{:noreply, Corex.Dialog.set_open(socket, "my-dialog", true)}
endStyling
Use data attributes to target elements:
[data-scope="dialog"][data-part="root"] {}
[data-scope="dialog"][data-part="trigger"] {}
[data-scope="dialog"][data-part="backdrop"] {}
[data-scope="dialog"][data-part="positioner"] {}
[data-scope="dialog"][data-part="content"] {}
[data-scope="dialog"][data-part="title"] {}
[data-scope="dialog"][data-part="description"] {}
[data-scope="dialog"][data-part="close-trigger"] {}If you wish to use the default Corex styling, you can use the class dialog on the component.
This requires to install Mix.Tasks.Corex.Design first and import the component css file.
@import "../corex/main.css";
@import "../corex/tokens/themes/neo/light.css";
@import "../corex/components/dialog.css";You can then use modifiers
<.dialog class="dialog dialog--accent dialog--lg">Learn more about modifiers and Corex Design
Summary
Components
Renders a dialog component.
Renders the dialog close button. Use inside <:content> when not using the top-level <:close_trigger> slot. Pass the same id as the parent dialog.
Renders the dialog description. Use inside <:content> when not using the top-level <:description> slot. Pass the same id as the parent dialog.
Renders the dialog title. Use inside <:content> when not using the top-level <:title> slot. Pass the same id as the parent dialog.
API
Sets the dialog open state from client-side. Returns a Phoenix.LiveView.JS command.
Sets the dialog open state from server-side. Pushes a LiveView event.
Components
Renders a dialog component.
Attributes
id(:string) - The id of the dialog, useful for API to identify the dialog.open(:boolean) - The initial open state or the controlled open state. Defaults tofalse.controlled(:boolean) - Whether the dialog is controlled. Only in LiveView, the on_open_change event is required. Defaults tofalse.modal(:boolean) - Whether the dialog is modal. Defaults tofalse.close_on_interact_outside(:boolean) - Whether to close the dialog when clicking outside. Defaults totrue.close_on_escape(:boolean) - Whether to close the dialog when pressing Escape. Defaults totrue.prevent_scroll(:boolean) - Whether to prevent body scroll when dialog is open. Defaults tofalse.restore_focus(:boolean) - Whether to restore focus when dialog closes. Defaults totrue.dir(:string) - The direction of the dialog. When nil, derived from document (html lang + config :rtl_locales). Defaults tonil. Must be one ofnil,"ltr", or"rtl".on_open_change(:string) - The server event name when the open state changes. Defaults tonil.on_open_change_client(:string) - The client event name when the open state changes. Defaults tonil.- Global attributes are accepted.
Slots
trigger(required) - Accepts attributes:class(:string)aria_label(:string)
content(required) - Accepts attributes:class(:string)
title- Accepts attributes:class(:string)
description- Accepts attributes:class(:string)
close_trigger- Accepts attributes:class(:string)
Renders the dialog close button. Use inside <:content> when not using the top-level <:close_trigger> slot. Pass the same id as the parent dialog.
Attributes
id(:string) (required)dir(:string) - Defaults tonil.Must be one ofnil,"ltr", or"rtl".- Global attributes are accepted.
Slots
inner_block(required)
Renders the dialog description. Use inside <:content> when not using the top-level <:description> slot. Pass the same id as the parent dialog.
Attributes
id(:string) (required)dir(:string) - Defaults tonil.Must be one ofnil,"ltr", or"rtl".- Global attributes are accepted.
Slots
inner_block(required)
Renders the dialog title. Use inside <:content> when not using the top-level <:title> slot. Pass the same id as the parent dialog.
Attributes
id(:string) (required)dir(:string) - Defaults tonil.Must be one ofnil,"ltr", or"rtl".- Global attributes are accepted.
Slots
inner_block(required)
API
Sets the dialog open state from client-side. Returns a Phoenix.LiveView.JS command.
Examples
<button phx-click={Corex.Dialog.set_open("my-dialog", true)}>
Open Dialog
</button>
Sets the dialog open state from server-side. Pushes a LiveView event.
Examples
def handle_event("open_dialog", _params, socket) do
socket = Corex.Dialog.set_open(socket, "my-dialog", true)
{:noreply, socket}
end