Closed matthewp closed 1 year ago
@matthewp Would this change open up the possibility of "conditional" directives?
I'd like to see something similar to client:only="vue"
but for ALL client directives accepting a boolean, with the help of props e.g:
---
import MyComponent from '../components/MyComponent.vue';
const { myProp } = Astro.props;
---
<MyComponent client:visible={myProp === 'A specific value that means it should hydrate'} />
Please do this I can then make a directive to solve one of the problems with Astro Right Now which is classes on the island and not the parent. See it in this issue.
@spacedawwwg Yes, I think this would definitely enable that sort of thing. Whether we would add that option to the built-ins or not I'm not sure, probably needs a separate discussion (but it makes sense to me).
@spacedawwwg Yes, I think this would definitely enable that sort of thing. Whether we would add that option to the built-ins or not I'm not sure, probably needs a separate discussion (but it makes sense to me).
export default function(load, opts, element) { element.addEventListener('click', async () => { const hydrate = await load(); await hydrate(); }, { once: true }); }
I hope this is not the final design. I'd like to be able to export as many as I need from one file. being forced to use an entrypoint will require too much boilerplate code. A file per directive!
cc @bluwy is there anything we can do to enable multiple directives per file?
We do have this same restriction in the renderer API and I haven't heard of it being a problem with anyone. But perhaps you might want to create a package of many small directives and it can be nice to export them all from an index module. Not a big deal imo though.
I could see a solution like in addClientDirective()
taking the export name as an option, defaulting to default
.
Yeah we could add a new option for the names export, but we currently don't have this pattern for renderers, image services, etc. I don't anticipate many directives to be needed at once, or that boilerplate would be an issue (?), so I think it's fine to keep the scope down for now.
Closing as this is completed and in stable.
Body
Summary
Provide an API for integrations to implement custom client: directives to provide greater control for when client-side JS is loaded and executed.
Background & Motivation
The last client directive added to core was the
client:only
directive in August 2021. Since that time the core team has been hesitant to add new client directives despite the community asking about them.Allowing custom client directives would both:
Some examples of custom directives that people have wanted in the past:
client:visible
currently does.Goals
client:
loading to get rid of the precompile step (this is a core repo refactor / improvement).Non-Goals
client:
.Future
client:only
load vs idle vs visible problem.Example
Usage
To use a custom directive in your app, add an integration like any other.
And then use it in your Astro components:
Implementation
An implementer of a custom client directive would do so through the hooks API:
The entrypoint being: