brainlid

brainlid

Blog Post: My Favorite new LiveView Feature: JS.toggle_class/1

Phoenix LiveView v0.20.4 offers a hidden gem with its handy little JS.toggle_class/1 feature. This addition simplifies toggling classes for developers, allowing for effortless visibility changes, multi-element toggles, and even animated transitions—all without needing additional tools. Check out the examples showcasing basic visibility toggles, simultaneous class manipulations, dynamic list item interactions, reusable click events, and more! Could this become your new favorite LiveView feature?

Most Liked

rhcarvalho

rhcarvalho

@brainlid Thanks for highlighting this new function with so many examples :slight_smile:

Two ingenious use cases:

  • replace classes by mentioning them both in the initial state and the toggle:
<.icon
  name="hero-bars-3"
  class="lg:hidden cursor-pointer"
  phx-click={JS.toggle_class("hero-bars-3 hero-x-mark")}
/>
  • simultaneously hide one element and show another (also some form of “replace”) by having multiple targets with different initial state. This one borrowed from the blog post:
<div
  id="example-2"
  phx-click={
    JS.toggle_class("hidden",
      to: ["#example-2 > .title-content", "#example-2 > .expanded-content"]
    )
  }
  class="px-4 py-3 cursor-pointer bg-purple-100 rounded-md border border-purple-300 text-purple-600"
>
  <div class="title-content font-medium">Click to reveal the text</div>
  <div class="expanded-content hidden mt-2 text-purple-800 text-md font-light">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </div>
</div>
SatelliteMan

SatelliteMan

Great tutorial!

There needs to be more Live View JS tutorials.

Wish I read this a month ago when I was building out modals and working through live view JS.

brainlid

brainlid

I agree. The JS features are powerfully but still hard to get your heads around.

Where Next?

Popular in Blog Posts Top

lawik
One of the Erlang ecosystem’s spiciest nerd snipes are hot code updates. Because it can do it. In ways that almost no other runtime can.
New
mudasobwa
Just blogged on the general approach to creating easily extendable applications with plugin support.
New
paulanthonywilson
Following up on the previous post on using UDP multicasting to broadcast and detect peers on a network, I create a registry of those peer...
New
mudasobwa
Wrote some rant on Business Process Driven Development with Finite Automata (and Finitomata library in particular.)
New
victorbjorklund
I’m showing you how you can customise the phx.new generator to give you a new Phoenix project the way YOU want it. In this post I show yo...
New
JKWA
I didn’t have room for this in my book, Advanced Functional Programming with Elixir , but I still thought it was worth sharing on my blog.
New
AstonJ
Update: How to use the blogs section You can post in one of the Official Blog Posts threads (like this one), or, via Devtalk and a new t...
New
axelson
I talk about how I really like to use runtime configuration and discuss some common pitfalls of configuration in Elixir.
New
paulanthonywilson
This is an overview of different ways to try and kill an OTP process (in Elixir) and the behaviour to expect when that happens. I know th...
New
sheharyarn
In this article, I talk about the Elixir library Delta we at Slab just open-sourced, its various features including support for Operation...
New

Other popular topics Top

New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement