Keep the header at the top of the window on wider screens

The signed-in header is sticky from the md breakpoint up, so only the
page content scrolls; on phones and at high zoom it still scrolls with
the page, where a wrapped sticky header would take too much of the view.
A small layout script sets scroll-padding-top to the header's height
while it is sticky, so focused elements, anchors and the skip link
target land below it (WCAG 2.4.11).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-25 07:18:55 +02:00
co-authored by Claude Opus 5.5
parent 45dbb04344
commit 60cab4b871
3 changed files with 78 additions and 1 deletions
+5
View File
@@ -34,6 +34,11 @@
--color-primary-800: #312e81; --color-primary-800: #312e81;
} }
/* The sticky header's height (set by the layout), so focus and anchors land below it. */
html {
scroll-padding-top: var(--app-header-offset, 0px);
}
[x-cloak] { [x-cloak] {
display: none !important; display: none !important;
} }
+33 -1
View File
@@ -48,7 +48,8 @@
</a> </a>
@auth @auth
<header class="dark border-b border-zinc-700 bg-zinc-800 text-zinc-100"> {{-- Sticky from md up; on narrow screens (and 400 % zoom) the wrapped header would take too much of the view. --}}
<header id="app-header" class="dark border-b border-zinc-700 bg-zinc-800 text-zinc-100 md:sticky md:top-0 md:z-30">
<div class="mx-auto flex max-w-5xl flex-wrap items-center gap-x-6 gap-y-2 px-4 py-3 sm:px-6"> <div class="mx-auto flex max-w-5xl flex-wrap items-center gap-x-6 gap-y-2 px-4 py-3 sm:px-6">
<a href="{{ route('home') }}" wire:navigate class="rounded font-semibold text-zinc-200 hover:text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-400"> <a href="{{ route('home') }}" wire:navigate class="rounded font-semibold text-zinc-200 hover:text-white focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-400">
{{ app(\App\Modules\Shared\Application\UseCases\CompanyDetailsStore::class)->menuName() }} {{ app(\App\Modules\Shared\Application\UseCases\CompanyDetailsStore::class)->menuName() }}
@@ -74,6 +75,37 @@
{{ $slot }} {{ $slot }}
</main> </main>
{{--
While the header is sticky, scroll-padding-top keeps focused elements, anchors (#tasks) and the skip link
target from scrolling under it (WCAG 2.4.11). The offset follows the header's height as it wraps or resizes,
and is set again after wire:navigate swaps the page (data-navigate-once: the listeners are added only once).
--}}
<script data-navigate-once>
(() => {
let observer = null;
const trackHeader = () => {
observer?.disconnect();
const header = document.getElementById('app-header');
const update = () => {
const sticky = header !== null && getComputedStyle(header).position === 'sticky';
document.documentElement.style.setProperty('--app-header-offset', sticky ? header.offsetHeight + 'px' : '0px');
};
update();
if (header !== null && 'ResizeObserver' in window) {
observer = new ResizeObserver(update);
observer.observe(header);
}
};
trackHeader();
window.addEventListener('resize', trackHeader);
document.addEventListener('livewire:navigated', trackHeader);
})();
</script>
@livewireScripts @livewireScripts
</body> </body>
</html> </html>
+40
View File
@@ -0,0 +1,40 @@
<?php
use App\Models\User;
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$user = User::factory()->management()->create();
$this->actingAs($user);
$this->job = CustomerJobModel::factory()->create([
'customer_id' => CustomerModel::factory()->create()->id,
'responsible_user_id' => $user->id,
'status' => 'order',
'instructions' => str_repeat("Ohjeen rivi.\n", 60),
]);
$workType = WorkTypeModel::query()->where('name', 'videokuvaus')->sole();
TaskModel::factory()->count(12)->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id]);
});
it('keeps the header at the top of the window and focus below it on desktop', function () {
visit(route('jobs.show', $this->job->id))->on()->desktop()
->assertScript('getComputedStyle(document.getElementById("app-header")).position', 'sticky')
->assertScript('getComputedStyle(document.documentElement).scrollPaddingTop === document.getElementById("app-header").offsetHeight + "px"', true)
->assertScript('window.scrollTo(0, document.body.scrollHeight); window.scrollY > 0', true)
->assertScript('document.getElementById("app-header").getBoundingClientRect().top', 0)
// From the bottom of the page, focusing the tasks heading scrolls it back up: it must land below the header.
->assertScript('document.getElementById("tasks-heading").getBoundingClientRect().bottom < 0', true)
->assertScript('(() => { document.getElementById("tasks-heading").focus(); return document.getElementById("tasks-heading").getBoundingClientRect().top >= document.getElementById("app-header").getBoundingClientRect().bottom; })()', true);
});
it('lets the header scroll with the page on a phone', function () {
visit(route('jobs.show', $this->job->id))->on()->mobile()
->assertScript('getComputedStyle(document.getElementById("app-header")).position', 'static')
->assertScript('getComputedStyle(document.documentElement).scrollPaddingTop', '0px');
});