@solid-primitives/bounds
Advanced tools
Comparing version 0.0.110 to 0.0.111
@@ -0,1 +1,2 @@ | ||
import { FalsyValue } from '@solid-primitives/utils'; | ||
import { Accessor } from 'solid-js'; | ||
@@ -11,3 +12,3 @@ | ||
}; | ||
type NullableBounds = Bounds | typeof NULLED_BOUNDS; | ||
type NullableBounds = Record<keyof Bounds, number | null>; | ||
type UpdateGuard = <Args extends unknown[]>(update: (...args: Args) => void) => (...args: Args) => void; | ||
@@ -19,10 +20,2 @@ type Options = { | ||
}; | ||
declare const NULLED_BOUNDS: { | ||
readonly top: null; | ||
readonly left: null; | ||
readonly bottom: null; | ||
readonly right: null; | ||
readonly width: null; | ||
readonly height: null; | ||
}; | ||
/** | ||
@@ -32,3 +25,3 @@ * @returns object of element's boundingClientRect with enumerable properties | ||
declare function getElementBounds(element: Element): Bounds; | ||
declare function getElementBounds(element: Element | undefined | null | false): NullableBounds; | ||
declare function getElementBounds(element: Element | FalsyValue): NullableBounds; | ||
/** | ||
@@ -60,5 +53,4 @@ * Creates a reactive store-like object of current element bounds — position on the screen, and size dimensions. Bounds will be automatically updated on scroll, resize events and updates to the dom. | ||
*/ | ||
declare function createElementBounds(target: Accessor<Element> | Element, options?: Options): Readonly<Bounds>; | ||
declare function createElementBounds(target: Accessor<Element | undefined | null | false> | Element, options?: Options): Readonly<NullableBounds>; | ||
declare function createElementBounds(target: Accessor<Element | FalsyValue> | Element, { trackMutation, trackResize, trackScroll }?: Options): Readonly<NullableBounds>; | ||
export { Bounds, NullableBounds, Options, UpdateGuard, createElementBounds, getElementBounds }; |
@@ -1,6 +0,7 @@ | ||
import { onMount, createComputed, on, onCleanup } from 'solid-js'; | ||
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, sharedConfig, onMount, onCleanup } from 'solid-js'; | ||
import { isServer } from 'solid-js/web'; | ||
import { createStaticStore, access } from '@solid-primitives/utils'; | ||
import { createResizeObserver } from '@solid-primitives/resize-observer'; | ||
import { makeEventListener } from '@solid-primitives/event-listener'; | ||
@@ -18,3 +19,3 @@ // src/index.ts | ||
if (isServer || !element) { | ||
return Object.assign({}, NULLED_BOUNDS); | ||
return { ...NULLED_BOUNDS }; | ||
} | ||
@@ -33,27 +34,29 @@ const rect = element.getBoundingClientRect(); | ||
if (isServer) { | ||
return Object.assign({}, NULLED_BOUNDS); | ||
return NULLED_BOUNDS; | ||
} | ||
const [bounds, setBounds] = createStaticStore(getElementBounds(access(target))); | ||
const updateBounds = () => setBounds(getElementBounds(access(target))); | ||
const updateBoundsOf = (el) => setBounds(getElementBounds(el)); | ||
if (typeof target === "function") { | ||
onMount(() => updateBoundsOf(target())); | ||
createComputed(on(target, updateBoundsOf, { defer: true })); | ||
} | ||
const isFn = typeof target === "function", [track, trigger] = createSignal(void 0, { equals: false }); | ||
let calc; | ||
if (sharedConfig.context) { | ||
calc = () => NULLED_BOUNDS; | ||
onMount(() => { | ||
calc = () => getElementBounds(access(target)); | ||
trigger(); | ||
}); | ||
} else if (isFn) { | ||
calc = () => getElementBounds(target()); | ||
onMount(trigger); | ||
} else | ||
calc = () => getElementBounds(target); | ||
const bounds = createDerivedStaticStore(() => (track(), calc())); | ||
if (trackResize) { | ||
const resizeHandler = (_, el) => updateBoundsOf(el); | ||
createResizeObserver( | ||
typeof target === "function" ? () => target() || [] : target, | ||
typeof trackResize === "function" ? trackResize(resizeHandler) : resizeHandler | ||
isFn ? () => target() || [] : target, | ||
typeof trackResize === "function" ? trackResize(trigger) : trigger | ||
); | ||
} | ||
if (trackScroll) { | ||
const scrollHandler = typeof target === "function" ? (e) => { | ||
const scrollHandler = isFn ? (e) => { | ||
const el = target(); | ||
if (el && e.target instanceof Node && e.target.contains(el)) | ||
updateBoundsOf(el); | ||
} : (e) => { | ||
if (e.target instanceof Node && e.target.contains(target)) | ||
updateBoundsOf(target); | ||
}; | ||
el && e.target instanceof Node && e.target.contains(el) && trigger(); | ||
} : (e) => e.target instanceof Node && e.target.contains(target) && trigger(); | ||
makeEventListener( | ||
@@ -68,3 +71,3 @@ window, | ||
const mo = new MutationObserver( | ||
typeof trackMutation === "function" ? trackMutation(updateBounds) : updateBounds | ||
typeof trackMutation === "function" ? trackMutation(trigger) : trigger | ||
); | ||
@@ -76,3 +79,3 @@ mo.observe(document.body, { | ||
}); | ||
onCleanup(mo.disconnect.bind(mo)); | ||
onCleanup(() => mo.disconnect()); | ||
} | ||
@@ -79,0 +82,0 @@ return bounds; |
{ | ||
"name": "@solid-primitives/bounds", | ||
"version": "0.0.110", | ||
"version": "0.0.111", | ||
"description": "Primitives for tracking HTML element size and position on screen as it changes.", | ||
@@ -52,5 +52,6 @@ "author": "Damian Tarnawski <gthetarnav@gmail.com>", | ||
"dependencies": { | ||
"@solid-primitives/event-listener": "^2.2.9", | ||
"@solid-primitives/resize-observer": "^2.0.13", | ||
"@solid-primitives/utils": "^5.5.1" | ||
"@solid-primitives/event-listener": "^2.2.10", | ||
"@solid-primitives/resize-observer": "^2.0.14", | ||
"@solid-primitives/static-store": "^0.0.2", | ||
"@solid-primitives/utils": "^6.0.0" | ||
}, | ||
@@ -57,0 +58,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
13546
5
210
+ Added@solid-primitives/static-store@0.0.2(transitive)
+ Addedseroval@1.2.0(transitive)
+ Addedseroval-plugins@1.2.0(transitive)
- Removed@solid-primitives/utils@5.5.2(transitive)
- Removedseroval@1.2.1(transitive)
- Removedseroval-plugins@1.2.1(transitive)