-
Notifications
You must be signed in to change notification settings - Fork 11
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
Update elements outside of main / restore scroll position on popstate #15
Comments
@blimpmason hey 👋 thanks for the kind words. These are great questions! I've had to deal with both in the past as well. One I love the data-attribute idea, that's how I usually approach it. I made this to help with those situations. Use in tandem with operator, they make for a pretty powerful SPA system. Two I don't think that it would be possible to use that in tandem with operator at the moment, but I could be wrong. The issue would be that operator doesn't emit a Perhaps we should add a Thoughts? |
Hey @estrattonbailey thanks for the quick response!
I took a quick look at that and it looks like state doesn't return the full HTML of the page you're navigating to, so I'm not clear on how I could pull the body classes and inject them into the page before rendering. This would work great for updating a nav as you can simply check the link path against the state location to match the active link element — but there too it could be easier to just update the classes set by liquid logic from the destination page. The PJAX library I've used most often in the past is barba.js and since it returns the full raw HTML it's pretty easy to parse to update classes or even fully replace components outside the main wrapper.
I've been exploring this tool as well as I convert a Shopify theme over to Slater. I'm not super clear on how to take full advantage but I haven't really spent the time to dig too deep yet. Basically I have a bunch of CommonJS modules that will need to be reinitialized after page transitions, and I'm hoping that library can help with that as well via mount/unmount without rewriting too much code. Many of those modules also have simple checks that look for elements in the DOM and only continue if they exist, otherwise return false. Could picoapp help with that logic as well?
I love the idea of adding both a popstate event and perhaps options for scroll restoration or transition disabling on popstate — moox/pjax library includes that as an option, and swup/swup library simply disables the transition on popstate (I believe). The option to disable transitions on popstate has an added advantage in Safari and any browsers that include their own built in popstate animations. It's a pretty glitchy experience when you swipe the page back for example, the olds page slides off screen, and then the old page fades out and the new page fades in. In any case I'm thinking that swup makes more sense if I can integrate it easily into Slater, which looks like it could be as easy as calling app.unmount() and app.mount() after page transitions. |
@blimpmason thanks for the clarification! Re: classes, totally understand where you're coming from now, that's a pretty common pattern in Shopify sites. As you've discovered, there's not way to parse the full DOM of the fetched page at the moment. Not sure what that would look like given the current API, but I'm open to suggestions! I'd say the easiest way to do it currently is to wrap each of your templates with an element that has your class attached (or as an attribute), parse that on the You could also use something like dcx and just manually set them instead of using the liquid tag. Something along these lines: import dcx from 'dcx'
const cx = dcx(document.body)
router.on('after', state => {
cx({
'template-index': /^\/$/.test(state.location),
'template-page': /^\/page/.test(state.location)
})
})
You betcha. At a base level, that's all it does. It also just adds a thin layer on top that allows for pub/sub events and state to be shared amongst registered components.
That's a great idea really, I can explore that! But yeah swup looks very cool! Had never seen this one before. I'm familiar with pjax and barba.js. And yeah, picoapp should integrate in basically the same way. As another option, I just found this library though the swup repo. Also never heard of that one. There's also this one and another that I can't seem to find right now, but will post if I do. |
Hi Eric and David - not sure if there's any more interest in this, but I could also see a need for setting body class if using operator with WordPress. What if we just add router.on('after', state => {
window.history.pushState({}, '', state.location)
document.body.className = state.document.body.className
document.title = state.title
}) |
Hey @estrattonbailey — I'm new to your open source toolset and found you via https://github.com/the-couch/slater/. I really appreciate all the work that went into these goodies! I just have a few questions about operator specifically:
main
element?Let me know if you have any thoughts!
The text was updated successfully, but these errors were encountered: