From d0184280df36dbb5ba728a957f605b71eafa8cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=8F=8E=EF=B8=8F=20Yumo?= Date: Fri, 12 Jul 2024 10:50:34 +0800 Subject: [PATCH] fix: code review. --- package.json | 1 - src/util/genStyleUtils.tsx | 8 ++++---- src/util/getAlphaColor.ts | 29 --------------------------- tests/util.test.tsx | 40 -------------------------------------- 4 files changed, 4 insertions(+), 74 deletions(-) delete mode 100644 src/util/getAlphaColor.ts delete mode 100644 tests/util.test.tsx diff --git a/package.json b/package.json index 114a83c..3a8e3f7 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,6 @@ }, "dependencies": { "@ant-design/cssinjs": "^1.21.0", - "@ant-design/fast-color": "^1.2.0", "@babel/runtime": "^7.23.2", "rc-util": "^5.38.0" }, diff --git a/src/util/genStyleUtils.tsx b/src/util/genStyleUtils.tsx index 51ff3e8..b6425e7 100644 --- a/src/util/genStyleUtils.tsx +++ b/src/util/genStyleUtils.tsx @@ -93,7 +93,7 @@ export type CSSVarRegisterProps = { }; }; -export type GenResetStyles = (token: OverrideTokenMap) => CSSInterpolation; +export type GetResetStyles = (token: OverrideTokenMap) => CSSInterpolation; export default function genStyleUtils< CompTokenMap extends TokenMap, @@ -104,7 +104,7 @@ export default function genStyleUtils< usePrefix: UsePrefix; useToken: UseToken; useCSP?: UseCSP; - genResetStyles?: GenResetStyles, + getResetStyles?: GetResetStyles, } ) { // Dependency inversion for preparing basic config. @@ -112,7 +112,7 @@ export default function genStyleUtils< useCSP = useDefaultCSP, useToken, usePrefix, - genResetStyles, + getResetStyles, } = config; function genStyleHooks>( @@ -375,7 +375,7 @@ export default function genStyleUtils< // Generate style for all need reset tags. useStyleRegister( { ...sharedConfig, clientOnly: false, path: ['Shared', rootPrefixCls] }, - () => genResetStyles?.(token) ?? [], + () => getResetStyles?.(token) ?? [], ); const wrapSSR = useStyleRegister( diff --git a/src/util/getAlphaColor.ts b/src/util/getAlphaColor.ts deleted file mode 100644 index 5333877..0000000 --- a/src/util/getAlphaColor.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { FastColor } from '@ant-design/fast-color'; - -function isStableColor(color: number): boolean { - return color >= 0 && color <= 255; -} - -function getAlphaColor(frontColor: string, backgroundColor: string): string { - const { r: fR, g: fG, b: fB, a: originAlpha } = new FastColor(frontColor).toRgb(); - if (originAlpha < 1) { - return frontColor; - } - - const { r: bR, g: bG, b: bB } = new FastColor(backgroundColor).toRgb(); - - for (let fA = 0.01; fA <= 1; fA += 0.01) { - const r = Math.round((fR - bR * (1 - fA)) / fA); - const g = Math.round((fG - bG * (1 - fA)) / fA); - const b = Math.round((fB - bB * (1 - fA)) / fA); - if (isStableColor(r) && isStableColor(g) && isStableColor(b)) { - return new FastColor({ r, g, b, a: Math.round(fA * 100) / 100 }).toRgbString(); - } - } - - // fallback - /* istanbul ignore next */ - return new FastColor({ r: fR, g: fG, b: fB, a: 1 }).toRgbString(); -} - -export default getAlphaColor; diff --git a/tests/util.test.tsx b/tests/util.test.tsx deleted file mode 100644 index 21b89e7..0000000 --- a/tests/util.test.tsx +++ /dev/null @@ -1,40 +0,0 @@ -import getAlphaColor from '../src/util/getAlphaColor'; -import genMaxMin from '../src/util/maxmin'; - -describe('util', () => { - describe('getAlphaColor', () => { - it('should not process color with alpha', () => { - expect(getAlphaColor('rgba(0, 0, 0, 0.5)', 'rgba(255, 255, 255)')).toBe('rgba(0, 0, 0, 0.5)'); - }); - }); - - describe('maxmin', () => { - const cases = [ - { - values: [1, 2, 3], - js: { - max: 3, - min: 1, - }, - css: { - max: 'max(1px,2px,3px)', - min: 'min(1px,2px,3px)', - }, - }, - ]; - - cases.forEach(({ values, js, css }, index) => { - it(`js maxmin ${index + 1}`, () => { - const { max, min } = genMaxMin('js'); - expect(max(...values)).toEqual(js.max); - expect(min(...values)).toEqual(js.min); - }); - - it(`css maxmin ${index + 1}`, () => { - const { max, min } = genMaxMin('css'); - expect(max(...values)).toEqual(css.max); - expect(min(...values)).toEqual(css.min); - }); - }); - }); -});