Browse Source

feat: cssinjs add MULTI_VALUE key (#6770)

pull/6799/head
Cherry7 1 year ago committed by GitHub
parent
commit
b49540a339
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 62
      components/_util/cssinjs/hooks/useStyleRegister.tsx
  2. 38
      components/style/roundedArrow.ts

62
components/_util/cssinjs/hooks/useStyleRegister.tsx

@ -28,7 +28,7 @@ import type { VueNode } from '../../type';
const isClientSide = canUseDom(); const isClientSide = canUseDom();
const SKIP_CHECK = '_skip_check_'; const SKIP_CHECK = '_skip_check_';
const MULTI_VALUE = '_multi_value_';
export type CSSProperties = Omit<CSS.PropertiesFallback<number | string>, 'animationName'> & { export type CSSProperties = Omit<CSS.PropertiesFallback<number | string>, 'animationName'> & {
animationName?: CSS.PropertiesFallback<number | string>['animationName'] | Keyframes; animationName?: CSS.PropertiesFallback<number | string>['animationName'] | Keyframes;
}; };
@ -39,6 +39,7 @@ export type CSSPropertiesWithMultiValues = {
| Extract<CSSProperties[K], string>[] | Extract<CSSProperties[K], string>[]
| { | {
[SKIP_CHECK]: boolean; [SKIP_CHECK]: boolean;
[MULTI_VALUE]?: boolean;
value: CSSProperties[K] | Extract<CSSProperties[K], string>[]; value: CSSProperties[K] | Extract<CSSProperties[K], string>[];
}; };
}; };
@ -65,7 +66,7 @@ export function normalizeStyle(styleStr: string) {
} }
function isCompoundCSSProperty(value: CSSObject[string]) { function isCompoundCSSProperty(value: CSSObject[string]) {
return typeof value === 'object' && value && SKIP_CHECK in value; return typeof value === 'object' && value && (SKIP_CHECK in value || MULTI_VALUE in value);
} }
// hash // hash
@ -224,32 +225,45 @@ export const parseStyle = (
styleStr += `${mergedKey}${parsedStr}`; styleStr += `${mergedKey}${parsedStr}`;
} else { } else {
const actualValue = (value as any)?.value ?? value; function appendStyle(cssKey: string, cssValue: any) {
if ( if (
process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'production' &&
(typeof value !== 'object' || !(value as any)?.[SKIP_CHECK]) (typeof value !== 'object' || !(value as any)?.[SKIP_CHECK])
) { ) {
[contentQuotesLinter, hashedAnimationLinter, ...linters].forEach(linter => [contentQuotesLinter, hashedAnimationLinter, ...linters].forEach(linter =>
linter(key, actualValue, { path, hashId, parentSelectors }), linter(cssKey, cssValue, { path, hashId, parentSelectors }),
); );
} }
// //
const styleName = key.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`); const styleName = cssKey.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`);
// Auto suffix with px // Auto suffix with px
let formatValue = actualValue; let formatValue = cssValue;
if (!unitless[key] && typeof formatValue === 'number' && formatValue !== 0) { if (!unitless[cssKey] && typeof formatValue === 'number' && formatValue !== 0) {
formatValue = `${formatValue}px`; formatValue = `${formatValue}px`;
} }
// handle animationName & Keyframe value // handle animationName & Keyframe value
if (key === 'animationName' && (value as Keyframes)?._keyframe) { if (cssKey === 'animationName' && (cssValue as Keyframes)?._keyframe) {
parseKeyframes(value as Keyframes); parseKeyframes(cssValue as Keyframes);
formatValue = (value as Keyframes).getName(hashId); formatValue = (cssValue as Keyframes).getName(hashId);
} }
styleStr += `${styleName}:${formatValue};`; styleStr += `${styleName}:${formatValue};`;
}
const actualValue = (value as any)?.value ?? value;
if (
typeof value === 'object' &&
(value as any)?.[MULTI_VALUE] &&
Array.isArray(actualValue)
) {
actualValue.forEach(item => {
appendStyle(key, item);
});
} else {
appendStyle(key, actualValue);
}
} }
}); });
} }

38
components/style/roundedArrow.ts

@ -10,31 +10,33 @@ export const roundedArrow = (
): CSSObject => { ): CSSObject => {
const unitWidth = width / 2; const unitWidth = width / 2;
const ax = unitWidth - outerRadius * (Math.sqrt(2) - 1); const ax = 0;
const ay = unitWidth; const ay = unitWidth;
const bx = unitWidth + outerRadius * (1 - 1 / Math.sqrt(2)); const bx = (outerRadius * 1) / Math.sqrt(2);
const by = unitWidth - outerRadius * (1 - 1 / Math.sqrt(2)); const by = unitWidth - outerRadius * (1 - 1 / Math.sqrt(2));
const cx = 2 * unitWidth - innerRadius * (1 / Math.sqrt(2)); const cx = unitWidth - innerRadius * (1 / Math.sqrt(2));
const cy = innerRadius * (1 / Math.sqrt(2)); const cy = outerRadius * (Math.sqrt(2) - 1) + innerRadius * (1 / Math.sqrt(2));
const dx = 4 * unitWidth - cx; const dx = 2 * unitWidth - cx;
const dy = cy; const dy = cy;
const ex = 4 * unitWidth - bx; const ex = 2 * unitWidth - bx;
const ey = by; const ey = by;
const fx = 4 * unitWidth - ax; const fx = 2 * unitWidth - ax;
const fy = ay; const fy = ay;
const shadowWidth = unitWidth * Math.sqrt(2) + outerRadius * (Math.sqrt(2) - 2);
const polygonOffset = outerRadius * (Math.sqrt(2) - 1);
return { return {
borderRadius: { _skip_check_: true, value: `0 0 ${innerRadius}px` },
pointerEvents: 'none', pointerEvents: 'none',
width: width * 2, width,
height: width * 2, height: width,
overflow: 'hidden', overflow: 'hidden',
'&::after': { '&::after': {
content: '""', content: '""',
position: 'absolute', position: 'absolute',
width: width / Math.sqrt(2), width: shadowWidth,
height: width / Math.sqrt(2), height: shadowWidth,
bottom: 0, bottom: 0,
insetInline: 0, insetInline: 0,
margin: 'auto', margin: 'auto',
@ -52,10 +54,18 @@ export const roundedArrow = (
position: 'absolute', position: 'absolute',
bottom: 0, bottom: 0,
insetInlineStart: 0, insetInlineStart: 0,
width: width * 2, width,
height: width / 2, height: width / 2,
background: bgColor, background: bgColor,
clipPath: `path('M ${ax} ${ay} A ${outerRadius} ${outerRadius} 0 0 0 ${bx} ${by} L ${cx} ${cy} A ${innerRadius} ${innerRadius} 0 0 1 ${dx} ${dy} L ${ex} ${ey} A ${outerRadius} ${outerRadius} 0 0 0 ${fx} ${fy} Z')`, clipPath: {
_multi_value_: true,
value: [
`polygon(${polygonOffset}px 100%, 50% ${polygonOffset}px, ${
2 * unitWidth - polygonOffset
}px 100%, ${polygonOffset}px 100%)`,
`path('M ${ax} ${ay} A ${outerRadius} ${outerRadius} 0 0 0 ${bx} ${by} L ${cx} ${cy} A ${innerRadius} ${innerRadius} 0 0 1 ${dx} ${dy} L ${ex} ${ey} A ${outerRadius} ${outerRadius} 0 0 0 ${fx} ${fy} Z')`,
],
} as any,
content: '""', content: '""',
}, },
}; };

Loading…
Cancel
Save