@casl/react
Version:
React component for CASL which makes it easy to add permissions in any React application
30 lines (25 loc) • 1.09 kB
JavaScript
import t, { createContext as n, createElement as o, useCallback as e, useContext as r, useSyncExternalStore as i } from "react";
const l = n(null), u = [], c = () => {};
function s({children: t, value: n}) {
return o(l.Provider, {
value: n
}, t);
}
function a() {
const t = r(l), n = e(n => t?.on("updated", n) || c, [ t ]), o = e(() => t?.rules || u, [ t ]);
if (i(n, o, o), !t) throw new Error("AbilityContext is not provided. Please make sure to wrap your component tree with <AbilityProvider>.");
return t;
}
const d = t.memo(function(n) {
const o = a(), e = n, r = e.of || e.a || e.an || e.this || e.on, i = e.I || e.do, l = e.field, u = t.useMemo(() => o.relevantRuleFor(i, r, l), [ o, o.rules, i, r, l ]);
let c = !!u && !u.inverted;
n.not && (c = !c);
const s = "function" == typeof n.children ? n.children({
isAllowed: c,
ability: o,
reason: u?.reason
}) : n.children;
return n.passThrough || c ? s : null;
});
export { s as AbilityProvider, d as Can, a as useAbility };
//# sourceMappingURL=index.mjs.map