stryrckt

stryrckt

Support for AlpineJS in LiveView was added in 0.13.3 and it works fabulously. I just wrote a blog article about it and plan another one soon on transitions with AlpineJS and Tailwind CSS.

Showing Posts 1 to 10

webuhu

webuhu

Thanks for your really good read.
I’m always very skeptical in using any framework (:wink: it took me a long time until I’ve settled to used Phoenix).
Anyway Alpine.js could by a candidate to use.

But I’ve one question:
What’s the real benefit in using spruce for state management?
Isn’t the usage of localStorage not just enough or even better.
Sprouce seems to store the state in window - so it’s always just available in the current browser tab.
With localStorage I could manage to sync state across tabs, right?

I just can’t see the benefit in using, but please convince me of the opposite.

stryrckt

stryrckt OP

You can certainly use localStorage from Alpine, as this article demonstrates: Sync Alpine.js x-data to localStorage/sessionStorage · Code with Hugo. So if you are skeptical about pulling in another framework, it is probably good enough.

For me, Spruce is nice because it integrates well with Alpine, allows you to watch for changes in state, and it works with x-model.

webuhu

webuhu

Hmm, I need to prototype and check out x-model. Thanks.

stryrckt

stryrckt OP

I just published a follow-on article showing how to use AlpineJS and Tailwind CSS to create LiveView modals.

sreyansjain

sreyansjain

I was trying to integrate Apline and Phoenix LiveView and your blog posts have been of great help. Thank you for sharing your knowledge with us.

I have a question, what if we have multiple components on a page that want to register for Alpine Hook.

For ex

Hooks.Counter = {
  mounted() {
    window.counterHook = this
  },
  decrement() {
    this.pushEvent('decrement', {})
  },
  increment(selector) {
    this.pushEventTo(selector, 'increment', {})
  }
}

How do you create uniqueness above?

So i tried to do CustomEvent dispatch and get the push_element. This works -

<div x-data @push-element-updated="console.log('captured custom event')">
    <div id="test_tb_<%= @id %>" phx-hook="EscapeHook">
  </div>
Hooks.EscapeHook = {
  mounted() {
    console.log("mounted escape hook")
    let main_this = this;
    var event = new CustomEvent('push-element-updated', {
      bubbles: true,
      detail: {
        push_element: main_this
      }
    });
    console.log("event dispatched =======")
    this.el.dispatchEvent(event);
  }
}

But the above doesn’t work when the EscapeHook element is put inside a conditional. For ex -

<%= if @is_being_edited do %>
<div x-data @push-element-updated="console.log('captured custom event')">
    <div id="test_tb_<%= @id %>" phx-hook="EscapeHook">
  </div>
<% end %>

How can we make it work inside a conditional?

Thank you once again for taking the time to enlighten us all.

stryrckt

stryrckt OP

Nothing is obviously wrong to me. I frequently use Alpine in conditionally rendered markup. I have an example of that in my article on modals. See the alternative modal implementation section.

sfusato

sfusato

Try adding an id on the container div inside the conditional. See if that makes any change.

mfilej

mfilej

I’m really excited to try this out. I just tried adding AlpineJS to a LiveView project (following this post) and the browser console is now reporting this issue:

alpine.js:2252 Uncaught ReferenceError: regeneratorRuntime is not defined
    at eval (alpine.js:2252)
    at eval (alpine.js:2293)
    at eval (alpine.js:34)
    at eval (alpine.js:39)
    at Object../node_modules/alpinejs/dist/alpine.js (app.js:142)
    at __webpack_require__ (app.js:20)
    at eval (app.js:2)
    at Module../js/app.js (app.js:131)
    at __webpack_require__ (app.js:20)
    at app.js:84

And I couldn’t google out anything that would apply (I can see lots of issues related to babel).

I npm install-ed AlpineJS 2.8.2.

Sebb

Sebb

so you did this?

assets$ git diff HEAD js/app.js
 import "../css/app.css"
+import Alpine from "alpinejs";
 ...
-let liveSocket = new LiveSocket("/live", Socket, { params: { _csrf_token: csrfToken } })
+let liveSocket = new LiveSocket("/live", Socket, {
+   params: { _csrf_token: csrfToken },
+   dom: {
+   	onBeforeElUpdated(from, to) {
+       	if (from.__x) {
+           	Alpine.clone(from.__x, to);
+            }
+       },
+   },
+}
+)

When I did this I had trouble with latest node, so I downgraded to node 14.
With latest phoenix (not really, 1.5.7 actually), node 14 and AlpineJS 2.8.1 (2.82 should not make a difference) it just works (under ubuntu 20.4). All I got to do is

assets$ npm install alpinejs

And the patch above.
Obviously you have to start from a working phx.new

mfilej

mfilej

Yes, all of the above is correct. Except I’m on node v10.24.1 and Elixir 1.5.8.

Where Next? Top

Trending in Blog Posts Top

bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
pckrishnadas88
Hey everyone! :waving_hand: I’ve published Part 7 of the Building Distributed Systems in Elixir series, where we build core distributed ...
New
zorn
An educational side project in Elixir, Phoenix, and Tauri. I share what I learned while wiring Automerge into the BEAM, including how I s...
New
nathanl
Process labels are useful for visualization and debugging. Here’s why you should use them.
New
abreujp
New article: Elixir Project Structure — From mix new to a Growing Codebase I’ve published a new article in my Elixir learning series on d...
New
andreasronge
What happens if you design tools for LLMs instead of letting LLM use human tools ? Wrote a blog on why and what that enables. As I see ...
New
mudasobwa
Somewhere, right now, a senior engineer is on the verge of a nervous breakdown because his company will not let him switch from Claude to...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews