@vitus-labs/core
Version:
Core and utility functions for vitus-labs packages
277 lines (269 loc) • 5.7 kB
JavaScript
export { get, merge, omit, pick, set, throttle } from 'lodash-es';
export { default as memoize } from 'moize';
import { ThemeProvider, styled, css } from 'styled-components';
import React, { createContext, useMemo, isValidElement, createElement, cloneElement } from 'react';
import { isFragment, isValidElementType } from 'react-is';
class Configuration {
css;
styled;
ExternalProvider;
component = 'div';
textComponent = 'span';
constructor(props) {
this.css = props.css;
this.styled = props.styled;
this.ExternalProvider = props.provider;
this.component = props.component;
this.textComponent = props.textComponent;
}
init = (props) => {
if (props.css) {
this.css = props.css;
}
if (props.styled) {
this.styled = props.styled;
}
if (props.provider) {
this.ExternalProvider = props.provider;
}
if (props.component) {
this.component = props.component;
}
if (props.textComponent) {
this.textComponent = props.textComponent;
}
};
}
const defaultParams = {
css,
styled,
provider: ThemeProvider,
component: 'div',
textComponent: 'span',
};
const config = new Configuration(defaultParams);
const { init } = config;
const isEmpty = (param) => {
if (!param || param === null)
return true;
if (typeof param !== 'object') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return true;
}
if (Array.isArray(param) && param.length === 0) {
return true;
}
if (Object.entries(param).length === 0 && param.constructor === Object) {
return true;
}
return false;
};
const context = createContext({});
const VitusLabsProvider = context.Provider;
const Provider = ({ theme, children, ...props }) => {
const ExternalProvider = useMemo(() => config.ExternalProvider, []);
const context = useMemo(() => ({ theme, ...props }), [theme, props]);
// eslint-disable-next-line react/jsx-no-useless-fragment
if (isEmpty(theme) || !theme)
return React.createElement(React.Fragment, null, children);
if (ExternalProvider) {
return (React.createElement(VitusLabsProvider, { value: context },
React.createElement(ExternalProvider, { theme: theme }, children)));
}
return React.createElement(VitusLabsProvider, { value: context }, children);
};
const compose = (...fns) => (p) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
fns.reduceRight((acc, cur) => cur(acc), p);
const render = (content, attachProps) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
if (!content)
return null;
const isValidEl = isValidElement(content);
const render = (child) => attachProps ? createElement(child, attachProps) : createElement(child);
if (typeof content === 'string' && isValidEl) {
return render(content);
}
if (['number', 'boolean', 'bigint', 'string'].includes(typeof content)) {
return content;
}
if (Array.isArray(content) || isFragment(content)) {
return content;
}
if (isValidElementType(content)) {
return render(content);
}
if (isValidEl) {
if (isEmpty(attachProps)) {
return content;
}
return cloneElement(content, attachProps);
}
return content;
};
const HTML_TAGS = [
'a',
'abbr',
// 'acronym',
'address',
// 'applet',
'area',
'article',
'aside',
'audio',
'b',
// 'base',
// 'basefont',
'bdi',
'bdo',
'big',
'blockquote',
'body',
'br',
'button',
'canvas',
'caption',
// 'center',
'cite',
'code',
'col',
'colgroup',
'data',
'datalist',
'dd',
'del',
'details',
'dfn',
'dialog',
// 'dir',
'div',
'dl',
'dt',
'em',
'embed',
'fieldset',
'figcaption',
'figure',
// 'font',
'footer',
'form',
// 'frame',
// 'frameset',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
// 'head',
'header',
'hr',
'html',
'i',
'iframe',
'img',
'input',
'ins',
'kbd',
'label',
'legend',
'li',
// 'link',
'main',
'map',
'mark',
// 'meta',
'meter',
'nav',
// 'noframes',
// 'noscript',
'object',
'ol',
'optgroup',
'option',
'output',
'p',
// 'param',
'picture',
'pre',
'progress',
'q',
'rp',
'rt',
'ruby',
's',
'samp',
// 'script',
'section',
'select',
'small',
'source',
'span',
// 'strike',
'strong',
// 'style',
'sub',
'summary',
'sup',
'svg',
'table',
'tbody',
'td',
'template',
'textarea',
'tfoot',
'th',
'thead',
'time',
// 'title',
'tr',
'track',
// 'tt',
'u',
'ul',
'var',
'video',
'wbr',
];
const HTML_TEXT_TAGS = [
'abbr',
'b',
'bdi',
'bdo',
'big',
'blockquote',
'cite',
'code',
'dl',
'dt',
'em',
'figcaption',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'i',
'ins',
'kbd',
'label',
'legend',
'li',
'p',
'pre',
'q',
'rp',
'rt',
's',
'small',
'span',
'strong',
'sub',
'summary',
'sup',
'time',
'u',
];
export { HTML_TAGS, HTML_TEXT_TAGS, Provider, compose, config, context, init, isEmpty, render };
//# sourceMappingURL=index.js.map