Skip to content
Merged
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-tabindex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': minor
---

Add `tabIndex` property on HTML elements, and introduce Button / Focusable mixin
28 changes: 28 additions & 0 deletions packages/ui/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -930,6 +932,7 @@ export class HTMLDocument extends Document {
['head', HTMLHeadElement],
['title', HTMLTitleElement],
['body', HTMLBodyElement],
['button', HTMLButtonElement],
['span', HTMLSpanElement],
['div', HTMLDivElement],
]);
Expand Down Expand Up @@ -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<Focusable, Constructor<HTMLElement>>(
(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 {}
Expand All @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
HTMLHtmlElement,
HTMLHeadElement,
HTMLBodyElement,
HTMLButtonElement,
HTMLSpanElement,
HTMLTitleElement,
HTMLUnknownElement,
Expand Down
51 changes: 51 additions & 0 deletions packages/ui/src/mixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* oxlint-disable bombshell-dev/exported-function-async -- Mixin factories return classes synchronously. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, we can probably move everything standard-shaped under a src/dom/* directory and turn this rule off in the config.


/** Define a class mixin that supports instanceof and type narrowing. */
export function createMixin<Members, Base extends Constructor = Constructor>(
define: (Base: Base) => Constructor<Members>,
): Mixin<Members, Base> {
const apply = (<B extends Base>(Base: B) => {
const Mixed = define(Base) as B & Constructor<Members>;
mixins.set(Mixed.prototype, apply);
return Mixed;
}) as Mixin<Members, Base>;
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 extends Constructor, const Mixins extends Mixin<unknown, Base>[]>(
Base: Base,
mixins: Mixins,
): Mixed<Base, Mixins> {
let Mixed: Base = Base;
for (const apply of mixins) {
Mixed = apply(Mixed);
}
return Mixed as Mixed<Base, Mixins>;
}

export type Constructor<T = object> = abstract new (...args: any[]) => T;

export type Mixin<Members, Base extends Constructor = Constructor> = (<B extends Base>(
Base: B,
) => B & Constructor<Members>) & {
[Symbol.hasInstance](value: unknown): value is Members;
};

type Mixed<Base, Mixins> = Mixins extends [Mixin<infer Members, any>, ...infer Rest]
? Mixed<Base & Constructor<Members>, Rest>
: Base;

const mixins = new WeakMap<object, Mixin<unknown, Constructor>>();
8 changes: 8 additions & 0 deletions packages/ui/src/parse-tab-index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 5 additions & 0 deletions packages/ui/wpt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DocumentType,
Element,
HTMLBodyElement,
HTMLButtonElement,
HTMLDivElement,
HTMLElement,
HTMLHeadElement,
Expand Down Expand Up @@ -54,6 +55,7 @@ const config: WPTConfig<Document> = {
Event: globalThis.Event,
EventTarget: globalThis.EventTarget,
HTMLBodyElement,
HTMLButtonElement,
HTMLDivElement,
HTMLCollection,
HTMLElement,
Expand Down Expand Up @@ -330,6 +332,9 @@ const config: WPTConfig<Document> = {
'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,
Expand Down
Loading