Fallthrough attributes

Intermediate · 3 min

How undeclared attributes and listeners fall through to a component's root, how class and style merge, and controlling it with inheritAttrs and $attrs.

Before thisComponents and propsClass and style bindings

LearnVue · 3 min

Fallthrough attributes

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by LearnVueReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Any attribute or `v-on` listener the parent passes that the child doesn't declare as a prop or emit is a fallthrough attribute. By default Vue adds it to the child's single root element.
  • `class` and `style` merge with the root element's own values instead of replacing them, and a fallthrough listener is added alongside any listener the root already has.
  • `defineOptions({ inheritAttrs: false })` turns automatic inheritance off, so you can apply the attributes yourself with `v-bind="$attrs"` on whichever element should get them.
  • `$attrs` includes `class`, `style`, plain attributes, and listeners (as `onClick`-style functions). Declared props and declared emits are left out.
  • Multi-root components have no single place to put fallthrough attributes. Vue warns until you bind `$attrs` on one of the roots explicitly.
  • `useAttrs()` gives script access to the same object. It always reflects the latest values but isn't reactive, so it can't be watched.

The problem: wrapping a native element

You build a BaseButton that wraps a native <button> with your design system’s styles. Then someone writes this:

<BaseButton class="w-full" type="submit" aria-label="Save" @click="save">
  Save
</BaseButton>

None of class, type, aria-label, or the click listener is declared as a prop or emit. If Vue dropped them, the component would be useless. Every native attribute would have to be declared and forwarded by hand, and there are hundreds.

Vue’s answer is attribute inheritance. Whatever the parent passes that the component doesn’t claim as a prop or emit “falls through” to the component’s root element.

Default behaviour: everything lands on the root

Take this component:

<!-- BaseButton.vue -->
<template>
  <button class="btn">
    <slot />
  </button>
</template>

and the usage above. Vue renders:

<button class="btn w-full" type="submit" aria-label="Save">Save</button>

with save attached as a native click listener. Three details matter:

  • class and style merge. The root’s own class="btn" is kept and the parent’s classes are added. The same goes for style, using the same merging rules as class and style bindings.
  • Listeners add up. If the root already has its own @click, both handlers run.
  • Props and emits are excluded. If BaseButton declared defineProps(['variant']), then variant="primary" is consumed as a prop and does not appear on the <button>. Declared emits are left out the same way, as covered in component events.

Inheritance also chains. If a component’s root is another component, the attributes pass on to that component, which applies its own rules. Props and emits the middle component declared are still filtered out first.

Turning inheritance off

The default is wrong whenever the root isn’t the element that should receive the attributes. A form field is the classic case:

<!-- TextField.vue -->
<script setup>
defineOptions({ inheritAttrs: false })
defineProps({ label: String })
</script>

<template>
  <label class="field">
    <span>{{ label }}</span>
    <input v-bind="$attrs" />
  </label>
</template>
<TextField label="Email" type="email" placeholder="you@example.com" required @blur="validate" />

Without inheritAttrs: false, type, placeholder, required, and the blur listener would all land on the <label>, where they do nothing useful, and assistive technology would miss them. With it, nothing falls through automatically, and v-bind="$attrs" places everything on the <input>. That matters for accessible forms, which is covered in accessible HTML and ARIA basics.

defineOptions is a compiler macro added in Vue 3.3 for setting component options from <script setup>. Before it existed, you needed a second plain <script> block that exported { inheritAttrs: false }.

What $attrs contains

$attrs is a plain object holding every fallthrough attribute:

  • ordinary attributes, keyed as the parent wrote them (aria-label stays $attrs['aria-label'])
  • class and style
  • v-on listeners, as functions with an on prefix: @click becomes $attrs.onClick

Since listeners are part of $attrs, v-bind="$attrs" forwards event listeners too. Vue 2 kept them in a separate $listeners object, which Vue 3 removed. Older tutorials still mention it.

