Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-laws-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wethegit/react-hooks": major
---

[hooks] fix(useInView): returned element type based on generic
12 changes: 6 additions & 6 deletions packages/wethegit-react-hooks/src/lib/hooks/use-in-view.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { useState, useRef, useEffect, useCallback } from "react"

export type InViewHook = [
export type InViewHook<T extends HTMLElement> = [
/**
* Pass this function to the `ref` prop of the DOM element you want to track visibility of.
*/
(node: HTMLElement) => void,
(node: T) => void,
/**
* Whether the target DOM element is in view, based on the provided `threshold` argument.
*/
boolean,
/**
* The DOM node itself, once set by the `setTargetRef` function.
*/
HTMLElement | undefined,
T | undefined,
]

/**
Expand All @@ -27,13 +27,13 @@ export type InViewHook = [
* <section ref={setSectionRef} className={sectionInView ? "in-view" : ""}>
*
*/
export function useInView(
export function useInView<T extends HTMLElement>(
threshold: number = 0.3,
once: boolean = true,
setInViewIfScrolledPast: boolean = false
): InViewHook {
): InViewHook<T> {
const [isIntersecting, setIntersecting] = useState(false)
const [targetRef, setTargetRef] = useState<HTMLElement>()
const [targetRef, setTargetRef] = useState<T>()
const observerRef = useRef<IntersectionObserver>()

const observerCallback = useCallback<IntersectionObserverCallback>(
Expand Down