UNPKG

@meonode/ui

Version:

A structured approach to component composition with built-in theming, prop separation, and dynamic children handling.

70 lines 6.92 kB
"use strict";function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}import{isContextConsumer,isContextProvider,isElement,isForwardRef,isFragment,isLazy,isMemo,isPortal,isProfiler,isReactClassComponent,isStrictMode,isSuspense,isSuspenseList}from"./react-is.helper.js";import cssProperties from"./data/css-properties";/** * Returns a string describing the type of a given React component or element. * * Checks for common React types (class, forwardRef, memo, etc.) and returns a string * such as 'class', 'forwardRef', 'memo', 'object-with-render', 'function', or other * React-specific types. Falls back to `typeof` or 'unknown' if not recognized. * @param component The React component, element type, or element-like object to check. * @returns A string describing the component type. * @example * getComponentType(class extends React.Component {}) // 'class' * getComponentType(React.forwardRef(() => <div/>)) // 'forwardRef' * getComponentType(React.memo(() => <div/>)) // 'memo' * getComponentType(() => <div/>) // 'function' */export var getComponentType=function getComponentType(a){return isForwardRef(a)?"forwardRef":isMemo(a)?"memo":isFragment(a)?"fragment":isPortal(a)?"portal":isProfiler(a)?"profiler":isStrictMode(a)?"strict-mode":isSuspense(a)?"suspense":isSuspenseList(a)?"suspense-list":isContextConsumer(a)?"context-consumer":isContextProvider(a)?"context-provider":isLazy(a)?"lazy":isElement(a)?"element":isReactClassComponent(a)?"class":_typeof(a)};/** * Generates a string name for an ElementType or ReactElement. * * This function attempts to extract a meaningful name from a React ElementType * (string, function, class, HOC) or a ReactElement instance. * It prioritizes `displayName` and `name` properties and unwraps HOCs like * `React.memo` and `React.forwardRef` to get the underlying component name. * * If a name cannot be determined, it returns a fallback like 'UnknownElementType' or 'AnonymousComponent'. * @param node The ElementType or ReactElement (e.g., 'div', MyComponent, <MyComponent />). * @returns A string representation of the element type's name. */export function getElementTypeName(a){var b,c;function getDisplayName(a,b){var c=(null===a||void 0===a?void 0:a.displayName)||(null===a||void 0===a?void 0:a.name);return!c||"render"===c?b:c}if(null===a||a===void 0)return"UnknownElementType";var d=a,e=getComponentType(d);switch(e){case"string":return a;case"class":return getDisplayName(d,"ClassComponent");case"function":return getDisplayName(d,"AnonymousFunctionComponent");case"forwardRef":return getDisplayName(d,"")||getDisplayName(d.render,"")||"ForwardRefComponent";case"memo":return getDisplayName(d,"")||(d.type?getElementTypeName(d.type):"MemoComponent");case"element":return getElementTypeName(d.type);case"fragment":return"Fragment";case"portal":return"Portal";case"profiler":return getDisplayName(d,"Profiler");case"strict-mode":return"StrictMode";case"suspense":return getDisplayName(d,"Suspense");case"suspense-list":return"SuspenseList";case"context-consumer":return null!==(b=d._context)&&void 0!==b&&b.displayName?"".concat(d._context.displayName,".Consumer"):"ContextConsumer";case"context-provider":return null!==(c=d._context)&&void 0!==c&&c.displayName?"".concat(d._context.displayName,".Provider"):"ContextProvider";case"lazy":return getDisplayName(d,"LazyComponent");case"object":return getDisplayName(d,"")?getDisplayName(d,""):"function"==typeof d.render?getDisplayName(d.render,"ObjectWithRender"):d.type&&d.type!==a?"Wrapped<".concat(getElementTypeName(d.type),">"):getDisplayName(d,"ObjectComponent");case"symbol":if("symbol"===_typeof(a)){var f;return(null===(f=a.description)||void 0===f?void 0:f.replace(/^react\./,"").split(".").map(function(a){var b;return(null===(b=a[0])||void 0===b?void 0:b.toUpperCase())+a.slice(1)}).join(""))||a.toString()}return"SymbolComponent";case"unknown":return"UnknownElementType";default:return"UnsupportedType<".concat(e,">")}}/** * A set of valid CSS property names in camelCase, including CSS custom properties, used for validation. * This set contains all CSS properties including non-standard vendor prefixed properties. */export var CSSPropertySet=new Set(cssProperties);/** * Filters an object to only include valid CSS properties * @param props The object containing potential CSS properties * @returns An object containing only valid CSS properties * @example * ```ts * getCSSProps({ * backgroundColor: 'red', * invalid: true * }) // { backgroundColor: 'red' } * ``` */export function getCSSProps(a){var b={};for(var c in a)Object.prototype.hasOwnProperty.call(a,c)&&CSSPropertySet.has(c)&&(b[c]=a[c]);return b}/** * Filters component props to include only valid DOM properties and attributes. * * This function iterates through the provided props and retains only those that * are not CSS properties (as determined by `cssPropertySet`). This is useful for * separating style-related props from standard DOM attributes when rendering * elements. * @ty E - The type of the React element. * @typeParam T - The type of the component props. * @param props The component props to filter. * @returns An object containing only valid DOM props. */export function getDOMProps(a){var b={};for(var c in a)Object.prototype.hasOwnProperty.call(a,c)&&!CSSPropertySet.has(c)&&(b[c]=a[c]);return b}/** * Retrieves a deeply nested value from an object using a dot-separated string path. * * This function traverses an object based on the provided path, which is a * string of keys separated by dots. It returns the value found at the end of * the path or `undefined` if any key in the path is not found or if the object * is nullish at any point during traversal. * @param obj The object to traverse. Defaults to an empty object if not provided. * @param path The dot-separated path string (e.g., 'background.primary'). * @returns The value at the specified path, or undefined if not found. */export function getValueByPath(){var a=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{},b=1<arguments.length?arguments[1]:void 0;return b.split(".").reduce(function(a,b){return null===a||void 0===a?void 0:a[b]},a)}/** * Type guard to check if an object is a NodeInstance. * * A NodeInstance is expected to be a non-null object with: * - an 'element' property, * - a 'render' method, * - a 'toPortal' method, * - and an 'isBaseNode' property. * @param obj The object to check. * @returns True if the object is a NodeInstance, false otherwise. */export var isNodeInstance=function isNodeInstance(a){return"object"===_typeof(a)&&null!==a&&"element"in a&&"function"==typeof a.render&&"function"==typeof a.toPortal&&"isBaseNode"in a};