Sticky
A wrapper that sticks at a set distance from the top and sets a `stuck` attribute while stuck.
<arc-sticky>Overview
Sticky is a wrapper with position: sticky. offset sets its top value. "0px", the default, sticks it flush with the top edge, and "64px" sticks it under a 64px top bar.
An IntersectionObserver, not a scroll listener, watches a 1px sentinel just above the wrapper. The observer's top edge sits at the offset, so the sentinel crosses it at the moment the wrapper sticks. Sticky then sets the stuck attribute on the host, removes it when the wrapper scrolls back down, and fires arc-stuck with { stuck } in the detail each time. The stuck state adds --shadow-sm by default. Style [stuck] to change the background, border or anything else.
Guidelines
Do
- Use it for section headers in long lists
- Use it for a toolbar row under a fixed top bar, with `offset` set to the top bar's height
- Style the stuck state with the `[stuck]` selector
- Listen for `arc-stuck` when app state needs to change on stick or unstick
Don't
- Don't use it for something that must stay on screen at all times. It scrolls away with its parent, so use `position: fixed`
- Don't set a negative offset. The element sticks above the top edge
- Don't expect it to stick past the end of its parent. A sticky element stays inside its parent's box
Features
- The wrapper sits at `z-index: 10`, above the content that scrolls under it
Preview
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<arc-sticky offset="0px">
<div class="section-header">Section Title</div>
</arc-sticky>
<style>
arc-sticky[stuck] .section-header {
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
background: var(--bg-surface-overlay);
backdrop-filter: blur(8px);
}
</style>
<script>
const sticky = document.querySelector('arc-sticky');
sticky.addEventListener('arc-stuck', (e) => {
console.log('Stuck:', e.detail.stuck);
});
</script>import { Sticky } from '@arclux/arc-ui-react';
function StickyHeader() {
return (
<Sticky offset="64px" onArcStuck={(e) => console.log('Stuck:', e.detail.stuck)}>
<div className="section-header">Section Title</div>
</Sticky>
);
}<script setup>
import { Sticky } from '@arclux/arc-ui-vue';
function onStuck(e: CustomEvent) {
console.log('Stuck:', e.detail.stuck);
}
</script>
<template>
<Sticky offset="64px" @arc-stuck="onStuck">
<div class="section-header">Section Title</div>
</Sticky>
</template><script>
import { Sticky } from '@arclux/arc-ui-svelte';
</script>
<Sticky offset="64px" on:arc-stuck={(e) => console.log('Stuck:', e.detail.stuck)}>
<div class="section-header">Section Title</div>
</Sticky>import { Component } from '@angular/core';
import { Sticky } from '@arclux/arc-ui-angular';
@Component({
imports: [Sticky],
template: `
<arc-sticky offset="64px" (arcStuck)="onStuck($event)">
<div class="section-header">Section Title</div>
</arc-sticky>
`,
})
export class StickyHeaderComponent {
onStuck(e: CustomEvent) {
console.log('Stuck:', e.detail.stuck);
}
}import { Sticky } from '@arclux/arc-ui-solid';
function StickyHeader() {
return (
<Sticky offset="64px" onArcStuck={(e) => console.log('Stuck:', e.detail.stuck)}>
<div class="section-header">Section Title</div>
</Sticky>
);
}import { Sticky } from '@arclux/arc-ui-preact';
function StickyHeader() {
return (
<Sticky offset="64px" onArcStuck={(e) => console.log('Stuck:', e.detail.stuck)}>
<div class="section-header">Section Title</div>
</Sticky>
);
}API
offsetstring'0px'- The CSS
topvalue for sticky positioning. Set to "64px" to stick below a 64px top bar, or "0px" to stick flush with the viewport/scroll container edge. stuckbooleanfalse- Read-only attribute set by the IntersectionObserver while the element is stuck at its
offset, whatever the offset is. Use the[stuck]CSS selector to style the stuck state.
Events
arc-stuck- Fired when the stuck state changes. Event detail contains
{ stuck: boolean }indicating whether the element is currently stuck.