Ability to transport DOM elements natively. #782
iivvaannxx
started this conversation in
Proposal
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Proposing a 'transport' directive for the Astro web framework, aimed at enhancing UI flexibility by allowing dynamic placement of DOM elements. This feature addresses the challenge of managing elements that are logically part of one component but need to be visually placed in a different location in the DOM, simplifying the development process and reducing the need for extensive client-side JavaScript.
Background & Motivation
I will use the following quote from the Vue docs as I find it to be very accurate and simple:
Why not use a Framework Component instead?
Frameworks like React, Vue, Alpine.js offer a way to do this already. While it could be possible to use a framework component for it, you may not want to add an entire integration for simpler projects. Specially if it's to add just a simple feature, as you will need the entire framework runtime to be downloaded on the client.
Additionally, with the upcoming Invokers proposal, components traditionally managed separately yet functionally interconnected (such as a modal and its activation button, or a navigation menu and its hamburger button) could be developed without any JavaScript. I believe this feature would complement that development nicely, allowing these related elements to be housed within the same file while still providing the option for their distinct placement as necessary.
Feature Description
The directive would follow a syntax similar to Alpine.js's x-teleport, allowing developers to specify the target location using an attribute like
transport:{where} = target
. The target should be CSS selector, used similarly todocument.querySelector
. Perhaps also a special key, that would allow to identify an element without having to set anid
, and could be set like the following:teleport:where = id
.The directive
where
would support the following values:prepend
: Inserts the element as the first child of the target.append
: Inserts the element as the last child of the target.before
: Places the element just before the target in the DOM.after
: Places the element just after the target in the DOM.This directive aims to be resolved statically by Astro during the build process for static site generation. While I don't know the details of where or how this could be implemented, I do guess that it should be done on the final stages of the build, where everything has been generated and in place, then elements with this directive could get their location swapped as expected.
This would need to be further elaborated, as I am not a maintainer or collaborator, I don't know very much the implementation details that should be taken into account.
Goals
Example
While this example uses the previously mentioned Invokers API which is not yet ready available to be used, I feel like it clarifies a little bit better one of the possible use-cases of this feature.
Beta Was this translation helpful? Give feedback.
All reactions