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
Enable programmatic access to slotted content structure in Astro components through a new children() API method, allowing people to analyze and manipulate child components and their slot hierarchy before rendering.
Background & Motivation
Currently, Astro slots provide an elegant way to pass content between components, but it lack programmatic introspection capabilities. While Astro.slots.render() allows rendering slot content to a string HTML of the slot contents, we'll use Astro.slots.render('default'), developers cannot analyze the structure of slotted content before rendering. This limitation makes it difficult to implement complex component patterns that require knowledge of child components and their attributes.
Many UI framework patterns, particularly in form controls and compound components, need to understand their children's structure. For example, custom select components often need to traverse their child options to build both a visible UI and a corresponding native <select> element for form submission. Current workarounds involve either using props (which may not meet design requirements) or parsing rendered HTML (which is inefficient and error-prone).
I've had difficulties with:
Building compound components that need to understand their child structure
Implementing progressive enhancement patterns that require multiple renderings of the same data
Creating component libraries with complex parent-child relationships
Accessing metadata about nested slots for dynamic behavior
Goals
Provide a programmatic API to access slot content structure without rendering to HTML
Enable inspection of child components, their types, props, and nested slots
Support both named and default slot access
Allow for analysis of deeply nested slot hierarchies
Facilitate progressive enhancement patterns by allowing multiple interpretations of slot content
Support both static site generation and server-side rendering modes
Ensure compatibility with existing slot features like fallback content
Example
The proposed API introduces a children() method that accepts Astro.slots as an argument and returns a structured representation of the slotted content:
---// With auto-generated names, the system would assign internal IDs// e.g., _slot_0, _slot_1, etc.---
<MyComponent>
<Fragment>
<Fragment>
<p>Hello, World</p>
</Fragment>
</Fragment>
</MyComponent>
The API would work with existing Astro features:
Respecting slot fallback content behavior Fallback content will only be displayed when there are no matching elements with the slot="name" attribute being passed in to a named slot
Supporting component factories and framework components
Maintaining the server-side rendering nature of Astro components
If there's already a solution to do this without explicit use of props, I'm all ears.
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.
-
Body
Summary
Enable programmatic access to slotted content structure in Astro components through a new
children()
API method, allowing people to analyze and manipulate child components and their slot hierarchy before rendering.Background & Motivation
Currently, Astro slots provide an elegant way to pass content between components, but it lack programmatic introspection capabilities. While
Astro.slots.render()
allows rendering slot content to a string HTML of the slot contents, we'll use Astro.slots.render('default'), developers cannot analyze the structure of slotted content before rendering. This limitation makes it difficult to implement complex component patterns that require knowledge of child components and their attributes.Many UI framework patterns, particularly in form controls and compound components, need to understand their children's structure. For example, custom select components often need to traverse their child options to build both a visible UI and a corresponding native
<select>
element for form submission. Current workarounds involve either using props (which may not meet design requirements) or parsing rendered HTML (which is inefficient and error-prone).I've had difficulties with:
Goals
Example
The proposed API introduces a
children()
method that acceptsAstro.slots
as an argument and returns a structured representation of the slotted content:Consider a custom combobox component that requires both a visible dropdown and a hidden
<select>
element for form submission:Usage with nested slots:
Optional auto-generated slot names:
The API would work with existing Astro features:
If there's already a solution to do this without explicit use of props, I'm all ears.
Beta Was this translation helpful? Give feedback.
All reactions