@rescui/switcher 0.9.8 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,264 @@
1
+ import "core-js/modules/es.function.name.js";
2
+ import "core-js/modules/es.regexp.exec.js";
3
+ import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
4
+ import _Array$from from "@babel/runtime-corejs3/core-js-stable/array/from";
5
+ import _Symbol from "@babel/runtime-corejs3/core-js-stable/symbol";
6
+ import _getIteratorMethod from "@babel/runtime-corejs3/core-js/get-iterator-method";
7
+ import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
8
+ import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
9
+ import "core-js/modules/es.object.to-string.js";
10
+
11
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof _Symbol !== "undefined" && _getIteratorMethod(o) || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
12
+
13
+ function _unsupportedIterableToArray(o, minLen) { var _context; if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = _sliceInstanceProperty(_context = Object.prototype.toString.call(o)).call(_context, 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); }
14
+
15
+ 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; }
16
+
17
+ import React, { createContext, useRef, useCallback, useState, useEffect, useContext, cloneElement } from 'react';
18
+ import PropTypes from 'prop-types';
19
+ import cn from 'classnames';
20
+ import { useThemeWithUndefined } from '@rescui/ui-contexts';
21
+ import { useResizeObserver } from '@rescui/use-resize-observer';
22
+ import styles from './vertical-switcher.p.module.css.js'; // eslint-disable-next-line @typescript-eslint/no-empty-function
23
+
24
+ var noop = function noop() {};
25
+
26
+ var verticalSwitcherContext = /*#__PURE__*/createContext({
27
+ switcherValue: null,
28
+ onChange: noop,
29
+ handleOptionRef: function handleOptionRef() {
30
+ return noop;
31
+ },
32
+ disabled: null
33
+ });
34
+ var sizeToClass = {
35
+ l: styles.sizeL,
36
+ m: styles.sizeM,
37
+ s: styles.sizeS,
38
+ xs: styles.sizeXS
39
+ };
40
+ var themeToClass = {
41
+ light: styles.themeLight,
42
+ dark: styles.themeDark
43
+ };
44
+ var modeToClass = {
45
+ classic: styles.modeClassic,
46
+ rock: styles.modeRock
47
+ }; // eslint-disable-next-line complexity
48
+
49
+ var processOptions = function processOptions(_ref) {
50
+ var _foundIndexByValue;
51
+
52
+ var rawOptions = _ref.rawOptions,
53
+ value = _ref.value,
54
+ iconPosition = _ref.iconPosition;
55
+ var options = [];
56
+ var foundIndexByValue = null;
57
+ var currentIndex = 0;
58
+ var withLeftIcons = false;
59
+
60
+ var _iterator = _createForOfIteratorHelper(rawOptions),
61
+ _step;
62
+
63
+ try {
64
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
65
+ var option = _step.value;
66
+ options.push({
67
+ label: option.label,
68
+ value: option.value,
69
+ icon: option.icon
70
+ });
71
+
72
+ if ( /*#__PURE__*/React.isValidElement(option.icon) && iconPosition === 'left') {
73
+ withLeftIcons = true;
74
+ }
75
+
76
+ if (option.value === value && foundIndexByValue === null) {
77
+ foundIndexByValue = currentIndex;
78
+ }
79
+
80
+ currentIndex++;
81
+ } // If some options on the first depth level have icons, we should align all other options accordingly
82
+
83
+ } catch (err) {
84
+ _iterator.e(err);
85
+ } finally {
86
+ _iterator.f();
87
+ }
88
+
89
+ if (withLeftIcons) {
90
+ options.forEach(function (option) {
91
+ if (! /*#__PURE__*/React.isValidElement(option.icon)) {
92
+ option.icon = /*#__PURE__*/React.createElement("span", {
93
+ className: styles.fakeIcon
94
+ });
95
+ }
96
+ });
97
+ }
98
+
99
+ return {
100
+ options: options,
101
+ activeIndex: (_foundIndexByValue = foundIndexByValue) !== null && _foundIndexByValue !== void 0 ? _foundIndexByValue : 0
102
+ };
103
+ }; // check for ignored jsx values and empty string, more info https://reactjs.org/docs/jsx-in-depth.html#booleans-null-and-undefined-are-ignored
104
+
105
+
106
+ var isNodeEmpty = function isNodeEmpty(node) {
107
+ return node === null || typeof node === 'undefined' || node === false || node === true || node === '';
108
+ };
109
+
110
+ function Option(_ref2) {
111
+ var label = _ref2.label,
112
+ value = _ref2.value,
113
+ icon = _ref2.icon,
114
+ index = _ref2.index;
115
+
116
+ var _useContext = useContext(verticalSwitcherContext),
117
+ switcherValue = _useContext.switcherValue,
118
+ onChange = _useContext.onChange,
119
+ handleOptionRef = _useContext.handleOptionRef,
120
+ disabled = _useContext.disabled;
121
+
122
+ var selected = switcherValue === value;
123
+ var isWithoutText = isNodeEmpty(label);
124
+ return /*#__PURE__*/React.createElement("button", {
125
+ key: String(value),
126
+ ref: handleOptionRef(index),
127
+ onClick: function onClick() {
128
+ return onChange(value);
129
+ },
130
+ type: "button",
131
+ "data-rs-internal": 'vertical-switcher__option',
132
+ disabled: disabled,
133
+ className: cn(styles.option, selected && styles.selected),
134
+ "data-test": cn("vertical-switcher-option", selected && ' vertical-switcher-option-selected')
135
+ }, /*#__PURE__*/React.isValidElement(icon) && /*#__PURE__*/cloneElement(icon, {
136
+ className: cn(icon.props.className, styles.icon)
137
+ }), !isWithoutText && /*#__PURE__*/React.createElement("span", {
138
+ className: styles.optionLabel
139
+ }, label));
140
+ }
141
+
142
+ function VerticalSwitcher(_ref3) {
143
+ var _options$activeIndex;
144
+
145
+ var className = _ref3.className,
146
+ themeFromProps = _ref3.theme,
147
+ _ref3$size = _ref3.size,
148
+ size = _ref3$size === void 0 ? 'm' : _ref3$size,
149
+ disabled = _ref3.disabled,
150
+ e2eId = _ref3['data-e2e'],
151
+ rawOptions = _ref3.options,
152
+ switcherValue = _ref3.value,
153
+ _ref3$mode = _ref3.mode,
154
+ mode = _ref3$mode === void 0 ? 'classic' : _ref3$mode,
155
+ _ref3$iconPosition = _ref3.iconPosition,
156
+ iconPosition = _ref3$iconPosition === void 0 ? 'left' : _ref3$iconPosition,
157
+ onChange = _ref3.onChange,
158
+ style = _ref3.style;
159
+ var refsArray = useRef([]);
160
+ var containerRef = useRef(null);
161
+ var theme = useThemeWithUndefined(themeFromProps);
162
+
163
+ var _processOptions = processOptions({
164
+ rawOptions: rawOptions,
165
+ value: switcherValue,
166
+ iconPosition: iconPosition
167
+ }),
168
+ options = _processOptions.options,
169
+ activeIndex = _processOptions.activeIndex;
170
+
171
+ var handleOptionRef = useCallback(function (index) {
172
+ return function (ref) {
173
+ if (!ref) {
174
+ return;
175
+ }
176
+
177
+ if (refsArray.current[index] !== ref) {
178
+ refsArray.current[index] = ref;
179
+ }
180
+ };
181
+ }, []);
182
+
183
+ var _useState = useState({
184
+ offset: 0,
185
+ height: 0
186
+ }),
187
+ _useState2 = _slicedToArray(_useState, 2),
188
+ mark = _useState2[0],
189
+ setMark = _useState2[1];
190
+
191
+ var onResize = function onResize() {
192
+ var activeEl = refsArray.current[activeIndex];
193
+
194
+ if (!activeEl) {
195
+ return;
196
+ }
197
+
198
+ var parentEl = activeEl.parentElement;
199
+ var activeElBox = activeEl.getBoundingClientRect();
200
+ var parentBox = parentEl.getBoundingClientRect();
201
+ setMark({
202
+ // standard activeEl.offsetTop always return rounded value, so it does not suit us
203
+ offset: activeElBox.top - parentBox.top + parentEl.scrollTop,
204
+ height: activeElBox.height
205
+ });
206
+ }; // eslint-disable-next-line react-hooks/exhaustive-deps
207
+
208
+
209
+ useEffect(function () {
210
+ return onResize();
211
+ }, [activeIndex]);
212
+ useResizeObserver(containerRef, onResize);
213
+ return /*#__PURE__*/React.createElement("span", {
214
+ className: cn(className, styles.verticalSwitcher, sizeToClass[size], themeToClass[theme], modeToClass[mode], disabled && styles.disabled, iconPosition === 'right' ? styles.alignIconRight : styles.alignIconLeft),
215
+ "data-e2e": e2eId,
216
+ ref: containerRef,
217
+ "data-rs-internal": 'vertical-switcher',
218
+ "data-test": cn("vertical-switcher", disabled && "vertical-switcher-disabled"),
219
+ style: style
220
+ }, /*#__PURE__*/React.createElement(verticalSwitcherContext.Provider, {
221
+ value: {
222
+ switcherValue: (_options$activeIndex = options[activeIndex]) === null || _options$activeIndex === void 0 ? void 0 : _options$activeIndex.value,
223
+ onChange: onChange,
224
+ handleOptionRef: handleOptionRef,
225
+ disabled: disabled
226
+ }
227
+ }, _mapInstanceProperty(options).call(options, function (_ref4, index) {
228
+ var value = _ref4.value,
229
+ label = _ref4.label,
230
+ icon = _ref4.icon;
231
+ return /*#__PURE__*/React.createElement(Option, {
232
+ key: String(value),
233
+ value: value,
234
+ label: label,
235
+ icon: icon,
236
+ index: index
237
+ });
238
+ }), /*#__PURE__*/React.createElement("span", {
239
+ className: cn(styles.mark),
240
+ style: {
241
+ top: mark.offset,
242
+ height: mark.height
243
+ }
244
+ })));
245
+ }
246
+
247
+ var optionPropType = PropTypes.shape({
248
+ label: PropTypes.node,
249
+ value: PropTypes.any.isRequired,
250
+ icon: PropTypes.node
251
+ });
252
+ VerticalSwitcher.propTypes = {
253
+ options: PropTypes.arrayOf(optionPropType).isRequired,
254
+ onChange: PropTypes.func.isRequired,
255
+ value: PropTypes.any.isRequired,
256
+ className: PropTypes.string,
257
+ size: PropTypes.oneOf(['l', 'm', 's', 'xs']),
258
+ theme: PropTypes.oneOf(['light', 'dark']),
259
+ iconPosition: PropTypes.oneOf(['left', 'right']),
260
+ disabled: PropTypes.bool,
261
+ style: PropTypes.object,
262
+ 'data-e2e': PropTypes.string
263
+ };
264
+ export { VerticalSwitcher };
@@ -0,0 +1,21 @@
1
+ var styles = {
2
+ "themeLight": "_themeLight_1mttwcn_6",
3
+ "themeDark": "_themeDark_1mttwcn_9",
4
+ "sizeXS": "_sizeXS_1mttwcn_13",
5
+ "sizeS": "_sizeS_1mttwcn_16",
6
+ "sizeM": "_sizeM_1mttwcn_19",
7
+ "sizeL": "_sizeL_1mttwcn_22",
8
+ "modeClassic": "_modeClassic_1mttwcn_26",
9
+ "modeRock": "_modeRock_1mttwcn_29",
10
+ "verticalSwitcher": "_verticalSwitcher_1mttwcn_33",
11
+ "disabled": "_disabled_1mttwcn_50",
12
+ "alignIconLeft": "_alignIconLeft_1mttwcn_55",
13
+ "alignIconRight": "_alignIconRight_1mttwcn_58",
14
+ "option": "_option_1mttwcn_62",
15
+ "selected": "_selected_1mttwcn_107",
16
+ "optionLabel": "_optionLabel_1mttwcn_126",
17
+ "icon": "_icon_1mttwcn_132",
18
+ "fakeIcon": "_fakeIcon_1mttwcn_143",
19
+ "mark": "_mark_1mttwcn_150"
20
+ };
21
+ export { styles as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rescui/switcher",
3
- "version": "0.9.8",
3
+ "version": "0.10.1",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "author": "JetBrains",
@@ -36,5 +36,5 @@
36
36
  "build-pcss-api": "rescui-scripts build-pcss-api --file public-api.p.css"
37
37
  },
