Open a row in a drawer
v0.0.12
A Gervaz table can open a row in a drawer beside the list, so the reader reads a record or edits it without leaving the table. The drawer is a panel of the UI adapter: it comes in from the side, and it is a bottom sheet on a screen narrower than 640px. This guide adds a :drawer row action, shows a record view or the resource's form in it, closes it after a save, and uses the same panels on a page of your own.
Before you begin
- Have a table with a name, a route and a
row_actionssection. See Add row and bulk actions. - Name a
getaction insource.actions, on the resource or the domain. The drawer reads the row with it, and an action named:getis used when none is set. Compiling does not check this for a:draweraction: with no such action the read fails and the drawer opens nothing. - For the form in the drawer, have a
formsection on the same resource. See Build an admin form. - Use the default table template, or a custom one that draws the overlays. See the last section of this page.
Open a record view
Give the action the type :drawer and an open function. The function receives the row and returns HEEx. The drawer option builds the head of the panel from the row.
row_actions do
action :view, type: :drawer,
open: fn record -> MyAppWeb.OrderViews.order(%{order: record}) end,
drawer: [
title: fn record -> record.number end,
sub: fn record -> record.customer_name end,
icon: "hero-receipt-percent",
tone: :info,
chips: fn record -> [{:ok, "Paid"}, record.provider] end,
actions: fn record -> MyAppWeb.OrderViews.order_actions(%{order: record}) end
]
endThe table reads the row again by its id as the drawer opens, with the table's get action, as the signed-in user and under the user's tenant, so a row that is gone opens nothing. These keys of drawer shape the head:
| Key | Meaning |
|---|---|
kind | :drawer, the default, or :sheet. |
title | The heading. A string or fn record -> string end. A string is translated. |
sub | The line under the title. |
icon | A heroicon name, drawn in a tile before the title. |
tone | The colour of the tile: :info (the default), :ok, :warn, :danger or :mute. |
chips | Chips under the title. Each is a label or a {tone, label} pair. |
actions | Buttons, fn record -> ~H"..." end. They stand in the foot of the panel. |
actions_at | :foot, the default, or :head, beside the close button. |
Every value but kind and actions_at is a value or a function of the row. An action of this type without open is reported when you compile.
Edit the row in the drawer
Set open: :form to put the resource's own form in the drawer, editing the row. The form has the id "<table id>-drawer-form" and gets the row's id as its record_id.
row_actions do
action :edit, type: :drawer, open: :form,
drawer: [kind: :sheet, title: "Edit order"]
endThe form tells your LiveView {:form_saved, mode, record}, as any form does. Close the drawer and put the saved row back in its place with close_drawer/2. The table reads the row again, as it does for a realtime update, and a row the table's read no longer finds leaves the list.
def handle_info({:form_saved, _mode, record}, socket) do
MishkaGervaz.Table.Web.Live.close_drawer("orders-table", record)
{:noreply, socket}
endThe drawer's close button, its backdrop and Escape close it on their own. Call close_drawer/1 with no record to close it without touching a row.
Use a panel on a page of your own
The UI adapter draws three panels you can use for any form or page: drawer/1, sheet/1 and window/1. A drawer comes from the right side, and from the left in a right-to-left page. A sheet rises from the bottom edge at every width. A window opens in the middle of the screen and is a bottom sheet below 640px. Each opens and closes with Phoenix.LiveView.JS, so no JavaScript of your own is needed:
| Panel | Open | Close |
|---|---|---|
| Drawer | show_drawer/2 | hide_drawer/2 |
| Sheet | show_sheet/2 | hide_sheet/2 |
| Window | show_window/2 | hide_window/2 |
Each takes an optional JS to chain from and the panel's id. A panel is drawn hidden. Render it with show, as :if={@open?} show, so it opens as it is drawn, or keep it rendered and run show_drawer(id) from a click:
<button phx-click={MishkaGervaz.UIAdapters.Tailwind.show_drawer("post-drawer")}>
Edit post
</button>
<MishkaGervaz.UIAdapters.Tailwind.drawer
id="post-drawer"
title="Edit post"
icon="hero-pencil-square"
confirm_close={@post_form_dirty? && "Discard your changes?"}
>
<.live_component
module={MishkaGervaz.Form.Web.Live}
id="post-form"
resource={MyApp.Post}
current_user={@current_user}
notify_dirty={true}
/>
</MishkaGervaz.UIAdapters.Tailwind.drawer>These assigns shape a panel:
| Assign | Meaning |
|---|---|
id | Required. The panel is <id>-panel, its title <id>-title, its body <id>-body and its close button <id>-close. |
show | Opens the panel as it is drawn. |
title, sub, icon, tone, chips | The head, as the table above describes it. A window with none of title, icon, sub and chips draws no head, and its close button stands in the corner. That suits a form that draws its own header. |
on_close | A JS that runs as the panel closes, such as JS.push("close_drawer"). |
confirm_close | A question the close button and the backdrop ask first, through the browser's data-confirm. nil closes at once. |
request_close | A JS that the close button, the backdrop and Escape run in place of closing. Use it when the server decides whether the panel may close, then close it with hide_drawer/2 or take it away. |
class | Replaces the width classes of the panel. |
actions, actions_at, footer | Slots for buttons and for content pinned under the body. actions_at is :foot or :head. |
Every panel keeps the focus inside it while it is open, and its close button is 44px tall on a phone. Escape closes it, or runs request_close when that is given, unless confirm_close is given.
Ask before the panel closes with unsaved changes
Mount the form with notify_dirty={true}. It sends its LiveView {:form_dirty, id, dirty?} each time it comes to hold changes and each time it holds none, and the wrapper of the form carries data-dirty. Keep the answer and pass it to confirm_close:
def handle_info({:form_dirty, "post-form", dirty?}, socket),
do: {:noreply, assign(socket, :post_form_dirty?, dirty?)}A cancel or discard button of the form's submit block with a confirm asks it only while the form holds changes.
Keep Save in reach
A form in the body of a panel keeps its tabs or steps at the top of the body and its submit row at the bottom, while only the fields scroll. Save stays in reach on a short screen and after a failed save. On a phone the tabs and the submit buttons are 44px tall. The body of a panel carries data-role="gervaz-panel-body".
Draw the overlays in a custom template
The default templates draw the drawer for you. A custom table template draws it, and the question windows of its actions, by ending its root element with Shared.render_overlays/1:
<div id={@static.id}>
...
{MishkaGervaz.Table.Templates.Shared.render_overlays(assigns)}
</div>Next steps
- Ask before an action: the question window of the same UI adapter.
- Show a toast: a short note after something happened.
- Write a table or form template: where the overlays go in your own template.
- Table actions DSL: every option of a row action.