Components Forms

Attachment

Select one or more local files and inspect the pending attachments.

Live previewGo + JavaScript

PDF, PNG, or JPEG. Files stay local until you submit.

    Usage

    Accept filters the file chooser, and Multiple allows multiple files. Selected files stay in the native input until your form submits. Use multipart/form-data and validate files on the server.

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

    Go usage
    Go usage
    err := mono.UI.Render(w, "attachment", mono.Props{
    	"Accept":   ".pdf,.png,.jpg,.jpeg",
    	"Hint":     "PDF, PNG, or JPEG. Files stay local until you submit.",
    	"Label":    "Attach a proof",
    	"Multiple": true,
    	"Name":     "proofs",
    })
    Rendered HTML
    Rendered HTML
    <div id="demo-attachment" data-mono="attachment" class="mu-stack">
      <label for="demo-attachment-files" class="mu-label">Attach a proof</label>
      <input class="mu-field" id="demo-attachment-files" name="proofs" type="file" accept=".pdf,.png,.jpg,.jpeg" multiple  aria-describedby="demo-attachment-hint">
      <p id="demo-attachment-hint" class="mu-small mu-muted">PDF, PNG, or JPEG. Files stay local until you submit.</p>
      <ul class="mu-list mu-small" data-file-list></ul>
      <button type="button" class="mu-btn mu-ghost" hidden>Clear selection</button>
      <output class="mu-small" role="status"></output>
    </div>
    Go template
    Go template
    <div id="{{.ID}}" data-mono="attachment" class="mu-stack">
      <label for="{{.ID}}-files" class="mu-label">{{.Label}}</label>
      <input class="mu-field" id="{{.ID}}-files" name="{{or .Name .ID}}" type="file" {{if .Accept}}accept="{{.Accept}}"{{end}} {{if .Multiple}}multiple{{end}} {{if .Required}}required{{end}} {{if .Hint}}aria-describedby="{{.ID}}-hint"{{end}}>
      {{if .Hint}}<p id="{{.ID}}-hint" class="mu-small mu-muted">{{.Hint}}</p>{{end}}
      <ul class="mu-list mu-small" data-file-list></ul>
      <button type="button" class="mu-btn mu-ghost" hidden>Clear selection</button>
      <output class="mu-small" role="status"></output>
    </div>
    Component CSS
    Component CSS
    [data-mono="attachment"] input::file-selector-button { margin-right: .65rem; background: #eee; color: #171717; border: 1px solid #999; border-radius: .25rem; padding: .25rem .5rem; cursor: pointer; }
    [data-mono="attachment"] button { justify-self: start; } [data-mono="attachment"] ul:empty { display: none; }
    JavaScript module
    JavaScript module
    export function mount(root) {
      const input = root.querySelector('input');
      const list = root.querySelector('ul');
      const clear = root.querySelector('button');
      const status = root.querySelector('output');
      function update() {
        const files = [...input.files];
        list.replaceChildren(...files.map(file => {
          const entry = document.createElement('li');
          entry.textContent = file.name + ' · ' + Math.ceil(file.size / 1024) + ' KB';
          return entry;
        }));
        clear.hidden = !files.length;
        status.textContent = files.length ? files.length + (files.length === 1 ? ' file selected.' : ' files selected.') : '';
      }
      input.addEventListener('change', update);
      clear.addEventListener('click', () => {
        input.value = '';
        input.dispatchEvent(new Event('change', { bubbles: true }));
        status.textContent = 'Selection cleared.';
        input.focus();
      });
      update();
      input.form?.addEventListener('reset', () => setTimeout(update, 0));
    }
    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="./attachment.css">
    <script type="module" src="./attachment.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 attachment
    Example properties (JSON)
    example.json
    {
      "Accept": ".pdf,.png,.jpg,.jpeg",
      "Hint": "PDF, PNG, or JPEG. Files stay local until you submit.",
      "Label": "Attach a proof",
      "Multiple": true,
      "Name": "proofs"
    }