larshei
Hello beloved forum people!
I am currently looking for an option to have a LiveView JS Hook which I can pass custom data to, fully client side.
What do I want to achieve?
The idea is to have a “ScrollTo” hook which I can put on e.g. buttons and add a scroll target, then on click have the webpage scroll to that specific area.
To have the button reusable, it would be great to do something like
#... in ~H/render
<.button phx-hook="ScrollTo" scroll-target="main">To Top<button>
<.button phx-hook="ScrollTo" scroll-target="footer">To Bottom<button>
What I tried
I went through the docs but could not find an option to define custom data that is then available in the hook. This seemed to only be an option for push_event from the server. So I tried some random stuff, like addingdata="main" or phx-value-data="main" or phx:data="main", then inspected/console.log’d the event, but I could not find the custom data anywhere.
Question
Is there an option to pass custom, known at render,-time data to a client side hook?
Or should I maybe even generate a script section and generate the script on render?
Any ideas are welcome
Thanks!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
larshei
I am not very smart some days
I can simply add an onclick with embedded JS.
However, I wonder if there is a more phoenix-y way.
Component
Call
benwilson512
On my phone but phx-click and the JS module functions should let you send data on click.
cmo
<button phx-click={JS.dispatch("click", to: ".nav")}>Click me!</butuon>larshei
Hey thanks for the suggestion.
I stumbled over this one while going through the docs. Looks like I can trigger an event, but not add any payload?
cmo
That’s what the
detailmap is for.codeanpeace
If the payload’s only the dom selector, then you might not even need the
detailoption if you specify thetooption and useevent.targetin the event handler.larshei
Uh, I like that! Thanks for the suggestion.
Side node if anyone stumbles over this later: At first I changed the event name
"scrollTo"to"scroll"and got hundreds of errors in the browsers log, because"scroll"is an actual event that is fired when the user is scrollingbenlime
Although you found a kind of solution I’d like to add what I am doing when I need custom data in a hook.
There are two approaches which I found useful.
Use a
dataattribute and fetch it from the hookmountcallback.I use that approach all the time if I want to process some data and the data is cheap to render inside the template.
Use
pushEventto send an init message and get the data via the reply.In your LiveView:
This approach is more suitable if you need to perform more complex tasks or have big payloads. For example I use this to initialize a rich text editor with a state stored in the db.