UNPKG

@catho/quantum-storybook-ui

Version:

A **Design System** is the complete set of design standards, documentation, and principles along with the toolkit (UI patterns and code components) to achieve those standards. Over time, these 'systems' are growing in popularity - a very popular one is [Q

164 lines (127 loc) 6.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _SimpleHighlight = _interopRequireDefault(require("../SimpleHighlight")); var _Title = _interopRequireDefault(require("../Title")); var _shared = require("../shared"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _templateObject() { var data = _taggedTemplateLiteral(["\n padding: 0;\n margin: 25px 0;\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) } })); } var MainTitle = (0, _styledComponents["default"])(_Title["default"])(_templateObject()); var formatJSON = function formatJSON(key, value) { if (typeof value === 'function') return '() => {}'; if (typeof value === 'string') return "'".concat(value, "'"); return value; }; var INDENTATION_SIZE = 2; var spaces = function spaces(size) { return ' '.repeat(size); }; var jsonStr = function jsonStr(json, indentation) { return JSON.stringify(json, formatJSON, INDENTATION_SIZE).replace(/\n/g, "\n".concat(indentation).concat(spaces(INDENTATION_SIZE))).replace(/"/g, ''); }; var renderPropValue = function renderPropValue(propValue, indentation) { if (_typeof(propValue) === 'object' && propValue instanceof RegExp) { return "{".concat(propValue, "}"); } var types = { "function": function _function() { return '{() => {}}'; }, string: function string(prop) { return "\"".concat(prop, "\""); }, number: function number(prop) { return "{".concat(prop, "}"); }, object: function object(prop) { return "{".concat(jsonStr(prop, indentation), "}"); }, instanceOf: function instanceOf(prop) { return "{".concat(prop, "}"); } }; var fn = types[_typeof(propValue)]; return fn ? fn(propValue) : propValue; }; function getProps(props, indentation) { var breakline = "\n".concat(indentation).concat(spaces(INDENTATION_SIZE)); return Object.entries(props).filter(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), name = _ref2[0], value = _ref2[1]; return value && !_shared.FORBIDDEN_PROPS.includes(name); }).map(function (_ref3, index) { var _ref4 = _slicedToArray(_ref3, 2), prop = _ref4[0], value = _ref4[1]; var propText = typeof value === 'boolean' ? prop : "".concat(prop, "=").concat(renderPropValue(value, indentation)); return "".concat(index === 0 ? breakline : '').concat(propText); }).join(breakline); } var componentToString = function componentToString(component, state) { var level = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; var content; var indentation = spaces(level); if (_typeof(component) === 'object') { var type = component.type, props = component.props; var name = type.displayName || type.name || type; var children = props ? props.children : null; content = "".concat(indentation, "<").concat(name).concat(Object.keys(state).length ? getProps(state, indentation) : ''); content += children ? ">\n".concat(_react["default"].Children.map(children, function (child) { return componentToString(child, child.props, level + INDENTATION_SIZE); }).join('\n'), "\n").concat(indentation, "</").concat(name, ">") : "\n".concat(indentation, "/>"); } else { content = component ? "".concat(indentation).concat(component) : ''; } return content; }; var msg = function msg(importModules) { return "import ".concat('{', " ", importModules, " ", '}', " from '@catho/quantum';\n\n"); }; var CodeExample = function CodeExample(_ref5) { var component = _ref5.component, state = _ref5.state, code = _ref5.code, withImport = _ref5.withImport, title = _ref5.title; var codeStr = code || componentToString(component, state || component.props); return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, title && /*#__PURE__*/_react["default"].createElement(MainTitle, { as: "h2" }, title), /*#__PURE__*/_react["default"].createElement(_SimpleHighlight["default"], null, withImport && msg(withImport), codeStr)); }; CodeExample.defaultProps = { code: '', title: '', withImport: '', state: null, component: {} }; CodeExample.propTypes = { component: _propTypes["default"].instanceOf(Object), state: _propTypes["default"].instanceOf(Object), code: _propTypes["default"].string, title: _propTypes["default"].string, withImport: _propTypes["default"].string }; var _default = CodeExample; exports["default"] = _default;