react-attractor
Version:
React library to map DOM elements to React components
41 lines (40 loc) • 1.16 kB
JavaScript
import { jsx as i } from "react/jsx-runtime";
import { createContext as T, useRef as d, useCallback as u, useContext as x } from "react";
const h = T(null), m = ({ children: t }) => {
const r = d(/* @__PURE__ */ new WeakMap()), s = u((e, o) => {
e && r.current.set(e, o);
}, []), a = u((e) => r.current.get(e), []), n = {
registerTether: s,
getTether: a
};
return /* @__PURE__ */ i(h.Provider, { value: n, children: t });
}, p = () => {
const t = x(h);
if (!t)
throw new Error("useTetherContext must be used within a TetherProvider");
return t;
};
function P(t) {
return t !== null && typeof t == "object" && t !== null && "$$typeof" in t;
}
function g(t) {
const r = (s) => {
const { tetherMetadata: a, ...n } = s, { registerTether: e } = p(), o = {
props: n,
metadata: a
}, f = u((c) => {
c && e(c, o);
}, [e, o]), l = {
...n,
ref: f
};
return /* @__PURE__ */ i(t, { ...l });
};
return r.displayName = `withTether(${t.displayName || t.name || "Component"})`, r;
}
export {
m as TetherProvider,
P as isForwardRefComponent,
p as useTetherContext,
g as withTether
};