@elastic/eui-theme-common 8.1.0 → 9.0.0-snapshot.1776170631053
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.
- package/lib/cjs/global_styling/functions/math.d.ts +5 -0
- package/lib/cjs/global_styling/functions/math.js +134 -1
- package/lib/cjs/global_styling/functions/math.test.js +73 -0
- package/lib/cjs/global_styling/variables/colors.d.ts +0 -10
- package/lib/esm/global_styling/functions/math.d.ts +5 -0
- package/lib/esm/global_styling/functions/math.js +116 -1
- package/lib/esm/global_styling/functions/math.test.js +45 -0
- package/lib/esm/global_styling/variables/colors.d.ts +0 -10
- package/package.json +3 -2
|
@@ -6,6 +6,11 @@
|
|
|
6
6
|
* mathWithUnits('4px', (x) => x / 2) = '2px';
|
|
7
7
|
* mathWithUnits(euiTheme.size.xs, (x) => x + 2) = '6px';
|
|
8
8
|
* mathWithUnits([euiTheme.size.l, euiTheme.size.s], (x, y) => x - y) = '16px';
|
|
9
|
+
*
|
|
10
|
+
* When any input contains a CSS var() reference, the callback is probed with
|
|
11
|
+
* test values to determine its linear coefficients, and an equivalent calc()
|
|
12
|
+
* expression is emitted instead:
|
|
13
|
+
* mathWithUnits('var(--size)', (x) => x / 2) = 'calc(var(--size) / 2)';
|
|
9
14
|
*/
|
|
10
15
|
declare type ValueTypes = string | number | undefined;
|
|
11
16
|
export declare const mathWithUnits: (values: ValueTypes | ValueTypes[], callback: (...args: number[]) => number, unit?: string) => string;
|
|
@@ -5,6 +5,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.mathWithUnits = void 0;
|
|
8
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
9
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
10
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
11
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
12
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
13
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
8
14
|
function _wrapRegExp() { _wrapRegExp = function _wrapRegExp(e, r) { return new BabelRegExp(e, void 0, r); }; var e = RegExp.prototype, r = new WeakMap(); function BabelRegExp(e, t, p) { var o = RegExp(e, t); return r.set(o, p || r.get(e)), _setPrototypeOf(o, BabelRegExp.prototype); } function buildGroups(e, t) { var p = r.get(t); return Object.keys(p).reduce(function (r, t) { var o = p[t]; if ("number" == typeof o) r[t] = e[o];else { for (var i = 0; void 0 === e[o[i]] && i + 1 < o.length;) i++; r[t] = e[o[i]]; } return r; }, Object.create(null)); } return _inherits(BabelRegExp, RegExp), BabelRegExp.prototype.exec = function (r) { var t = e.exec.call(this, r); if (t) { t.groups = buildGroups(t, this); var p = t.indices; p && (p.groups = buildGroups(p, this)); } return t; }, BabelRegExp.prototype[Symbol.replace] = function (t, p) { if ("string" == typeof p) { var o = r.get(this); return e[Symbol.replace].call(this, t, p.replace(/\$<([^>]+)>/g, function (e, r) { var t = o[r]; return "$" + (Array.isArray(t) ? t.join("$") : t); })); } if ("function" == typeof p) { var i = this; return e[Symbol.replace].call(this, t, function () { var e = arguments; return "object" != _typeof(e[e.length - 1]) && (e = [].slice.call(e)).push(buildGroups(e, i)), p.apply(this, e); }); } return e[Symbol.replace].call(this, t, p); }, _wrapRegExp.apply(this, arguments); }
|
|
9
15
|
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
|
|
10
16
|
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
|
|
@@ -24,12 +30,35 @@ function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf
|
|
|
24
30
|
* mathWithUnits('4px', (x) => x / 2) = '2px';
|
|
25
31
|
* mathWithUnits(euiTheme.size.xs, (x) => x + 2) = '6px';
|
|
26
32
|
* mathWithUnits([euiTheme.size.l, euiTheme.size.s], (x, y) => x - y) = '16px';
|
|
33
|
+
*
|
|
34
|
+
* When any input contains a CSS var() reference, the callback is probed with
|
|
35
|
+
* test values to determine its linear coefficients, and an equivalent calc()
|
|
36
|
+
* expression is emitted instead:
|
|
37
|
+
* mathWithUnits('var(--size)', (x) => x / 2) = 'calc(var(--size) / 2)';
|
|
27
38
|
*/
|
|
28
39
|
// Unfortunately, this is the CSSProperties[] type used for several euiTheme vars
|
|
29
40
|
|
|
41
|
+
// --- Temporary perf instrumentation ---
|
|
42
|
+
var _callCount = 0;
|
|
43
|
+
var _totalTime = 0;
|
|
44
|
+
// --- End instrumentation ---
|
|
45
|
+
|
|
30
46
|
var mathWithUnits = exports.mathWithUnits = function mathWithUnits(values, callback) {
|
|
31
47
|
var unit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
|
48
|
+
var _t0 = performance.now();
|
|
49
|
+
_callCount++;
|
|
32
50
|
if (!Array.isArray(values)) values = [values];
|
|
51
|
+
|
|
52
|
+
// When any value is a CSS var() reference, emit a calc() expression
|
|
53
|
+
// instead of evaluating the callback in JS
|
|
54
|
+
if (values.some(function (v) {
|
|
55
|
+
return typeof v === 'string' && v.includes('var(');
|
|
56
|
+
})) {
|
|
57
|
+
var _elapsed2 = performance.now() - _t0;
|
|
58
|
+
_totalTime += _elapsed2;
|
|
59
|
+
console.log("mathWithUnits #".concat(_callCount, " (var path): ").concat(_elapsed2.toFixed(3), "ms | total: ").concat(_totalTime.toFixed(3), "ms"), values);
|
|
60
|
+
return _emitCalcForVars(values, callback, unit);
|
|
61
|
+
}
|
|
33
62
|
var foundNumericValues = [];
|
|
34
63
|
var foundUnit = '';
|
|
35
64
|
values.forEach(function (value) {
|
|
@@ -59,5 +88,109 @@ var mathWithUnits = exports.mathWithUnits = function mathWithUnits(values, callb
|
|
|
59
88
|
throw new Error('Invalid value type - pass a string or number');
|
|
60
89
|
}
|
|
61
90
|
});
|
|
62
|
-
|
|
91
|
+
var _result = "".concat(callback.apply(void 0, foundNumericValues)).concat(unit || foundUnit);
|
|
92
|
+
var _elapsed = performance.now() - _t0;
|
|
93
|
+
_totalTime += _elapsed;
|
|
94
|
+
console.log("mathWithUnits #".concat(_callCount, ": ").concat(_elapsed.toFixed(3), "ms | total: ").concat(_totalTime.toFixed(3), "ms"), values, '->', _result);
|
|
95
|
+
return _result;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* When CSS var() references are present, probe the callback with test values
|
|
100
|
+
* to determine its linear coefficients, then emit an equivalent calc() expression.
|
|
101
|
+
*
|
|
102
|
+
* Only the var() positions are probed (at 0 vs 1). Literal values (numbers and
|
|
103
|
+
* parseable CSS strings) are fixed at their actual numeric values during probing,
|
|
104
|
+
* so callbacks like `(x, y) => x * y` work correctly when one arg is a literal.
|
|
105
|
+
*
|
|
106
|
+
* Assumes the callback is linear *in the var() arguments*.
|
|
107
|
+
*/
|
|
108
|
+
var _emitCalcForVars = function _emitCalcForVars(values, callback, overrideUnit) {
|
|
109
|
+
var n = values.length;
|
|
110
|
+
|
|
111
|
+
// Classify each value: is it a var() ref or a literal we can parse?
|
|
112
|
+
var isVar = [];
|
|
113
|
+
var baselineArgs = []; // Actual numeric values for literals, 0 for var refs
|
|
114
|
+
var cssUnit = overrideUnit;
|
|
115
|
+
for (var i = 0; i < n; i++) {
|
|
116
|
+
var v = values[i];
|
|
117
|
+
if (typeof v === 'string' && v.includes('var(')) {
|
|
118
|
+
isVar.push(true);
|
|
119
|
+
baselineArgs.push(0);
|
|
120
|
+
} else if (typeof v === 'number') {
|
|
121
|
+
isVar.push(false);
|
|
122
|
+
baselineArgs.push(v);
|
|
123
|
+
} else if (typeof v === 'string') {
|
|
124
|
+
isVar.push(false);
|
|
125
|
+
var match = /(-?[\d.]+)(%|[a-zA-Z]*)/.exec(v);
|
|
126
|
+
baselineArgs.push(Number(match === null || match === void 0 ? void 0 : match[1]) || 0);
|
|
127
|
+
if (!cssUnit && match !== null && match !== void 0 && match[2]) {
|
|
128
|
+
cssUnit = match[2];
|
|
129
|
+
}
|
|
130
|
+
} else {
|
|
131
|
+
isVar.push(false);
|
|
132
|
+
baselineArgs.push(0);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (!cssUnit) cssUnit = 'px';
|
|
136
|
+
|
|
137
|
+
// Probe: fix literal positions at their actual values,
|
|
138
|
+
// probe each var() position at 0 (baseline) then 1 to determine its coefficient
|
|
139
|
+
var constant = _preciseRound(callback.apply(void 0, baselineArgs));
|
|
140
|
+
var varIndices = [];
|
|
141
|
+
var coefficients = [];
|
|
142
|
+
for (var _i = 0; _i < n; _i++) {
|
|
143
|
+
if (!isVar[_i]) continue;
|
|
144
|
+
varIndices.push(_i);
|
|
145
|
+
var probe = [].concat(baselineArgs);
|
|
146
|
+
probe[_i] = 1;
|
|
147
|
+
coefficients.push(_preciseRound(callback.apply(void 0, _toConsumableArray(probe)) - constant));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Build calc() expression — only var() positions appear as variable terms
|
|
151
|
+
var varTerms = [];
|
|
152
|
+
for (var j = 0; j < varIndices.length; j++) {
|
|
153
|
+
var coeff = coefficients[j];
|
|
154
|
+
if (coeff === 0) continue;
|
|
155
|
+
var valueStr = String(values[varIndices[j]]);
|
|
156
|
+
if (varTerms.length === 0) {
|
|
157
|
+
varTerms.push(_formatTerm(coeff, valueStr));
|
|
158
|
+
} else if (coeff > 0) {
|
|
159
|
+
varTerms.push("+ ".concat(_formatTerm(coeff, valueStr)));
|
|
160
|
+
} else {
|
|
161
|
+
varTerms.push("- ".concat(_formatTerm(Math.abs(coeff), valueStr)));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// If no var() terms contribute (callback ignores its input), return the constant directly
|
|
166
|
+
if (varTerms.length === 0) {
|
|
167
|
+
return constant === 0 ? "0".concat(cssUnit) : "".concat(constant).concat(cssUnit);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Append constant if non-zero
|
|
171
|
+
if (constant !== 0) {
|
|
172
|
+
var cStr = "".concat(Math.abs(constant)).concat(cssUnit);
|
|
173
|
+
varTerms.push(constant > 0 ? "+ ".concat(cStr) : "- ".concat(cStr));
|
|
174
|
+
}
|
|
175
|
+
return "calc(".concat(varTerms.join(' '), ")");
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/** Format a coefficient × value term for calc(). Coefficient may be negative for first terms. */
|
|
179
|
+
var _formatTerm = function _formatTerm(coeff, value) {
|
|
180
|
+
var absCoeff = Math.abs(coeff);
|
|
181
|
+
if (absCoeff === 1) {
|
|
182
|
+
return coeff < 0 ? "".concat(value, " * -1") : value;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Use division for readability when coefficient is a simple reciprocal
|
|
186
|
+
var reciprocal = 1 / absCoeff;
|
|
187
|
+
if (Number.isInteger(reciprocal) && reciprocal > 1) {
|
|
188
|
+
return "".concat(value, " / ").concat(coeff < 0 ? -reciprocal : reciprocal);
|
|
189
|
+
}
|
|
190
|
+
return "".concat(coeff, " * ").concat(value);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/** Round to avoid floating-point artifacts from callback probing */
|
|
194
|
+
var _preciseRound = function _preciseRound(n) {
|
|
195
|
+
return Math.round(n * 1e10) / 1e10;
|
|
63
196
|
};
|
|
@@ -73,6 +73,79 @@ describe('mathWithUnits', function () {
|
|
|
73
73
|
}, 'px')).toEqual('10px');
|
|
74
74
|
});
|
|
75
75
|
});
|
|
76
|
+
describe('CSS var() references emit calc() expressions', function () {
|
|
77
|
+
test('single var with multiplication', function () {
|
|
78
|
+
expect((0, _math.mathWithUnits)('var(--size)', function (x) {
|
|
79
|
+
return x * 2;
|
|
80
|
+
})).toEqual('calc(2 * var(--size))');
|
|
81
|
+
});
|
|
82
|
+
test('single var with division', function () {
|
|
83
|
+
expect((0, _math.mathWithUnits)('var(--size)', function (x) {
|
|
84
|
+
return x / 2;
|
|
85
|
+
})).toEqual('calc(var(--size) / 2)');
|
|
86
|
+
});
|
|
87
|
+
test('single var with fractional multiplication', function () {
|
|
88
|
+
expect((0, _math.mathWithUnits)('var(--size)', function (x) {
|
|
89
|
+
return x * 0.75;
|
|
90
|
+
})).toEqual('calc(0.75 * var(--size))');
|
|
91
|
+
});
|
|
92
|
+
test('single var with addition', function () {
|
|
93
|
+
expect((0, _math.mathWithUnits)('var(--size)', function (x) {
|
|
94
|
+
return x + 2;
|
|
95
|
+
})).toEqual('calc(var(--size) + 2px)');
|
|
96
|
+
});
|
|
97
|
+
test('single var with negative division', function () {
|
|
98
|
+
expect((0, _math.mathWithUnits)('var(--size)', function (x) {
|
|
99
|
+
return x / -2;
|
|
100
|
+
})).toEqual('calc(var(--size) / -2)');
|
|
101
|
+
});
|
|
102
|
+
test('single var identity', function () {
|
|
103
|
+
expect((0, _math.mathWithUnits)('var(--size)', function (x) {
|
|
104
|
+
return x;
|
|
105
|
+
})).toEqual('calc(var(--size))');
|
|
106
|
+
});
|
|
107
|
+
test('multiple vars with subtraction', function () {
|
|
108
|
+
expect((0, _math.mathWithUnits)(['var(--a)', 'var(--b)'], function (x, y) {
|
|
109
|
+
return x - y;
|
|
110
|
+
})).toEqual('calc(var(--a) - var(--b))');
|
|
111
|
+
});
|
|
112
|
+
test('multiple vars with complex linear', function () {
|
|
113
|
+
expect((0, _math.mathWithUnits)(['var(--a)', 'var(--b)'], function (x, y) {
|
|
114
|
+
return x - 3 * y;
|
|
115
|
+
})).toEqual('calc(var(--a) - 3 * var(--b))');
|
|
116
|
+
});
|
|
117
|
+
test('mixed var and literal', function () {
|
|
118
|
+
expect((0, _math.mathWithUnits)(['var(--a)', '4px'], function (x, y) {
|
|
119
|
+
return x + y;
|
|
120
|
+
})).toEqual('calc(var(--a) + 4px)');
|
|
121
|
+
});
|
|
122
|
+
test('unit detection from literal input', function () {
|
|
123
|
+
expect((0, _math.mathWithUnits)(['var(--a)', '2rem'], function (x, y) {
|
|
124
|
+
return x + y;
|
|
125
|
+
})).toEqual('calc(var(--a) + 2rem)');
|
|
126
|
+
});
|
|
127
|
+
test('override unit is respected', function () {
|
|
128
|
+
expect((0, _math.mathWithUnits)('var(--size)', function (x) {
|
|
129
|
+
return x + 4;
|
|
130
|
+
}, '%')).toEqual('calc(var(--size) + 4%)');
|
|
131
|
+
});
|
|
132
|
+
test('constant-only result', function () {
|
|
133
|
+
expect((0, _math.mathWithUnits)('var(--size)', function (_x) {
|
|
134
|
+
return 16;
|
|
135
|
+
})).toEqual('16px');
|
|
136
|
+
});
|
|
137
|
+
test('var mixed with number input — multiplication', function () {
|
|
138
|
+
expect((0, _math.mathWithUnits)(['var(--size)', 3], function (x, y) {
|
|
139
|
+
return x * y;
|
|
140
|
+
})).toEqual('calc(3 * var(--size))');
|
|
141
|
+
});
|
|
142
|
+
test('var mixed with number input — addition with closure', function () {
|
|
143
|
+
var base = 16;
|
|
144
|
+
expect((0, _math.mathWithUnits)('var(--size)', function (x) {
|
|
145
|
+
return x + base;
|
|
146
|
+
})).toEqual('calc(var(--size) + 16px)');
|
|
147
|
+
});
|
|
148
|
+
});
|
|
76
149
|
describe('weird edge cases', function () {
|
|
77
150
|
it('throws on undefined', function () {
|
|
78
151
|
expect(function () {
|
|
@@ -275,8 +275,6 @@ export declare type _EuiThemeVisColors = {
|
|
|
275
275
|
euiColorVis7: string;
|
|
276
276
|
euiColorVis8: string;
|
|
277
277
|
euiColorVis9: string;
|
|
278
|
-
euiColorVis10: string;
|
|
279
|
-
euiColorVis11: string;
|
|
280
278
|
/** @deprecated - temp token; used only during theme migration */
|
|
281
279
|
euiColorVisBehindText0: string;
|
|
282
280
|
/** @deprecated - temp token; used only during theme migration */
|
|
@@ -297,10 +295,6 @@ export declare type _EuiThemeVisColors = {
|
|
|
297
295
|
euiColorVisBehindText8: string;
|
|
298
296
|
/** @deprecated - temp token; used only during theme migration */
|
|
299
297
|
euiColorVisBehindText9: string;
|
|
300
|
-
/** @deprecated - temp token; used only during theme migration */
|
|
301
|
-
euiColorVisBehindText10: string;
|
|
302
|
-
/** @deprecated - temp token; used only during theme migration */
|
|
303
|
-
euiColorVisBehindText11: string;
|
|
304
298
|
euiColorVisText0: string;
|
|
305
299
|
euiColorVisText1: string;
|
|
306
300
|
euiColorVisText2: string;
|
|
@@ -311,8 +305,6 @@ export declare type _EuiThemeVisColors = {
|
|
|
311
305
|
euiColorVisText7: string;
|
|
312
306
|
euiColorVisText8: string;
|
|
313
307
|
euiColorVisText9: string;
|
|
314
|
-
euiColorVisText10: string;
|
|
315
|
-
euiColorVisText11: string;
|
|
316
308
|
euiColorVisNeutral0: string;
|
|
317
309
|
euiColorVisNeutral1: string;
|
|
318
310
|
euiColorVisSuccess0: string;
|
|
@@ -323,7 +315,6 @@ export declare type _EuiThemeVisColors = {
|
|
|
323
315
|
euiColorVisRisk1: string;
|
|
324
316
|
euiColorVisDanger0: string;
|
|
325
317
|
euiColorVisDanger1: string;
|
|
326
|
-
euiColorVisAssistance: string;
|
|
327
318
|
euiColorVisBase0: string;
|
|
328
319
|
euiColorVisGrey0: string;
|
|
329
320
|
euiColorVisGrey1: string;
|
|
@@ -345,7 +336,6 @@ export declare type _EuiThemeSeverityColors = {
|
|
|
345
336
|
warning: string;
|
|
346
337
|
risk: string;
|
|
347
338
|
danger: string;
|
|
348
|
-
assistance: string;
|
|
349
339
|
};
|
|
350
340
|
export declare type _EuiThemeConstantColors = {
|
|
351
341
|
plainLight: string;
|
|
@@ -6,6 +6,11 @@
|
|
|
6
6
|
* mathWithUnits('4px', (x) => x / 2) = '2px';
|
|
7
7
|
* mathWithUnits(euiTheme.size.xs, (x) => x + 2) = '6px';
|
|
8
8
|
* mathWithUnits([euiTheme.size.l, euiTheme.size.s], (x, y) => x - y) = '16px';
|
|
9
|
+
*
|
|
10
|
+
* When any input contains a CSS var() reference, the callback is probed with
|
|
11
|
+
* test values to determine its linear coefficients, and an equivalent calc()
|
|
12
|
+
* expression is emitted instead:
|
|
13
|
+
* mathWithUnits('var(--size)', (x) => x / 2) = 'calc(var(--size) / 2)';
|
|
9
14
|
*/
|
|
10
15
|
declare type ValueTypes = string | number | undefined;
|
|
11
16
|
export declare const mathWithUnits: (values: ValueTypes | ValueTypes[], callback: (...args: number[]) => number, unit?: string) => string;
|
|
@@ -5,12 +5,26 @@
|
|
|
5
5
|
* in compliance with, at your election, the Elastic License 2.0 or the Server
|
|
6
6
|
* Side Public License, v 1.
|
|
7
7
|
*/
|
|
8
|
+
// --- Temporary perf instrumentation ---
|
|
9
|
+
let _callCount = 0;
|
|
10
|
+
let _totalTime = 0;
|
|
11
|
+
// --- End instrumentation ---
|
|
8
12
|
export const mathWithUnits = (values, // Can accept a single input or array of inputs
|
|
9
13
|
callback, // Can be multiplication, division, addition, etc.
|
|
10
14
|
unit = '' // Optional: allow specifying an override unit to return
|
|
11
15
|
) => {
|
|
16
|
+
const _t0 = performance.now();
|
|
17
|
+
_callCount++;
|
|
12
18
|
if (!Array.isArray(values))
|
|
13
19
|
values = [values];
|
|
20
|
+
// When any value is a CSS var() reference, emit a calc() expression
|
|
21
|
+
// instead of evaluating the callback in JS
|
|
22
|
+
if (values.some((v) => typeof v === 'string' && v.includes('var('))) {
|
|
23
|
+
const _elapsed = performance.now() - _t0;
|
|
24
|
+
_totalTime += _elapsed;
|
|
25
|
+
console.log(`mathWithUnits #${_callCount} (var path): ${_elapsed.toFixed(3)}ms | total: ${_totalTime.toFixed(3)}ms`, values);
|
|
26
|
+
return _emitCalcForVars(values, callback, unit);
|
|
27
|
+
}
|
|
14
28
|
const foundNumericValues = [];
|
|
15
29
|
let foundUnit = '';
|
|
16
30
|
values.forEach((value) => {
|
|
@@ -40,5 +54,106 @@ unit = '' // Optional: allow specifying an override unit to return
|
|
|
40
54
|
throw new Error('Invalid value type - pass a string or number');
|
|
41
55
|
}
|
|
42
56
|
});
|
|
43
|
-
|
|
57
|
+
const _result = `${callback(...foundNumericValues)}${unit || foundUnit}`;
|
|
58
|
+
const _elapsed = performance.now() - _t0;
|
|
59
|
+
_totalTime += _elapsed;
|
|
60
|
+
console.log(`mathWithUnits #${_callCount}: ${_elapsed.toFixed(3)}ms | total: ${_totalTime.toFixed(3)}ms`, values, '->', _result);
|
|
61
|
+
return _result;
|
|
44
62
|
};
|
|
63
|
+
/**
|
|
64
|
+
* When CSS var() references are present, probe the callback with test values
|
|
65
|
+
* to determine its linear coefficients, then emit an equivalent calc() expression.
|
|
66
|
+
*
|
|
67
|
+
* Only the var() positions are probed (at 0 vs 1). Literal values (numbers and
|
|
68
|
+
* parseable CSS strings) are fixed at their actual numeric values during probing,
|
|
69
|
+
* so callbacks like `(x, y) => x * y` work correctly when one arg is a literal.
|
|
70
|
+
*
|
|
71
|
+
* Assumes the callback is linear *in the var() arguments*.
|
|
72
|
+
*/
|
|
73
|
+
const _emitCalcForVars = (values, callback, overrideUnit) => {
|
|
74
|
+
const n = values.length;
|
|
75
|
+
// Classify each value: is it a var() ref or a literal we can parse?
|
|
76
|
+
const isVar = [];
|
|
77
|
+
const baselineArgs = []; // Actual numeric values for literals, 0 for var refs
|
|
78
|
+
let cssUnit = overrideUnit;
|
|
79
|
+
for (let i = 0; i < n; i++) {
|
|
80
|
+
const v = values[i];
|
|
81
|
+
if (typeof v === 'string' && v.includes('var(')) {
|
|
82
|
+
isVar.push(true);
|
|
83
|
+
baselineArgs.push(0);
|
|
84
|
+
}
|
|
85
|
+
else if (typeof v === 'number') {
|
|
86
|
+
isVar.push(false);
|
|
87
|
+
baselineArgs.push(v);
|
|
88
|
+
}
|
|
89
|
+
else if (typeof v === 'string') {
|
|
90
|
+
isVar.push(false);
|
|
91
|
+
const match = /(-?[\d.]+)(%|[a-zA-Z]*)/.exec(v);
|
|
92
|
+
baselineArgs.push(Number(match?.[1]) || 0);
|
|
93
|
+
if (!cssUnit && match?.[2]) {
|
|
94
|
+
cssUnit = match[2];
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
isVar.push(false);
|
|
99
|
+
baselineArgs.push(0);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!cssUnit)
|
|
103
|
+
cssUnit = 'px';
|
|
104
|
+
// Probe: fix literal positions at their actual values,
|
|
105
|
+
// probe each var() position at 0 (baseline) then 1 to determine its coefficient
|
|
106
|
+
const constant = _preciseRound(callback(...baselineArgs));
|
|
107
|
+
const varIndices = [];
|
|
108
|
+
const coefficients = [];
|
|
109
|
+
for (let i = 0; i < n; i++) {
|
|
110
|
+
if (!isVar[i])
|
|
111
|
+
continue;
|
|
112
|
+
varIndices.push(i);
|
|
113
|
+
const probe = [...baselineArgs];
|
|
114
|
+
probe[i] = 1;
|
|
115
|
+
coefficients.push(_preciseRound(callback(...probe) - constant));
|
|
116
|
+
}
|
|
117
|
+
// Build calc() expression — only var() positions appear as variable terms
|
|
118
|
+
const varTerms = [];
|
|
119
|
+
for (let j = 0; j < varIndices.length; j++) {
|
|
120
|
+
const coeff = coefficients[j];
|
|
121
|
+
if (coeff === 0)
|
|
122
|
+
continue;
|
|
123
|
+
const valueStr = String(values[varIndices[j]]);
|
|
124
|
+
if (varTerms.length === 0) {
|
|
125
|
+
varTerms.push(_formatTerm(coeff, valueStr));
|
|
126
|
+
}
|
|
127
|
+
else if (coeff > 0) {
|
|
128
|
+
varTerms.push(`+ ${_formatTerm(coeff, valueStr)}`);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
varTerms.push(`- ${_formatTerm(Math.abs(coeff), valueStr)}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// If no var() terms contribute (callback ignores its input), return the constant directly
|
|
135
|
+
if (varTerms.length === 0) {
|
|
136
|
+
return constant === 0 ? `0${cssUnit}` : `${constant}${cssUnit}`;
|
|
137
|
+
}
|
|
138
|
+
// Append constant if non-zero
|
|
139
|
+
if (constant !== 0) {
|
|
140
|
+
const cStr = `${Math.abs(constant)}${cssUnit}`;
|
|
141
|
+
varTerms.push(constant > 0 ? `+ ${cStr}` : `- ${cStr}`);
|
|
142
|
+
}
|
|
143
|
+
return `calc(${varTerms.join(' ')})`;
|
|
144
|
+
};
|
|
145
|
+
/** Format a coefficient × value term for calc(). Coefficient may be negative for first terms. */
|
|
146
|
+
const _formatTerm = (coeff, value) => {
|
|
147
|
+
const absCoeff = Math.abs(coeff);
|
|
148
|
+
if (absCoeff === 1) {
|
|
149
|
+
return coeff < 0 ? `${value} * -1` : value;
|
|
150
|
+
}
|
|
151
|
+
// Use division for readability when coefficient is a simple reciprocal
|
|
152
|
+
const reciprocal = 1 / absCoeff;
|
|
153
|
+
if (Number.isInteger(reciprocal) && reciprocal > 1) {
|
|
154
|
+
return `${value} / ${coeff < 0 ? -reciprocal : reciprocal}`;
|
|
155
|
+
}
|
|
156
|
+
return `${coeff} * ${value}`;
|
|
157
|
+
};
|
|
158
|
+
/** Round to avoid floating-point artifacts from callback probing */
|
|
159
|
+
const _preciseRound = (n) => Math.round(n * 1e10) / 1e10;
|
|
@@ -46,6 +46,51 @@ describe('mathWithUnits', () => {
|
|
|
46
46
|
expect(mathWithUnits(['50%', '5rem'], (x, y) => x / y, 'px')).toEqual('10px');
|
|
47
47
|
});
|
|
48
48
|
});
|
|
49
|
+
describe('CSS var() references emit calc() expressions', () => {
|
|
50
|
+
test('single var with multiplication', () => {
|
|
51
|
+
expect(mathWithUnits('var(--size)', (x) => x * 2)).toEqual('calc(2 * var(--size))');
|
|
52
|
+
});
|
|
53
|
+
test('single var with division', () => {
|
|
54
|
+
expect(mathWithUnits('var(--size)', (x) => x / 2)).toEqual('calc(var(--size) / 2)');
|
|
55
|
+
});
|
|
56
|
+
test('single var with fractional multiplication', () => {
|
|
57
|
+
expect(mathWithUnits('var(--size)', (x) => x * 0.75)).toEqual('calc(0.75 * var(--size))');
|
|
58
|
+
});
|
|
59
|
+
test('single var with addition', () => {
|
|
60
|
+
expect(mathWithUnits('var(--size)', (x) => x + 2)).toEqual('calc(var(--size) + 2px)');
|
|
61
|
+
});
|
|
62
|
+
test('single var with negative division', () => {
|
|
63
|
+
expect(mathWithUnits('var(--size)', (x) => x / -2)).toEqual('calc(var(--size) / -2)');
|
|
64
|
+
});
|
|
65
|
+
test('single var identity', () => {
|
|
66
|
+
expect(mathWithUnits('var(--size)', (x) => x)).toEqual('calc(var(--size))');
|
|
67
|
+
});
|
|
68
|
+
test('multiple vars with subtraction', () => {
|
|
69
|
+
expect(mathWithUnits(['var(--a)', 'var(--b)'], (x, y) => x - y)).toEqual('calc(var(--a) - var(--b))');
|
|
70
|
+
});
|
|
71
|
+
test('multiple vars with complex linear', () => {
|
|
72
|
+
expect(mathWithUnits(['var(--a)', 'var(--b)'], (x, y) => x - 3 * y)).toEqual('calc(var(--a) - 3 * var(--b))');
|
|
73
|
+
});
|
|
74
|
+
test('mixed var and literal', () => {
|
|
75
|
+
expect(mathWithUnits(['var(--a)', '4px'], (x, y) => x + y)).toEqual('calc(var(--a) + 4px)');
|
|
76
|
+
});
|
|
77
|
+
test('unit detection from literal input', () => {
|
|
78
|
+
expect(mathWithUnits(['var(--a)', '2rem'], (x, y) => x + y)).toEqual('calc(var(--a) + 2rem)');
|
|
79
|
+
});
|
|
80
|
+
test('override unit is respected', () => {
|
|
81
|
+
expect(mathWithUnits('var(--size)', (x) => x + 4, '%')).toEqual('calc(var(--size) + 4%)');
|
|
82
|
+
});
|
|
83
|
+
test('constant-only result', () => {
|
|
84
|
+
expect(mathWithUnits('var(--size)', (_x) => 16)).toEqual('16px');
|
|
85
|
+
});
|
|
86
|
+
test('var mixed with number input — multiplication', () => {
|
|
87
|
+
expect(mathWithUnits(['var(--size)', 3], (x, y) => x * y)).toEqual('calc(3 * var(--size))');
|
|
88
|
+
});
|
|
89
|
+
test('var mixed with number input — addition with closure', () => {
|
|
90
|
+
const base = 16;
|
|
91
|
+
expect(mathWithUnits('var(--size)', (x) => x + base)).toEqual('calc(var(--size) + 16px)');
|
|
92
|
+
});
|
|
93
|
+
});
|
|
49
94
|
describe('weird edge cases', () => {
|
|
50
95
|
it('throws on undefined', () => {
|
|
51
96
|
expect(() => mathWithUnits(undefined, (x) => x * 3)).toThrow('Invalid value type');
|
|
@@ -275,8 +275,6 @@ export declare type _EuiThemeVisColors = {
|
|
|
275
275
|
euiColorVis7: string;
|
|
276
276
|
euiColorVis8: string;
|
|
277
277
|
euiColorVis9: string;
|
|
278
|
-
euiColorVis10: string;
|
|
279
|
-
euiColorVis11: string;
|
|
280
278
|
/** @deprecated - temp token; used only during theme migration */
|
|
281
279
|
euiColorVisBehindText0: string;
|
|
282
280
|
/** @deprecated - temp token; used only during theme migration */
|
|
@@ -297,10 +295,6 @@ export declare type _EuiThemeVisColors = {
|
|
|
297
295
|
euiColorVisBehindText8: string;
|
|
298
296
|
/** @deprecated - temp token; used only during theme migration */
|
|
299
297
|
euiColorVisBehindText9: string;
|
|
300
|
-
/** @deprecated - temp token; used only during theme migration */
|
|
301
|
-
euiColorVisBehindText10: string;
|
|
302
|
-
/** @deprecated - temp token; used only during theme migration */
|
|
303
|
-
euiColorVisBehindText11: string;
|
|
304
298
|
euiColorVisText0: string;
|
|
305
299
|
euiColorVisText1: string;
|
|
306
300
|
euiColorVisText2: string;
|
|
@@ -311,8 +305,6 @@ export declare type _EuiThemeVisColors = {
|
|
|
311
305
|
euiColorVisText7: string;
|
|
312
306
|
euiColorVisText8: string;
|
|
313
307
|
euiColorVisText9: string;
|
|
314
|
-
euiColorVisText10: string;
|
|
315
|
-
euiColorVisText11: string;
|
|
316
308
|
euiColorVisNeutral0: string;
|
|
317
309
|
euiColorVisNeutral1: string;
|
|
318
310
|
euiColorVisSuccess0: string;
|
|
@@ -323,7 +315,6 @@ export declare type _EuiThemeVisColors = {
|
|
|
323
315
|
euiColorVisRisk1: string;
|
|
324
316
|
euiColorVisDanger0: string;
|
|
325
317
|
euiColorVisDanger1: string;
|
|
326
|
-
euiColorVisAssistance: string;
|
|
327
318
|
euiColorVisBase0: string;
|
|
328
319
|
euiColorVisGrey0: string;
|
|
329
320
|
euiColorVisGrey1: string;
|
|
@@ -345,7 +336,6 @@ export declare type _EuiThemeSeverityColors = {
|
|
|
345
336
|
warning: string;
|
|
346
337
|
risk: string;
|
|
347
338
|
danger: string;
|
|
348
|
-
assistance: string;
|
|
349
339
|
};
|
|
350
340
|
export declare type _EuiThemeConstantColors = {
|
|
351
341
|
plainLight: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elastic/eui-theme-common",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.0-snapshot.1776170631053",
|
|
4
4
|
"description": "EUI theme common",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"scripts": {
|
|
@@ -83,5 +83,6 @@
|
|
|
83
83
|
],
|
|
84
84
|
"installConfig": {
|
|
85
85
|
"hoistingLimits": "workspaces"
|
|
86
|
-
}
|
|
86
|
+
},
|
|
87
|
+
"stableVersion": "9.0.0"
|
|
87
88
|
}
|