KevinConti
I’m working with LiveView.JS for the first time. It’s one of the features that’s brought me back to LiveView on the frontend, and I feel like it was a critical piece that was missing for the community in earlier versions of the websocket-based paradigm.
By default, Phoenix comes installed with TailwindCSS. In Tailwind, transitions often occur via CSS classes. Take the following example:
<!-- When a menu is 'opened', it should receive the "block" class. When 'closed', it should receive the "hidden" class -->
<div id="mobile-menu" class="block sm:hidden">...</div>
<div id="desktop-menu" class="hidden sm:block">...</div>
This is a very common occurrence in Tailwind - transitions are almost exclusively driven via class addition and subtraction. When I went to try and apply this pattern with LiveView.JS, I was surprised to not see an obvious way to approach this.
The challenge with JS.toggle() is that the display property is directly applied via the style=display: attribute, where tailwind classes are invalid. This causes problems in the reactive example above, because the mobile menu could be toggled to be in the open state (style= display:block), and if the window is resized, this style will override the existing reactive behavior defined in tailwind (sm:hidden), causing both menus to be shown at once!
Of course, you could avoid this by using the JS.addClass and JS.removeClass functions, but that requires you to know the current state of the element to determine which function to call… meaning we’d need to keep state on the server, exactly what we are trying to avoid for something like a menu’s visibility!
I looked over the other functions available in LiveView.JS, but none seem to handle what I imagine must be an incredibly common use-case.
So, please help me out LiveView devs - am I missing something obvious? All I want is the ability to toggle two sets of arbitrary tailwind classes and let the state be contained entirely on the client via JavaScript. If I can do that, Tailwind will take care of everything else.
Thanks for your suggestions!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
JohnnyCurran
Your example seems to be empty - was it a copy/paste that didn’t get into the post?
Also, have you seen JS.transition? It supports tailwind classes Phoenix.LiveView.JS — Phoenix LiveView v1.2.5
al2o3cr
The post originally tried to use single backquotes on separate lines to quote a whole block of code, which resulted in the code not getting quoted. I’ve replaced them with triple-backquotes
```which work as the author intended.chrismccord
LiveBeats uses tailwind and toggles menu and such just fine from TailwindUI. Have you looked there?:
check the
<.dropdown>in core components and theshow_dropdownandhide_dropdownfunctions:https://github.com/fly-apps/live_beats/blob/master/lib/live_beats_web/components/core_components.ex#L318-L336
KevinConti
Thanks, it was in the mod queue so I didn’t get a chance to review for formatting beforehand
KevinConti
Hey Chris, thanks a bunch for the reply (and for your work)!
I took a brief look, it seems to me like your approach to this problem is as follows:
This is clever and seems like a viable approach, but where I think it gets a bit ugly is when you have the desire for a single button to control both the “show” and “hide” behavior of a component. In that case, you’d need to end up having two buttons with most things duplicated, and two methods - something like this:
But I do feel that a more smooth developer experience would be something like this:
I’m very open to being wrong about this, because what I’m suggesting is that LiveView.JS should manage the state of the classes for a component. But that said,
JS.toggleis already managing this state to determine if it should applydisplay: noneordisplay: block, so I imagine the implementation would be similar.What I’m trying to get at is this - as it stands, it looks like
JS.toggleis quite limited in use - it requires you to be okay with your component having its display overridden withdisplay: block | flex | inline. In most cases with Tailwind, you really want a specific tailwind class to be applied, and you definitely don’t want your reactive modifiers (sm:, md:, etc) to be overridden.Very curious as to your thoughts! Thanks again.
chrismccord
that’s what my linked dropdown component does:

The phx-click-away does the hide which you’ll need for a menu anyway, and the same button acts as show and hide because of that.
chrismccord
Also sounds like this is what you’re interested in: feat: toggle_classes (updated 2021/01/08) by nbw · Pull Request #1721 · phoenixframework/phoenix_live_view · GitHub
sezaru
Note sure if you meant the PR on itself or the workaround proposed in the comments. If the latter, that works for classes, but fails if what you are trying to toggle is something else (like attributes for example).
codeanpeace
For attributes, try swapping
JS.add_classandJS.remove_classinto @Nik’s workaround re-posted below.sezaru
I tried that, couldn’t make it work, did it work for you? If so I will try to revisit it and see if it was something that I messed up