Skip to content

Change the type of refs for react 19 #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions packages/usehooks-ts/src/useEventListener/useEventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function useEventListener<
handler:
| ((event: HTMLElementEventMap[K]) => void)
| ((event: SVGElementEventMap[K]) => void),
element: RefObject<T>,
element: RefObject<T | null | undefined>,
options?: boolean | AddEventListenerOptions,
): void

Expand All @@ -51,7 +51,7 @@ function useEventListener<K extends keyof DocumentEventMap>(
* @template T - The type of the DOM element (default is `HTMLElement`).
* @param {KW | KH | KM} eventName - The name of the event to listen for.
* @param {(event: WindowEventMap[KW] | HTMLElementEventMap[KH] | SVGElementEventMap[KH] | MediaQueryListEventMap[KM] | Event) => void} handler - The event handler function.
* @param {RefObject<T>} [element] - The DOM element or media query list to attach the event listener to (optional).
* @param {RefObject<T | null | undefined>} [element] - The DOM element or media query list to attach the event listener to (optional).
* @param {boolean | AddEventListenerOptions} [options] - An options object that specifies characteristics about the event listener (optional).
* @public
* @see [Documentation](https://usehooks-ts.com/react-hook/use-event-listener)
Expand Down Expand Up @@ -88,7 +88,7 @@ function useEventListener<
| MediaQueryListEventMap[KM]
| Event,
) => void,
element?: RefObject<T>,
element?: RefObject<T | null | undefined>,
options?: boolean | AddEventListenerOptions,
) {
// Create a ref that stores handler
Expand Down
4 changes: 2 additions & 2 deletions packages/usehooks-ts/src/useHover/useHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useEventListener } from '../useEventListener'
/**
* Custom hook that tracks whether a DOM element is being hovered over.
* @template T - The type of the DOM element. Defaults to `HTMLElement`.
* @param {RefObject<T>} elementRef - The ref object for the DOM element to track.
* @param {RefObject<T | null | undefined>} elementRef - The ref object for the DOM element to track.
* @returns {boolean} A boolean value indicating whether the element is being hovered over.
* @public
* @see [Documentation](https://usehooks-ts.com/react-hook/use-hover)
Expand All @@ -19,7 +19,7 @@ import { useEventListener } from '../useEventListener'
* ```
*/
export function useHover<T extends HTMLElement = HTMLElement>(
elementRef: RefObject<T>,
elementRef: RefObject<T | null | undefined>,
): boolean {
const [value, setValue] = useState<boolean>(false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type EventType =
/**
* Custom hook that handles clicks outside a specified element.
* @template T - The type of the element's reference.
* @param {RefObject<T> | RefObject<T>[]} ref - The React ref object(s) representing the element(s) to watch for outside clicks.
* @param {RefObject<T | null | undefined> | RefObject<T | null | undefined>[]} ref - The React ref object(s) representing the element(s) to watch for outside clicks.
* @param {(event: MouseEvent | TouchEvent | FocusEvent) => void} handler - The callback function to be executed when a click outside the element occurs.
* @param {EventType} [eventType] - The mouse event type to listen for (optional, default is 'mousedown').
* @param {?AddEventListenerOptions} [eventListenerOptions] - The options object to be passed to the `addEventListener` method (optional).
Expand All @@ -30,7 +30,7 @@ type EventType =
* ```
*/
export function useOnClickOutside<T extends HTMLElement = HTMLElement>(
ref: RefObject<T> | RefObject<T>[],
ref: RefObject<T | null | undefined> | RefObject<T | null | undefined>[],
handler: (event: MouseEvent | TouchEvent | FocusEvent) => void,
eventType: EventType = 'mousedown',
eventListenerOptions: AddEventListenerOptions = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Size = {
/** The options for the ResizeObserver. */
type UseResizeObserverOptions<T extends HTMLElement = HTMLElement> = {
/** The ref of the element to observe. */
ref: RefObject<T>
ref: RefObject<T | null | undefined>
/**
* When using `onResize`, the hook doesn't re-render on element size changes; it delegates handling to the provided callback.
* @default undefined
Expand Down