Fallthrough
A parent often needs to reach the element inside a component it doesn't own: give a button an id that a label points at, describe it with aria-describedby, tag it for a test, listen to its focus or give it some margin. So what a parent passes a component, and the component doesn't declare, falls through to the component's root element. It's Vue's rule, and every target follows it.
This page walks you through it. We'll look at:
- the attributes that fall through, and the ones that don't
- the DOM listeners that fall through
classandstyle, which merge with the root's own- a component whose root is another component
- a ref on a component that exposes nothing
...rest, which puts all of it on another element- turning fallthrough off, and the components that can't take it
What falls through
Let's start with the rule. A component declares its props, its models, its slots and the events it lists in defineEmits. Everything else a parent passes it falls through:
- Attributes, like
id,aria-*,data-*ortype, land on its root element. - Listeners of DOM events, like
onClickoronFocus, listen on its root element. classandstylemerge with the root element's own.
The root element is the one element the component renders at its top. A root that is a component passes all of it on to that component, which places it by the same rule.
A declared prop never falls through, so a prop named like an attribute stays a prop. Where the root element sets an attribute the parent passes too, the parent's value wins. key is no attribute, and a ref holds the root element (Refs).
Pass a prop by its own name. aria-label on a component that declares ariaLabel is UF3035, with a fix to ariaLabel, since Vue would give it to the prop and the other targets would put it on the root element. The same goes for a model's name.
Attributes
Here a parent passes a button an id, two ARIA attributes, a test id and a type. The button declares only title:
// Declares one prop, `title`, which it renders as text: a declared prop never falls through.
export default function SaveButton({ title }: { title: string }) {
return (
<button type="button" class="button">
{title}
</button>
);
}
import { ref } from "@uxfront/unframework";
import SaveButton from "./SaveButton.uf.tsx";
export default function Editor() {
const pressed = ref(false);
return (
<div class="editor">
<p id="save-hint">Saves the draft.</p>
<SaveButton
title="Save"
id="save"
aria-describedby="save-hint"
aria-pressed={pressed.value}
data-testid="save-button"
type="submit"
class="primary"
/>
<button type="button" onClick={() => (pressed.value = !pressed.value)}>
Toggle
</button>
</div>
);
}
title is a prop SaveButton declares and renders as text, so it never reaches the <button> as an attribute. Everything else lands on the <button>: the id, the ARIA attributes, the data-testid and the type, where the parent's submit wins over the button's own button. The bound aria-pressed follows pressed as it changes. The corpus records the rendered DOM this way, with each text node in quotes:
<div class="editor">
<p id="save-hint">
"Saves the draft."
</p>
<button aria-describedby="save-hint" aria-pressed="false" class="button primary" data-testid="save-button" id="save" type="submit">
"Save"
</button>
<button type="button">
"Toggle"
</button>
</div>
The parent checks each attribute as one of that element's own, so the element's rules hold: HTML's names, the values an enumerated attribute allows and ARIA's (Attributes). A name that is no prop of the child and no attribute of its root element is UF3035, with a fix to the closest prop or attribute, like type for tpye.
import { cx } from "./_unframework/cx";
import type { ComponentPropsWithRef } from "react";
export type SaveButtonAttributes = Omit<ComponentPropsWithRef<"button">, "title" | "children">;
export default function SaveButton({
title,
className,
style,
...attributes
}: { title: string } & SaveButtonAttributes) {
return (
<button type="button" className={cx("button", className)} style={style} {...attributes}>
{title}
</button>
);
}
import SaveButton from "./SaveButton";
import { cx } from "./_unframework/cx";
import { type ComponentPropsWithRef, useRef, useState } from "react";
export type EditorAttributes = Omit<ComponentPropsWithRef<"div">, "children">;
export default function Editor({ className, style, ...attributes }: EditorAttributes) {
const [pressed, setPressed] = useState(false);
const pressedRef = useRef(pressed);
return (
<div className={cx("editor", className)} style={style} {...attributes}>
<p id="save-hint">Saves the draft.</p>
<SaveButton
title="Save"
className="primary"
id="save"
aria-describedby="save-hint"
aria-pressed={pressed}
data-testid="save-button"
type="submit"
/>
<button
type="button"
onClick={() => {
pressedRef.current = !pressedRef.current;
setPressed(pressedRef.current);
}}
>
Toggle
</button>
</div>
);
}
- React types what falls through as the root element's own props without the declared ones,
SaveButtonAttributes, and the root spreads the props' rest,{...attributes}, after its own attributes, so the parent's win. A parent writes them in React's spelling for that element, likeclassNameortabIndex. - Vue passes them on by itself, as its own
$attrs. - Svelte types the props with the root element's attributes from
svelte/elements, without the component's own names, and the root spreads the props' rest,{...attributes}, last. - Angular has no attribute spread, and a host element that carries an attribute isn't unwrapped (The Angular host). So each component declares two more inputs:
ufAttributes, a record of attributes by name, andufListeners, a record of listeners by event. The parent binds them on the host,[ufAttributes]="{ id: 'save', … }", which keeps no attribute. The root binds a shared attribute directive,Fallthrough([ufFallthrough]), from_unframework/Fallthrough.ts, which sets and removes the attributes throughRenderer2, on the server and in the browser, the parent's winning (fallthrough, emulated). Where the root binds an attribute of its own that the parent passes too, like[attr.aria-label], it binds the parent's value whenever the record holds that name, so the parent's still wins after the root's own value changes. A hand-written Angular parent that writesid="save"on<uf-save-button>puts it on the host, so it binds[ufAttributes]="{ id: 'save' }"instead. - Solid takes the declared props out with
splitProps, and the root spreads the others after its own attributes, so the parent's win. - Qwik types the props with the root element's own,
Omit<PropsOf<"button">, …>, and the root spreads the props' rest last. - Astro spreads the rest of
Astro.propslast on the root. Astro writes every attribute it is given and the browser keeps the first of two, so the root tests whether the parent passed each of its own attributes,"type" in Astro.props ? type : "button". The parent's value wins whenever it passes the key, even asundefined, which removes the attribute, as Vue does. A bare boolean attribute Astro doesn't know, likemultiple, passes on as"", so the element writes it bare.
So every output but Vue's types and spreads what falls through on each component with one root element, and you'll see it on every page of these docs, even where no parent passes anything. The rest the root spreads also carries a styled parent's data-uf-* scope attribute, so the parent's stylesheet reaches the root (Styles).
Listeners
Next, let's look at listeners. Here a parent listens to a chip's click, focus and keydown, and to pick, the one event the chip declares:
import { defineEmits } from "@uxfront/unframework";
// Declares one event, `pick`, which its own click emits: a consumer's `onClick` is a native
// listener beside it.
export default function Chip({ label }: { label: string }) {
const emit = defineEmits<{ pick: [label: string] }>();
return (
<button type="button" class="chip" onClick={() => emit("pick", label)}>
{label}
</button>
);
}
import { ref } from "@uxfront/unframework";
import Chip from "./Chip.uf.tsx";
export default function Picker() {
const log = ref<string[]>([]);
function record(line: string) {
log.value = [...log.value, line];
}
return (
<section aria-label="Picker">
<Chip
label="Ada"
onClick={(event) => record(`click on ${(event.currentTarget as Element).localName}`)}
onPick={(label) => record(`pick ${label}`)}
onFocus={(event) => record(`focus on ${(event.currentTarget as Element).localName}`)}
onKeydown={(event) =>
record(`keydown ${event.key} on ${(event.currentTarget as Element).localName}`)
}
/>
<ol aria-label="Log">
{log.value.map((entry, index) => (
<li key={index}>{entry}</li>
))}
</ol>
</section>
);
}
onClick, onFocus and onKeydown aren't events Chip declares, so they're native listeners on its <button>, typed as that element's own would be: the onKeydown handler receives a KeyboardEvent. Where the root listens to the same event itself, both run, the root's own first, as Vue merges them. Each of the parent's listeners logs its event's currentTarget, the element it listens on. So tab to the chip, press a and click it, and the log reads "focus on button", "keydown a on button", "pick Ada" and "click on button": the listeners are on the <button> itself, not on an element around it.
An onX that is neither an event the child declares nor a DOM event is UF3036, with a fix to the closest event, one the child declares or a DOM event, like onClick for onClik. A listener option on a listener that falls through, like onClickOnce, is UF1002.
import { cx } from "./_unframework/cx";
import type { ComponentPropsWithRef } from "react";
export interface ChipEvents {
onPick?: (label: string) => void;
}
export type ChipAttributes = Omit<ComponentPropsWithRef<"button">, "label" | "onPick" | "children">;
export default function Chip({
label,
onPick,
className,
style,
onClick,
...attributes
}: { label: string } & ChipEvents & ChipAttributes) {
return (
<button
type="button"
className={cx("chip", className)}
onClick={(event) => {
onPick?.(label);
onClick?.(event);
}}
style={style}
{...attributes}
>
{label}
</button>
);
}
- React takes the parent's
onClickout of the props' rest and calls it from the root's own handler, after it emitspick. That handler takes the event type React gives the prop, so the parent's handler gets the same event. React'sonFocusalso hears focus that moves inside an element, so the parent's React output checksevent.targetto keep the DOM'sfocus. A listener whose React prop wouldn't keep the DOM's meaning on that element is UF1002 on React:changeon a text field,focusin,focusout,selectandbeforeinput. - Vue merges the parent's listener with the root's own by itself.
- Svelte takes
onclickout of the props' rest and calls it from the root's own handler, after its own code. - Angular's parent binds
[ufListeners]="{ click: …, focus: …, keydown: … }"on the host, andFallthroughadds each listener withRenderer2.listenin the browser, after the element's own. - Solid takes
onClickout withsplitPropsand calls it from the root's own handler, after its own code. - Qwik passes the root an array of both,
onClick$={[$(() => onPick$?.(label)), onClick$]}, which runs the root's own first, and the parent'sevent.currentTargetreads Qwik's second parameter, the element. Where the root listens in the capture phase or passively, the parent's listener stays out of that group, so it runs in the bubble phase and itspreventDefault()works. ApreventDefault()at the top of the parent's listener runs in async$handler, not as apreventdefault:attribute, since a root that listens from the window would apply that attribute to the event anywhere on the page. - Astro renders on the server only, so no listener runs: the output drops it, and the compiler reports it for information (
interactivity).
Class and style
Now let's look at class and style. A component with one root element takes them like any attribute it doesn't declare, and the parent passes them as it would to an element, static or bound:
export default function Tag({ label }: { label: string }) {
return (
<span class="tag" style="padding: 2px 6px; color: rgb(30, 30, 30)">
{label}
</span>
);
}
import { ref } from "@uxfront/unframework";
import Tag from "./Tag.uf.tsx";
export default function Tags() {
const active = ref(false);
return (
<div class="tags">
<Tag label="plain" />
<Tag label="wide" class="wide" style="margin-left: 8px" />
<Tag
label="toggled"
class={["picked", { active: active.value }]}
style={{ color: active.value ? "rgb(200, 0, 0)" : undefined }}
/>
<button type="button" onClick={() => (active.value = !active.value)}>
Toggle
</button>
</div>
);
}
Unlike the other attributes, they merge with the root's own rather than replace them. The root's class is the union of both sets of names: tag wide, or tag picked active. For a style property both set, the parent's declaration wins, and a property the parent sets to null or undefined is removed from the root, as an object merge does. So while active is false, the toggled tag has no color at all.
import { cx } from "./_unframework/cx";
import type { ComponentPropsWithRef } from "react";
export type TagAttributes = Omit<ComponentPropsWithRef<"span">, "label" | "children">;
export default function Tag({
label,
className,
style,
...attributes
}: { label: string } & TagAttributes) {
return (
<span
className={cx("tag", className)}
style={{ padding: "2px 6px", color: "rgb(30, 30, 30)", ...style }}
{...attributes}
>
{label}
</span>
);
}
import Tag from "./Tag";
import { cx } from "./_unframework/cx";
import { type ComponentPropsWithRef, useRef, useState } from "react";
export type TagsAttributes = Omit<ComponentPropsWithRef<"div">, "children">;
export default function Tags({ className, style, ...attributes }: TagsAttributes) {
const [active, setActive] = useState(false);
const activeRef = useRef(active);
return (
<div className={cx("tags", className)} style={style} {...attributes}>
<Tag label="plain" />
<Tag label="wide" className="wide" style={{ marginLeft: "8px" }} />
<Tag
label="toggled"
className={cx("picked", { active })}
style={{ color: active ? "rgb(200, 0, 0)" : undefined }}
/>
<button
type="button"
onClick={() => {
activeRef.current = !activeRef.current;
setActive(activeRef.current);
}}
>
Toggle
</button>
</div>
);
}
- React, Solid, Qwik and Astro merge the class and the style into the root: the class through
cxon React, a template string on Solid, an array on Qwik andclass:liston Astro, the style by spreading the parent's object over the root's own. - Vue passes both on by itself.
- Svelte declares
classandstyleprops too. A Sveltestyleis a string, which can't unset a property, so where the root has a style of its own a shared helper,mergeStyle, merges the parent's declarations over it and drops the root's declaration of a property the parent left empty (fallthrough, emulated). A root with no style of its own takes{style}as it is. - Angular declares
classandstyleinputs, merges them into the root, and clears them from its host element, which keeps onlydisplay: contents. A parent binds them,[class]="'wide'", so they reach the inputs.
Through a component root
A component whose root is another component passes everything on to that component's root. Here SaveAction renders Pressable, whose root is a <button>, and a toolbar passes SaveAction an id, an aria-label, a listener and a ref:
// The root element: a button with a class of its own.
export default function Pressable({ text }: { text: string }) {
return (
<button type="button" class="pressable">
{text}
</button>
);
}
import Pressable from "./Pressable.uf.tsx";
// Its root is a component: what its consumer passes passes on to that component's root.
export default function SaveAction({ text }: { text: string }) {
return <Pressable text={text} class="save" />;
}
import { ref, useTemplateRef } from "@uxfront/unframework";
import SaveAction from "./SaveAction.uf.tsx";
export default function Toolbar() {
const clicks = ref(0);
const save = useTemplateRef<HTMLButtonElement>();
return (
<div role="toolbar" aria-label="Actions">
<SaveAction
ref={save}
text="Save"
id="save"
aria-label="Save draft"
onClick={() => clicks.value++}
/>
<button type="button" onClick={() => save.value?.focus()}>
Focus save
</button>
<p>Clicks: {clicks.value}</p>
</div>
);
}
The attributes land on Pressable's <button>, beside the class SaveAction passes it. A click on it runs the toolbar's handler, and the ref holds it, so "Focus save" focuses it:
<div aria-label="Actions" role="toolbar">
<button aria-label="Save draft" class="pressable save" id="save" type="button">
"Save"
</button>
<button type="button">
"Focus save"
</button>
<p>
"Clicks: 0"
</p>
</div>
A class merges all the way down: <SaveAction text="Save" class="wide" /> renders <button class="pressable save wide">.
import Pressable from "./Pressable";
import { cx } from "./_unframework/cx";
import type { ComponentPropsWithRef } from "react";
export type SaveActionAttributes = Omit<
ComponentPropsWithRef<typeof Pressable>,
"text" | "children"
>;
export default function SaveAction({
text,
className,
style,
...attributes
}: { text: string } & SaveActionAttributes) {
return <Pressable text={text} className={cx("save", className)} style={style} {...attributes} />;
}
- React, Svelte, Qwik and Astro spread the props' rest on
Pressable, after the class they merge. Svelte bindsbind:refon it too, so a ref passes on. - React, Solid and Qwik type what passes on with the root component's own props,
ComponentPropsWithRef<typeof Pressable>,ComponentProps<typeof Pressable>andPropsOf<typeof Pressable>, without the propsSaveActionsets itself. So a root prop typed narrower than the attribute of its name, likevalue: number, still type-checks. On Qwik, a generic root component keeps its tag's attributes, sincePropsOfcan't infer its props. - Vue passes it on by itself.
- Solid takes
textandclassout withsplitProps, and spreads the others onPressable. - Angular passes both records on,
[ufAttributes]="this.ufAttributes()". ItsufElementis acomputedofPressable's own, so a ref onSaveActionreaches the button. - Qwik serialises a component's props, and a function isn't serialisable. So where the root component also carries the component's own template ref, Qwik passes it one signal, the parent's ref or else the own one, and a visible task fills the own one from the parent's.
A name the root component declares can't fall through to it. If Pressable emitted click, onClick on SaveAction would be UF1002, and so would a prop or a model Pressable declares, since Vue would give it to that declaration and the other targets to the <button>. Pass it to Pressable directly, or declare it on SaveAction.
Refs
A ref on a component that doesn't call defineExpose holds its root element, as Vue's $el does: through a chain of root components, and whatever inheritAttrs says. Type the ref as that element's DOM interface, by its name:
import { onMounted, ref, useTemplateRef } from "@uxfront/unframework";
// Exposes nothing, so a ref on it holds its root element. Its own template ref on that element
// still holds it once mounted, beside the parent's: `onMounted` reads it, and its focus listener
// fills the field only if it did.
export default function NameField({ label }: { label: string }) {
const input = useTemplateRef<HTMLInputElement>();
const mounted = ref(false);
onMounted(() => {
mounted.value = input.value !== null;
});
function fill() {
if (input.value && !input.value.value && mounted.value) input.value.value = "Ada";
}
return <input ref={input} name="name" aria-label={label} onFocus={fill} />;
}
import { defineOptions } from "@uxfront/unframework";
// Turns fallthrough off and spreads no rest: a ref on it still holds its root element.
export default function Bio({ label }: { label: string }) {
defineOptions({ inheritAttrs: false });
return <input name="bio" aria-label={label} />;
}
import { useTemplateRef } from "@uxfront/unframework";
import Bio from "./Bio.uf.tsx";
import NameField from "./NameField.uf.tsx";
export default function Profile() {
const name = useTemplateRef<HTMLInputElement>();
const bio = useTemplateRef<HTMLInputElement>();
return (
<form aria-label="Profile">
<NameField ref={name} label="Name" />
<button type="button" onClick={() => name.value?.focus()}>
Edit name
</button>
<Bio ref={bio} label="Bio" />
<button type="button" onClick={() => bio.value?.focus()}>
Edit bio
</button>
</form>
);
}
NameField's root is an <input>, so the parent's ref is useTemplateRef<HTMLInputElement>(), and "Edit name" focuses it. Bio turns fallthrough off and spreads no rest, and a ref on it still holds its root, so "Edit bio" focuses its <input>. A ref of another type, or on a component whose root isn't one element, is UF3046.
A component that exposes functions gives a ref those alone, never its element. So a ref through a root component that exposes functions is UF3046 too: it would hold those functions, which the component you put the ref on doesn't expose, and no element. Expose a function from that component, or give it an element root. Expose and component refs shows what each target writes for this case.
Putting it on another element
Sometimes the root isn't the element a parent means. A text field's root is often a <label> around an <input>, and the id, the placeholder and the focus listener belong on the <input>. So a component can turn fallthrough to its root off with defineOptions({ inheritAttrs: false }), end its props pattern with a rest, ...rest, and spread it whole on the element that should take it:
import { defineOptions } from "@uxfront/unframework";
// Turns fallthrough off and spreads what its consumer passes on its <input>, not on its root.
export default function Field({ label, ...rest }: { label: string }) {
defineOptions({ inheritAttrs: false });
return (
<label class="field">
{label} <input {...rest} name={label.toLowerCase()} />
</label>
);
}
import { ref } from "@uxfront/unframework";
import Field from "./Field.uf.tsx";
export default function SignUp() {
const focuses = ref(0);
return (
<form aria-label="Sign up">
<p id="email-hint">We never share it.</p>
<Field
label="Email"
id="email"
aria-describedby="email-hint"
placeholder="you@example.com"
class="wide"
style="margin-top: 4px"
onFocus={() => focuses.value++}
/>
<p>Focuses: {focuses.value}</p>
</form>
);
}
rest holds exactly what would have fallen through to the root, its class, style and listeners included. So the <input> takes all of it, and the <label> keeps only its own class:
<form aria-label="Sign up">
<p id="email-hint">
"We never share it."
</p>
<label class="field">
"Email "
<input aria-describedby="email-hint" class="wide" id="email" name="email" placeholder="you@example.com" style="margin-top: 4px;" uf:value="">
</label>
<p>
"Focuses: 0"
</p>
</form>
The parent checks what it passes against the <input>, or as SVG where the element is inside an <svg>, like a <circle>. The parent's value still wins over the element's own, wherever the spread sits. rest never holds a declared prop, and a styled parent's data-uf-* scope attribute stays on the root.
The rest has one canonical form, and one use. It needs defineOptions({ inheritAttrs: false }), or it is UF2001, whose safe fix adds the call.
Its one use is a whole spread on one element outside any list. A member read like rest.id, a second spread, a spread in a list or a spread on a component is UF2001 too, since each target holds the rest its own way, and a member of it would mean something different on each. So is a spread inside a slot's fill, which the child may render many times, and a spread on <component is>, which chooses among several elements.
import { scopeAttributes } from "./_unframework/scopeAttributes";
import type { ComponentPropsWithoutRef, Ref } from "react";
export type FieldAttributes = Omit<ComponentPropsWithoutRef<"input">, "label" | "children"> & {
ref?: Ref<HTMLLabelElement>;
};
export default function Field({
label,
className,
style,
ref,
...attributes
}: { label: string } & FieldAttributes) {
return (
<label className="field" {...scopeAttributes(attributes)} ref={ref}>
{label}{" "}
<input
name={label.toLowerCase()}
className={className}
style={style}
{...restAttributes(attributes)}
/>
</label>
);
}
/** What the consumer passes, but the scope attributes of its styled parents, which the root keeps. */
function restAttributes<T extends object>(props: T): T {
const rest = { ...props };
for (const key of Object.keys(rest)) {
if (key.startsWith("data-uf-")) Reflect.deleteProperty(rest, key);
}
return rest;
}
- React, Svelte, Solid and Qwik spread the rest on the
<input>through an inline helper,restAttributes, which leaves out the styled parents' scope attributes, and the<label>keeps those throughscopeAttributes. The<label>also takes the parent'sref, since a ref holds the root element. - Vue binds
restAttrs($attrs)on the<input>andscopeAttrs($attrs)on the<label>. Its props type declares the input's attributes behind/* @vue-ignore */, since vue-tsc reads them only from a root or a bare$attrs. - Angular binds
[ufFallthrough]on the<input>, beside theclassandstyleinputs, andufElementstays the<label>. - Astro spreads
withoutScope(attrs)on the<input>, and gives the input its ownnameonly where the parent passes none,"name" in Astro.props ? name : label.toLowerCase().
Turning it off
defineOptions({ inheritAttrs: false }) without a rest makes a component take nothing it doesn't declare, so its root keeps a layout of its own:
import { defineOptions } from "@uxfront/unframework";
// Renders no class or style a consumer passes: its own layout is fixed.
export default function Field({ label }: { label: string }) {
defineOptions({ inheritAttrs: false });
return (
<label class="field">
{label} <input name={label.toLowerCase()} />
</label>
);
}
Vue writes the same defineOptions. The other targets type and spread nothing that falls through, and Angular's host still clears class and style. The argument is that object literal and nothing else (UF2031).
A <component is> root
A component whose root is a <component is> over tags takes what a parent passes as a one-element root does, on whichever tag renders: class and style, the attributes every tag takes, the DOM listeners and a ref. So does a component whose root component renders one. Here Base renders a link with an href and a button without one, and Primary renders Base:
// Its root is a `<component is>` over tags: a link with an `href`, and a button without one. It
// declares no `id`, ARIA, `data-*` or `title`, no listener and exposes nothing, so a consumer's
// attributes, listeners and ref fall through to whichever tag renders (ADR-0122). Its own `title`
// gives way to a consumer's, and its own click listener runs before a consumer's.
export default function Base({
href,
disabled,
text,
}: {
href?: string;
disabled?: boolean;
text: string;
}) {
// A consumer handles a click, as a router would: the link does not navigate.
function press(event: MouseEvent) {
event.preventDefault();
}
return (
<component
is={href ? "a" : "button"}
href={href}
disabled={disabled}
class="base"
title="Base"
onClick={press}
>
{text}
</component>
);
}
import Base from "./Base.uf.tsx";
// Its root is a component: what falls through to it passes on to whichever tag Base renders.
export default function Primary({ href, text }: { href?: string; text: string }) {
return <Base href={href} text={text} class="primary" />;
}
The page passes each an id, an ARIA and a data-* attribute, listeners and a ref, and a button toggles both between a link and a button:
import { ref, useTemplateRef } from "@uxfront/unframework";
import Base from "./Base.uf.tsx";
import Primary from "./Primary.uf.tsx";
export default function Page() {
const linked = ref(true);
const log = ref<string[]>([]);
const direct = useTemplateRef<HTMLAnchorElement | HTMLButtonElement>();
const chained = useTemplateRef<HTMLAnchorElement | HTMLButtonElement>();
function record(line: string) {
log.value = [...log.value, line];
}
return (
<section aria-label="Page">
<Base
href={linked.value ? "#direct" : undefined}
text="Direct"
id="direct"
aria-label="Direct"
data-testid="direct"
title="Go"
onClick={(event) => record(`click on ${(event.currentTarget as Element).localName}`)}
onKeydown={(event) =>
record(`keydown ${event.key} on ${(event.currentTarget as Element).localName}`)
}
ref={direct}
/>
<Primary
href={linked.value ? "#chained" : undefined}
text="Chained"
id="chained"
aria-label="Chained"
data-testid="chained"
onClick={(event) => record(`chained on ${(event.currentTarget as Element).localName}`)}
ref={chained}
/>
<button type="button" onClick={() => (linked.value = !linked.value)}>
Toggle
</button>
<button type="button" onClick={() => direct.value?.focus()}>
Focus direct
</button>
<button type="button" onClick={() => chained.value?.focus()}>
Focus chained
</button>
<ol aria-label="Log">
{log.value.map((entry, index) => (
<li key={index}>{entry}</li>
))}
</ol>
</section>
);
}
Each attribute lands on the link, then on the button once the toggle runs, and the page's title wins over Base's own. Each listener logs the element it runs on, a or button, after Base's own click listener, and each ref focuses the element that renders, before and after the toggle.
What falls through reaches the child whole, and the child chooses its tag as it renders, so an attribute falls through only where every tag takes it: id, title, tabindex, aria-* and data-*, with type on a link or a button. One that only some tags take, like disabled, which <a> doesn't take, is UF1002: declare it as a prop and set it on the <component is>, which renders it only on the tags that take it. One that no tag takes is UF3035.
A ref holds the element that renders, so type it as the union of the tags' DOM interfaces, in any order: useTemplateRef<HTMLAnchorElement | HTMLButtonElement>(). Tags that share an interface, like h2 and h3, give it once, HTMLHeadingElement. A ref typed as one tag's interface, or as a supertype such as HTMLElement, is UF3046.
Each target puts what falls through on the tag that renders, and types it as what every tag takes:
import { cx } from "./_unframework/cx";
import { mergeRefs } from "./_unframework/mergeRefs";
import {
type ComponentPropsWithoutRef,
type DOMAttributes,
type MouseEvent,
type Ref,
useMemo,
} from "react";
export type BaseAttributes = Omit<
ComponentPropsWithoutRef<"a"> & ComponentPropsWithoutRef<"button">,
| "href"
| "disabled"
| "text"
| "children"
| keyof DOMAttributes<HTMLAnchorElement | HTMLButtonElement>
> &
Omit<
DOMAttributes<HTMLAnchorElement | HTMLButtonElement>,
"children" | "dangerouslySetInnerHTML"
> & { ref?: Ref<HTMLAnchorElement | HTMLButtonElement> };
function press(event: MouseEvent) {
event.preventDefault();
}
export default function Base({
href,
disabled,
text,
className,
style,
onClick,
ref,
...attributes
}: { href?: string; disabled?: boolean; text: string } & BaseAttributes) {
const rootRef = useMemo(() => mergeRefs(ref), [ref]);
const Tag = href ? "a" : "button";
return (
<Tag
href={Tag === "a" ? href : undefined}
disabled={Tag === "button" ? disabled : undefined}
className={cx("base", className)}
title="Base"
onClick={(event) => {
press(event);
onClick?.(event);
}}
style={style}
ref={rootRef}
{...attributes}
>
{text}
</Tag>
);
}
Angular writes each tag in an @case of its own, and each tag's element binds the Fallthrough directive and holds the ufElement ref. Qwik writes a <component is> with listeners of its own as one element per tag, and each takes the rest. React and Solid hand the ref to whichever tag renders through a callback, since each tag types its ref for itself. Astro renders on the server only, so a listener that falls through is dropped, with UF4001 for information.
Where nothing can fall through
A parent that passes a component something it doesn't declare has it rejected rather than dropped (UF3045) where the component has no element to take it: it turns fallthrough off and spreads no rest, or its root is a fragment, a conditional or a list, a <component is> whose is reads a setup value, whose tags the parent can't read, or a root component that takes nothing itself. Pass what the child declares, spread its props' rest on one of its elements, or give it one root element.
A <component is> whose candidates are components passes them class and style only. Anything else that would fall through to a candidate is UF1002, so pass what every candidate declares, or render each by its own tag.
The decisions behind this page are ADR-0054, ADR-0056, ADR-0118, ADR-0119 and ADR-0122.
Models
defineModel declares a value a parent binds two ways with v-model, and v-model on a form control binds to a ref. Each framework gets its own two-way binding.
Expose and component refs
defineExpose lists the functions a parent calls through a component ref. A ref on a component that exposes nothing holds its root element.