Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save Menu State? #2

Closed
prodigerati opened this issue Jan 31, 2021 · 2 comments
Closed

Save Menu State? #2

prodigerati opened this issue Jan 31, 2021 · 2 comments

Comments

@prodigerati
Copy link

First off, thanks for sharing this clean design. :)

I'm new to tailwindcss and alpinejs so I apologize if these questions are outside the scope of the code shared here.

  1. How do I save the isSidebarOpen state?

    • I would like when the user clicks to open or close the menu the option survives a reload.
  2. Upon reload the menu Flashes open then to a closed state each time the page is reloaded.

Note: I am using Rails 6.1, Turbolinks, webpack

Any assistance with these 2 issues would be very helpful.

Thank you,

@Kamona-WD
Copy link
Owner

Kamona-WD commented Jan 31, 2021

I am sorry for my late.

I apologize if these questions are outside the scope of the code shared here.

No need to apologize.

How do I save the isSidebarOpen state?

You can use localstorage to persist user settings something like

const setup = () => {
  function getSidebarStateFromLocalStorage() {
    // if it already there, use it
    if (window.localStorage.getItem('isSidebarOpen')) {
      return JSON.parse(window.localStorage.getItem('isSidebarOpen'))
    }

    // else return the initial state you want
    return (
     false
    )
  }

  function setSidebarStateToLocalStorage(value) {
    window.localStorage.setItem('isSidebarOpen', value)
  }

return {
      loading: true,
      isSidebarOpen: getSidebarStateFromLocalStorage(),
      toggleSidbarMenu() {
        this.isSidebarOpen = !this.isSidebarOpen
        setSidebarStateToLocalStorage(this.isSidebarOpen)
      },
      isSettingsPanelOpen: false,
      isSearchBoxOpen: false,
  }
}

i hope this helps and this what you want.

Upon reload the menu Flashes open then to a closed state each time the page is reloaded

quick fix for that using x-cloak x-cloak reference

in html

<aside
    x-cloak
    x-transition:enter="transition transform duration-300"
    x-transition:enter-start="-translate-x-full opacity-30  ease-in"
    x-transition:enter-end="translate-x-0 opacity-100 ease-out"
    ...

in css

[x-cloak] { display: none; }

see this demo on code pen

I hope this helps you, if not or if there is any other problem, ask in any time.

Sorry if there something wrong in my English, it's not my first language

@prodigerati
Copy link
Author

This is perfect, thank you for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants