UNPKG

7.52 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7require('./slicedToArray-ef426d0f.js');
8require('./unsupportedIterableToArray-8a00e599.js');
9var React = require('react');
10var React__default = _interopDefault(React);
11require('./_commonjsHelpers-72d386ba.js');
12var index = require('./index-b0606964.js');
13require('./defineProperty-0921a47c.js');
14require('./toConsumableArray-7f36359f.js');
15var _styled = require('styled-components');
16var _styled__default = _interopDefault(_styled);
17require('./getPrototypeOf-e2d1e599.js');
18require('./color.js');
19require('./components.js');
20require('./contains-component.js');
21var css = require('./css.js');
22require('./dayjs.min-aa59a48e.js');
23require('./date.js');
24require('./miscellaneous.js');
25require('./environment.js');
26require('./font.js');
27require('./math-ecfd5d91.js');
28require('./characters.js');
29require('./format.js');
30require('./keycodes.js');
31require('./url.js');
32require('./web3.js');
33var constants = require('./constants.js');
34require('./breakpoints.js');
35require('./springs.js');
36var textStyles = require('./text-styles.js');
37require('./theme-dark.js');
38require('./theme-light.js');
39var Theme = require('./Theme.js');
40var _extends = require('./extends-40571110.js');
41var objectWithoutProperties = require('./objectWithoutProperties-35db8ab0.js');
42
43var MODE_INDICATOR = 'indicator';
44var MODE_IDENTIFIER = 'identifier';
45var MODE_NEW = 'new';
46var MODE_ACTIVITY = 'activity';
47var SIZE_NORMAL = 'normal';
48var SIZE_SMALL = 'small';
49var COUNT_DEFAULT = 2;
50
51function useMode(mode) {
52 var theme = Theme.useTheme();
53
54 if (mode === MODE_IDENTIFIER) {
55 return {
56 background: theme.tagIdentifier,
57 color: theme.tagIdentifierContent,
58 size: SIZE_NORMAL
59 };
60 }
61
62 if (mode === MODE_NEW) {
63 return {
64 background: theme.tagNew,
65 color: theme.tagNewContent,
66 size: SIZE_NORMAL
67 };
68 }
69
70 if (mode === MODE_ACTIVITY) {
71 return {
72 background: theme.tagActivity,
73 color: theme.tagActivityContent,
74 size: SIZE_SMALL
75 };
76 } // mode === MODE_INDICATOR (default)
77
78
79 return {
80 background: theme.tagIndicator,
81 color: theme.tagIndicatorContent,
82 size: SIZE_NORMAL
83 };
84}
85
86function getSize(size, _ref) {
87 var uppercase = _ref.uppercase,
88 discMode = _ref.discMode,
89 iconAndLabel = _ref.iconAndLabel;
90
91 if (size === SIZE_SMALL) {
92 return "\n min-width: ".concat(2 * constants.GU, "px;\n width: ").concat(discMode ? "".concat(2 * constants.GU, "px") : 'auto', ";\n height: ").concat(2 * constants.GU, "px;\n padding: ").concat(discMode ? '0' : "0 ".concat(0.5 * constants.GU, "px"), ";\n padding-top: ").concat(uppercase ? '0.5px' : 0, ";\n border-radius: ").concat(2 * constants.GU, "px;\n ").concat(textStyles.textStyle('label3'), ";\n font-weight: 600;\n ");
93 } // normal
94
95
96 return "\n min-width: ".concat(2.5 * constants.GU, "px;\n width: ").concat(discMode ? "".concat(3 * constants.GU, "px") : 'auto', ";\n height: ").concat(2.5 * constants.GU, "px;\n padding: ").concat(discMode ? '0' : "0 ".concat(1.5 * constants.GU, "px"), " ;\n padding-top: ").concat(uppercase ? '1px' : 0, ";\n ").concat(iconAndLabel ? "padding-left: ".concat(1.25 * constants.GU, "px") : '', ";\n border-radius: ").concat(2.5 * constants.GU, "px;\n ").concat(textStyles.textStyle('label2'), ";\n font-weight: 600;\n ");
97}
98
99function useLabel(_ref2) {
100 var _ref2$label = _ref2.label,
101 label = _ref2$label === void 0 ? '' : _ref2$label,
102 limitDigits = _ref2.limitDigits;
103 var finalLabel = React.useMemo(function () {
104 if (limitDigits === false) {
105 return typeof label === 'number' ? String(label) : label || '';
106 }
107
108 var digits = typeof limitDigits === 'number' ? limitDigits : COUNT_DEFAULT;
109 var parsed = parseInt(label, 10);
110
111 if (isNaN(parsed)) {
112 return label || '';
113 }
114
115 var max = Math.pow(10, digits) - 1;
116 var formattedValue = parsed > max ? "".concat(max, "+") : parsed;
117 return String(formattedValue);
118 }, [label, limitDigits]);
119 return finalLabel;
120}
121
122var _StyledSpan = _styled__default("span").withConfig({
123 displayName: "Tag___StyledSpan",
124 componentId: "sc-875dmt-0"
125})(["display:inline-flex;align-items:center;justify-content:center;white-space:nowrap;", ";", ";color:", ";background:", ";", ";"], function (p) {
126 return p._css;
127}, function (p) {
128 return p._css2;
129}, function (p) {
130 return p._css3;
131}, function (p) {
132 return p._css4;
133}, css.unselectable);
134
135var _StyledSpan2 = _styled__default("span").withConfig({
136 displayName: "Tag___StyledSpan2",
137 componentId: "sc-875dmt-1"
138})(["display:flex;align-items:center;margin-right:", "px;"], function (p) {
139 return p._css5;
140});
141
142var _StyledSpan3 = _styled__default("span").withConfig({
143 displayName: "Tag___StyledSpan3",
144 componentId: "sc-875dmt-2"
145})(["overflow:hidden;text-overflow:ellipsis;margin-top:", ";"], function (p) {
146 return p._css6;
147});
148
149function Tag(_ref3) {
150 var background = _ref3.background,
151 children = _ref3.children,
152 color = _ref3.color,
153 limitDigits = _ref3.limitDigits,
154 icon = _ref3.icon,
155 label = _ref3.label,
156 mode = _ref3.mode,
157 size = _ref3.size,
158 uppercase = _ref3.uppercase,
159 props = objectWithoutProperties._objectWithoutProperties(_ref3, ["background", "children", "color", "limitDigits", "icon", "label", "mode", "size", "uppercase"]);
160
161 if ((icon || label !== undefined) && children !== undefined) {
162 throw new Error('Tag: you cannot use icon or label with children.');
163 }
164
165 var modeProps = useMode(mode);
166 var finalSize = size || modeProps.size;
167 var finalLabel = useLabel({
168 label: label !== undefined ? label : children,
169 limitDigits: limitDigits
170 });
171 var sizeStyles = getSize(finalSize, {
172 uppercase: uppercase,
173 discMode: // icon only
174 icon && !finalLabel || // label only, using 1 or 0 chars
175 !icon && typeof finalLabel === 'string' && finalLabel.length < 2,
176 iconAndLabel: icon && finalLabel
177 }); // Slightly tweak the alignment when there are no descenders,
178 // to make the characters look more aligned.
179
180 var alignmentCorrection = finalSize === SIZE_NORMAL && (uppercase || typeof label === 'number' || limitDigits !== false);
181 return /*#__PURE__*/React__default.createElement(_StyledSpan, _extends._extends({}, props, {
182 _css: sizeStyles,
183 _css2: !uppercase ? 'text-transform: unset' : '',
184 _css3: color || modeProps.color,
185 _css4: background || modeProps.background
186 }), icon && /*#__PURE__*/React__default.createElement(_StyledSpan2, {
187 _css5: finalLabel ? 0.25 * constants.GU : 0
188 }, icon), /*#__PURE__*/React__default.createElement(_StyledSpan3, {
189 _css6: alignmentCorrection ? '1px' : '0'
190 }, finalLabel));
191}
192
193Tag.propTypes = {
194 background: index.PropTypes.string,
195 children: index.PropTypes.node,
196 color: index.PropTypes.string,
197 limitDigits: index.PropTypes.oneOfType([index.PropTypes.bool, index.PropTypes.number]),
198 icon: index.PropTypes.node,
199 label: index.PropTypes.oneOfType([index.PropTypes.node, index.PropTypes.number]),
200 mode: index.PropTypes.oneOf([MODE_IDENTIFIER, MODE_NEW, MODE_INDICATOR, MODE_ACTIVITY]),
201 size: index.PropTypes.oneOf([SIZE_NORMAL, SIZE_SMALL]),
202 uppercase: index.PropTypes.bool
203};
204Tag.defaultProps = {
205 uppercase: true,
206 limitDigits: false
207};
208
209exports.default = Tag;
210//# sourceMappingURL=Tag.js.map