duncanphillips

duncanphillips

Keeping a side navigation components open/close state

Hi,

I have a site layout which has a side navigation which is collapsible (i.e. it has a button to make wider/thinner). One thing that I can’t quite figure out the best approach for is how to keep the open/close state persisted between page transitions. So if the default is open, and I navigate to another page, it will always go back to the open state. I was thinking about maybe using a JS hook and local storage, but wanted to see if others have solved this more elegantly?

Side note on the open/close implementation:
I’m using a data attribute for the open/close state, in conjunction with some CSS rules.

  <aside
    id={@id}
    class="w-20 data-[state=open]:w-[250px] transition-[width] ..."
    data-state="open"
    phx-update="ignore"
  >
    ...
  </aside>

thanks

Marked As Solved

codeanpeace

codeanpeace

For persisting when navigating across liveviews or live sessions, you could live render the sidebar as its own “sticky” liveview.

Another option is to append that sidebar state to the url as parameters, but not sure how elegant that is for this use case.

Also Liked

rhcarvalho

rhcarvalho

The LiveView, and particularly the sticky LV, is the abstraction for compartmentalizing state (and events).
If you want to keep the state on the server, then sticky LV is a good option, I had a positive experience implementing a similar navigation component after making a similar question myself.

The phx-update="ignore" and local/session storage direction is if you prefer to keep the state in the browser only.

Both options are viable. State on the server opens up some creative possibilities, like syncing the state across tabs, easily “studying” how users use the UI, remote-controlling the UI, conditional events based on UI state, etc.

duncanphillips

duncanphillips

Thanks for the input, seems like I should take another look at the sticky liveview :slight_smile:

rhcarvalho

rhcarvalho

Coming back to this, I wanted to share that there are other options worth exploring that don’t involve a separate (sticky) LiveView.

For instance, one can save the open/close state on the client using localStorage and then pass it as params to the LiveSocket, see the example in the docs at Phoenix.LiveView.get_connect_params/1.

For a collection of this and other techniques, there’s a talk from ElixirConf EU 2025: Phoenix LiveView: Mastering Reconnects for a Seamless User Experience -Giovanni Francischelli.

Where Next?

Popular in Questions Top

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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
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
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement