UNPKG

@kopexa/react-utils

Version:

A set of utilities for react on client side

109 lines (104 loc) 3.34 kB
"use client"; "use strict"; 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); // src/index.ts var index_exports = {}; __export(index_exports, { createContext: () => createContext, getSubtree: () => getSubtree, getValidChildren: () => getValidChildren, mergeRefs: () => mergeRefs }); module.exports = __toCommonJS(index_exports); // src/children.ts var import_react = require("react"); function getValidChildren(children) { return import_react.Children.toArray(children).filter( (child) => (0, import_react.isValidElement)(child) ); } // src/context.ts var import_react2 = require("react"); function getErrorMessage(hook, provider) { return `${hook} returned \`undefined\`. Seems you forgot to wrap component within ${provider}`; } function createContext(options = {}) { const { name, strict = true, hookName = "useContext", providerName = "Provider", errorMessage, defaultValue } = options; const Context = (0, import_react2.createContext)(defaultValue); Context.displayName = name; function useContext() { var _a; const context = (0, import_react2.useContext)(Context); if (!context && strict) { const error = new Error( errorMessage != null ? errorMessage : getErrorMessage(hookName, providerName) ); error.name = "ContextError"; (_a = Error.captureStackTrace) == null ? void 0 : _a.call(Error, error, useContext); throw error; } return context; } return [Context.Provider, useContext, Context]; } // src/dom.ts var import_react3 = require("react"); function mergeRefs(...inputRefs) { const filteredInputRefs = inputRefs.filter(Boolean); if (filteredInputRefs.length <= 1) { const firstRef = filteredInputRefs[0]; return firstRef || null; } return function mergedRefs(ref) { for (const inputRef of filteredInputRefs) { if (typeof inputRef === "function") { inputRef(ref); } else if (inputRef) { inputRef.current = ref; } } }; } function getSubtree(options, content) { const { asChild, children } = options; if (!asChild) return typeof content === "function" ? content(children) : content; const firstChild = import_react3.Children.only(children); return (0, import_react3.cloneElement)(firstChild, { // @ts-expect-error children: typeof content === "function" ? ( // @ts-expect-error content(firstChild.props.children) ) : content }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createContext, getSubtree, getValidChildren, mergeRefs });