Open History to see the last commit
1import { isValidElement, type ReactElement } from "react";
2
3export function visitElements(
4 node: unknown,
5 visitor: (element: ReactElement<Record<string, unknown>>) => boolean,
6): ReactElement<Record<string, unknown>> | null {
7 if (Array.isArray(node)) {
8 for (const child of node) {
9 const found = visitElements(child, visitor);
10 if (found) return found;
11 }
12 return null;
13 }
14 if (!isValidElement<Record<string, unknown>>(node)) return null;
15 if (visitor(node)) return node;
16 for (const value of Object.values(node.props)) {
17 const found = visitElements(value, visitor);
18 if (found) return found;
19 }
20 return null;
21}