Skip to content

Commit 2c79d2a

Browse files
Revert "Use less @plugin" (ant-design#13613)
1 parent 377062b commit 2c79d2a

14 files changed

Lines changed: 1524 additions & 831 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ components/**/*.js
3737
components/**/*.jsx
3838
!components/**/__tests__/*.js
3939
!components/**/__tests__/*.js.snap
40-
!components/style/color/*.js
4140
/.history
4241
# Docs templates
4342
site/theme/template/IconDisplay/*.js

.stylelintrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
{
22
"extends": ["stylelint-config-standard", "stylelint-config-prettier"],
33
"rules": {
4-
"at-rule-no-unknown": [true, {
5-
ignoreAtRules: ["plugin"]
6-
}],
74
"comment-empty-line-before": null,
85
"declaration-empty-line-before": null,
96
"function-comma-newline-after": null,

components/button/style/mixin.less

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@
2929

3030
&:hover,
3131
&:focus {
32-
.button-color(@color; color-palette(@background, 5) ; color-palette(@background, 5));
32+
.button-color(
33+
@color; ~`colorPalette('@{background}', 5) `; ~`colorPalette('@{background}', 5) `
34+
);
3335
}
3436

3537
&:active,
3638
&.active {
37-
.button-color(@color; color-palette(@background, 7) ; color-palette(@background, 7));
39+
.button-color(
40+
@color; ~`colorPalette('@{background}', 7) `; ~`colorPalette('@{background}', 7) `
41+
);
3842
}
3943

4044
.button-disabled();
@@ -60,16 +64,20 @@
6064
.button-color(@color; @background; @border);
6165

6266
&:hover {
63-
.button-color(@btn-primary-color; color-palette(@color, 5) ; color-palette(@color, 5));
67+
.button-color(
68+
@btn-primary-color; ~`colorPalette('@{color}', 5) `; ~`colorPalette('@{color}', 5) `
69+
);
6470
}
6571

6672
&:focus {
67-
.button-color(color-palette(@color, 5) ; #fff; color-palette(@color, 5));
73+
.button-color(~`colorPalette('@{color}', 5) `; #fff; ~`colorPalette('@{color}', 5) `);
6874
}
6975

7076
&:active,
7177
&.active {
72-
.button-color(@btn-primary-color; color-palette(@color, 7) ; color-palette(@color, 7));
78+
.button-color(
79+
@btn-primary-color; ~`colorPalette('@{color}', 7) `; ~`colorPalette('@{color}', 7) `
80+
);
7381
}
7482

7583
.button-disabled();
@@ -81,12 +89,12 @@
8189

8290
&:hover,
8391
&:focus {
84-
.button-color(color-palette(@color, 5) ; transparent; color-palette(@color, 5));
92+
.button-color(~`colorPalette('@{color}', 5) `; transparent; ~`colorPalette('@{color}', 5) `);
8593
}
8694

8795
&:active,
8896
&.active {
89-
.button-color(color-palette(@color, 7) ; transparent; color-palette(@color, 7));
97+
.button-color(~`colorPalette('@{color}', 7) `; transparent; ~`colorPalette('@{color}', 7) `);
9098
}
9199

92100
.button-disabled();

components/input/style/mixin.less

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
// input status
1919
// == when focus or actived
2020
.active(@color: @outline-color) {
21-
border-color: color-palette(@color, 5);
21+
border-color: ~`colorPalette('@{color}', 5) `;
2222
outline: 0;
2323
box-shadow: @input-outline-offset @outline-blur-size @outline-width fade(@color, 20%);
2424
border-right-width: @border-width-base !important;
2525
}
2626

2727
// == when hoverd
2828
.hover(@color: @input-hover-border-color) {
29-
border-color: color-palette(@color, 5);
29+
border-color: ~`colorPalette('@{color}', 5) `;
3030
border-right-width: @border-width-base !important;
3131
}
3232

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* stylelint-disable */
2+
.bezierEasingMixin() {
3+
@functions: ~`(function() {
4+
var NEWTON_ITERATIONS = 4;
5+
var NEWTON_MIN_SLOPE = 0.001;
6+
var SUBDIVISION_PRECISION = 0.0000001;
7+
var SUBDIVISION_MAX_ITERATIONS = 10;
8+
9+
var kSplineTableSize = 11;
10+
var kSampleStepSize = 1.0 / (kSplineTableSize - 1.0);
11+
12+
var float32ArraySupported = typeof Float32Array === 'function';
13+
14+
function A (aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1; }
15+
function B (aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1; }
16+
function C (aA1) { return 3.0 * aA1; }
17+
18+
// Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2.
19+
function calcBezier (aT, aA1, aA2) { return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT; }
20+
21+
// Returns dx/dt given t, x1, and x2, or dy/dt given t, y1, and y2.
22+
function getSlope (aT, aA1, aA2) { return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1); }
23+
24+
function binarySubdivide (aX, aA, aB, mX1, mX2) {
25+
var currentX, currentT, i = 0;
26+
do {
27+
currentT = aA + (aB - aA) / 2.0;
28+
currentX = calcBezier(currentT, mX1, mX2) - aX;
29+
if (currentX > 0.0) {
30+
aB = currentT;
31+
} else {
32+
aA = currentT;
33+
}
34+
} while (Math.abs(currentX) > SUBDIVISION_PRECISION && ++i < SUBDIVISION_MAX_ITERATIONS);
35+
return currentT;
36+
}
37+
38+
function newtonRaphsonIterate (aX, aGuessT, mX1, mX2) {
39+
for (var i = 0; i < NEWTON_ITERATIONS; ++i) {
40+
var currentSlope = getSlope(aGuessT, mX1, mX2);
41+
if (currentSlope === 0.0) {
42+
return aGuessT;
43+
}
44+
var currentX = calcBezier(aGuessT, mX1, mX2) - aX;
45+
aGuessT -= currentX / currentSlope;
46+
}
47+
return aGuessT;
48+
}
49+
50+
var BezierEasing = function (mX1, mY1, mX2, mY2) {
51+
if (!(0 <= mX1 && mX1 <= 1 && 0 <= mX2 && mX2 <= 1)) {
52+
throw new Error('bezier x values must be in [0, 1] range');
53+
}
54+
55+
// Precompute samples table
56+
var sampleValues = float32ArraySupported ? new Float32Array(kSplineTableSize) : new Array(kSplineTableSize);
57+
if (mX1 !== mY1 || mX2 !== mY2) {
58+
for (var i = 0; i < kSplineTableSize; ++i) {
59+
sampleValues[i] = calcBezier(i * kSampleStepSize, mX1, mX2);
60+
}
61+
}
62+
63+
function getTForX (aX) {
64+
var intervalStart = 0.0;
65+
var currentSample = 1;
66+
var lastSample = kSplineTableSize - 1;
67+
68+
for (; currentSample !== lastSample && sampleValues[currentSample] <= aX; ++currentSample) {
69+
intervalStart += kSampleStepSize;
70+
}
71+
--currentSample;
72+
73+
// Interpolate to provide an initial guess for t
74+
var dist = (aX - sampleValues[currentSample]) / (sampleValues[currentSample + 1] - sampleValues[currentSample]);
75+
var guessForT = intervalStart + dist * kSampleStepSize;
76+
77+
var initialSlope = getSlope(guessForT, mX1, mX2);
78+
if (initialSlope >= NEWTON_MIN_SLOPE) {
79+
return newtonRaphsonIterate(aX, guessForT, mX1, mX2);
80+
} else if (initialSlope === 0.0) {
81+
return guessForT;
82+
} else {
83+
return binarySubdivide(aX, intervalStart, intervalStart + kSampleStepSize, mX1, mX2);
84+
}
85+
}
86+
87+
return function BezierEasing (x) {
88+
if (mX1 === mY1 && mX2 === mY2) {
89+
return x; // linear
90+
}
91+
// Because JavaScript number are imprecise, we should guarantee the extremes are right.
92+
if (x === 0) {
93+
return 0;
94+
}
95+
if (x === 1) {
96+
return 1;
97+
}
98+
return calcBezier(getTForX(x), mY1, mY2);
99+
};
100+
};
101+
102+
this.colorEasing = BezierEasing(0.26, 0.09, 0.37, 0.18);
103+
// less 3 requires a return
104+
return '';
105+
})()`;
106+
}
107+
// It is hacky way to make this function will be compiled preferentially by less
108+
// resolve error: `ReferenceError: colorPalette is not defined`
109+
// https://github.com/ant-design/ant-motion/issues/44
110+
.bezierEasingMixin();

components/style/color/color-palette.js

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* stylelint-disable no-duplicate-selectors */
2+
@import "bezierEasing";
3+
@import "tinyColor";
4+
5+
// We create a very complex algorithm which take the place of original tint/shade color system
6+
// to make sure no one can understand it 👻
7+
// and create an entire color palette magicly by inputing just a single primary color.
8+
// We are using bezier-curve easing function and some color manipulations like tint/shade/darken/spin
9+
.colorPaletteMixin() {
10+
@functions: ~`(function() {
11+
var hueStep = 2;
12+
var saturationStep = 16;
13+
var saturationStep2 = 5;
14+
var brightnessStep1 = 5;
15+
var brightnessStep2 = 15;
16+
var lightColorCount = 5;
17+
var darkColorCount = 4;
18+
19+
var getHue = function(hsv, i, isLight) {
20+
var hue;
21+
if (hsv.h >= 60 && hsv.h <= 240) {
22+
hue = isLight ? hsv.h - hueStep * i : hsv.h + hueStep * i;
23+
} else {
24+
hue = isLight ? hsv.h + hueStep * i : hsv.h - hueStep * i;
25+
}
26+
if (hue < 0) {
27+
hue += 360;
28+
} else if (hue >= 360) {
29+
hue -= 360;
30+
}
31+
return Math.round(hue);
32+
};
33+
var getSaturation = function(hsv, i, isLight) {
34+
var saturation;
35+
if (isLight) {
36+
saturation = Math.round(hsv.s * 100) - saturationStep * i;
37+
} else if (i == darkColorCount) {
38+
saturation = Math.round(hsv.s * 100) + saturationStep;
39+
} else {
40+
saturation = Math.round(hsv.s * 100) + saturationStep2 * i;
41+
}
42+
if (saturation > 100) {
43+
saturation = 100;
44+
}
45+
if (isLight && i === lightColorCount && saturation > 10) {
46+
saturation = 10;
47+
}
48+
if (saturation < 6) {
49+
saturation = 6;
50+
}
51+
return Math.round(saturation);
52+
};
53+
var getValue = function(hsv, i, isLight) {
54+
if (isLight) {
55+
return Math.round(hsv.v * 100) + brightnessStep1 * i;
56+
}
57+
return Math.round(hsv.v * 100) - brightnessStep2 * i;
58+
};
59+
60+
this.colorPalette = function(color, index) {
61+
var isLight = index <= 6;
62+
var hsv = tinycolor(color).toHsv();
63+
var i = isLight ? lightColorCount + 1 - index : index - lightColorCount - 1;
64+
return tinycolor({
65+
h: getHue(hsv, i, isLight),
66+
s: getSaturation(hsv, i, isLight),
67+
v: getValue(hsv, i, isLight),
68+
}).toHexString();
69+
};
70+
})()`;
71+
}
72+
// It is hacky way to make this function will be compiled preferentially by less
73+
// resolve error: `ReferenceError: colorPalette is not defined`
74+
// https://github.com/ant-design/ant-motion/issues/44
75+
.colorPaletteMixin();

0 commit comments

Comments
 (0)