threeaccents
Integrating AlpineJS with Surface
I’m trying to integrate AlpineJS in a LiveView project using Surface. I’ve tried a few different interpolation techniques but none seem to work. Here is some of the trials.
def render(assigns) do
~F"""
<div x-data="{message: 'hello world'}" x-text="message" />
"""
end
def render(assigns) do
~F"""
<div x-data={"{message: 'hello world'}"} x-text="message" />
"""
end
def render(assigns) do
~F"""
<div x-data={
"""
{message: 'hello world'}\
"""}
x-text="message" />
"""
end
But none of these work. Either nothing gets displayed or I get some compilation errors. Anyone had any luck with this?
First Post!
cmo
This sort of thing works:
<div
x-data="{
open: false,
toggle() {
if (this.open) { return this.close() }
this.open = true
},
close(focusAfter) {
if (! this.open) return
this.open = false
focusAfter && focusAfter.focus()
}
}"
x-on:keydown.escape.prevent.stop="close($refs.button)"
x-on:focusin.window="! $refs.panel.contains($event.target) && close()"
x-id="['dropdown-button']"
class="relative"
>
x-data={"{ on: #{@on} }"}
If you’re using Alpine to change the DOM, you need phx-update="ignore" and an id on the element.
At times you’ll want/need to use opts={"x-some-alpine-thing": "something"} for components.
<ButtonSmall click="reset_to_defaults" color="gray" opts={"x-on:click": "open = false"}>
Reset to defaults
</ButtonSmall>
Where you have an opts prop
@doc "Additional attributes to add onto the generated element"
prop opts, :keyword, default: []
def render(assigns) do
~F"""
<button ... {...@opts}>
<#slot />
</button>
"""
end
Popular in Questions
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it.
I’m very interested in Elixir,...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir
iex(2)...
New
Other popular topics
can someone please explain to me how Enum.reduce works with maps
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New







