Collapsible Section¶
A section whose header toggles a body. The header is a native button. Its header can also hold an icon tile, a subtitle, and an aside, and with collapsible: false it is a fixed panel that is always open.
Decision¶
| When | Use |
|---|---|
| Optional or long groups of settings | Collapsible Section |
| A framed panel with a rich header, such as one card per library mapping with its route and its mode | collapsible: false |
| Content that must stay visible | Section |
Usage¶
<div class="stolon-collapsible-section">
<button type="button" class="stolon-collapsible-header stolon-collapsed" aria-expanded="false" data-target="advancedBody">
<span class="stolon-collapsible-title">Advanced</span>
<span class="stolon-collapsible-icon"></span>
</button>
<div id="advancedBody" class="stolon-collapsible-content stolon-collapsed">
<div class="inputContainer"><label for="txtName">Name</label><input is="emby-input" type="text" id="txtName" /></div>
<div class="stolon-collapsible-actions">
<button is="emby-button" type="button" class="raised stolon-button stolon-primary stolon-small"><span>Save</span></button>
</div>
</div>
</div>
import { initCollapsibles } from './configurationpage?name=myplugin_stolon.js';
initCollapsibles(view); // safe to call again after you add more sections
Or build one in code, with an optional badge in its header:
import { createCollapsibleSection } from './configurationpage?name=myplugin_stolon.js';
var section = createCollapsibleSection({
title: 'Certificate',
badge: { label: 'Expires Soon', color: 'orange' },
content: '<p>The certificate expires in 9 days.</p>',
collapsed: true
});
view.querySelector('#host').appendChild(section.element);
section.setBadge({ label: 'Renewed', color: 'green' });
The handle is { element, header, content, getValue, setValue, setTitle, setBadge, setSubtitle, setAside, destroy }, where getValue is whether it is open.
A header with slots, as a fixed panel:
var route = document.createElement('span');
route.textContent = 'Main Server Movies to Movies';
var mode = document.createElement('span');
mode.className = 'stolon-badge gray';
mode.textContent = 'Copy';
var card = createCollapsibleSection({
title: 'Movies',
icon: 'video_library',
subtitle: route,
aside: mode,
collapsible: false,
content: fieldsNode
});
card.setSubtitle('Main Server Shows to Shows');
The same panel written by hand. A fixed header is a div, and initCollapsibles leaves it alone:
<div class="stolon-collapsible-section stolon-fixed stolon-panel">
<div class="stolon-collapsible-header">
<span class="stolon-collapsible-tile" aria-hidden="true"><span class="material-icons">video_library</span></span>
<span class="stolon-collapsible-heading">
<span class="stolon-collapsible-title" title="Movies">Movies</span>
<span class="stolon-collapsible-subtitle">Main Server to This Server</span>
</span>
<span class="stolon-collapsible-aside"><span class="stolon-badge stolon-gray">Copy</span></span>
</div>
<div class="stolon-collapsible-content">Fields go here.</div>
</div>
A collapsible header with an aside written by hand. A button cannot hold a control, so the header button, the aside, and the chevron sit side by side in a stolon-collapsible-bar, and initCollapsibles makes a click on the bar outside the aside toggle it:
<div class="stolon-collapsible-section">
<div class="stolon-collapsible-bar">
<button type="button" class="stolon-collapsible-header" data-target="relayBody">
<span class="stolon-collapsible-heading"><span class="stolon-collapsible-title">Relay</span></span>
</button>
<span class="stolon-collapsible-aside"><button is="emby-button" type="button" class="raised stolon-button stolon-small"><span>Test</span></button></span>
<span class="stolon-collapsible-icon"></span>
</div>
<div id="relayBody" class="stolon-collapsible-content">Fields go here.</div>
</div>
Also on the bag as shared.initCollapsibles(). Classes: stolon-collapsible-section, stolon-collapsible-header, stolon-collapsible-title, stolon-collapsible-badge, stolon-collapsible-icon, stolon-collapsible-content, stolon-collapsible-actions, stolon-collapsible-tile, stolon-collapsible-heading, stolon-collapsible-subtitle, stolon-collapsible-aside, stolon-collapsible-bar, and the stolon-fixed, stolon-compact, and stolon-has-tile modifiers.
States and Modifiers¶
- Start collapsed by putting
collapsedon both the header and the content. initCollapsiblesbinds each header once, setsaria-controlsfromdata-target, and keepsaria-expandedin step.- The icon is replaced by the web client's
expand_moreMaterial icon, whatever glyph the markup holds. - A click opens or closes the content to match the header, so the two never drift apart.
stolon-collapsible-iconturns witharia-expandedon any control that holds it, so the Sortable Card List head shares the same chevron.- Enter and Space toggle it because the header is a button. The state is not stored.
- A built section calls
onToggle(open)when its header opens or closes it, andclassNameadds classes to the section. stolon-collapsible-actionsis a footer row inside a body, with a rule above it. Its buttons and anystolon-status-textsit centered on one line.flushdrops the rule and the space above it, for a row that follows its fields directly.- A header can show a badge or a status dot between its title and its chevron.
setCollapsibleBadge(header, { label, color, dot })adds or replaces it on any header, built or bound, andnullremoves it.coloris a status color name, a hex code, or a Badge state name, anddot: truedraws a Status Dot named bylabel. In markup, putstolon-collapsible-badgeon astolon-badgebefore the icon. icondraws a Material icon in a rounded tile before the title, the size of a large avatar, on the accent wash.iconColoris a status color name or a hex code.subtitleis a dimmed line under the title, as text that ends in an ellipsis with its tooltip, or a node such as a From and To route.setSubtitlereplaces it, andnullremoves it.asideis a node at the right of the header, such as a badge, a button, or a toggle, centered on the header's line.setAsidereplaces it, andnullempties it. A collapsible header with an aside sits in astolon-collapsible-barwith the chevron beside it, so a control in the aside never sits inside the header button, and a click on the aside never toggles the section.- With any slot the title sits in
stolon-collapsible-headingand ends in an ellipsis with its full text in its tooltip. A section with no slot keeps the plain markup above. - A section with an aside narrower than 480 pixels, by its own width, takes
stolon-compactand puts the aside on its own line under the title, so the title is never squeezed. The heading keeps its own width, so the aside drops to its own line rather than covering the title. Hand written markup can addstolon-compactitself. collapsible: falsedraws a fixed panel: the section takesstolon-fixedandstolon-panel, so it is framed on the panel surface, the header is adivwith no chevron, it is always open,setValuedoes nothing, andonTogglenever runs.stolon-panelon a collapsible section, throughclassName, frames it the same way.numberedon each section of a run numbers their titles 1, 2, 3 in order, for a form split into ordered steps. The number comes from a CSS counter, so markup holds no numbers, and a numbered title keeps the case it is written in. Sections that share a parent count as one run.
Gotchas¶
- Use
type="button", or the header submits a surrounding form. - The content is found inside the
viewyou pass, so a section in a body level dialog is not found by a page level call. - The title shows exactly as written, so a name such as
yt-dlpkeeps its case. Write a title in Title Case.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
initCollapsibles¶
function initCollapsibles(view: ParentNode): void
Binds every collapsible header in the view so a click toggles its target, once per header. The arrow becomes the web client expand_more icon, and the header gets aria-controls and aria-expanded.
setCollapsibleBadge¶
function setCollapsibleBadge(header: Element, badge: CollapsibleBadge | null | undefined): void
Shows a badge, or a status dot, in a collapsible header between its title and its chevron, replacing any it already shows. Null removes it.
createCollapsibleSection¶
function createCollapsibleSection(options?: CollapsibleSectionOptions): CollapsibleSectionHandle
Builds a collapsible section, the same markup initCollapsibles binds, with an optional badge in its header. The header also takes a Material icon in a rounded tile, a dimmed subtitle under the title, and an aside on the right, such as a badge, a button, or a toggle. With collapsible false it is a fixed panel, always open, whose header is a plain heading.
| Option | Type | What it does |
|---|---|---|
title |
string |
the header title [optional] |
badge |
CollapsibleBadge | null |
{ label, color, dot } shown in the header, as setCollapsibleBadge takes it [optional] |
content |
string | Node |
HTML or a node for the body [optional] |
collapsed |
boolean |
true starts it closed (default false) |
numbered |
boolean |
true numbers it within a run of numbered sections (default false) |
className |
string |
classes added to the section [optional] |
icon |
string |
a Material icon shown in a rounded tile before the title [optional] |
iconColor |
string |
the tile's status color name or hex code (default the accent) [optional] |
subtitle |
string | Node | null |
a dimmed line under the title, as text or a node such as a route [optional] |
aside |
Node | null |
a node at the right of the header, such as a badge, a button, or a toggle, that goes under the title when the section is narrow [optional] |
collapsible |
boolean |
false draws a fixed panel, framed, always open, with no chevron and a header that is not a button (default true) |
onToggle |
(open: boolean): void |
function(open) called when the header opens or closes it [optional] |
| Handle member | Type |
|---|---|
element |
HTMLDivElement |
header |
HTMLButtonElement |
content |
HTMLDivElement |
getValue |
(): boolean |
setValue |
(open: boolean): void |
setTitle |
(text: string): void |
setBadge |
(badge: CollapsibleBadge | null): void |
setSubtitle |
(subtitle: string | Node | null): void |
setAside |
(aside: Node | null): void |
destroy |
(): void |
| Type | What it is |
|---|---|
CollapsibleBadge |
A badge or status dot in a collapsible header. color is a status color name, a hex code, or a badge state name, and dot draws a dot named by label. |
| Class | What it looks like |
|---|---|
stolon-collapsible-section |
Flat collapsible section so its fields align with the rest of the form |
stolon-collapsible-header |
Clickable header row that toggles the section, flush with the form left edge |
stolon-collapsible-title |
Header title, shown as written, that takes the free width |
stolon-collapsible-badge |
A badge or status dot in the header, between the title and the chevron, that keeps its own size |
stolon-collapsible-icon |
The expand_more chevron, down while collapsed and turned up while open, as Jellyfin's expanders turn. Any control with aria-expanded that holds it turns it too, such as a card head |
stolon-collapsible-content |
Section body, flush left, hidden when collapsed |
stolon-collapsible-actions |
Footer action row inside a section, with a divider above, its buttons and status text centered on one line |
stolon-collapsible-actions.stolon-flush |
A footer action row with no divider or space above it, for a row that follows its fields directly |
stolon-collapsible-section.stolon-numbered |
A section in a numbered run, whose title starts with a round number counted from the sections before it |
stolon-collapsible-tile |
A Material icon in a rounded tile before the title, the size of an avatar, in the accent or a status color or hex code |
stolon-collapsible-heading |
The title and its subtitle stacked in the free width, the title ending in an ellipsis with the full text in its tooltip |
stolon-collapsible-subtitle |
A dimmed line under the title, as text that ends in an ellipsis or a node such as a route |
stolon-collapsible-aside |
A node at the right of the header, such as a badge, a button, or a toggle, centered on the header's line |
stolon-collapsible-bar |
The row of a collapsible header that has an aside: the header button, the aside beside it, and the chevron, since a button cannot hold a control |
stolon-collapsible-section.stolon-compact |
A narrow section, chosen by its own width, puts its aside on a line of its own under the title |
stolon-collapsible-section.stolon-fixed |
A fixed panel, always open, whose header is a plain heading with no chevron |
stolon-collapsible-section.stolon-panel |
A section on the panel surface, its header and body inset by the panel's padding, collapsible or fixed |
Example¶
Example Code¶
<!-- title: Collapsible Section -->
<div class="stolon-collapsible-section">
<button type="button" class="stolon-collapsible-header" aria-expanded="true" data-target="generalBody">
<span class="stolon-collapsible-title">General</span>
<span class="stolon-collapsible-icon"></span>
</button>
<div id="generalBody" class="stolon-collapsible-content">
<div class="inputContainer">
<label for="txtName">Name</label>
<input is="emby-input" type="text" id="txtName" value="Living room" />
</div>
<div class="stolon-collapsible-actions">
<button is="emby-button" type="button" class="raised stolon-button stolon-primary stolon-small"><span>Save</span></button>
</div>
</div>
</div>
<div class="stolon-collapsible-section">
<button type="button" class="stolon-collapsible-header stolon-collapsed" aria-expanded="false" data-target="advancedBody">
<span class="stolon-collapsible-title">Advanced</span>
<span class="stolon-collapsible-icon"></span>
</button>
<div id="advancedBody" class="stolon-collapsible-content stolon-collapsed">
<p>Hidden until the header is pressed.</p>
</div>
</div>
<p class="fieldDescription">Numbered sections, for a form in ordered steps.</p>
<div class="stolon-collapsible-section stolon-numbered">
<button type="button" class="stolon-collapsible-header" aria-expanded="true" data-target="stepConnection">
<span class="stolon-collapsible-title">Connection</span>
<span class="stolon-collapsible-icon"></span>
</button>
<div id="stepConnection" class="stolon-collapsible-content"><p>Fields for the first step.</p></div>
</div>
<div class="stolon-collapsible-section stolon-numbered">
<button type="button" class="stolon-collapsible-header stolon-collapsed" aria-expanded="false" data-target="stepLibraries">
<span class="stolon-collapsible-title">Libraries</span>
<span class="stolon-collapsible-icon"></span>
</button>
<div id="stepLibraries" class="stolon-collapsible-content stolon-collapsed"><p>Fields for the second step.</p></div>
</div>
<p class="fieldDescription">A badge or a status dot in the header.</p>
<div id="badged"></div>
<p class="fieldDescription">A fixed panel with an icon, a route under its title, and a badge on the right.</p>
<div id="panels"></div>
<p class="fieldDescription">A collapsible header with a button on the right, beside its chevron.</p>
<div id="asided"></div>
<script type="module">
const base = await window.__baseReady;
const shared = base.createShared(document.getElementById('view'), 'preview-plugin', 'Preview');
shared.initCollapsibles();
const badged = document.getElementById('badged');
badged.appendChild(base.createCollapsibleSection({ title: 'Certificate', badge: { label: 'Expires Soon', color: 'orange' }, content: '<p>The certificate expires in 9 days.</p>', collapsed: true }).element);
badged.appendChild(base.createCollapsibleSection({ title: 'Relay', badge: { label: 'Connected', color: 'green', dot: true }, content: '<p>The relay is connected.</p>', collapsed: true }).element);
function route(from, to) {
const line = document.createElement('span');
line.className = 'stolon-icon-text stolon-small';
line.appendChild(document.createTextNode(from));
const arrow = document.createElement('span');
arrow.className = 'material-icons';
arrow.setAttribute('aria-hidden', 'true');
arrow.textContent = 'arrow_forward';
line.appendChild(arrow);
line.appendChild(document.createTextNode(to));
return line;
}
function badge(label, color) {
const el = document.createElement('span');
el.className = 'stolon-badge stolon-' + color;
el.textContent = label;
return el;
}
const panels = document.getElementById('panels');
panels.appendChild(base.createCollapsibleSection({ title: 'Movies', icon: 'video_library', subtitle: route('Main Server Movies', 'Movies'), aside: badge('Copy', 'gray'), collapsible: false, content: '<p>Storage settings for this library.</p>' }).element);
panels.appendChild(base.createCollapsibleSection({ title: 'A Very Long Library Name That Ends in an Ellipsis', icon: 'music_note', iconColor: 'purple', subtitle: 'Main Server to This Server', aside: badge('Link', 'blue'), collapsible: false, content: '<p>The title ends in an ellipsis, and at phone width the badge goes under it.</p>' }).element);
const test = document.createElement('button');
test.setAttribute('is', 'emby-button');
test.type = 'button';
test.className = 'raised stolon-button stolon-small';
test.textContent = 'Test';
test.addEventListener('click', function () { test.textContent = 'Tested'; });
document.getElementById('asided').appendChild(base.createCollapsibleSection({ title: 'Relay', icon: 'hub', subtitle: 'relay.example.com', aside: test, collapsed: true, content: '<p>The relay settings.</p>' }).element);
</script>