remove deprecations, remove header when scrolling
This commit is contained in:
@@ -1,21 +1,86 @@
|
||||
<script setup>
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
import { ref, computed, onMounted, onUnmounted } from 'vue'
|
||||
import { RouterLink, useRouter, useRoute } from 'vue-router'
|
||||
import { useFeeds, logout as logoutSession } from '@/composables/useFeeds'
|
||||
import Modal from './modal/AddUrl.vue'
|
||||
|
||||
const router = useRouter()
|
||||
const route = useRoute()
|
||||
const { sync, showModal, viewMode, toggleViewMode, layout, toggleLayout, markAllRead, feeds } = useFeeds()
|
||||
const { sync, showModal, viewMode, toggleViewMode, layout, toggleLayout, markAllRead, feeds, lastProgrammaticScroll } = useFeeds()
|
||||
|
||||
const headerRef = ref(null)
|
||||
|
||||
// Scroll-driven show/hide: the header slides out of view on scroll-down and
|
||||
// back in on scroll-up. This is show/hide via `transform` (not the old
|
||||
// resize behaviour) — the header is position:fixed, so translating it never
|
||||
// reflows content, and the app's programmatic scrolls resolve to sensible
|
||||
// states: scrollTo(0, 0) lands near the top → shown; the list-view
|
||||
// read-correction scrollBy moves only a few px → stays under the threshold.
|
||||
const hidden = ref(false)
|
||||
const REVEAL_THRESHOLD = 12 // px of accumulated travel before toggling
|
||||
// When the feed list mutates itself (read-correction scrollBy + array-splice
|
||||
// scroll anchoring) the page jumps *upward* without user intent. For this long
|
||||
// after such a jump we gate only the reveal direction, so the jump can't pop
|
||||
// the header back into view mid-read. Hiding stays allowed the whole time
|
||||
// (the jump never scrolls down), so scrolling down still hides normally even
|
||||
// while articles are being marked read. See lastProgrammaticScroll in useFeeds.
|
||||
const PROGRAMMATIC_SUPPRESS_MS = 300
|
||||
let lastY = 0
|
||||
let accumulated = 0
|
||||
|
||||
function onScroll() {
|
||||
const y = Math.max(0, window.scrollY)
|
||||
const headerH = headerRef.value?.offsetHeight ?? 0
|
||||
|
||||
// Always reveal near the very top, and keep it visible while the menu is
|
||||
// open (the dropdown is anchored to the header, so hiding it would slide the
|
||||
// open menu off-screen).
|
||||
if (y <= headerH || menuOpen.value) {
|
||||
hidden.value = false
|
||||
accumulated = 0
|
||||
lastY = y
|
||||
return
|
||||
}
|
||||
|
||||
const delta = y - lastY
|
||||
lastY = y
|
||||
// Reset the accumulator whenever direction flips, so the threshold is
|
||||
// measured from the last turning point (not from page load).
|
||||
if ((delta > 0) !== (accumulated > 0)) accumulated = 0
|
||||
accumulated += delta
|
||||
|
||||
// Hiding (scroll-down) is always allowed. Revealing (scroll-up) is gated for
|
||||
// a short window after a programmatic list update, whose induced jump is
|
||||
// upward and would otherwise pop the header back into view mid-read. A
|
||||
// genuine scroll-up reveals once the window has elapsed.
|
||||
const afterProgrammatic = performance.now() - lastProgrammaticScroll.value < PROGRAMMATIC_SUPPRESS_MS
|
||||
if (accumulated > REVEAL_THRESHOLD) hidden.value = true // scrolling down
|
||||
else if (accumulated < -REVEAL_THRESHOLD && !afterProgrammatic) hidden.value = false // scrolling up
|
||||
}
|
||||
|
||||
let ticking = false
|
||||
function onScrollRaf() {
|
||||
if (ticking) return
|
||||
ticking = true
|
||||
requestAnimationFrame(() => {
|
||||
onScroll()
|
||||
ticking = false
|
||||
})
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
// Drives #app's padding-top / RssFeeds' scroll-margin-top so content below
|
||||
// the fixed header isn't hidden behind it at scroll position 0. The header is
|
||||
// a fixed size, so this is measured once on mount and never changes.
|
||||
const h = headerRef.value?.getBoundingClientRect().height ?? 0
|
||||
document.documentElement.style.setProperty('--app-nav-height', `${h}px`)
|
||||
|
||||
lastY = Math.max(0, window.scrollY)
|
||||
window.addEventListener('scroll', onScrollRaf, { passive: true })
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
window.removeEventListener('scroll', onScrollRaf)
|
||||
})
|
||||
|
||||
const onFeedsPage = computed(() => route.path === '/feeds')
|
||||
@@ -65,7 +130,7 @@ function handleToggleLayout() {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<header ref="headerRef" class="app-nav">
|
||||
<header ref="headerRef" class="app-nav" :class="{ 'app-nav--hidden': hidden }">
|
||||
<div class="app-nav__wrapper">
|
||||
<span class="app-nav__title">RSS Reader<span v-if="unreadCount" class="app-nav__unread"> ({{ unreadCount }})</span></span>
|
||||
<button
|
||||
@@ -127,6 +192,12 @@ function handleToggleLayout() {
|
||||
z-index: 20;
|
||||
background: var(--color-background);
|
||||
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
|
||||
transition: transform 0.25s ease;
|
||||
will-change: transform;
|
||||
}
|
||||
|
||||
.app-nav--hidden {
|
||||
transform: translateY(-100%);
|
||||
}
|
||||
|
||||
.app-nav__wrapper {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { mount, flushPromises } from '@vue/test-utils'
|
||||
import { nextTick } from 'vue'
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import axios from 'axios'
|
||||
import AppNav from '../AppNav.vue'
|
||||
@@ -210,6 +211,109 @@ describe('AppNav', () => {
|
||||
expect(wrapper.find('.app-nav__unread').exists()).toBe(false)
|
||||
})
|
||||
|
||||
describe('scroll-driven show/hide', () => {
|
||||
// The scroll handler is rAF-throttled; run rAF synchronously so a single
|
||||
// dispatched scroll event resolves before we assert. Per the CLAUDE.md
|
||||
// Vitest gotcha, avoid bare fake timers here — they'd clobber this stub.
|
||||
beforeEach(() => {
|
||||
// Reset scroll position so each mount's lastY baseline starts at 0.
|
||||
Object.defineProperty(window, 'scrollY', { value: 0, configurable: true, writable: true })
|
||||
vi.stubGlobal('requestAnimationFrame', (cb) => { cb(); return 0 })
|
||||
// offsetHeight is 0 in jsdom; give the header a real height so the
|
||||
// "near the top" guard (scrollY <= headerH) has something to compare to.
|
||||
vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(50)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
function scrollTo(y) {
|
||||
Object.defineProperty(window, 'scrollY', { value: y, configurable: true, writable: true })
|
||||
window.dispatchEvent(new Event('scroll'))
|
||||
}
|
||||
|
||||
it('hides the header when scrolling down past the threshold', async () => {
|
||||
const wrapper = mountNav()
|
||||
scrollTo(200)
|
||||
await nextTick()
|
||||
|
||||
expect(wrapper.find('header').classes()).toContain('app-nav--hidden')
|
||||
})
|
||||
|
||||
it('reveals the header again when scrolling back up past the threshold', async () => {
|
||||
const wrapper = mountNav()
|
||||
scrollTo(200)
|
||||
await nextTick()
|
||||
expect(wrapper.find('header').classes()).toContain('app-nav--hidden')
|
||||
|
||||
scrollTo(150)
|
||||
await nextTick()
|
||||
|
||||
expect(wrapper.find('header').classes()).not.toContain('app-nav--hidden')
|
||||
})
|
||||
|
||||
it('always shows the header near the top of the page', async () => {
|
||||
const wrapper = mountNav()
|
||||
scrollTo(400)
|
||||
await nextTick()
|
||||
expect(wrapper.find('header').classes()).toContain('app-nav--hidden')
|
||||
|
||||
// Back within the header's own height of the top → always revealed.
|
||||
scrollTo(10)
|
||||
await nextTick()
|
||||
|
||||
expect(wrapper.find('header').classes()).not.toContain('app-nav--hidden')
|
||||
})
|
||||
|
||||
it('does not let a programmatic upward jump reveal the header mid-read', async () => {
|
||||
const { markProgrammaticScroll } = useFeeds()
|
||||
const nowSpy = vi.spyOn(performance, 'now').mockReturnValue(1000)
|
||||
const wrapper = mountNav()
|
||||
|
||||
// Hide it first via a normal scroll-down (no programmatic flag active).
|
||||
scrollTo(400)
|
||||
await nextTick()
|
||||
expect(wrapper.find('header').classes()).toContain('app-nav--hidden')
|
||||
|
||||
// A read-correction flags a programmatic scroll, then the page jumps
|
||||
// upward. Within the window that upward jump must NOT reveal the header.
|
||||
markProgrammaticScroll() // records lastProgrammaticScroll = 1000
|
||||
nowSpy.mockReturnValue(1100) // 100ms later — inside the 300ms window
|
||||
scrollTo(200)
|
||||
await nextTick()
|
||||
expect(wrapper.find('header').classes()).toContain('app-nav--hidden')
|
||||
|
||||
// Still allows hiding on scroll-down even while the flag is active.
|
||||
scrollTo(500)
|
||||
await nextTick()
|
||||
expect(wrapper.find('header').classes()).toContain('app-nav--hidden')
|
||||
|
||||
// Once the window elapses, a genuine scroll-up reveals it again.
|
||||
nowSpy.mockReturnValue(1500) // 500ms after the flag — outside the window
|
||||
scrollTo(450)
|
||||
await nextTick()
|
||||
expect(wrapper.find('header').classes()).not.toContain('app-nav--hidden')
|
||||
})
|
||||
|
||||
it('does not toggle on sub-threshold jitter', async () => {
|
||||
const wrapper = mountNav()
|
||||
// Start well below the top so the "near the top" guard doesn't apply.
|
||||
scrollTo(300)
|
||||
await nextTick()
|
||||
// Reveal first so we're testing that small moves don't hide it.
|
||||
scrollTo(260)
|
||||
await nextTick()
|
||||
expect(wrapper.find('header').classes()).not.toContain('app-nav--hidden')
|
||||
|
||||
scrollTo(268) // +8px, under the 12px threshold
|
||||
await nextTick()
|
||||
|
||||
expect(wrapper.find('header').classes()).not.toContain('app-nav--hidden')
|
||||
})
|
||||
})
|
||||
|
||||
it('does not mark articles as read when the confirmation is dismissed', async () => {
|
||||
const { feeds } = useFeeds()
|
||||
feeds.value = [
|
||||
|
||||
@@ -15,6 +15,16 @@ const layout = ref(localStorage.getItem('layout') || 'list') // 'list' | 'cards'
|
||||
let observer; // Declare observer outside the setup function
|
||||
let initialLoad = false
|
||||
|
||||
// Timestamp (performance.now()) of the most recent programmatic scroll / list
|
||||
// mutation that moves the page without user intent — currently the list-view
|
||||
// read-correction below. AppNav's auto-hide handler resyncs its scroll baseline
|
||||
// (instead of treating the induced jump as a user scroll) for a short window
|
||||
// after this, so removing read articles can't pop the header in/out mid-read.
|
||||
const lastProgrammaticScroll = ref(0)
|
||||
function markProgrammaticScroll() {
|
||||
lastProgrammaticScroll.value = performance.now()
|
||||
}
|
||||
|
||||
export function authHeaders() {
|
||||
return {
|
||||
headers: {
|
||||
@@ -247,6 +257,9 @@ function handleIntersection(entries, topbarHeight = 0) {
|
||||
observer = null
|
||||
}
|
||||
|
||||
// Both the array splice (via scroll anchoring) and the scrollBy correction
|
||||
// below move the page — flag it so AppNav's header auto-hide ignores the jump.
|
||||
markProgrammaticScroll()
|
||||
const readIds = new Set(readFeeds.map(feed => feed.id))
|
||||
feeds.value = feeds.value.filter(feed => !readIds.has(feed.id))
|
||||
|
||||
@@ -265,6 +278,7 @@ function handleIntersection(entries, topbarHeight = 0) {
|
||||
if (first) {
|
||||
const top = first.getBoundingClientRect().top
|
||||
if (top < topbarHeight) {
|
||||
markProgrammaticScroll()
|
||||
window.scrollBy(0, top - topbarHeight)
|
||||
}
|
||||
}
|
||||
@@ -389,5 +403,7 @@ export function useFeeds() {
|
||||
disconnectObserver,
|
||||
setInitialLoad,
|
||||
handleIntersection,
|
||||
lastProgrammaticScroll,
|
||||
markProgrammaticScroll,
|
||||
}
|
||||
}
|
||||
|
||||
+7
-17
@@ -31,23 +31,13 @@ const router = createRouter({
|
||||
|
||||
]
|
||||
})
|
||||
router.beforeEach((to, from, next) => {
|
||||
if (to.meta.requiresAuth) {
|
||||
let isAuthenticated = false;
|
||||
if (localStorage.getItem("user-token") != null){
|
||||
isAuthenticated = true;
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
// Redirect to the login page
|
||||
next('/login');
|
||||
} else {
|
||||
// Proceed to the protected route
|
||||
next();
|
||||
}
|
||||
} else {
|
||||
// For routes that don't require authentication, proceed without checking
|
||||
next();
|
||||
router.beforeEach((to) => {
|
||||
const isAuthenticated = localStorage.getItem("user-token") != null;
|
||||
// Redirect unauthenticated users hitting a protected route to login;
|
||||
// returning a value (instead of the deprecated next() callback) is the
|
||||
// modern vue-router guard API. Returning nothing lets navigation proceed.
|
||||
if (to.meta.requiresAuth && !isAuthenticated) {
|
||||
return '/login';
|
||||
}
|
||||
});
|
||||
export default router
|
||||
|
||||
Reference in New Issue
Block a user