Slab
Slab

What's new

1.1.0

Upcoming

Validation errors from markup, more of the native form, field and popup APIs, and Ember types.

1.1.0 is the first release since 1.0, and its headline is validation errors: every form control can now show one from markup, in your own words, with no script. It fills in more of the platform too. Form controls have the defaults and labels a native input has, and popups and fields gain the methods their native elements have. It's a minor release, so nothing needs changing.

1.1.0 isn't on npm yet. This is what's in it, from main, ahead of the release.

TL;DR The short version

  1. custom-validity and a validity-message slot show a validation error, in your own words, from markup.
  2. Every form control has defaultValue or defaultChecked, and labels, and an outside <label> works as it does for a native one.
  3. toggle(force), showPicker(), stepUp() and setRangeText() are on the components whose native elements have them.
  4. Ember templates are type-checked with Glint, and every component page shows its usage in Ember.
  5. Nothing to change: every addition adds to what you have.

Validation errors, your way

1.0 gave every form control the native validation API: required, pattern, setCustomValidity(), reportValidity() and validationMessage, with the browser's own wording. All of that still works. But rewording an error, or showing one from your server, took script. 1.1 makes it markup: a validity-message slot for your own words, and a custom-validity attribute for an error you already know about. They work on every form control, in every framework, and the message is still the control's accessible description.

1.0: script to reword a required field
const field = document.querySelector('slab-text-field');

const check = () => {
  field.setCustomValidity('');

  if (field.validity.valueMissing) {
    field.setCustomValidity(
      'Give the job a name so the crew can find it.',
    );
  }
};

check();
field.addEventListener('input', check);
1.1: markup
<slab-text-field label="Job name" required>
  <span slot="validity-message">
    Give the job a name so the crew can find it.
  </span>
</slab-text-field>
  • Give the job a name so the crew can find it.

    Your own words

    The snippet above, live. Tab into the field and away again, and your message shows in place of the browser's.

    index.html
    <slab-text-field label="Job name" required>
      <span slot="validity-message">
        Give the job a name so the crew can find it.
      </span>
    </slab-text-field>
  • Errors from your server

    custom-validity is setCustomValidity() as an attribute, so a page rendered on a server can say what's wrong. It shows at once, with no script. Set it to an empty string to clear it.

    index.html
    <slab-text-field
      label="Email"
      type="email"
      value="sam@example.com"
      custom-validity="That email already has an account."
    ></slab-text-field>
  • Read the safety notes before you book. North South Pick a crew for this job.

    Every control

    Checkbox, Select and the rest of the form controls take the slot too, so a whole form's errors read the same. Tab through both and away.

    index.html
    <slab-checkbox
      label="I've read the safety notes"
      required
    >
      <span slot="validity-message">
        Read the safety notes before you book.
      </span>
    </slab-checkbox>
    
    <slab-select
      label="Crew"
      placeholder="Pick a crew"
      required
    >
      <slab-option value="north">North</slab-option>
      <slab-option value="south">South</slab-option>
      <span slot="validity-message">
        Pick a crew for this job.
      </span>
    </slab-select>
  • A message for each problem

    The slot is markup, so it can hold a link or your page's own language, and you can change it. Pick the words on "invalid", which fires just before the message shows, and validity says what's wrong.

    index.js
    const field = document.querySelector('slab-text-field');
    const message = field.querySelector('[slot="validity-message"]');
    
    field.addEventListener('invalid', () => {
      message.textContent = field.validity.valueMissing
        ? 'Enter an email so we can send the schedule.'
        : "That doesn't look like an email. Check for typos?";
    });

Read the validation guide, in every framework

New features

  • Errors from markup

    custom-validity is setCustomValidity() as an attribute. The control is invalid with that message, shown right away, for errors you already know about, such as ones from a server.

  • Your own words

    Content in the validity-message slot shows in place of the message while the control is invalid, for wording of your own, a translation or a link.

  • Default values

    Form controls have defaultValue, and Checkbox, Switch and Radio have defaultChecked, as native inputs do: the value a form resets to.

  • Labels

    Every form control has a read-only labels, the <label> elements associated with it, from ElementInternals.

  • toggle(force)

    Menu, Context Menu, Select, Combobox, Tooltip and Hover Card have toggle(force?), as Popover does, like a native togglePopover().

  • showPicker()

    Select and Combobox open from showPicker(), as Date Picker does, and throw as a native select does outside a user gesture.

  • Stepping a slider

    Slider and Range Slider have stepUp() and stepDown(), as a native range input does.

  • Selecting text

    Text Field, Textarea, Search Field and Password Field have selectionStart, selectionEnd, selectionDirection and setRangeText(), as a native input does.

  • Ember

    Import slab-elements/ember.js once, and Glint checks each tag's attributes by their HTML names. Every component page shows its usage in Ember too.

Fixes

A line each, and the components it's in.

Labels

Validation

Upgrade

Install
pnpm add slab-elements@1.1.0

Every change, in full, will be in the release notes once it's out.