Repository navigation
Smart header: hide on scroll down, show on scroll up #2879
Unanswered
bridgeyuwa
asked this question in
Feature Requests
Replies: 1 comment
|
Hey @bridgeyuwa! +1 on this. Until there's a built-in prop, this works today with a bit of Alpine on top of the existing <flux:header sticky
x-data="{
state: 'top',
lastY: 0,
init() { this.reset() },
reset() {
this.lastY = window.scrollY
this.state = this.lastY < this.$el.offsetHeight ? 'top' : 'pinned'
},
onScroll() {
let y = window.scrollY
if (y < this.$el.offsetHeight) return this.reset()
if (Math.abs(y - this.lastY) < 10) return
let busy = this.$el.querySelector(':focus-visible, [data-open]')
this.state = y > this.lastY && ! busy ? 'unpinned' : 'pinned'
this.lastY = y
},
}"
x-bind:data-state="state"
x-on:scroll.window="onScroll()"
x-on:focusin="if (state === 'unpinned') state = 'pinned'"
x-on:livewire:navigated.document="reset()"
class="transition-transform duration-200 motion-reduce:transition-none data-[state=unpinned]:-translate-y-full data-[state=pinned]:shadow-sm"
>
{{-- brand, navbar, profile dropdown... --}}
</flux:header>A few notes:
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
flux:headeris currently either static orsticky. A sticky header always takes up screen space, which is a real cost on mobile and on content-heavy pages like articles, docs and dashboards.I’d like to suggest a “smart” header: it slides out of view when the user scrolls down and comes back as soon as they scroll up. It’s a common pattern, and it would be a good fit for Flux’s layout components.
Something like this:
A few things I think it should handle:
prefers-reduced-motion.wire:navigate.data-state="top | pinned | unpinned") so it can be styled with Tailwind variants.Right now everyone has to build this by hand with Alpine, and the edge cases above are easy to get wrong.
One thing I’m unsure about is how it should interact with a sticky
flux:sidebarorflux:aside. If the header hides, should they move up to fill the space?This will be a good fit for Flux, and I believe a simple
smartprop would be proper.Thanks!
All reactions