Skip to content

Persistent sidebar collapse using middleware and async-provider#170

Merged
brookslybrand merged 4 commits intomainfrom
brooks/persistent-sidebar-collapse
Jun 17, 2025
Merged

Persistent sidebar collapse using middleware and async-provider#170
brookslybrand merged 4 commits intomainfrom
brooks/persistent-sidebar-collapse

Conversation

@brookslybrand
Copy link
Contributor

@brookslybrand brookslybrand commented Jan 27, 2025

Persistent Sidebar Collapse

This features retains each user's open/collapse preferences for the sidebar using:

  • Cookies
  • Middleware
  • New context

Collapse states work with SSR (no flash of collapse/expand). Persisting the collapse state only works once JS has been loaded (progressively enhanced)

How does it work

  • middleware reads the value from the cookie (if it exists) and loads it into context
    • Middleware is also responsible for setting the cookie
    • Cookie lasts 1 week -- if a user hasn't been on the site in a bit, having everything open isn't a bad thing
  • The docs-layout route reads the context in the loader and passes it down as loaderData
    • All sections default to open if there's no value set
    • Only use the user's preferences on the main docs -- keeping track of dev, v6, and every other tag is kind of unnecessary
  • When the user toggles, they trigger an action. The action updates the context
  • middleware, after processing the request from the action, sets the new context to the cookie

Demo

persistent-collapse.mov

What else does this PR change

I got a little carried away and refactored the color scheme changer, just to match the structure of the code I added.

@brookslybrand brookslybrand force-pushed the brooks/persistent-sidebar-collapse branch from e7aa035 to 661f028 Compare March 5, 2025 22:29
@brookslybrand brookslybrand force-pushed the brooks/persistent-sidebar-collapse branch from ccf9f6a to 9a01a8e Compare June 17, 2025 14:39
@brookslybrand brookslybrand merged commit fb6d69e into main Jun 17, 2025
5 checks passed
@brookslybrand brookslybrand deleted the brooks/persistent-sidebar-collapse branch June 17, 2025 18:34
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

Successfully merging this pull request may close these issues.

2 participants