-
Notifications
You must be signed in to change notification settings - Fork 424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add registerDefaultEventName #661
base: main
Are you sure you want to change the base?
Conversation
This allows configure default events for arbitrary elements. Closes hotwired#660
@marcoroth here's a first pass at implementing this feature. One thing that tripped me up was needing to ensure that the event name is registered before stimulus attaches to the DOM and queries for elements. I guess in practice this would mean users need to call Another thing I'm unsure about, is the "global" nature of this data; registering a default event name will effect all Application instances. Given that Stimulus already has a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks for working on this
It would be nice to have a bit of documentation. The example with shoelace component is great
Hi @adrienpoly, could you clarify if you prefer this PR to the approach in #662? Coming back to this now after a few months, I feel that #662 is a better design. I'm happy to update the docs, just want to highlight the other option here. |
I think I prefer this solution here over the solution proposed in #662. In #622 you need to override the schema and need to make sure that you still handle the default events for the elements. With the solution proposed in this PR you can call I guess the other solution would be to combine both approaches. So that you can call |
The very first version of #662 was based on the way custom keyMappings are defined and looked like this:
(emphasis mine) The problem here is that (by default) all applications share a single This makes What would you think of changing the default value of the Concretely, that could look like: const app = new Application()
app.registerDefaultEventName('some-element', 'click')
app.registerKeyMapping('at', '@')
app.start() and inside class Application {
...
registerDefaultEventName(tagName: string, eventName: string | ((el: Element) => string)) {
// mutating is fine here, because application uses it's own copy of the default schema
if (typeof eventName === 'string') {
this.schema.defaultEventNames[tagName] = () => eventName
} else {
this.schema.defaultEventNames[tagName] = eventName
}
}
registerKeyMapping(alias: string, key: string) {
// same here, much easier to document, and easier for users
this.schema.keyMappings[alias] = key
}
...
} |
Oh right, I didn't think that far tbh, I kinda just assumed that's what we are already doing right now. And that every application instance already copies the We definitely shouldn't modify the global I also like your proposal of having For now I would just focus on |
This allows configuring default events for arbitrary elements.
Closes #660