Skip to content
Elements

useParentElement

Returns the parentElement of a target DOM node as a reactive Observable. Re-evaluates whenever the target Ref$ or Observable changes. Targets can be Ref$, MaybeElement, or a plain Element.

Parent Element

Move your mouse over the page to highlight elements.

current: parent:
import {
const useRef$: {
<T = any>(initialValue: null): Ref$<T | null>;
<T = any>(initialValue: T): Ref$<T>;
<T = any>(): Ref$<T | null>;
}
useRef$
} from "@usels/core";
import {
const useParentElement: (element?: MaybeEventTarget) => Observable<OpaqueObject<HTMLElement | SVGElement> | null>
useParentElement
} from "@usels/web";
function
function Component(): JSX.Element
Component
() {
const
const el$: Ref$<HTMLDivElement | null>
el$
=
useRef$<HTMLDivElement>(): Ref$<HTMLDivElement | null> (+2 overloads)

Core (framework-agnostic) version of useRef$. Creates an observable element ref with opaque wrapping.

  • non-null value → Ref$<T>: current, get(), peek() return T
  • null / no arg → Ref$<T | null>: current, get(), peek() return T | null

Nullability is expressed via the type parameter, mirroring T | null at the call site.

useRef$
<
interface HTMLDivElement
HTMLDivElement
>();
const
const parent$: any
parent$
=
function useParentElement(element?: MaybeEventTarget): Observable<OpaqueObject<HTMLElement | SVGElement> | null>

Framework-agnostic reactive parent-element tracker.

Tracks the parentElement of a DOM node. Accepts Ref$, Observable<Element|null>, or a raw Element. When the input is a Ref$ or Observable, the parent is re-read reactively whenever the underlying element reference changes.

NOTE: For a plain (non-Observable) element, the parent is read at construction time only — subsequent DOM moves are not detected. Use Ref$ or Observable<Element> for dynamic parent tracking.

Document / Window have no parentElement, so they resolve to null.

@returnsObservable<OpaqueObject<HTMLElement | SVGElement> | null> — current parent, wrapped opaquely.

useParentElement
(
const el$: Ref$<HTMLDivElement | null>
el$
);
return <
JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div
RefAttributes<HTMLDivElement>.ref?: Ref<HTMLDivElement> | undefined

Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref).

ref
={
const el$: Ref$<HTMLDivElement | null>
el$
}>{
const parent$: any
parent$
.
any
get
()?.
any
tagName
}</
JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div
>;
}
import { observable } from "@usels/core";
import { useParentElement } from "@usels/web";
function Component() {
const target$ = observable<HTMLElement | null>(null);
const parent$ = useParentElement(target$);
// ...
}

A plain (non-Observable) element is read once at construction time; later DOM moves are not detected. Use Ref$ or Observable<Element> for dynamic parent tracking.

import { useParentElement } from "@usels/web";
function Component({ el }: { el: HTMLElement }) {
const parent$ = useParentElement(el);
// ...
}
ParameterTypeDescription
elementMaybeEventTarget (optional)-

ObservablePrimitive<OpaqueObject<HTMLElement \| SVGElement> \| null>

NameTypeDescription
peek{ (): HTMLElement | SVGElement | null; (): OpaqueObject<HTMLElement | SVGElement> | null; }-
get(trackingType?: TrackingType | GetOptions) => HTMLElement | SVGElement | null-
onChange(cb: ListenerFn<OpaqueObject<HTMLElement | SVGElement> | null>, options?: { trackingType?: TrackingType; initial?: boolean | undefined; immediate?: boolean | undefined; noArgs?: boolean | undefined; } | undefined) => () => void-
set{ (value: (prev: HTMLElement | SVGElement | null) => HTMLElement | SVGElement | null): void; (value: Observable<HTMLElement | SVGElement | null>): void; (value: ImmutableObservableBase<...> | ... 11 more ... | null): void; (value: Promise<...>): void; (value: HTMLElement | ... 1 more ... | null): void; }-
delete() => void-

View on GitHub