@onesy/style-react
Version:
Onesy CSS in JS styling solution for React
150 lines (132 loc) • 5.28 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React from 'react';
import is from '@onesy/utils/is';
import merge from '@onesy/utils/merge';
import { pure as onesyPureMethod } from '@onesy/style';
import { useOnesyStyle, useOnesyTheme } from '.';
import { propsAreNew } from './style';
export default function pure(value) {
let options_ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
const responses = [];
const {
name
} = options_;
function usePure(props_) {
const onesyStyle = useOnesyStyle();
const onesyTheme = useOnesyTheme();
const refs = {
update: React.useRef(undefined)
};
const resolve = function () {
let theme = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : onesyTheme;
let valueNew = value;
if (is('function', value)) valueNew = value(theme);
// Add style add & overrides
if (onesyTheme.ui?.elements?.[name]?.style) {
const {
add,
override
} = onesyTheme.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 onesyTheme
const method = React.useCallback((updateValue, updatedTheme) => {
if (is('function', value)) {
const valueNew = resolve(updatedTheme);
// Update
if (response?.update !== undefined) response.update(valueNew);
}
}, []);
const makeResponse = () => {
// Object
let response_ = is('object', value) && responses[0];
if (response_) return response_;
// Method
// If it's a new instance of onesyTheme
// make a new responses with it
response_ = responses.find(item => item.onesyTheme.id === onesyTheme.id);
if (response_) return response_;
// If there's not add a new response and use it
const options = {
onesy_style: {
value: undefined
},
onesy_theme: {
value: undefined
}
};
// OnesyStyle
if (onesyStyle !== undefined) options.onesy_style.value = onesyStyle;
// OnesyTheme
if (onesyTheme !== undefined) options.onesy_theme.value = onesyTheme;
response_ = onesyPureMethod(resolve(), merge(options, options_, {
copy: true
}));
// Add the onesyTheme to the response_
response_.onesyTheme = onesyTheme;
// Add value to the responses
responses.push(response_);
// Update
if (onesyTheme) onesyTheme.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 onesyStyleSheetManager
if (!response?.onesy_style_sheet_manager?.users) {
const index = responses.findIndex(item => item.onesyTheme.id === onesyTheme.id);
if (index > -1) {
responses.splice(index, 1);
// Unsubscribe
if (onesyTheme) onesyTheme.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 usePure;
}