38
38
  "nx": {},
39
- "gitHead": "efe72850816ad50de41544ab1d6f84db0d89b322"
39
+ "gitHead": "9269aa5b525b43a4a876dbf12d15c7e88debae18"
40
40
  }
@@ -1,20 +0,0 @@
1
- var styles = {
2
- "themeLight": "_themeLight_es4omi_6",
3
- "themeDark": "_themeDark_es4omi_9",
4
- "sizeXS": "_sizeXS_es4omi_13",
5
- "sizeS": "_sizeS_es4omi_16",
6
- "sizeM": "_sizeM_es4omi_19",
7
- "sizeL": "_sizeL_es4omi_22",
8
- "modeClassic": "_modeClassic_es4omi_26",
9
- "modeRock": "_modeRock_es4omi_29",
10
- "compact": "_compact_es4omi_33",
11
- "switcher": "_switcher_es4omi_37",
12
- "alignIconLeft": "_alignIconLeft_es4omi_61",
13
- "alignIconRight": "_alignIconRight_es4omi_64",
14
- "option": "_option_es4omi_68",
15
- "disabled": "_disabled_es4omi_104",
16
- "selected": "_selected_es4omi_117",
17
- "icon": "_icon_es4omi_138",
18
- "mark": "_mark_es4omi_151"
19
- };
20
- export { styles as default };