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_actions section. See Add row and bulk actions.
  • Name a get action in source.actions, on the resource or the domain. The drawer reads the row with it, and an action named :get is used when none is set. Compiling does not check this for a :drawer action: with no such action the read fails and the drawer opens nothing.
  • For the form in the drawer, have a form section 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.

elixir
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
    ]
end

The 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:

KeyMeaning
kind:drawer, the default, or :sheet.
titleThe heading. A string or fn record -> string end. A string is translated.
subThe line under the title.
iconA heroicon name, drawn in a tile before the title.
toneThe colour of the tile: :info (the default), :ok, :warn, :danger or :mute.
chipsChips under the title. Each is a label or a {tone, label} pair.
actionsButtons, 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.

elixir
row_actions do
  action :edit, type: :drawer, open: :form,
    drawer: [kind: :sheet, title: "Edit order"]
end

The 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.

elixir
def handle_info({:form_saved, _mode, record}, socket) do
  MishkaGervaz.Table.Web.Live.close_drawer("orders-table", record)
  {:noreply, socket}
end

The 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:

PanelOpenClose
Drawershow_drawer/2hide_drawer/2
Sheetshow_sheet/2hide_sheet/2
Windowshow_window/2hide_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:

heex
<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:

AssignMeaning
idRequired. The panel is <id>-panel, its title <id>-title, its body <id>-body and its close button <id>-close.
showOpens the panel as it is drawn.
title, sub, icon, tone, chipsThe 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_closeA JS that runs as the panel closes, such as JS.push("close_drawer").
confirm_closeA question the close button and the backdrop ask first, through the browser's data-confirm. nil closes at once.
request_closeA 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.
classReplaces the width classes of the panel.
actions, actions_at, footerSlots 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:

elixir
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:

heex
<div id={@static.id}>
  ...
  {MishkaGervaz.Table.Templates.Shared.render_overlays(assigns)}
</div>

Next steps