@solid-primitives/scroll

Reactive primitives to react to element/window scrolling, and to prevent scroll outside of a given element.
Installation
npm install @solid-primitives/scroll
# or
yarn add @solid-primitives/scroll
# or
pnpm add @solid-primitives/scroll
createScrollPosition
Reactive primitive providing a store-like object with current scroll position of specified target.
How to use it
import { createScrollPosition } from "@solid-primitives/scroll";
const windowScroll = createScrollPosition();
createEffect(() => {
windowScroll.x;
windowScroll.y;
});
With element refs
let ref: HTMLDivElement | undefined;
const scroll = createScrollPosition(() => ref);
onSettled(() => {
const scroll = createScrollPosition(ref!);
});
<div ref={e => (ref = e)} />;
Reactive Target
The element target can be a reactive signal.
const [target, setTarget] = createSignal<Element | undefined>(element);
const scroll = createScrollPosition(target);
scroll.x;
scroll.y;
setTarget(ref);
setTarget(undefined);
Destructuring
If you are interested in listening to only single axis, you'd still have to access scroll.y as a property. To use it as a separate signal, you can wrap it with a function, or use destructure helper.
const scroll = createScrollPosition();
const x = () => scroll.x;
x();
import { destructure } from "@solid-primitives/destructure";
const { x, y } = destructure(createScrollPosition());
x();
y();
useWindowScrollPosition
Returns a reactive object with current window scroll position.
useWindowScrollPosition is a singleton root primitive, hence the object instance, signals and event-listeners are shared between dependents, making it more optimized to use in multiple places at once.
const scroll = useWindowScrollPosition();
createEffect(() => {
console.log(
scroll.x,
scroll.y,
);
});
Additional helpers
getScrollPosition
Get an { x: number, y: number } object of element/window scroll position.
createPreventScroll
Prevents scrolling outside of the given element by intercepting wheel and touchmove events and optionally hiding the <body> scrollbar.
Adapted from solid-prevent-scroll by Jasmin Noetzli (GiyoMoon), part of the corvu project, which is itself inspired by react-remove-scroll by Anton Korzunov. Adapted for Solid 2.0 and solid-primitives conventions.
How to use it
import { createPreventScroll } from "@solid-primitives/scroll";
createPreventScroll();
createPreventScroll({ element: () => myElement });
const [ref, setRef] = createSignal<HTMLElement>();
createPreventScroll({ element: ref });
<div ref={setRef} />;
const [open, setOpen] = createSignal(false);
createPreventScroll({ enabled: open });
Props
element | MaybeAccessor<HTMLElement | undefined> | undefined | Allow scroll inside this element. Events outside it are cancelled. |
enabled | MaybeAccessor<boolean> | true | Whether scroll prevention is active. |
hideScrollbar | MaybeAccessor<boolean> | true | Hide the <body> scrollbar while active. |
preventScrollbarShift | MaybeAccessor<boolean> | true | Compensate for the hidden scrollbar width to avoid layout shift. |
preventScrollbarShiftMode | MaybeAccessor<"padding" | "margin"> | "padding" | Which CSS property to use for the scrollbar shift compensation. |
restoreScrollPosition | MaybeAccessor<boolean> | true | Restore <body> scroll position via window.scrollTo when disabled. |
allowPinchZoom | MaybeAccessor<boolean> | false | Allow two-finger pinch-zoom gestures. |
Multiple active instances are stacked; only the topmost one installs event listeners. Body styles are shared and only restored once all instances clean up.
Primitive ideas:
PRs Welcome :)
createScrollTo - A primitive to support scroll to a target
createHashScroll - A primitive to support scrolling based on a hashtag change
Changelog
See CHANGELOG.md