Sites in other frameworks
`@shadestack/a11y`
The website editor's accessibility checker. Framework-free, on @shadestack/tree and
@shadestack/styles only, and not a site package itself: the editor runs it as an editor works (in a Web Worker), to
show issues on their nodes, and the website module runs it on the server on every publish, as its thread task
accessibility in a worker thread with a 30-second budget, and publishes only when it gives no errors. A report sent by the
editor is never trusted.
import { canPublish, checkAccessibility } from '@shadestack/a11y'
const report = checkAccessibility({
page: { id: 'page-1', tree }, // the page's record id: its local styles are keyed by it
layout: { before: [{ id: 'header', tree: header }], after: [{ id: 'footer', tree: footer }], main: true },
components: {
card: { tree: cardTree, treeId: 'component-card', defaults: { cta: { en: 'Read more' } } },
map: { code: true } // a code component, from the frontend's registry
},
styles, // @shadestack/styles' StyleSheet
languages: { default: 'nl', others: ['en'] },
reservedIds: ['content'] // ids the frontend's layout uses (its skip link's target)
})
if (!canPublish(report)) refuse(report.errors) // errors block publishing
show(report.warnings) // warnings don't
- Issues (
AccessibilityIssue):rule(one ofRULES),severity,message(plain English with the fix; plain text, never HTML), and where:treeIdandnodeIdof the tree that holds the node,componentwhen that tree is a component's, andinstances, the component instances from the page down to it ([{ treeId, nodeId, component }], the first on the page). Contrast issues addmode,breakpointorcontainer,state,ratioandrequired; an issue in one language only,language; a duplicate,related. The same issue is listed once with itsoccurrences, in document order, at mostlimits.maxIssues(200) of each severity. - Where it looks: the rendered page. An instance is replaced by its component's tree, with the instance's slot
content where the slots are,
props.<name>bindings resolved from the instance (or the component'sdefaults; a property neither set nor defaulted renders nothing, and an attribute bound to it is left out), and the instance's classes on the component's top elements. Every If and Each branch is checked, and the rules know which content can be on the page together: only the two branches of one If (or a list's items and its empty branch) exclude each other. A library entry is a tree or a code component (code: true, from the frontend's registry): the checker can't see inside a code component and warns about each one; an entry with neither is refused. SHADE's widgets (shade:names) are known without the library: the checker trusts a widget's own markup, checks what its slots hold, and counts a labelledshade:iconas a name. An instance of a variant is refused: the checker does not know variants yet. - The renderers' contract: what the checker assumes a renderer renders (an instance's classes on its component's
top elements, slot content without them, the island's wrapper without classes, an attribute bound to an unset
property left out, and more) is pinned in
@shadestack/render/renderer-contract.json: page trees with their components and the HTML they render. Render's conformance suite holds every renderer to it (a case'srendererHtmlwhere a renderer's HTML rightly differs: its<shade-island>wrapper,alt=""for an image left without alt). - Errors and warnings: an error is a WCAG 2.1 AA failure the checker can see (
image-alt,link-name,link-empty,button-name,button-empty,form-label,aria-hidden-focus,control-keyboard,heading-empty,duplicate-id(also an id inreservedIds, which the layout already uses),id-reference,contrast,text-outline-thin,focus-removed) or input it can't read (input,too-large,component-missing,component-cycle,component-variant,internal, andtruncatedamong the errors). What WCAG doesn't require is a warning, however questionable:heading-one,heading-order,nav-label,main-one,nesting,summary-first,legend-first,caption-first,contrast-unresolved,text-masked,focus-style, and the heuristics and content it can't see into (heading-missing,main-missing,image-alt-text,contrast-unknown,embed-unchecked,code-component-unchecked,lang-fallbackand the rest).RULES[rule].severityis the one place a severity is set. - The rules and their severities are
RULESinrules.ts: headings (order, oneh1, not empty), alt texts (an empty alt marks an image decorative), names of links, buttons and form fields, anathat answers a click without anhref,aria-hiddenon focusable content, the tree's nesting rules again on the rendered page (a component may have changed since the page was saved), the summary, legend and caption first, unique ids and the ids references name (an element with the id that can show with the reference, not one only in another branch), landmarks (onemain, labelled navigations), contrast and focus styles, and the language of fallback text. - Contrast is computed from the styles as the browser would, through
@shadestack/styles' cascade, in every mode, at the base, every breakpoint and container query, under reduced motion and each feature test the styles have, on hover, when checked and on keyboard focus: 4.5:1, or 3:1 for large text. Not under forced colours, where the browser sets the colours; the focus checks count that condition too. The page's own colours are thecolor-textandcolor-backgroundvariables when the styles have them (the theme's), else black on white; passcolors(a hex colour or a colour variable) to say otherwise. A gradient counts at its least readable point (its stops placed as CSS places them; a stop with no width shows nothing; a radial gradient's last colour too, which fills the box beyond its ending shape; for a repeating gradient or stops at lengths, every stretch between two stops),currentcoloras a background is the text's colour, and text on a background image is checked against the colour below the image (an error when it fails) with a warning to check the image by hand. Anopacitybelow 1 fades the element as one layer, its background and text together, over what is behind it, and opacities around one another multiply (text faded out completely,opacity: 0, isn't checked); text under afilter,backdrop-filterormix-blend-modeis acontrast-unresolvedwarning, to check by hand. A colour it can't resolve is acontrast-unresolvedwarning (check that text by hand), and so is a stack of see-through gradients that would come to more than 256 colours, given up on before it is composited. Pagecolorsthat are colours but don't resolve in every mode are that warning too, on the page and on each text on them; anything else given as a colour is aninputerror. Input buttons and filled-in fields are checked too. Outlined text (text-stroke-widthandtext-stroke-color, inherited as CSS has them) whose colour is fully transparent is checked with its outline's colour instead, at the same 4.5:1 or 3:1; an outline incurrentcolorfollows the transparent fill and fails. Such text with an outline thinner than 2px is atext-outline-thinerror (WCAG 1.4.3: a hairline around nothing doesn't read, whatever its colour), unless it is decoration: hidden from screen readers (aria-hidden="true"on its element or one around it) and every word of it shown elsewhere on the page, in a text that isn't hidden and can show with it (not in the other branch of the same condition), in every language. Words are letters and digits, compared in lowercase, in any order; a text bound to data the checker can't read counts as words it can't find, so it stays an error. Masked text: text under amask-image(on its element or one around it) whose gradient has a fully transparent stop in some mode may be hidden, depending on where the mask falls, which the checker can't see: atext-maskedwarning, to check by hand. Text hidden from screen readers (aria-hidden="true"on it or around it) gets none, as decoration. Keyboard focus on one element is computed again only for that element (and what is inside it, when its focus changes what inherits), so deep trees of links and buttons stay fast. - Focus styles are compared with the element without focus, per mode and condition: only what shows counts (an
outline with a style, a width and a colour, a shadow with a colour, a colour that differs, a border with a style, an
underline that comes or goes, a gradient that shows a colour that wasn't behind); declarations are compared as
values, and
text-decoration-styleand-thicknessagainst their defaults (solid,auto). A hover style on an element inside a link asks the link for a focus style. A hover style without a focus style of its own is afocus-stylewarning (the browser's focus ring still shows); a style that takes the outline away with nothing else showing focus is afocus-removederror (WCAG 2.4.7). - It fails closed: a tree that isn't one, a tag or attribute off the tree's allow-lists, styles that don't
validate, page colours that aren't colours, a missing component, an entry that is neither a tree nor a code
component, one that contains itself, a variant, and a page past the limits (50,000 rendered nodes and four steps per
node, every component, slot and branch read counted; 16 nested components) are errors. It never throws for its
input: an exception becomes an
internalerror.
Code, in shade-frontend: packages/a11y/src/. Tests: packages/a11y/test/*.test.ts.