@solid-primitives/bounds
Advanced tools
Comparing version 0.0.108 to 0.0.109-beta.0
@@ -1,25 +0,27 @@ | ||
import { Accessor } from 'solid-js'; | ||
import { Accessor } from "solid-js"; | ||
type Bounds = { | ||
top: number; | ||
left: number; | ||
bottom: number; | ||
right: number; | ||
width: number; | ||
height: number; | ||
top: number; | ||
left: number; | ||
bottom: number; | ||
right: number; | ||
width: number; | ||
height: number; | ||
}; | ||
type NullableBounds = Bounds | typeof NULLED_BOUNDS; | ||
type UpdateGuard = <Args extends unknown[]>(update: (...args: Args) => void) => (...args: Args) => void; | ||
type UpdateGuard = <Args extends unknown[]>( | ||
update: (...args: Args) => void, | ||
) => (...args: Args) => void; | ||
type Options = { | ||
trackScroll?: boolean | UpdateGuard; | ||
trackMutation?: boolean | UpdateGuard; | ||
trackResize?: boolean | UpdateGuard; | ||
trackScroll?: boolean | UpdateGuard; | ||
trackMutation?: boolean | UpdateGuard; | ||
trackResize?: boolean | UpdateGuard; | ||
}; | ||
declare const NULLED_BOUNDS: { | ||
readonly top: null; | ||
readonly left: null; | ||
readonly bottom: null; | ||
readonly right: null; | ||
readonly width: null; | ||
readonly height: null; | ||
readonly top: null; | ||
readonly left: null; | ||
readonly bottom: null; | ||
readonly right: null; | ||
readonly width: null; | ||
readonly height: null; | ||
}; | ||
@@ -57,5 +59,11 @@ /** | ||
*/ | ||
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> | Element, | ||
options?: Options, | ||
): Readonly<Bounds>; | ||
declare function createElementBounds( | ||
target: Accessor<Element | undefined | null | false> | Element, | ||
options?: Options, | ||
): Readonly<NullableBounds>; | ||
export { Bounds, NullableBounds, Options, UpdateGuard, createElementBounds, getElementBounds }; |
@@ -1,5 +0,5 @@ | ||
import { onMount, createComputed, on, onCleanup } from 'solid-js'; | ||
import { createStaticStore, access } from '@solid-primitives/utils'; | ||
import { createResizeObserver } from '@solid-primitives/resize-observer'; | ||
import { makeEventListener } from '@solid-primitives/event-listener'; | ||
import { onMount, createComputed, on, onCleanup } from "solid-js"; | ||
import { createStaticStore, access } from "@solid-primitives/utils"; | ||
import { createResizeObserver } from "@solid-primitives/resize-observer"; | ||
import { makeEventListener } from "@solid-primitives/event-listener"; | ||
@@ -13,3 +13,3 @@ // src/index.ts | ||
width: null, | ||
height: null | ||
height: null, | ||
}; | ||
@@ -27,9 +27,12 @@ function getElementBounds(element) { | ||
width: rect.width, | ||
height: rect.height | ||
height: rect.height, | ||
}; | ||
} | ||
function createElementBounds(target, { trackMutation = true, trackResize = true, trackScroll = true } = {}) { | ||
function createElementBounds( | ||
target, | ||
{ trackMutation = true, trackResize = true, trackScroll = true } = {}, | ||
) { | ||
const [bounds, setBounds] = createStaticStore(getElementBounds(access(target))); | ||
const updateBounds = () => setBounds(getElementBounds(access(target))); | ||
const updateBoundsOf = (el) => setBounds(getElementBounds(el)); | ||
const updateBoundsOf = el => setBounds(getElementBounds(el)); | ||
if (typeof target === "function") { | ||
@@ -43,14 +46,15 @@ onMount(() => updateBoundsOf(target())); | ||
typeof target === "function" ? () => target() || [] : target, | ||
typeof trackResize === "function" ? trackResize(resizeHandler) : resizeHandler | ||
typeof trackResize === "function" ? trackResize(resizeHandler) : resizeHandler, | ||
); | ||
} | ||
if (trackScroll) { | ||
const scrollHandler = typeof target === "function" ? (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); | ||
}; | ||
const scrollHandler = | ||
typeof target === "function" | ||
? 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); | ||
}; | ||
makeEventListener( | ||
@@ -60,3 +64,3 @@ window, | ||
typeof trackScroll === "function" ? trackScroll(scrollHandler) : scrollHandler, | ||
{ capture: true } | ||
{ capture: true }, | ||
); | ||
@@ -66,3 +70,3 @@ } | ||
const mo = new MutationObserver( | ||
typeof trackMutation === "function" ? trackMutation(updateBounds) : updateBounds | ||
typeof trackMutation === "function" ? trackMutation(updateBounds) : updateBounds, | ||
); | ||
@@ -72,3 +76,3 @@ mo.observe(document.body, { | ||
subtree: true, | ||
childList: true | ||
childList: true, | ||
}); | ||
@@ -75,0 +79,0 @@ onCleanup(mo.disconnect.bind(mo)); |
@@ -1,5 +0,5 @@ | ||
import 'solid-js'; | ||
import '@solid-primitives/utils'; | ||
import '@solid-primitives/resize-observer'; | ||
import '@solid-primitives/event-listener'; | ||
import "solid-js"; | ||
import "@solid-primitives/utils"; | ||
import "@solid-primitives/resize-observer"; | ||
import "@solid-primitives/event-listener"; | ||
@@ -13,3 +13,3 @@ // src/index.ts | ||
width: null, | ||
height: null | ||
height: null, | ||
}; | ||
@@ -21,3 +21,6 @@ function getElementBounds(element) { | ||
} | ||
function createElementBounds(target, { trackMutation = true, trackResize = true, trackScroll = true } = {}) { | ||
function createElementBounds( | ||
target, | ||
{ trackMutation = true, trackResize = true, trackScroll = true } = {}, | ||
) { | ||
{ | ||
@@ -24,0 +27,0 @@ return Object.assign({}, NULLED_BOUNDS); |
{ | ||
"name": "@solid-primitives/bounds", | ||
"version": "0.0.108", | ||
"version": "0.0.109-beta.0", | ||
"description": "Primitives for tracking HTML element size and position on screen as it changes.", | ||
@@ -83,5 +83,5 @@ "author": "Damian Tarnawski <gthetarnav@gmail.com>", | ||
"dependencies": { | ||
"@solid-primitives/event-listener": "^2.2.7", | ||
"@solid-primitives/resize-observer": "^2.0.10", | ||
"@solid-primitives/utils": "^5.2.1" | ||
"@solid-primitives/event-listener": "^2.2.8-beta.0", | ||
"@solid-primitives/resize-observer": "^2.0.11-beta.0", | ||
"@solid-primitives/utils": "^5.4.0-beta.0" | ||
}, | ||
@@ -88,0 +88,0 @@ "peerDependencies": { |
@@ -0,0 +0,0 @@ <p> |
Sorry, the diff of this file is not supported yet
16376
9
295