UNPKG

@amaui/style-react

Version:

Amaui CSS in JS styling solution for React

149 lines (131 loc) 5.44 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } import React from 'react'; import is from '@amaui/utils/is'; import merge from '@amaui/utils/merge'; import { reset as amauiResetMethod } from '@amaui/style'; import { useAmauiStyle, useAmauiTheme } from '.'; import { propsAreNew } from './style'; export default function reset() { let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; let options_ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; const responses = []; const { name = 'Reset' } = options_; function useReset(props_) { const amauiStyle = useAmauiStyle(); const amauiTheme = useAmauiTheme(); const refs = { update: React.useRef(undefined) }; const resolve = function () { let theme = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : amauiTheme; let valueNew = value; if (is('function', value)) valueNew = value(theme); // Add style add & overrides if (amauiTheme.ui?.elements?.[name]?.style) { const { add, override } = amauiTheme.ui.elements[name].style; // Add if (add) { const object = is('function', add) ? add(theme) : add; valueNew = merge(object, valueNew, { copy: true }); } // Override if (override) { const object = is('function', override) ? override(theme) : override; valueNew = _objectSpread(_objectSpread({}, valueNew), object); } } return valueNew; }; // Updates for amauiTheme const method = React.useCallback((updateValue, updatedTheme) => { if (is('function', value)) { const valueNew = resolve(updatedTheme); // Update if (response?.update !== undefined) response.update(valueNew); } }, []); const makeResponse = () => { let response_; // Method // If it's a new instance of amauiTheme // make a new responses with it response_ = responses.find(item => item.amauiTheme.id === amauiTheme.id); if (response_) return response_; // If there's not add a new response and use it const options = { amaui_style: { value: undefined }, amaui_theme: { value: undefined } }; // AmauiStyle if (amauiStyle !== undefined) options.amaui_style.value = amauiStyle; // AmauiTheme if (amauiTheme !== undefined) options.amaui_theme.value = amauiTheme; response_ = amauiResetMethod(resolve(), merge(options, options_, { copy: true })); // Add the amauiTheme to the response_ response_.amauiTheme = amauiTheme; // Add value to the responses responses.push(response_); // Update if (amauiTheme) amauiTheme.subscriptions.update.subscribe(method); return response_; }; const response = React.useState(makeResponse())[0]; let props = props_; if (is('object', props)) { const newProps = {}; const allowed = Object.keys(props).filter(prop => is('array', props[prop]) ? !props[prop].some(item => /*#__PURE__*/React.isValidElement(item)) : ! /*#__PURE__*/React.isValidElement(props[prop])); allowed.forEach(prop => newProps[prop] = props[prop]); props = newProps; } const [values, setValues] = React.useState(() => response.add(props)); // Add React.useEffect(() => { if (!values || ['refresh'].includes(refs.update.current)) setValues(() => { refs.update.current = undefined; return response.add(props); }); // Clean up return () => { // Remove response?.remove(values?.ids?.dynamic); // Refresh refs.update.current = 'refresh'; // Remove response from the responses // if users is 0 in amauiStyleSheetManager if (!response?.amaui_style_sheet_manager?.users) { const index = responses.findIndex(item => item.amauiTheme.id === amauiTheme.id); if (index > -1) { responses.splice(index, 1); // Unsubscribe if (amauiTheme) amauiTheme.subscriptions.update.unsubscribe(method); } } }; }, []); // Update props React.useEffect(() => { if (response !== undefined && values?.ids) response.props = { ids: values.ids.dynamic, props }; // Only 1 lvl of values }, [propsAreNew(props)]); return values; } return useReset; }