Skip to content

Scope chip

A list's scope (every site, or one) as a GET form with one select, the choice kept in the address.

<form method="get" action="#scope" class="cap-scope" data-cap="scope-chip" id="scope-all">
  <input type="hidden" name="status" value="draft" />
  <label class="cap-scope-label">
    <span class="cap-sr-only">Show posts from</span>
    <select class="cap-select" name="site"><option value="">All sites</option><option value="example-org">example.org</option><option value="example-net">example.net</option></select>
  </label>
  <button class="cap-btn cap-scope-submit" type="submit">Show</button>
</form>

Installation

// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/scope-chip.css";
import { enhance } from "capsomer/behavior/scope-chip";

enhance();

Usage

<form method="get" class="cap-scope" data-cap="scope-chip">
  <input type="hidden" name="status" value="draft" />   <!-- one per kept value -->
  <label class="cap-scope-label">
    <span class="cap-sr-only">Show posts from</span>
    <select class="cap-select" name="site">
      <option value="">All sites</option>
      <option value="example-org">example.org</option>
    </select>
  </label>
  <button class="cap-btn cap-scope-submit" type="submit">Show</button>
</form>

Examples

One scope required (no unscoped choice)

<form method="get" action="#scope" class="cap-scope" data-cap="scope-chip" id="scope-one">
  <label class="cap-scope-label">
    <span class="cap-sr-only">Show media from</span>
    <select class="cap-select" name="site"><option value="example-org" selected>example.org</option><option value="example-net">example.net</option></select>
  </label>
  <button class="cap-btn cap-scope-submit" type="submit">Show</button>
</form>

With no script: the Show button

<form method="get" action="#scope" class="cap-scope" id="scope-noscript">
  <label class="cap-scope-label">
    <span class="cap-sr-only">Show mentions from</span>
    <select class="cap-select" name="site"><option value="">All sites</option><option value="example-org">example.org</option><option value="example-net">example.net</option></select>
  </label>
  <button class="cap-btn cap-scope-submit" type="submit">Show</button>
</form>

API reference

ScopeChip

PropTypeRequiredDoes
optionsScopeOption[]YesThe scopes to choose from (each site, say).
valuestringYesThe value in force, or "" for the unscoped choice.
labelstringYesThe select's hidden label: what the list shows ("Show posts from").
allstring | nullThe unscoped choice's label ("All sites"); null when the page needs one scope.
namestringThe query parameter.
keepRecord<string, string | undefined>Other query values to carry (a status tab, a search) when the scope changes.
actionstringWhere the form goes; the current page when left out.
renderForm(props: ScopeFormProps) => ReactElementThe form element, for a router's own form (React Router's <Form>), so the change is a client navigation. It is given every prop the plain form would get.

Functions

FunctionSignatureDoes
enhance(root?: ParentNode): () => voidThe scope chip's behavior for a page with no React: choosing submits the form at once, and the Show button (there for a page with no script) is hidden. React's ScopeChip does the same itself.

Parts and data attributes

ClassIs
.cap-scopethe root
.cap-scope-labela part
.cap-scope-submita part
AttributeValues
data-cap-readypresent or absent

Accessibility

KeyDoes
TabMoves to the select (and to Show with no script)
Arrow keys, lettersChoose a scope (the browser's select); with script the list reloads on the change
  • The select has a name from its label, hidden with .cap-sr-only.
  • The Show button stays in the page until script runs, so a keyboard user with no script can submit.