Skip to content

Commit

Permalink
refactor: observer hooks to take obj param
Browse files Browse the repository at this point in the history
  • Loading branch information
lovrozagar committed Sep 6, 2024
1 parent 67e9e72 commit f6cf091
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@renderui/core",
"version": "1.7.5",
"version": "1.7.6",
"private": false,
"sideEffects": false,
"description": "React UI library with highly modular and ready-out-of-the-box components",
Expand Down
22 changes: 14 additions & 8 deletions src/components/_shared/hooks/use-intersection-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
import { useFreshRef } from '@/components/_shared/hooks/use-fresh-ref'
import React from 'react'

function useIntersectionObserver<T extends HTMLElement>(
node: T | null,
callback: IntersectionObserverCallback,
options: IntersectionObserverInit,
) {
const freshCallback = useFreshRef(callback)
const freshOptions = useFreshRef(options)
type UseIntersectionObserverProps<T extends HTMLElement> = {
node: T | null
callback: IntersectionObserverCallback
options: IntersectionObserverInit
}

function useIntersectionObserver<T extends HTMLElement>(props: UseIntersectionObserverProps<T>) {
const freshProps = useFreshRef(props)

const node = freshProps.current.node

/* biome-ignore lint/correctness/useExhaustiveDependencies: using fresh ref pattern, ref dep not needed */
React.useEffect(() => {
if (!node) return undefined

const observer = new IntersectionObserver(freshCallback.current, freshOptions.current)
const observer = new IntersectionObserver(
freshProps.current.callback,
freshProps.current.options,
)

observer.observe(node)

Expand Down
21 changes: 12 additions & 9 deletions src/components/_shared/hooks/use-mutation-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
import { useFreshRef } from '@/components/_shared/hooks/use-fresh-ref'
import React from 'react'

function useMutationObserver<T extends HTMLElement>(
node: T | null,
callback: MutationCallback,
options: MutationObserverInit,
) {
const freshCallback = useFreshRef(callback)
const freshOptions = useFreshRef(options)
type UseMutationObserverProps<T extends HTMLElement> = {
node: T | null
callback: MutationCallback
options: MutationObserverInit
}

function useMutationObserver<T extends HTMLElement>(props: UseMutationObserverProps<T>) {
const freshProps = useFreshRef(props)

const node = freshProps.current.node

/* biome-ignore lint/correctness/useExhaustiveDependencies: using fresh ref pattern, ref dep not needed */
React.useEffect(() => {
if (!node) return undefined

const observer = new MutationObserver(freshCallback.current)
const observer = new MutationObserver(freshProps.current.callback)

observer.observe(node, freshOptions.current)
observer.observe(node, freshProps.current.options)

return () => {
observer.disconnect()
Expand Down
10 changes: 5 additions & 5 deletions src/components/command/hooks/use-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ function useSearch(value: string, setValue: React.Dispatch<React.SetStateAction<
)
}, [htmlDocument?.querySelector])

useMutationObserver(
htmlDocument?.documentElement ?? null,
storeCommandItems,
SEARCH_OBSERVER_OPTIONS,
)
useMutationObserver({
node: htmlDocument?.documentElement ?? null,
callback: storeCommandItems,
options: SEARCH_OBSERVER_OPTIONS,
})

React.useEffect(() => {
setTimeout(storeCommandItems, 0)
Expand Down
6 changes: 5 additions & 1 deletion src/components/ripple/hooks/use-keyboard-ripple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ function useKeyboardRipple<T extends HTMLElement>(

const parentRef = ref.current?.parentElement ?? null

useMutationObserver(parentRef, mutationHandler, KEYBOARD_RIPPLE_MUTATION_OBSERVER_OPTIONS)
useMutationObserver({
node: parentRef,
callback: mutationHandler,
options: KEYBOARD_RIPPLE_MUTATION_OBSERVER_OPTIONS,
})
}

export { useKeyboardRipple }
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export { useFreshRef } from '@/components/_shared/hooks/use-fresh-ref'
export { useKeyboardHotkey } from '@/components/_shared/hooks/use-keyboard-hotkey'
export { useMergedRef } from '@/components/_shared/hooks/use-merged-ref'
export { useMutationObserver } from '@/components/_shared/hooks/use-mutation-observer'
export { useIntersectionObserver } from '@/components/_shared/hooks/use-intersection-observer'
export { useOnClickOutside } from '@/components/_shared/hooks/use-on-click-outside'
export {
useUncontrolledState,
Expand Down

0 comments on commit f6cf091

Please sign in to comment.