What's new
1.1.0
UpcomingValidation 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.
- 9 new features
- 3 fixes
- 10 components fixed
TL;DR The short version
custom-validityand avalidity-messageslot show a validation error, in your own words, from markup.- Every form control has
defaultValueordefaultChecked, andlabels, and an outside<label>works as it does for a native one. toggle(force),showPicker(),stepUp()andsetRangeText()are on the components whose native elements have them.- Ember templates are type-checked with Glint, and every component page shows its usage in Ember.
- 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.
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);
<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-validityissetCustomValidity()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
validitysays 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?"; });
New features
-
Errors from markup
custom-validityissetCustomValidity()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-messageslot 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 havedefaultChecked, 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, fromElementInternals. -
toggle(force)
Menu, Context Menu, Select, Combobox, Tooltip and Hover Card have
toggle(force?), as Popover does, like a nativetogglePopover(). -
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()andstepDown(), as a native range input does. -
Selecting text
Text Field, Textarea, Search Field and Password Field have
selectionStart,selectionEnd,selectionDirectionandsetRangeText(), as a native input does. -
Ember
Import
slab-elements/ember.jsonce, 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
-
Clicking an outside
<label>toggles a Checkbox or Switch, opens File Drop, and focuses every other control, as it does for a native one.
Validation
-
Cancelling "invalid" keeps the validity message unshown, on a blur, a submit and
reportValidity(), as on a native control. -
A
setCustomValidity()message on a control that isn't required stays until it's cleared.
Upgrade
pnpm add slab-elements@1.1.0
Every change, in full, will be in the release notes once it's out.