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.

larshei

larshei

Glad this was added, making toggle menus and content was not much fun before :smiley:

Where Next?

Popular in Blog Posts Top

danschultzer
How to set up the Content Security Policy header with Phoenix LiveView and support inline styles and scripts with CSP nonces.
New
New
New
New
New
aymanosman
The desire to produce structured logs is common. In this article, I will survey the major approaches one could take to achieve this goal ...
New
adolfont
Hi, You all probably know that José Valim is Brazilian and that his native language is Portuguese. I, Herminio Torres, Matheus Pesanha a...
New
shanesveller
Introduction and first content post in my new series about running Elixir Phoenix apps on Kubernetes are live! I’m describing an opiniona...
New
brainlid
Dialyzer is a tool that you’ve probably heard about in the Elixir community. You may have even used it. However, adding Dialyzer to an ex...
New
AstonJ
I think <span class="hashtag-icon-placeholder"></span>liveview is going to be a big topic in the foreseeable future, so wondering whether...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement