@crossed/primitive
Version:
A universal & performant styling library for React Native, Next.js & React
53 lines (52 loc) • 1.9 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var useEvent_exports = {};
__export(useEvent_exports, {
useEvent: () => useEvent,
useGet: () => useGet
});
module.exports = __toCommonJS(useEvent_exports);
var import_react = require("react");
function useEvent(callback) {
return useGet(callback, defaultValue, true);
}
const defaultValue = () => {
throw new Error("Cannot call an event handler while rendering.");
};
const isClient = typeof window !== "undefined";
const useIsomorphicLayoutEffect = isClient ? import_react.useLayoutEffect : import_react.useEffect;
function useGet(currentValue, initialValue, forwardToFunction) {
const curRef = (0, import_react.useRef)(initialValue ?? currentValue);
useIsomorphicLayoutEffect(() => {
curRef.current = currentValue;
});
return (0, import_react.useCallback)(
forwardToFunction ? (...args) => {
var _a;
return (_a = curRef.current) == null ? void 0 : _a.apply(null, args);
} : () => curRef.current,
[]
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useEvent,
useGet
});
//# sourceMappingURL=useEvent.js.map