I don’t think you can acheive this with JS commands alone right now. In cases like these I usually just JS.dispatch("some-event" and implement the rest in JavaScript.
Yes, as you said, I think our best bet now is JS.dispatch/2.
JS commands used to be always blocking, so that’s probably why the only supported API closest to temporarily setting an attribute is JS.transition (which was meant to stay within 200~300ms as per Chris), which temporarily sets classes.
Steffen added a new option blocking: false to all JS commands that can do transitions in LV 1.0, so perhaps there’s a way forward to introducing a new temporary attribute command now.
A recent one I had was to set a data attribute temporarily. Could be done with a class, though.
The data attribute was used to render different states solely from CSS/JS on the client side. To be more specific, the particular case I had was to show a message like “Copied!” after clicking a button that copies content to clipboard (example).
In Tailwind, it becomes something like [data-copied]: vs [.copied]: for a custom selector.
FYI there is import {createHook} from “phoenix_liveview”` where you can drive programmatic js commands from client code, which could for example do a “temporary” set thing by removing the attr:
I believe Chris’ example is how to use it outside of a hook. Within a hook, you can do this.js().setAttribute (note the function call here). This is super recent—I learned it from another thread here—and I believe is still undocumented.
Thanks, @LostKobrakai, for helping me out with that one.
While I’m sick of hearing the term “game changing,” this is pretty game changing since if we’ve constructed a JS command that stands on its own, then need something more complicated in a hook that also needs the same functionality as the command, we no longer need to re-implement it JS.
Is the idea here to just create a <script> tag inside a component and place the code there?
If so, that could be interesting to make hooks live alongside the component instead of separated in another path.