diff --git a/.changeset/dom-tabindex.md b/.changeset/dom-tabindex.md new file mode 100644 index 0000000..64bc9c7 --- /dev/null +++ b/.changeset/dom-tabindex.md @@ -0,0 +1,5 @@ +--- +'@clack/ui': minor +--- + +Add `tabIndex` property on HTML elements, and introduce Button / Focusable mixin diff --git a/packages/ui/src/dom.ts b/packages/ui/src/dom.ts index cbd56aa..cf8db7d 100644 --- a/packages/ui/src/dom.ts +++ b/packages/ui/src/dom.ts @@ -8,6 +8,8 @@ import { HTMLCollection } from './html-collection.ts'; import { StyleDeclaration } from './style.ts'; import type { Window } from './window.ts'; import { serialize, serializeOuter } from './serialize.ts'; +import { createMixin, mixin, type Constructor } from './mixin.ts'; +import { parseTabIndex } from './parse-tab-index.ts'; import { addEventListener, removeEventListener, @@ -930,6 +932,7 @@ export class HTMLDocument extends Document { ['head', HTMLHeadElement], ['title', HTMLTitleElement], ['body', HTMLBodyElement], + ['button', HTMLButtonElement], ['span', HTMLSpanElement], ['div', HTMLDivElement], ]); @@ -972,8 +975,31 @@ export class HTMLElement extends Element { get dataset(): DOMStringMap { return this.#dataset; } + + get tabIndex(): number { + return parseTabIndex(this.getAttributeNS(null, 'tabindex')) ?? -1; + } + + set tabIndex(value: number) { + this.setAttributeNS(null, 'tabindex', String(value | 0)); + } } +export interface Focusable extends HTMLElement {} + +export const Focusable = createMixin>( + (Base) => + class extends Base { + override get tabIndex(): number { + return parseTabIndex(this.getAttributeNS(null, 'tabindex')) ?? 0; + } + + override set tabIndex(value: number) { + super.tabIndex = value; + } + }, +); + export class HTMLHtmlElement extends HTMLElement {} export class HTMLHeadElement extends HTMLElement {} @@ -986,6 +1012,8 @@ export class HTMLSpanElement extends HTMLElement {} export class HTMLDivElement extends HTMLElement {} +export class HTMLButtonElement extends mixin(HTMLElement, [Focusable]) {} + export class HTMLUnknownElement extends HTMLElement {} /** XML */ diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts index b4e8847..9fda78f 100644 --- a/packages/ui/src/index.ts +++ b/packages/ui/src/index.ts @@ -9,6 +9,7 @@ export { HTMLHtmlElement, HTMLHeadElement, HTMLBodyElement, + HTMLButtonElement, HTMLSpanElement, HTMLTitleElement, HTMLUnknownElement, diff --git a/packages/ui/src/mixin.ts b/packages/ui/src/mixin.ts new file mode 100644 index 0000000..8301d56 --- /dev/null +++ b/packages/ui/src/mixin.ts @@ -0,0 +1,51 @@ +/* oxlint-disable bombshell-dev/exported-function-async -- Mixin factories return classes synchronously. */ + +/** Define a class mixin that supports instanceof and type narrowing. */ +export function createMixin( + define: (Base: Base) => Constructor, +): Mixin { + const apply = ((Base: B) => { + const Mixed = define(Base) as B & Constructor; + mixins.set(Mixed.prototype, apply); + return Mixed; + }) as Mixin; + return Object.defineProperty(apply, Symbol.hasInstance, { + value: (value: unknown): value is Members => { + if (value === null || (typeof value !== 'object' && typeof value !== 'function')) { + return false; + } + for (let proto = Object.getPrototypeOf(value); proto; proto = Object.getPrototypeOf(proto)) { + if (mixins.get(proto) === apply) { + return true; + } + } + return false; + }, + }); +} + +/** Apply mixins in order, so later mixins override earlier ones. */ +export function mixin[]>( + Base: Base, + mixins: Mixins, +): Mixed { + let Mixed: Base = Base; + for (const apply of mixins) { + Mixed = apply(Mixed); + } + return Mixed as Mixed; +} + +export type Constructor = abstract new (...args: any[]) => T; + +export type Mixin = (( + Base: B, +) => B & Constructor) & { + [Symbol.hasInstance](value: unknown): value is Members; +}; + +type Mixed = Mixins extends [Mixin, ...infer Rest] + ? Mixed, Rest> + : Base; + +const mixins = new WeakMap>(); diff --git a/packages/ui/src/parse-tab-index.ts b/packages/ui/src/parse-tab-index.ts new file mode 100644 index 0000000..51ce7f6 --- /dev/null +++ b/packages/ui/src/parse-tab-index.ts @@ -0,0 +1,8 @@ +const INT32_LIMIT = 2147483648; + +// https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex +export function parseTabIndex(attribute: string | null): number | null { + const match = attribute?.match(/^[\t\n\f\r ]*([+-]?[0-9]+)/); + const value = match ? Number(match[1]) : NaN; + return value >= -INT32_LIMIT && value < INT32_LIMIT ? value || 0 : null; +} diff --git a/packages/ui/wpt.config.ts b/packages/ui/wpt.config.ts index eee44e6..e51a2f6 100644 --- a/packages/ui/wpt.config.ts +++ b/packages/ui/wpt.config.ts @@ -10,6 +10,7 @@ import { DocumentType, Element, HTMLBodyElement, + HTMLButtonElement, HTMLDivElement, HTMLElement, HTMLHeadElement, @@ -54,6 +55,7 @@ const config: WPTConfig = { Event: globalThis.Event, EventTarget: globalThis.EventTarget, HTMLBodyElement, + HTMLButtonElement, HTMLDivElement, HTMLCollection, HTMLElement, @@ -330,6 +332,9 @@ const config: WPTConfig = { 'dom/nodes/querySelector-id-nth-child.html': [ 'querySelectorAll matching #id + constant nth-child()', ], + 'html/dom/reflection-misc.html': [/\.tabIndex:/], + 'html/dom/reflection-forms.html': [/\.tabIndex:/], + 'html/interaction/focus/sequential-focus-navigation-and-the-tabindex-attribute/focus-tabindex-default-value.html': true, '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,