@onesy/style-react
Version:
Onesy CSS in JS styling solution for React
80 lines (76 loc) • 3.08 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
const _excluded = ["ref", "root", "remove", "value", "children"];
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 { OnesyStyle, makeClassName, unit, rtl, sort, valueObject, prefix } from '@onesy/style';
import StyleContext from './Context';
import { jsx as _jsx } from "react/jsx-runtime";
function makeOnesyStyle(element) {
const onesyStyle = new OnesyStyle({
element
});
// Add all the plugins
onesyStyle.plugins.add = [unit, makeClassName, prefix, sort, rtl, valueObject];
return onesyStyle;
}
const Style = props => {
const {
ref,
root = false,
remove,
value: value_,
children
} = props,
other = _objectWithoutProperties(props, _excluded);
const refs = {
root: React.useRef(undefined)
};
const [value, setValue] = React.useState(() => {
if (value_ === undefined || !(value_ instanceof OnesyStyle)) return makeOnesyStyle();
value_.remove = remove;
return value_;
});
React.useEffect(() => {
if (refs.root.current) {
value.element = refs.root.current;
value.remove = remove;
// Init
value.init();
const valueNew = new OnesyStyle();
// Copy over from value
Object.keys(value).forEach(prop => valueNew[prop] = value[prop]);
setValue(valueNew);
}
}, []);
const update = updateValue => {
if (updateValue !== undefined) {
const valueNew_0 = new OnesyStyle();
valueNew_0.remove = remove;
Object.keys(value).forEach(prop_0 => valueNew_0[prop_0] = value[prop_0]);
is('object', updateValue) && Object.keys(updateValue).forEach(prop_1 => valueNew_0[prop_1] = updateValue[prop_1]);
setValue(valueNew_0);
return valueNew_0;
}
};
// Update method
value.updateWithRerender = update;
if (root) return /*#__PURE__*/_jsx(StyleContext.Provider, {
value: value,
children: /*#__PURE__*/_jsx("div", _objectSpread(_objectSpread({
ref: item => {
refs.root.current = item;
if (ref?.current) ref.current = item;
}
}, other), {}, {
children: children
}))
});
return /*#__PURE__*/_jsx(StyleContext.Provider, {
value: value,
children: children
});
};
export default Style;