Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

data-show-when

Show or hide a form section based on other fields' values. Hidden sections get hidden + inert and their controls are disabled, so hidden required fields never block submit and hidden values are not sent.

Overview

data-show-when shows an element only while a condition on other form fields is true; data-hide-when is the inverse. Use them for sections that only apply to some answers: business details for a business account, a shipping address when delivery is chosen.

A hidden section gets the hidden and inert attributes, and every enabled form control inside it is disabled and marked data-conditional-disabled. Disabling is what matters for forms: disabled controls are barred from constraint validation, so a hidden required field never blocks submit, and their values are not submitted. When the section is shown again, only the controls it disabled are re-enabled; controls you disabled yourself stay disabled.

Without JavaScript every section is visible and every control is enabled, so the whole form still works.

Usage

Name the controlling field and the value that reveals the section.

Put the conditional section directly after the field that controls it. Revealed content is not announced, so DOM order is what lets keyboard and screen reader users find it next.

Options

AttributeValuesDefaultEffect
data-show-whencondition (see below)—The element is shown while the condition is true, hidden otherwise.
data-hide-whencondition—The element is hidden while the condition is true. Ignored when data-show-when is also present.
data-conditional-initbooleanset by scriptMarks a wired section. Read-only; a clone that carries it is still wired.
data-conditional-disabledbooleanset by scriptOn each control the script disabled. Only these are re-enabled on show.

Condition syntax

The same language as data-wizard-if; = and : are interchangeable.

SyntaxTrue whenExample
nameThe field has a value: non-empty text, a selected option, a checked box.data-show-when="newsletter"
!nameThe field has no value.data-show-when="!newsletter"
name=value or name:valueOne of the field's values equals value.data-show-when="delivery=ship"
name!=value or name:!valueNone of the field's values equals value.data-show-when="plan!=free"
a && bBoth terms are true. Binds tighter than ||.data-show-when="role=admin && plan=pro"
a || bEither term is true.data-show-when="role=admin || role=editor"

What counts as a field value

  • Text-like inputs, textarea, single select: the current value; empty is "no value".
  • Radio group: the checked radio's value.
  • Checkbox: its value while checked (a checkbox without a value attribute is "on", so name=true never matches; use the bare name form).
  • Checkbox group (several boxes, one name): every checked value. topics=sports matches whenever the Sports box is checked, whatever its position.
  • select[multiple]: every selected value.
  • Form-associated components (combo-box, date-picker, star-rating and other components that submit a value): their value.

Scope

Inside a <form>, names resolve through form.elements, so controls attached with form="id" count and other forms on the page do not. A section outside any form looks the name up in the whole document.

Limits

  • Comparisons are exact string matches; there are no numeric or pattern operators.
  • Conditions re-run on input and change. If script sets a control's .value, dispatch an input event afterwards (el.dispatchEvent(new Event('input', { bubbles: true }))).
  • Changing the data-show-when attribute itself does not re-evaluate the section until the next field change.

Behaviour

  • When it runs: every section is evaluated on load and when it is inserted later (fetched fragments, cloned templates, forms added after load). One capture-phase listener on the document handles every form and every section outside a form.
  • Reset: after form.reset() or a reset button, sections re-evaluate against the restored values. A reset cancelled with preventDefault() changes nothing.
  • Nesting: a control inside two hidden sections is re-enabled only when neither is hidden. Sections inside a hidden data-wizard-if step follow the same rule.

Events

None. The attribute listens to the form's own input, change and reset events.

Styling

The shipped rule (in form-enhancements.css) makes sure an author display value cannot override hidden:

There are no custom properties or parts. Because the section switches between display: none and its normal display, a plain opacity transition never runs. To animate the reveal, use @starting-style:

Accessibility

  • Hidden sections are removed from the accessibility tree and the tab order (hidden + inert), so nobody lands in an invisible field.
  • Their controls are disabled, so validation never points at a field the user cannot see.
  • Revealing a section is not announced and does not move focus. Place the section right after its trigger so it is the next thing users reach.
  • The trigger gets no aria-expanded / aria-controls. If the reveal is a user-driven disclosure rather than a consequence of an answer, use <details> or data-toggle instead.
  • Without JavaScript all sections are visible, so no content or field is lost.

Examples

Hide when (inverse)

The field is visible by default and hides when the box is checked.

Radio group

The address is required only while "Ship" is chosen; with "Pickup" the hidden field is disabled and the form submits.

Truthy check

Leave out the value to show the section whenever the field has one: a checked box, a non-empty input, a selected option.

Checkbox group and compound conditions

A term matches when any checked box in the group has the value; && needs both.

Related

Use thisWhen
data-show-whenA section applies only for some answers to other fields, and its fields must not block or join the submission while hidden.
<details>The user chooses to open extra content (help, advanced options). Native, announced as expanded/collapsed.
data-toggleA button shows or hides a region, with aria-expanded on the button.
hiddenContent that is hidden regardless of form state. Note that hidden alone does not stop a required field from blocking submit.
CSS :has(:checked)A purely visual reveal with no fields inside (fields would still validate and submit).
data-wizard-ifWhole wizard steps that apply only for some answers. Same condition language, same disabling.
  • <form> and <fieldset> — the usual containers for conditional sections.
  • <form-field> — wrap a single conditional field with its label and messages.
  • data-autosave — a restored draft dispatches input, so sections follow the restored answers.
↑