Components Overlays

Dialog

Edit a short value in a focused, modal form.

Live previewGo + JavaScript

Edit project title

Choose the name shown on the project cover.

Usage

Set Title, Description, FieldLabel and Value for the editor. Enter saves a valid value; Escape and Cancel dismiss without saving. Listen for mono:save and read detail.value to persist it.

Register dialog with Head and pass its properties to Render. Go setup guide

Go usage
Go usage
err := mono.UI.Render(w, "dialog", mono.Props{
	"Cancel":      "Cancel",
	"Confirm":     "Save title",
	"Description": "Choose the name shown on the project cover.",
	"FieldLabel":  "Project title",
	"SavedLabel":  "Saved title",
	"Title":       "Edit project title",
	"Trigger":     "Edit title",
	"Value":       "Field Notes",
})
Rendered HTML
Rendered HTML
<div id="demo-dialog" data-mono="dialog" data-saved-label="Saved title" class="mu-stack">
  <button type="button" class="mu-btn mu-primary" data-open commandfor="demo-dialog-dialog" command="show-modal">Edit title</button>
  <dialog id="demo-dialog-dialog" class="mu-panel" aria-labelledby="demo-dialog-title" aria-describedby="demo-dialog-description">
    <form method="dialog" class="mu-stack">
      <header class="mu-stack">
        <h3 id="demo-dialog-title" class="mu-title">Edit project title</h3>
        <p id="demo-dialog-description" class="mu-muted">Choose the name shown on the project cover.</p>
      </header>
      <label class="mu-stack mu-label">Project title
        <input class="mu-field" name="title" value="Field Notes" required autofocus>
      </label>
      <div class="mu-row">
        <button class="mu-btn" type="button" data-cancel commandfor="demo-dialog-dialog" command="close">Cancel</button>
        <button class="mu-btn mu-primary" type="submit" value="confirm">Save title</button>
      </div>
    </form>
  </dialog>
  <p class="mu-small" data-result role="status"></p>
</div>
Go template
Go template
<div id="{{.ID}}" data-mono="dialog" data-saved-label="{{.SavedLabel}}" class="mu-stack">
  <button type="button" class="mu-btn mu-primary" data-open commandfor="{{.ID}}-dialog" command="show-modal">{{.Trigger}}</button>
  <dialog id="{{.ID}}-dialog" class="mu-panel" aria-labelledby="{{.ID}}-title" aria-describedby="{{.ID}}-description">
    <form method="dialog" class="mu-stack">
      <header class="mu-stack">
        <h3 id="{{.ID}}-title" class="mu-title">{{.Title}}</h3>
        <p id="{{.ID}}-description" class="mu-muted">{{.Description}}</p>
      </header>
      <label class="mu-stack mu-label">{{.FieldLabel}}
        <input class="mu-field" name="title" value="{{.Value}}" required autofocus>
      </label>
      <div class="mu-row">
        <button class="mu-btn" type="button" data-cancel commandfor="{{.ID}}-dialog" command="close">{{.Cancel}}</button>
        <button class="mu-btn mu-primary" type="submit" value="confirm">{{.Confirm}}</button>
      </div>
    </form>
  </dialog>
  <p class="mu-small" data-result role="status"></p>
</div>
Component CSS
Component CSS
[data-mono="dialog"] > button { justify-self: start; align-self: flex-start; }
[data-mono="dialog"] > [data-result]:empty { display: none; }
[data-mono="dialog"] dialog { color: #171717; background: white; border: 1px solid #ccc; border-radius: .75rem; padding: 1.5rem; width: min(25rem, calc(100% - 2rem)); max-height: calc(100dvh - 2rem); overflow: auto; margin: auto; box-shadow: 0 1.25rem 4rem #0003; }
[data-mono="dialog"] dialog::backdrop { background: #1118; }
[data-mono="dialog"] form > .mu-row:last-child { justify-content: flex-end; }
JavaScript module
JavaScript module
export function mount(root) {
  const dialog = root.querySelector('dialog');
  const form = dialog.querySelector('form');
  const trigger = root.querySelector('[data-open]');
  const result = root.querySelector('[data-result]');

  trigger.addEventListener('click', event => {
    event.preventDefault();
    form.reset();
    if (!dialog.open) dialog.showModal();
  });
  dialog.querySelector('[data-cancel]').addEventListener('click', event => {
    event.preventDefault();
    dialog.close('cancel');
  });
  dialog.addEventListener('close', () => trigger.focus());
  form.addEventListener('submit', () => {
    const field = form.elements.namedItem('title');
    field.defaultValue = field.value;
    result.textContent = root.dataset.savedLabel + ': ' + field.value;
    root.dispatchEvent(new CustomEvent('mono:save', { bubbles: true, detail: { value: field.value } }));
  });
}
Use the HTML in a static site

Copy the rendered HTML above. Download these assets into the same folder as your page, then add the tags below to its head.

Shared CSS Component CSS Ready-to-use module

HTML head
<link rel="stylesheet" href="./base.css">
<link rel="stylesheet" href="./dialog.css">
<script type="module" src="./dialog.js"></script>

Copy into your project

Run this command from the Mono checkout. Set -out to the component directory in your application.

Terminal
go run ./cmd/mono-ui add -out ./components dialog
Example properties (JSON)
example.json
{
  "Cancel": "Cancel",
  "Confirm": "Save title",
  "Description": "Choose the name shown on the project cover.",
  "FieldLabel": "Project title",
  "SavedLabel": "Saved title",
  "Title": "Edit project title",
  "Trigger": "Edit title",
  "Value": "Field Notes"
}