Skip to content

Placeholder Hint

A "Placeholders" help toggle under a field that lists {name} buttons and inserts the pressed one at the cursor, or copies it.

Decision

When Use
Template fields such as message formats Placeholder Hint
Explaining a field A Help Hint, which inserts nothing

Usage

import { placeholderHint, bindPlaceholders } from './configurationpage?name=myplugin_stolon.js';

container.innerHTML =
    '<div class="inputContainer">' +
        '<label for="txtTitleFormat">Title format</label>' +
        '<input is="emby-input" type="text" id="txtTitleFormat" />' +
        placeholderHint([['title', 'The item title'], ['year', 'The release year']]) +
    '</div>';
bindPlaceholders(container, function (field) { tracker.check(); });

Classes: stolon-placeholder-hint, stolon-hint-button, stolon-placeholders, stolon-tag-button. insertAtCursor(field, text) replaces the selection, puts the cursor after the text, focuses the field, and fires an input event. All three functions are also on the bag.

States and Modifiers

  • placeholderHint(placeholders, options) turns [[name, description], ...] into an HTML string. Names and descriptions are escaped.
  • open and close set the delimiters, { and } by default, so { open: '{{', close: '}}' } draws and inserts {{name}} for a template language that uses them.
  • mode: 'copy' copies the pressed token to the clipboard instead of inserting it, for a field that is not beside the hint. The token turns green for a moment and a hidden live region says copiedText, "Copied {value}." by default. A hint in copy mode works outside an inputContainer.
  • label replaces the toggle's "Placeholders".
  • The Placeholders toggle is a text button with no chevron. It starts stolon-collapsed with aria-expanded="false" and aria-controls on its list. Its icon and text are secondary gray while the list is hidden and the accent while it shows, with the accent on hover and the accent focus ring.
  • bindPlaceholders(root, onInsert) makes every hint in root work, once per root. A placeholder button inserts its token into the first textarea or input of its inputContainer, and onInsert(field) follows each insert.
  • Pressing a placeholder does not take focus from the field, so the insertion lands at the cursor.

Gotchas

  • Put the hint inside the same inputContainer as the field.

Reference

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

insertAtCursor

function insertAtCursor(field: PlaceholderField | null, text: string): void

Puts text into an input or text area at the cursor, replacing any selection, and leaves the cursor after it.

placeholderHint

function placeholderHint(placeholders?: Placeholder[] | null, options?: PlaceholderHintOptions | null): string

Returns a field's Placeholders hint and the hidden list it shows. Put it after the field, in the same inputContainer. The hint is a collapsed toggle with aria-expanded and aria-controls on its list, accent while the list shows and gray while it is hidden.

Option Type What it does
open string the opening delimiter (default '{')
close string the closing delimiter (default '}')
mode 'insert' | 'copy' 'insert' puts the token in the field at the cursor, and 'copy' copies it instead (default 'insert')
label string the toggle's label (default 'Placeholders')
copiedText string the message after a copy, where {value} is the token (default 'Copied {value}.')

bindPlaceholders

function bindPlaceholders(root: HTMLElement | null, onInsert?: (field: PlaceholderField) => void): void

Makes every hint inside root work: Placeholders shows or hides the list, and a placeholder goes into the field in the same inputContainer at the cursor, or onto the clipboard for a hint in copy mode. Binds once per root.

Type What it is
Placeholder One placeholder as [name, description], where name is used as {name}.
PlaceholderField A field a placeholder goes into.
Class What it looks like
stolon-placeholder-hint A field's Placeholders hint, under the field where Jellyfin puts a field's help
stolon-hint-button The hint's toggle, a text button built as small stolon-icon-text whose color shows its state: secondary gray while its list is hidden, accent while it shows and on hover
stolon-placeholders List of placeholder tags that a hint shows
stolon-tag-button A tag that is a button, such as a placeholder to put in
stolon-tag-button.stolon-copied A placeholder just copied in a hint in copy mode, green for a moment as a copy that worked

Example

Example Code

<!-- title: Placeholder Hint -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const mount = document.getElementById('mount');
mount.innerHTML =
    '<div class="inputContainer">' +
        '<label for="txtTitleFormat">Title format</label>' +
        '<input is="emby-input" type="text" id="txtTitleFormat" value="Now playing " />' +
        base.placeholderHint([['title', 'The item title'], ['year', 'The release year']]) +
    '</div>' +
    '<div class="inputContainer">' +
        '<label for="txtScript">Script</label>' +
        '<textarea is="emby-textarea" id="txtScript" rows="3">echo "{{item.name}}"</textarea>' +
        base.placeholderHint([['item.name', 'The item name'], ['item.path', 'The file path']], { open: '{{', close: '}}', mode: 'copy' }) +
    '</div>';
base.bindPlaceholders(mount, () => {});
</script>