All files / hooks/use-focus-within/src utils.ts

100% Statements 3/3
100% Branches 4/4
100% Functions 1/1
100% Lines 2/2

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                                      41x 38x    
/**
 * Whether `target` is the `container` itself or one of its descendants.
 *
 * A tiny, SSR-agnostic wrapper around `Node.contains` that also folds in the
 * two "no focus" cases: a `null`/absent `container` and a `null` `target`
 * (e.g. a `focusout` whose `relatedTarget` is `null`, or `document.activeElement`
 * being `null`) both mean "focus is not inside", so this returns `false`.
 *
 * `Node.contains(node)` is reflexive — `container.contains(container)` is
 * `true` — so focus resting on the container element itself counts as inside.
 *
 * @param container - The subtree root, or `null` when nothing is attached.
 * @param target - The element to test (an `EventTarget`, `Node`, or `null`).
 * @returns `true` when `target` is `container` or a descendant of it.
 */
export function isFocusInside(
  container: Node | null,
  target: EventTarget | Node | null
): boolean {
  if (!container || !target) return false;
  return container.contains(target as Node);
}