storybook-addon-customize-antd-theme
Version:
a Storybook addon help you visually customize an ant design theme
229 lines (203 loc) • 10.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = ArgsTable;
exports.TableWrapper = void 0;
var _react = _interopRequireDefault(require("react"));
var _theming = require("@storybook/theming");
var _polished = require("polished");
var _SectionRow = _interopRequireDefault(require("./SectionRow"));
var _ArgRow = _interopRequireDefault(require("./ArgRow"));
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) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var TableWrapper = _theming.styled.table(function (_ref) {
var _trLastChild$concat, _;
var theme = _ref.theme,
compact = _ref.compact,
inAddonPanel = _ref.inAddonPanel;
return {
'&&': (_ = {
// Resets for cascading/system styles
borderCollapse: 'collapse',
borderSpacing: 0,
color: theme.color.defaultText,
'td, th': {
padding: 0,
border: 'none',
verticalAlign: 'top',
textOverflow: 'ellipsis'
},
// End Resets
fontSize: theme.typography.size.s2 - 1,
lineHeight: '20px',
textAlign: 'left',
width: '100%',
borderTop: '1px solid #e6e6e6',
// Margin collapse
marginTop: inAddonPanel ? 0 : 25,
marginBottom: inAddonPanel ? 0 : 40,
'thead>tr>th': {
background: '#DDDDDD',
border: 'unset'
},
'thead th:first-of-type, td:first-of-type': {
// intentionally specify thead here
width: '25%'
},
'th:first-of-type, td:first-of-type': {
paddingLeft: 20
},
'th:nth-of-type(2), td:nth-of-type(2)': _objectSpread({}, compact ? null : {
// Description column
width: '35%'
}),
'td:nth-of-type(3)': _objectSpread({}, compact ? null : {
// Defaults column
width: '15%'
}),
'th:last-of-type, td:last-of-type': _objectSpread({
paddingRight: 20
}, compact ? null : {
// Controls column
width: '25%'
}),
th: {
color: theme.base === 'light' ? (0, _polished.transparentize)(0.25, theme.color.defaultText) : (0, _polished.transparentize)(0.45, theme.color.defaultText),
paddingTop: 10,
paddingBottom: 10,
paddingLeft: 15,
paddingRight: 15
},
td: {
paddingTop: '10px',
paddingBottom: '10px',
'&:not(:first-of-type)': {
paddingLeft: 15,
paddingRight: 15
},
'&:last-of-type': {
paddingRight: 20
}
},
// Table "block" styling
// Emphasize tbody's background and set borderRadius
// Calling out because styling tables is finicky
// Makes border alignment consistent w/other DocBlocks
marginLeft: inAddonPanel ? 0 : 1,
marginRight: inAddonPanel ? 0 : 1
}, _defineProperty(_, "tr:last-child".concat(_theming.ignoreSsrWarning), (_trLastChild$concat = {}, _defineProperty(_trLastChild$concat, "td:first-child".concat(_theming.ignoreSsrWarning), {
borderBottomLeftRadius: inAddonPanel ? 0 : theme.appBorderRadius
}), _defineProperty(_trLastChild$concat, "td:last-child".concat(_theming.ignoreSsrWarning), {
borderBottomRightRadius: inAddonPanel ? 0 : theme.appBorderRadius
}), _trLastChild$concat)), _defineProperty(_, "tbody", {
// slightly different than the other DocBlock shadows to account for table styling gymnastics
boxShadow: !inAddonPanel && (theme.base === 'light' ? "rgba(0, 0, 0, 0.10) 0 1px 3px 1px,\n ".concat((0, _polished.transparentize)(0.035, theme.appBorderColor), " 0 0 0 1px") : "rgba(0, 0, 0, 0.20) 0 2px 5px 1px,\n ".concat((0, _polished.opacify)(0.05, theme.appBorderColor), " 0 0 0 1px")),
borderRadius: theme.appBorderRadius,
// for safari only
// CSS hack courtesy of https://stackoverflow.com/questions/16348489/is-there-a-css-hack-for-safari-only-not-chrome
'@media not all and (minResolution:.001dpcm)': {
'@supports (webkitAppearance:none)': {
borderWidth: 1,
borderStyle: 'solid',
borderColor: !inAddonPanel && (theme.base === 'light' ? (0, _polished.transparentize)(0.035, theme.appBorderColor) : (0, _polished.opacify)(0.05, theme.appBorderColor))
}
},
tr: _objectSpread({
background: 'transparent',
overflow: 'hidden'
}, inAddonPanel ? {
borderTopWidth: 1,
borderTopStyle: 'solid',
borderTopColor: theme.base === 'light' ? (0, _polished.darken)(0.1, theme.background.content) : (0, _polished.lighten)(0.05, theme.background.content)
} : _defineProperty({}, "&:not(:first-child".concat(_theming.ignoreSsrWarning, ")"), {
borderTopWidth: 1,
borderTopStyle: 'solid',
borderTopColor: theme.base === 'light' ? (0, _polished.darken)(0.1, theme.background.content) : (0, _polished.lighten)(0.05, theme.background.content)
})),
td: {
background: theme.background.content
}
}), _)
};
});
exports.TableWrapper = TableWrapper;
var groupRows = function groupRows(rows) {
var sections = {
ungrouped: [],
sections: {}
};
if (!rows) return sections;
Object.entries(rows).forEach(function (_ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
key = _ref4[0],
row = _ref4[1];
var category = row.category;
if (category) {
var section = sections.sections[category] || {
ungrouped: []
};
section.ungrouped.push(_objectSpread({
key: key
}, row));
sections.sections[category] = section;
} else {
sections.ungrouped.push(_objectSpread({
key: key
}, row));
}
});
return sections;
};
function ArgsTable(_ref5) {
var rows = _ref5.rows,
args = _ref5.args,
updateArgs = _ref5.updateArgs,
resetArgs = _ref5.resetArgs;
var groups = groupRows(rows);
var expandable = Object.keys(groups.sections).length > 0;
var colSpan = 4; // 实现按category展示,默认收起
var initialExpanded = Object.keys(rows).length < 40; // item number less then 40 expanded
return /*#__PURE__*/_react["default"].createElement(TableWrapper, {
className: "docblock-argstable"
}, /*#__PURE__*/_react["default"].createElement("thead", {
className: "docblock-argstable-head"
}, /*#__PURE__*/_react["default"].createElement("tr", null, /*#__PURE__*/_react["default"].createElement("th", null, "Name"), /*#__PURE__*/_react["default"].createElement("th", null, "Description"), /*#__PURE__*/_react["default"].createElement("th", null, "Default"), /*#__PURE__*/_react["default"].createElement("th", null, "Value"))), /*#__PURE__*/_react["default"].createElement("tbody", {
className: "docblock-argstable-body"
}, groups.ungrouped.map(function (row) {
return /*#__PURE__*/_react["default"].createElement(_ArgRow["default"], {
key: row.key,
expandable: expandable,
row: row,
arg: args && args[row.key],
updateArgs: updateArgs
});
}), Object.entries(groups.sections).map(function (_ref6) {
var _ref7 = _slicedToArray(_ref6, 2),
category = _ref7[0],
section = _ref7[1];
return /*#__PURE__*/_react["default"].createElement(_SectionRow["default"], {
key: category,
label: category,
level: "subsection",
colSpan: colSpan,
initialExpanded: initialExpanded
}, section.ungrouped.map(function (row) {
return /*#__PURE__*/_react["default"].createElement(_ArgRow["default"], {
key: row.key,
expandable: expandable,
row: row,
arg: args && args[row.key],
updateArgs: updateArgs
});
}));
})));
}