You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some components cannot change the DOM structure — for example, components used inside tables or other strict layouts where adding a wrapper element would break the markup.
In such cases, the component renders only a <slot /> but still needs access to the actual DOM element provided by that slot.
Right now, this can only be done by reading internal structures like vm.subTree.children, which is unreliable and depends on undocumented internals.
This works but is fragile and may break in future Vue versions.
Why not use directives?
A common suggestion might be to use custom directives for DOM access. However, directives have significant limitations in this scenario:
No Vue component scope — Directives don't have access to the component's reactive state, props, or composables
Cannot use VueUse composables — Popular utilities like useResizeObserver, useIntersectionObserver, useElementSize etc. require component context
No lifecycle integration — Directives can't easily integrate with component lifecycle hooks like onBeforeUnmount for cleanup
Awkward communication — Passing data between directive and component requires workarounds (events, provide/inject, or global state)
Type safety issues — Directive bindings are harder to type correctly compared to composables
Directives are great for reusable DOM manipulations across different components, but when a component needs to interact with its own slotted content, a composable-based solution is more natural and maintainable.
Proposed API
It would be helpful to have an official API, for example:
constel=useTemplateRefFromSlot('default')
which would return a ref to the first rendered element from the given slot (or null if empty).
Allow components to interact with slotted DOM elements (e.g. attach scroll logic, measure size, or observe mutations) without changing the template structure and without relying on private internals.
This would enable:
Scroll containers that don't wrap content
Layout components that measure slotted content
Accessibility enhancements on slotted elements
Integration with VueUse and other composable libraries
Type-safe, maintainable code that won't break with Vue updates
Use Cases
<script setup lang="ts">
import { useSlotElements } from'vue'import { useResizeObserver } from'@vueuse/core'// Access slot content without wrapperconst slotElements =useSlotElements('default')// Use with VueUse composablesuseResizeObserver(slotElements, (entries) => {console.log('Slot content resized:', entries)})
</script>
<template>
<!-- No wrapper needed -->
<slot />
</template>
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Some components cannot change the DOM structure — for example, components used inside tables or other strict layouts where adding a wrapper element would break the markup.
In such cases, the component renders only a
<slot />but still needs access to the actual DOM element provided by that slot.Right now, this can only be done by reading internal structures like
vm.subTree.children, which is unreliable and depends on undocumented internals.Example of current workaround:
This works but is fragile and may break in future Vue versions.
Why not use directives?
A common suggestion might be to use custom directives for DOM access. However, directives have significant limitations in this scenario:
useResizeObserver,useIntersectionObserver,useElementSizeetc. require component contextonBeforeUnmountfor cleanupDirectives are great for reusable DOM manipulations across different components, but when a component needs to interact with its own slotted content, a composable-based solution is more natural and maintainable.
Proposed API
It would be helpful to have an official API, for example:
which would return a ref to the first rendered element from the given slot (or
nullif empty).Alternative API could support multiple elements:
Motivation
Allow components to interact with slotted DOM elements (e.g. attach scroll logic, measure size, or observe mutations) without changing the template structure and without relying on private internals.
This would enable:
Use Cases
Beta Was this translation helpful? Give feedback.
All reactions