Skip to content

Copy Field

A read only value with a copy button beside it that shows its result. A secret value is masked, and with allowExpose an eye inside the box shows and hides it.

Decision

When Use
A link or a key the user copies, such as a webhook URL Copy Field
A credential the user types Secret Field

Usage

import { createCopyField } from './configurationpage?name=myplugin_stolon.js';

var url = createCopyField({ value: config.WebhookUrl, secret: false });
container.appendChild(url.element);

Classes: stolon-copy-field, stolon-copy-field-text, stolon-copy-button, stolon-copy-reveal. The handle is { element, input, getValue, setValue, copy, setSize, destroy }.

States and Modifiers

  • secret: true masks the value. allowExpose: true adds an eye inside the box, at its trailing end, that shows and hides it. It is off by default, so a secret stays masked unless the page opts in. mono (default true) shows the value in the mono font.
  • wrap: true adds wrap and shows the whole value as read-only text in stolon-copy-field-text, broken anywhere over as many lines as it needs, with the buttons level with its first line. Focus or Ctrl+A selects all of it, and input stays hidden, holding the value. A secret ignores it.
  • inline: true adds inline, for a field in a row or a table cell, with none of a form field's space below it.
  • The copy button is a stolon-row-btn. It takes success for the feedback, and shows a check and the feedback text. A hidden status region announces it.
  • The feedback is "Copied" or "Copy failed." for 1500 ms (feedbackMs). copiedText and failedText replace the text.
  • The labels default to "Value", "Copy", "Show", and "Hide". label, copyLabel, revealLabel, and hideLabel replace them.
  • copy() returns a promise of whether the copy worked. onCopy(value, ok) runs after each copy.
  • The copy uses copyToClipboard, which is also exported on its own.
  • size is small, medium (default), or large, and sizes its fields on the size scale. setSize(size) changes it later.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createCopyField

function createCopyField(options?: CopyFieldOptions): CopyFieldHandle

A read-only value with a copy button beside it that shows its result. A secret value is masked, and with allowExpose an eye inside the box shows and hides it.

Option Type What it does
value unknown the text to show and copy [optional]
inline boolean true sits the field in a row or a table cell, with none of a form field's space below it (default false)
wrap boolean true shows the whole value, broken over as many lines as it needs, with the buttons level with its first line. Ignored for a secret (default false)
secret boolean true masks the value (default false)
allowExpose boolean true adds an eye inside a secret's box that shows and hides it (default false)
mono boolean true shows the value in the mono font (default true)
label string aria-label of the value (default 'Value')
copyLabel string title and aria-label of the copy button (default 'Copy')
copiedText string feedback after a copy (default 'Copied')
failedText string feedback when the copy failed (default 'Copy failed.')
revealLabel string title of the eye while masked (default 'Show')
hideLabel string title of the eye while shown (default 'Hide')
feedbackMs number how long the feedback stays (default 1500)
size 'small' | 'medium' | 'large' 'small', 'medium', or 'large' for its fields (default 'medium')
onCopy (value: string, ok: boolean): void function(value, ok) called after each copy [optional]
Handle member Type
input HTMLInputElement
getValue (): string
setValue (value: unknown): void
copy (): Promise<boolean>
setSize (size?: 'small' | 'medium' | 'large'): void
destroy (): void
Class What it looks like
stolon-copy-field Read-only value with a copy button beside it, and an optional eye inside a secret's box
stolon-copy-field.stolon-wrap A wrapped copy field: the whole value as read-only text over as many lines as it needs, broken anywhere, with the buttons level with its first line
stolon-copy-field.stolon-inline An inline copy field, in a row or a table cell, with none of a form field's space below it

Example

Example Code

<!-- title: Copy Field -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
mount.appendChild(base.createCopyField({ value: 'https://example.test/hooks/abc123' }).element);
const secret = base.createCopyField({ value: 'sk-demo-secret-0123456789', secret: true, allowExpose: true }).element;
secret.style.marginTop = '12px';
mount.appendChild(secret);
</script>