import { forwardRef } from "react"; type AtLeastOne = [T, ...T[]]; type ContextHook = (prop?: { strict?: boolean; }) => { stateProps: React.HTMLAttributes } | null; type ContextConfig = ContextHook | { useContext: ContextHook; strict?: boolean }; export function createWithStateProps(useContexts: AtLeastOne) { return function withStateProps(Component: React.ComponentType

>) { const Node = forwardRef((props, ref) => { const stateProps = {}; for (const contextConfig of useContexts) { if (typeof contextConfig === "function") { Object.assign(stateProps, contextConfig({ strict: true })?.stateProps); } else { const { useContext, strict = false } = contextConfig; Object.assign(stateProps, useContext({ strict })?.stateProps); } } // @ts-ignore return ; }); Node.displayName = Component.displayName || Component.name; return Node; }; }