UNPKG

10.2 kBJavaScriptView Raw
1function _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); }
2
3import "core-js/modules/es.array.concat.js";
4import "core-js/modules/es.array.map.js";
5
6function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
7
8function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
9
10function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
11
12function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
13
14function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
15
16function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
17
18function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
19
20function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
21
22function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
23
24function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
25
26function _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; }
27
28import React, { Children, Component, Fragment, memo } from 'react';
29import { styled } from '@storybook/theming';
30import { sanitize } from '@storybook/csf';
31import { Placeholder } from '../placeholder/placeholder';
32import { FlexBar } from '../bar/bar';
33import { TabButton } from '../bar/button';
34var ignoreSsrWarning = '/* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */';
35var Wrapper = styled.div(function (_ref) {
36 var theme = _ref.theme,
37 bordered = _ref.bordered;
38 return bordered ? {
39 backgroundClip: 'padding-box',
40 border: "1px solid ".concat(theme.appBorderColor),
41 borderRadius: theme.appBorderRadius,
42 overflow: 'hidden',
43 boxSizing: 'border-box'
44 } : {};
45}, function (_ref2) {
46 var absolute = _ref2.absolute;
47 return absolute ? {
48 width: '100%',
49 height: '100%',
50 boxSizing: 'border-box',
51 display: 'flex',
52 flexDirection: 'column'
53 } : {
54 display: 'block'
55 };
56});
57export var TabBar = styled.div({
58 overflow: 'hidden',
59 '&:first-of-type': {
60 marginLeft: 0
61 }
62});
63var Content = styled.div({
64 display: 'block',
65 position: 'relative'
66}, function (_ref3) {
67 var theme = _ref3.theme;
68 return {
69 fontSize: theme.typography.size.s2 - 1,
70 background: theme.background.content
71 };
72}, function (_ref4) {
73 var bordered = _ref4.bordered,
74 theme = _ref4.theme;
75 return bordered ? {
76 borderRadius: "0 0 ".concat(theme.appBorderRadius - 1, "px ").concat(theme.appBorderRadius - 1, "px")
77 } : {};
78}, function (_ref5) {
79 var absolute = _ref5.absolute,
80 bordered = _ref5.bordered;
81 return absolute ? _defineProperty({
82 height: "calc(100% - ".concat(bordered ? 42 : 40, "px)"),
83 position: 'absolute',
84 left: 0 + (bordered ? 1 : 0),
85 right: 0 + (bordered ? 1 : 0),
86 bottom: 0 + (bordered ? 1 : 0),
87 top: 40 + (bordered ? 1 : 0),
88 overflow: 'auto'
89 }, "& > *:first-child".concat(ignoreSsrWarning), {
90 position: 'absolute',
91 left: 0 + (bordered ? 1 : 0),
92 right: 0 + (bordered ? 1 : 0),
93 bottom: 0 + (bordered ? 1 : 0),
94 top: 0 + (bordered ? 1 : 0),
95 height: "calc(100% - ".concat(bordered ? 2 : 0, "px)"),
96 overflow: 'auto'
97 }) : {};
98});
99var VisuallyHidden = styled.div(function (_ref7) {
100 var active = _ref7.active;
101 return active ? {
102 display: 'block'
103 } : {
104 display: 'none'
105 };
106});
107export var TabWrapper = function TabWrapper(_ref8) {
108 var active = _ref8.active,
109 render = _ref8.render,
110 children = _ref8.children;
111 return /*#__PURE__*/React.createElement(VisuallyHidden, {
112 active: active
113 }, render ? render() : children);
114};
115TabWrapper.displayName = "TabWrapper";
116export var panelProps = {};
117
118var childrenToList = function childrenToList(children, selected) {
119 return Children.toArray(children).map(function (_ref9, index) {
120 var _ref9$props = _ref9.props,
121 title = _ref9$props.title,
122 id = _ref9$props.id,
123 color = _ref9$props.color,
124 childrenOfChild = _ref9$props.children;
125 var content = Array.isArray(childrenOfChild) ? childrenOfChild[0] : childrenOfChild;
126 return {
127 active: selected ? id === selected : index === 0,
128 title: title,
129 id: id,
130 color: color,
131 render: typeof content === 'function' ? content : function (_ref10) {
132 var active = _ref10.active,
133 key = _ref10.key;
134 return /*#__PURE__*/React.createElement(VisuallyHidden, {
135 key: key,
136 active: active,
137 role: "tabpanel"
138 }, content);
139 }
140 };
141 });
142};
143
144export var Tabs = /*#__PURE__*/memo(function (_ref11) {
145 var children = _ref11.children,
146 selected = _ref11.selected,
147 actions = _ref11.actions,
148 absolute = _ref11.absolute,
149 bordered = _ref11.bordered,
150 tools = _ref11.tools,
151 backgroundColor = _ref11.backgroundColor,
152 htmlId = _ref11.id;
153 var list = childrenToList(children, selected);
154 return list.length ? /*#__PURE__*/React.createElement(Wrapper, {
155 absolute: absolute,
156 bordered: bordered,
157 id: htmlId
158 }, /*#__PURE__*/React.createElement(FlexBar, {
159 border: true,
160 backgroundColor: backgroundColor
161 }, /*#__PURE__*/React.createElement(TabBar, {
162 role: "tablist"
163 }, list.map(function (_ref12) {
164 var title = _ref12.title,
165 id = _ref12.id,
166 active = _ref12.active,
167 color = _ref12.color;
168 var tabTitle = typeof title === 'function' ? title() : title;
169 return /*#__PURE__*/React.createElement(TabButton, {
170 id: "tabbutton-".concat(sanitize(tabTitle)),
171 className: "tabbutton ".concat(active ? 'tabbutton-active' : ''),
172 type: "button",
173 key: id,
174 active: active,
175 textColor: color,
176 onClick: function onClick(e) {
177 e.preventDefault();
178 actions.onSelect(id);
179 },
180 role: "tab"
181 }, tabTitle);
182 })), tools ? /*#__PURE__*/React.createElement(Fragment, null, tools) : null), /*#__PURE__*/React.createElement(Content, {
183 id: "panel-tab-content",
184 bordered: bordered,
185 absolute: absolute
186 }, list.map(function (_ref13) {
187 var id = _ref13.id,
188 active = _ref13.active,
189 render = _ref13.render;
190 return render({
191 key: id,
192 active: active
193 });
194 }))) : /*#__PURE__*/React.createElement(Placeholder, null, /*#__PURE__*/React.createElement(Fragment, {
195 key: "title"
196 }, "Nothing found"));
197});
198Tabs.displayName = 'Tabs';
199Tabs.defaultProps = {
200 id: null,
201 children: null,
202 tools: null,
203 selected: null,
204 absolute: false,
205 bordered: false
206};
207export var TabsState = /*#__PURE__*/function (_Component) {
208 _inherits(TabsState, _Component);
209
210 var _super = _createSuper(TabsState);
211
212 function TabsState(props) {
213 var _this;
214
215 _classCallCheck(this, TabsState);
216
217 _this = _super.call(this, props);
218 _this.handlers = {
219 onSelect: function onSelect(id) {
220 return _this.setState({
221 selected: id
222 });
223 }
224 };
225 _this.state = {
226 selected: props.initial
227 };
228 return _this;
229 }
230
231 _createClass(TabsState, [{
232 key: "render",
233 value: function render() {
234 var _this$props = this.props,
235 _this$props$bordered = _this$props.bordered,
236 bordered = _this$props$bordered === void 0 ? false : _this$props$bordered,
237 _this$props$absolute = _this$props.absolute,
238 absolute = _this$props$absolute === void 0 ? false : _this$props$absolute,
239 children = _this$props.children,
240 backgroundColor = _this$props.backgroundColor;
241 var selected = this.state.selected;
242 return /*#__PURE__*/React.createElement(Tabs, {
243 bordered: bordered,
244 absolute: absolute,
245 selected: selected,
246 backgroundColor: backgroundColor,
247 actions: this.handlers
248 }, children);
249 }
250 }]);
251
252 return TabsState;
253}(Component);
254TabsState.displayName = "TabsState";
255TabsState.defaultProps = {
256 children: [],
257 initial: null,
258 absolute: false,
259 bordered: false,
260 backgroundColor: ''
261};
\No newline at end of file