Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dom-label-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': minor
---

Add label control association and focus forwarding.
53 changes: 53 additions & 0 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,16 @@ export class Element extends Node {
return this.#attributes.toggleAttribute(name, force);
}

getRootNode(): Node {
// oxlint-disable-next-line typescript/no-this-alias -- This cursor walks the node's ancestry.
for (let node: Node | null = this; node; node = node.parentNode) {
if (!node.parentNode) {
return node;
}
}
return this;
}

get id(): string {
return this.#attributes.getNamedItemNS(null, 'id')?.value ?? '';
}
Expand Down Expand Up @@ -977,6 +987,7 @@ export class HTMLDocument extends Document {
['title', HTMLTitleElement],
['body', HTMLBodyElement],
['button', HTMLButtonElement],
['label', HTMLLabelElement],
['span', HTMLSpanElement],
['div', HTMLDivElement],
]);
Expand Down Expand Up @@ -1099,6 +1110,33 @@ export class HTMLDivElement extends HTMLElement {}

export class HTMLButtonElement extends mixin(HTMLElement, [Focusable]) {}

export class HTMLLabelElement extends HTMLElement {
get control(): HTMLElement | null {
const id = this.getAttributeNodeNS(null, 'for');
if (id) {
if (id.value === '') {
return null;
}

const root = this.getRootNode();
const nodes = root instanceof Element ? [root] : [...root.childNodes];
const target = findElement(nodes, (element) => element.id === id.value);

return target instanceof HTMLElement && isLabelable(target) ? target : null;
} else {
return findElement([this], isLabelable) as HTMLElement;
}
}

override focus(): void {
if (parseTabIndex(this.getAttributeNS(null, 'tabindex')) !== null) {
super.focus();
} else {
this.control?.focus();
}
}
}

export class HTMLUnknownElement extends HTMLElement {}

/** XML */
Expand Down Expand Up @@ -1163,6 +1201,21 @@ function transitionActiveElement(document: Document, to: Element | null): void {
}
}

function isLabelable(element: Element): element is HTMLElement {
if (element instanceof HTMLElement) {
if (
element.localName === 'input' &&
asciiLowercase(element.getAttributeNS(null, 'type') ?? '') === 'hidden'
) {
return false;
}
return ['button', 'input', 'meter', 'output', 'progress', 'select', 'textarea'].includes(
element.localName,
);
}
return false;
}

/** internal helpers **/

function getTextContent(node: Element | DocumentFragment): string {
Expand Down
29 changes: 29 additions & 0 deletions packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,35 @@ const config: WPTConfig<Document> = {
'html/editing/focus/focus-management/focusing-disconnected-noop.html': true,
// focusing-focused-noop.html requires the innerHTML setter for setup and cleanup.
'html/interaction/focus/focus-management/focus-events.html': true,
'inert/inert-label-focus.html': [
'Calling focus() on an inert label should still send focus to its target.',
// Forwarding to a control in an inert subtree requires inert focus handling (#119).
// The click case requires an input backend; its catch also swallows unsupported clicks.
],
'html/semantics/forms/the-label-element/labelable-elements.html': [
'Check if the output element is a labelable element',
'Check if the progress element is a labelable element',
'Check if the select element is a labelable element',
'Check if the textarea element is a labelable form-element',
'Check if the button element is a labelable element',
'Check if the hidden input element is not a labelable element.',
'Check if the input element in radio state is a labelable element',
'Check if the keygen element is not a labelable element',
'Check if the meter element is a labelable element',
'Check if the fieldset element is not a labelable element',
'Check if the label element is not a labelable element',
'Check if the object element is not a labelable element',
'Check if the img element is not a labelable element',
// The remaining cases cover the controls' labels collections.
],
'html/semantics/forms/the-label-element/forward-focus-to-associated-element.html': [
'focusing a label with for attribute should forward focus to the associated element',
'focusing a label without for attribute should fowrad focus to the first labelable child',
'focusing a label with tabindex should not forward focus to the labelable element',
'focusing a label with negative tabindex should not forward focus to the labelable element',
'focusing a label with empty tabindex should forward focus to the labelable element',
// The hidden-label case requires focusability to account for rendered visibility.
],
'dom/nodes/Document-createEvent.https.html': [
/^(?:Event|Events|HTMLEvents|SVGEvents) should be an alias for Event\.$/i,
/^createEvent\('(?:Event|Events|HTMLEvents|SVGEvents)'\) should be initialized correctly\.$/i,
Expand Down
Loading