Roma Komarov on Nostr: Let's say I want to have a web component. Whenever I use it, I would like to provide ...
Let's say I want to have a web component.
Whenever I use it, I would like to provide a custom callback to run in its context, defining it in HTML.
Is there an idiomatic way to do so? The way I'm thinking of is to use `currentScript`:
<my-element>
<script> document.currentScript.myCallback = () => console.log('Hey!');</script>
</my-element>
And then call this `myCallback` from inside the `connectedCallback`.
Is there anything better (apart from using a unique `id` for this)?
Published at
2024-05-09 13:03:52Event JSON
{
"id": "114b619e0469e9d638f8a429ad2e18fa02432b1a3201d18feaa197141a33997c",
"pubkey": "0ea36331db38ccfa0aff7c4f83a7eb402f4e698a24b53d632909829a8a018118",
"created_at": 1715259832,
"kind": 1,
"tags": [
[
"proxy",
"https://front-end.social/@kizu/112411268359325836",
"web"
],
[
"proxy",
"https://front-end.social/users/kizu/statuses/112411268359325836",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://front-end.social/users/kizu/statuses/112411268359325836",
"pink.momostr"
]
],
"content": "Let's say I want to have a web component.\n\nWhenever I use it, I would like to provide a custom callback to run in its context, defining it in HTML.\n\nIs there an idiomatic way to do so? The way I'm thinking of is to use `currentScript`:\n\n \u003cmy-element\u003e\n \u003cscript\u003e document.currentScript.myCallback = () =\u003e console.log('Hey!');\u003c/script\u003e\n \u003c/my-element\u003e\n\nAnd then call this `myCallback` from inside the `connectedCallback`.\n\nIs there anything better (apart from using a unique `id` for this)?",
"sig": "4dac0cbd956bbc4a2a2c114c756af8a89923e7aab298ad14047bd4147434069edeca5071e088328c27c4869977326b7d190dcd4ce86500823100e6223f0f03f5"
}