Replies: 1 comment
-
Thanks for your thoughts and feedback. As you've mentioned, the current best practice is to use: <body>
<div>
<button>Button</button>
<script>
me("button", me()).on("click", (e) => { alert("button was clicked") })
</script>
</div>
</body> Next version of Surreal will very likely add relative chained versions of <body>
<div>
<button>Button</button>
<script>
me().me("button").on("click", (e) => { alert("button was clicked") })
</script>
</div>
</body> Also will likely be adding an alias for parentElement, so you can reach back up the DOM tree without typing so much. Considering Example... me().parent().parent().send("open") ...or Example... me().up().up().send("open") Will fire an event called "open", from 2 parents up the tree. The equivalent in non-surreal JS: let ev = new CustomEvent("open", { bubbles: true })
currentScript.parentElement.parentElement.parentElement.dispatchEvent(ev) Thoughts and feedback appreciated! Always starting relative to the parent element was considered many times in the past- root was kept as |
Beta Was this translation helpful? Give feedback.
-
Currently, Surreal uses the
document
object as the query root when no starting point is passed to eitherme
orany
(ex. me()). Since the theme of the library is colocation, this default caught me by surprise. To scope the script when using child selectors, I had to capture a root variable withme()
and then useme(selector, root)
inside the script. While this works, I see it becoming a repeated pattern and worth thinking through if/how thecurrentScript.parentElement
should be the default.Here's a minimal example that shows scoping to a "component" (the divs with ids):
Relatedly, I started experimenting with Surreal along side your
css-scope-inline
library. In the css library,me
refers to the parent of the style element, which makes sense. Having Surreal usecurrentScript.parentElement
would match this behavior.Happy to discuss and brainstorm ideas. I figured I'd ask here first to understand the design decision.
Beta Was this translation helpful? Give feedback.
All reactions