2.0.23 • Published 3 months ago

@solid-primitives/scroll v2.0.23

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

@solid-primitives/scroll

turborepo size size stage

Reactive primitives to react to element/window scrolling.

Installation

npm install @solid-primitives/scroll
# or
yarn 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";

// target will be window by default
const windowScroll = createScrollPosition();

createEffect(() => {
  // returned object is a reactive store-like structure
  windowScroll.x; // => number
  windowScroll.y; // => number
});

With element refs

let ref: HTMLDivElement | undefined;

// pass as function
const scroll = createScrollPosition(() => ref);
// or wrap with onMount
onMount(() => {
  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);

// if target is undefined, scroll values will be 0
scroll.x; // => number
scroll.y; // => number

// update the tracking element
setTarget(ref);

// disable tracking
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(); // => number

// or destructure

import { destructure } from "@solid-primitives/destructure";
const { x, y } = destructure(createScrollPosition());
x(); // => number
y(); // => number

Demo

https://codesandbox.io/s/solid-primitives-scroll-xy19c8?file=/index.tsx

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, // => number
    scroll.y, //  => number
  );
});

Additional helpers

getScrollPosition

Get an { x: number, y: number } object of element/window scroll position.

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

2.0.23

3 months ago

2.0.22

3 months ago

2.0.21

4 months ago

2.0.19

9 months ago

2.0.18

10 months ago

2.0.20

9 months ago

2.0.15

1 year ago

2.0.16

1 year ago

2.0.17

1 year ago

2.0.13

1 year ago

2.0.14

1 year ago

2.0.11

1 year ago

2.0.12

1 year ago

2.0.9

1 year ago

2.0.10

1 year ago

2.0.10-beta.0

1 year ago

2.0.8

1 year ago

2.0.7

1 year ago

2.0.5

2 years ago

2.0.6

2 years ago

2.0.4

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

3 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

0.0.100

3 years ago