UNPKG

@pubsweet/ui-toolkit

Version:

Helper functions and reusable blocks for use with @pubsweet/ui

69 lines (50 loc) 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _styledComponents = require("styled-components"); var _lodash = require("lodash"); var _themeHelper = require("./themeHelper"); function _templateObject2() { var data = _taggedTemplateLiteral(["\n ", ";\n "]); _templateObject2 = function _templateObject2() { return data; }; return data; } function _templateObject() { var data = _taggedTemplateLiteral(["\n ", ";\n "]); _templateObject = function _templateObject() { return data; }; return data; } function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } /* Will be using ui.Button as an example component override to explain the code. */ var override = function override(name) { var overrideKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cssOverrides'; return function (props) { // Find (props.theme.cssOverrides.) ui.Button var target = (0, _lodash.get)(props.theme[overrideKey], name); // ui.Button is not there. if (!target) return null; // css`` functions from styled components come in as arrays var isStyledCss = Array.isArray(target); var hasRoot = (0, _lodash.has)(target, 'Root'); /* ui.Button is there, but there is no ui.Button.Root or ui.Button: css``. This also covers the case where you only target children of the component, eg. if your override looks like ui.Button = { Icon: css`` }. In this case, there would be no overrides for ui.Button, but only for ui.Button.Icon, which would have its own override. */ if (!isStyledCss && !hasRoot) return null; // ui.Button.Root exists if (hasRoot) { return (0, _styledComponents.css)(_templateObject(), (0, _themeHelper.th)("".concat(overrideKey, ".").concat(name, ".Root"))); } // ui.Button: css`` exists return (0, _styledComponents.css)(_templateObject2(), (0, _themeHelper.th)("".concat(overrideKey, ".").concat(name))); }; }; var _default = override; exports["default"] = _default;