Files
gui/apps/gui.hanzo.ai/hooks/useOnIntersecting.tsx
zeekayandHanzo Dev 751ef5c778 rename the framework to @hanzo/gui and the loader to @hanzogui/loader
Both old names are owned by a different npm account (artemis-prime), so
publishing them returns 403 — which is how this surfaced: the 8.0.0 run
shipped @hanzogui/logo and @hanzogui/demos depending on [email protected], a
version that cannot be published and therefore does not exist. Those two
were uninstallable.

@hanzo/gui and @hanzogui/* are ours, so the framework now carries the name
it is imported by everywhere downstream, and the loader moves under the
scope we control.

The rewrite is limited to package references. `DEBUG?.startsWith('hanzogui')`
is a log-namespace filter and join(dir, 'hanzogui') is a path — rewriting
either would change behaviour, so both are untouched.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-30 20:23:18 -07:00

93 lines
2.3 KiB
TypeScript

import type { MutableRefObject } from 'react'
import { startTransition, useEffect, useState } from 'react'
import { isWeb, useEvent } from '@hanzo/gui'
type DisposeFn = () => void
type IntersectCallback = (
props: (IntersectionObserverEntry | null)[],
didResize?: boolean
) => DisposeFn | void | null
type HTMLRef = MutableRefObject<HTMLElement | null>
export function useIsIntersecting<Ref extends HTMLRef | HTMLRef[]>(
refs: Ref,
{ once, ...opts }: IntersectionObserverInit & { once?: boolean } = {}
): Ref extends any[] ? boolean[] : boolean {
const [values, setValues] = useState<boolean[]>([])
if (isWeb) {
useOnIntersecting(
refs,
(entries) => {
const intersecting = entries.some((x) => x?.isIntersecting)
if (once && !intersecting) return
startTransition(() => {
setValues((prev) => {
const next = entries.map((e) => e?.isIntersecting ?? false)
if (prev.length === next.length && prev.every((e, i) => e === next[i])) {
return prev
}
return next
})
})
},
opts
)
}
return (Array.isArray(refs) ? values : values[0]) as any
}
export function useOnIntersecting<Ref extends HTMLRef | HTMLRef[]>(
refsIn: Ref,
incomingCbRaw: IntersectCallback,
{ threshold = 0, root, rootMargin }: IntersectionObserverInit = {},
mountArgs: any[] = []
) {
const onIntersectEvent = useEvent(incomingCbRaw)
useEffect(() => {
if (!isWeb) return
const refs = (Array.isArray(refsIn) ? refsIn : [refsIn]) as HTMLRef[]
if (!refs.length) return
let dispose: DisposeFn | null = null
let currentEntries: (IntersectionObserverEntry | null)[] = []
const options = {
threshold,
root,
rootMargin,
}
const io = new IntersectionObserver((entries) => {
currentEntries = refs.map((ref, index) => {
return (
entries.find((x) => x.target === ref.current) ?? currentEntries[index] ?? null
)
})
dispose?.()
dispose = onIntersectEvent(currentEntries) || null
}, options)
for (const ref of refs) {
if (ref.current) {
io.observe(ref.current)
}
}
return () => {
dispose?.()
io.disconnect()
}
}, [
onIntersectEvent,
refsIn,
root,
rootMargin,
threshold,
...mountArgs,
])
}