threeaccents

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

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

Where Next?

Popular in Questions Top

LegitStack
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
quazar
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
alice
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
fireproofsocks
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
baxterw3b
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
hpopp
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
belgoros
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
siddhant3030
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
idi527
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 Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
axelson
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...
239 45766 226
New
_russellb
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
lucidguppy
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
AstonJ
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
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
romenigld
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

We're in Beta

About us Mission Statement