@solid-primitives/bounds
Advanced tools
+28
-12
| import { makeEventListener } from "@solid-primitives/event-listener"; | ||
| import { createResizeObserver } from "@solid-primitives/resize-observer"; | ||
| import { createDerivedStaticStore } from "@solid-primitives/static-store"; | ||
| import { access } from "@solid-primitives/utils"; | ||
| import { createSignal, onCleanup, onMount, sharedConfig } from "solid-js"; | ||
| import { isServer } from "solid-js/web"; | ||
| import { createEffect, createMemo, createSignal, onCleanup, onSettled, sharedConfig } from "solid-js"; | ||
| import { isServer } from "@solidjs/web"; | ||
| const NULLED_BOUNDS = { | ||
@@ -61,6 +59,6 @@ top: null, | ||
| let calc; | ||
| // during hydration we need to make sure the initial state is the same as the server | ||
| if (sharedConfig.context) { | ||
| // during hydration we need to match the server-rendered state initially | ||
| if (sharedConfig.hydrating) { | ||
| calc = () => NULLED_BOUNDS; | ||
| onMount(() => { | ||
| onSettled(() => { | ||
| calc = () => getElementBounds(access(target)); | ||
@@ -70,13 +68,31 @@ trigger(); | ||
| } | ||
| // a function target might be a jsx ref, so it may not be reactive - retrigger manually on mount | ||
| // a function target might be a jsx ref, so it may not be reactive — retrigger manually on settle | ||
| else if (isFn) { | ||
| calc = () => getElementBounds(target()); | ||
| onMount(trigger); | ||
| onSettled(trigger); | ||
| } | ||
| // otherwise we can just use the target directly - it will never change | ||
| // otherwise the target is static and will never change | ||
| else | ||
| calc = () => getElementBounds(target); | ||
| const bounds = createDerivedStaticStore(() => (track(), calc())); | ||
| const memo = createMemo(() => (track(), calc())); | ||
| const bounds = {}; | ||
| Object.keys(NULLED_BOUNDS).forEach(key => { | ||
| Object.defineProperty(bounds, key, { get: () => memo()[key], enumerable: true }); | ||
| }); | ||
| if (trackResize) { | ||
| createResizeObserver(isFn ? () => target() || [] : target, typeof trackResize === "function" ? trackResize(trigger) : trigger); | ||
| const ro = new ResizeObserver(typeof trackResize === "function" ? trackResize(trigger) : trigger); | ||
| if (isFn) { | ||
| createEffect(() => target() || null, el => { | ||
| if (el) | ||
| ro.observe(el); | ||
| return () => { | ||
| if (el) | ||
| ro.unobserve(el); | ||
| }; | ||
| }); | ||
| } | ||
| else { | ||
| ro.observe(target); | ||
| } | ||
| onCleanup(() => ro.disconnect()); | ||
| } | ||
@@ -83,0 +99,0 @@ if (trackScroll) { |
+11
-10
| { | ||
| "name": "@solid-primitives/bounds", | ||
| "version": "0.1.5", | ||
| "version": "1.0.0-next.0", | ||
| "description": "Primitives for tracking HTML element size and position on screen as it changes.", | ||
@@ -18,7 +18,8 @@ "author": "Damian Tarnawski <gthetarnav@gmail.com>", | ||
| "name": "bounds", | ||
| "stage": 1, | ||
| "stage": 3, | ||
| "list": [ | ||
| "createElementBounds" | ||
| ], | ||
| "category": "Display & Media" | ||
| "category": "Display & Media", | ||
| "gzip": 884 | ||
| }, | ||
@@ -49,13 +50,13 @@ "keywords": [ | ||
| "devDependencies": { | ||
| "solid-js": "^1.9.7", | ||
| "@solid-primitives/scheduled": "^1.5.3" | ||
| "@solidjs/web": "2.0.0-beta.15", | ||
| "solid-js": "2.0.0-beta.15", | ||
| "@solid-primitives/scheduled": "^2.0.0-next.0" | ||
| }, | ||
| "dependencies": { | ||
| "@solid-primitives/event-listener": "^2.4.5", | ||
| "@solid-primitives/resize-observer": "^2.1.5", | ||
| "@solid-primitives/static-store": "^0.1.3", | ||
| "@solid-primitives/utils": "^6.4.0" | ||
| "@solid-primitives/event-listener": "^3.0.0-next.0", | ||
| "@solid-primitives/utils": "^7.0.0-next.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "solid-js": "^1.6.12" | ||
| "@solidjs/web": "^2.0.0-beta.15", | ||
| "solid-js": "^2.0.0-beta.15" | ||
| }, | ||
@@ -62,0 +63,0 @@ "typesVersions": {}, |
+4
-5
@@ -7,5 +7,6 @@ <p> | ||
| [](https://bundlephobia.com/package/@solid-primitives/bounds) | ||
| [](https://bundlephobia.com/package/@solid-primitives/bounds) | ||
| [](https://www.npmjs.com/package/@solid-primitives/bounds) | ||
| [](https://github.com/solidjs-community/solid-primitives#contribution-process) | ||
| [](https://vitest.dev) | ||
@@ -22,2 +23,4 @@ Primitives for tracking HTML element size and position on screen as it changes. | ||
| yarn add @solid-primitives/bounds | ||
| # or | ||
| pnpm add @solid-primitives/bounds | ||
| ``` | ||
@@ -100,8 +103,4 @@ | ||
| ## Demo | ||
| https://codesandbox.io/s/solid-primitives-bounds-64rls0?file=/index.tsx | ||
| ## Changelog | ||
| See [CHANGELOG.md](./CHANGELOG.md) |
Unstable ownership
Supply chain riskA new collaborator has begun publishing package versions. Package stability and security risk may be elevated.
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
12885
3.67%4
-20%165
10.74%3
50%104
-0.95%1
Infinity%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed