alpine-collective / alpinejs-devtools

Chrome/Firefox DevTools extension for debugging Alpine.js applications.
MIT License
531 stars 18 forks source link

suggestion: add global variable for current selected component #122

Closed MordiSacks closed 3 years ago

MordiSacks commented 3 years ago

Similar to the $0 of chrome and $vm of vue, it would be very productive to have this for alpine
maybe $a

Thank you all for the great work.

HugoDF commented 3 years ago

$x0 ($x1 and so on) is probably the name we want to go for.

Getting a proof of concept to work is probably not to complicated:

MordiSacks commented 3 years ago

That is what I initially thought but $x is used in chrome for xpath selector https://developers.google.com/web/tools/chrome-devtools/console/utilities#xpath

HugoDF commented 3 years ago

That is what I initially thought but $x is used in chrome for xpath selector https://developers.google.com/web/tools/chrome-devtools/console/utilities#xpath

$x0, $x1 though?

Actually, it might be simpler to expose all the component "instance"-es in the console under $x{componentId} (componentId is an incrementing integer) without a UI to trigger it, then we can just have a "access using $x1 in the console" on hover of the component in the devtools panel.

Do you think we should be exposing the whole component instance or just the data? On the Alpine Discord people always seem a bit confused about Proxies etc

ryangjchandler commented 3 years ago

@HugoDF I think it might make sense to expose both separately, otherwise you need knowledge of the __x.$data stuff.

Alternatively, you could actually wrap it into a custom object in the console. For example:


{
    el: $el, // the component el,
    data: $el.__x.$data, // the data / `Proxy`
}
``
HugoDF commented 3 years ago

Interesting, for reference this is Vue.js

UI in the panel, we could use = $x0 + tooltip or "title" attr or even just Available as $x0 in console

Screenshot 2020-12-14 at 12 38 43

Contents of $vm:

Screenshot 2020-12-14 at 12 39 08

I think the most in line with that would be to expose rootEl.__x which contains unobservedData, el and so on

Screenshot 2020-12-14 at 12 43 05

Maybe we could spread the unobservedData or $data in the top-level scope (depending on whether spread $data values will update the component or not)? Edit: when spreading $data, the component state doesn't update (due to how proxies), but I guess people trying to edit the data should probably be using the devtools panel...

HugoDF commented 3 years ago

Ok just done a release today but this will be in the next one

Screenshot 2020-12-14 at 20 53 29
MordiSacks commented 3 years ago

Looks awesome,
Thank you.

HugoDF commented 3 years ago

Keep your eyes peeled for 0.1.0 (this is the milestone https://github.com/alpine-collective/alpinejs-devtools/milestone/11)

When it goes out will depend on how busy the repo is over the next couple of days