You can also forward selectively. class and style often belong on the wrapper while everything else goes on the input:

<script setup>
import { useAttrs } from 'vue'
defineOptions({ inheritAttrs: false })

const attrs = useAttrs()
function inputAttrs() {
  const { class: _c, style: _s, ...rest } = attrs
  return rest
}
</script>

<template>
  <label :class="$attrs.class" :style="$attrs.style">
    <input v-bind="inputAttrs()" />
  </label>
</template>

inputAttrs() is a plain function rather than a computed on purpose. It runs on every render, so it always sees the current attributes. A computed caches its result until a reactive dependency changes, and the attrs object isn’t reactive, as the next section explains.

useAttrs is not reactive

useAttrs() returns the same object in script. The Vue docs are explicit: it always reflects the latest fallthrough attributes, but it is not reactive, for performance reasons. watch(useAttrs(), ...) or watch(() => attrs.foo, ...) won’t fire on changes. If the component needs to react to a value, declare it as a prop. If you need to run a side effect with the latest attrs, use onUpdated() from the lifecycle hooks.

Multi-root components

Vue 3 allows templates with several root nodes:

<!-- PageLayout.vue -->
<template>
  <header>…</header>
  <main>…</main>
  <footer>…</footer>
</template>

With no single root, Vue can’t guess where id="page" or @click should go. It applies them nowhere and logs a warning. Bind $attrs explicitly on the right element and the warning goes away:

<template>
  <header>…</header>
  <main v-bind="$attrs">…</main>
  <footer>…</footer>
</template>

The rule of thumb: if the component doesn’t have exactly one root element, decide where attributes go yourself.

A comparison with React

React has no automatic inheritance. A wrapper component spreads the rest of its props onto the inner element by hand, <button {...rest}>, and forgetting is a common bug. Vue makes forwarding the default and gives you the switch to opt out. Each approach has a cost. Vue’s default is easy to miss when the root is a wrapper <div>, which is why inheritAttrs: false with v-bind="$attrs" is a habit worth forming for any component that wraps a form control.

What the video covers

LearnVue’s three-minute video shows the core mechanism: non-prop attributes landing on the root element, then turning inheritance off and binding $attrs where they should go. It is short and leaves out several pieces: class and style merging, listener fallthrough, defineOptions, useAttrs and its non-reactivity, and multi-root components. Those are covered above. Codevolution’s episode is a slightly longer walkthrough of the same ideas in Options API style.

Check yourself

  1. 01A `BaseButton` renders `<button class="btn">`. The parent writes `<BaseButton class="large" disabled @click="save" />` and the component declares no props or emits. What is rendered and what happens on click?

    All three fall through to the root `<button>`. It renders as `<button class="btn large" disabled>`, with the two class lists merged, and the `click` listener is attached to it as a native listener, so clicking the button calls `save`.

  2. 02A `TextField` component wraps its `<input>` in a `<div>` with a label. Why do `placeholder` and `@focus` passed by the parent end up on the wrong element, and how do you fix it?

    Fallthrough attributes go to the root element, which here is the wrapper `<div>`, not the `<input>`. Set `defineOptions({ inheritAttrs: false })` to stop automatic inheritance, then put `v-bind="$attrs"` on the `<input>` so every attribute and listener lands there.

  3. 03A component's template has three root elements: `<header>`, `<main>`, `<footer>`. The parent passes `id="page"`. What happens?

    Vue can't decide which root should receive the attribute, so it applies it to none of them and logs a runtime warning. Binding `v-bind="$attrs"` on one of the roots tells Vue where they go and silences the warning.

  4. 04Why does `watch(useAttrs(), ...)` not work, and what should you use if you need to react to a value?

    The attrs object is kept up to date but isn't reactive, for performance reasons, so watchers can't track it. If the component needs to react to a value, declare it as a prop. For side effects that should use the latest attrs, `onUpdated()` runs after each re-render.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark