How to keep/reuse DOM elements across routes?
So, when using
solid-start
with routes, what is the most idiosyncratic way of keeping one element of the DOM the same, while the user changes pages?
I.e. like youtube or spotify, where one element in the DOM is the <audio>/<video>
that stays on screen no matter which page I load9 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
thank you, I will look into that. I hadn't seen it yet / read over it since it looked more like an internal feature
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Hi, if you want the element to retain state and stay in mostly the same place, you can use the
<Outlet />
component in solid start, where you make a file shadowing a directory containing the component that you want to share, and the Outlet
component, which is where all child routes will be rendered: https://start.solidjs.com/api/OutletSolidStart Beta Docuentation
SolidStart Beta Documentation
Early release documentation and resources for SolidStart Beta
Mhmm, so the outlet is a placeholder where all the children in a directory sit, like they say for path
site.com/users
you would have routes/users.tsx
:
In my case it seems like my player-component will be rendered across all paths, so I'm doing:
[root.tsx]
It feels a little like I'm abusing root.tsx
? But maybe I don't need Outlet
at all?
Mhmm, After working on it for a while, I don't think Outlet
fits the bill, since it renders the whole path under the element.
In my case the element is a small widget like player, that is presented in different spots of the larger UI depending on the route.Huh, and you want to use the exact same DOM nodes even when the route changes?
well, the player may be playing an audio file, so it should continue doing that while the user browses the site
like spotify or music.youtube.com where the playe rwidget is always somehwere
Maybe you could have it "floating", where only the css changes between routes, but that's probably not ideal
the createRoot solution looks like the best bet
mhmm yeah, I do change the css for responsive mode, i.e. when the screen gets small it floats above content on the bottom. When screen is big it's included in the normal UI
So in #general @fabiospampinato pointed out I could do
And then export that player and import it everywhere where it's needed like so