mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-06 16:06:28 +08:00
refactor: wave css variables (#36263)
* refactor: wave use css variables * fix: get wave color * chore: code clean * test: update test case * test: add test case * chore: code clean * test: update test case
This commit is contained in:
parent
619d786948
commit
2c7a8af2f4
@ -49,7 +49,10 @@ describe('Wave component', () => {
|
|||||||
it('wave color is grey', async () => {
|
it('wave color is grey', async () => {
|
||||||
const wrapper = mount(
|
const wrapper = mount(
|
||||||
<Wave>
|
<Wave>
|
||||||
<button type="button" style={{ borderColor: 'rgb(0, 0, 0)' }}>
|
<button
|
||||||
|
type="button"
|
||||||
|
style={{ borderColor: 'rgb(0, 0, 0)', backgroundColor: 'transparent' }}
|
||||||
|
>
|
||||||
button
|
button
|
||||||
</button>
|
</button>
|
||||||
</Wave>,
|
</Wave>,
|
||||||
@ -197,4 +200,36 @@ describe('Wave component', () => {
|
|||||||
it('should not throw when no children', () => {
|
it('should not throw when no children', () => {
|
||||||
expect(() => mount(<Wave />)).not.toThrow();
|
expect(() => mount(<Wave />)).not.toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('wave color should inferred if border is transparent and background is not', async () => {
|
||||||
|
const wrapper = mount(
|
||||||
|
<Wave>
|
||||||
|
<button type="button" style={{ borderColor: 'transparent', background: 'red' }}>
|
||||||
|
button
|
||||||
|
</button>
|
||||||
|
</Wave>,
|
||||||
|
);
|
||||||
|
wrapper.find('button').getDOMNode().click();
|
||||||
|
await sleep(200);
|
||||||
|
const styles = wrapper.find('button').getDOMNode().getRootNode().getElementsByTagName('style');
|
||||||
|
expect(styles.length).toBe(1);
|
||||||
|
expect(styles[0].innerHTML).toContain('--antd-wave-shadow-color: red;');
|
||||||
|
wrapper.unmount();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('wave color should inferred if borderTopColor is transparent and borderColor is not', async () => {
|
||||||
|
const wrapper = mount(
|
||||||
|
<Wave>
|
||||||
|
<button type="button" style={{ borderColor: 'red', borderTopColor: 'transparent' }}>
|
||||||
|
button
|
||||||
|
</button>
|
||||||
|
</Wave>,
|
||||||
|
);
|
||||||
|
wrapper.find('button').getDOMNode().click();
|
||||||
|
await sleep(200);
|
||||||
|
const styles = wrapper.find('button').getDOMNode().getRootNode().getElementsByTagName('style');
|
||||||
|
expect(styles.length).toBe(1);
|
||||||
|
expect(styles[0].innerHTML).toContain('--antd-wave-shadow-color: red;');
|
||||||
|
wrapper.unmount();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -27,6 +27,31 @@ function isNotGrey(color: string) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isValidWaveColor(color: string) {
|
||||||
|
return (
|
||||||
|
color &&
|
||||||
|
color !== '#ffffff' &&
|
||||||
|
color !== 'rgb(255, 255, 255)' &&
|
||||||
|
isNotGrey(color) &&
|
||||||
|
!/rgba\((?:\d*, ){3}0\)/.test(color) && // any transparent rgba color
|
||||||
|
color !== 'transparent'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTargetWaveColor(node: HTMLElement) {
|
||||||
|
const computedStyle = getComputedStyle(node);
|
||||||
|
const borderTopColor = computedStyle.getPropertyValue('border-top-color');
|
||||||
|
const borderColor = computedStyle.getPropertyValue('border-color');
|
||||||
|
const backgroundColor = computedStyle.getPropertyValue('background-color');
|
||||||
|
if (isValidWaveColor(borderTopColor)) {
|
||||||
|
return borderTopColor;
|
||||||
|
}
|
||||||
|
if (isValidWaveColor(borderColor)) {
|
||||||
|
return borderColor;
|
||||||
|
}
|
||||||
|
return backgroundColor;
|
||||||
|
}
|
||||||
|
|
||||||
export interface WaveProps {
|
export interface WaveProps {
|
||||||
insertExtraNode?: boolean;
|
insertExtraNode?: boolean;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
@ -90,14 +115,7 @@ class InternalWave extends React.Component<WaveProps> {
|
|||||||
const attributeName = this.getAttributeName();
|
const attributeName = this.getAttributeName();
|
||||||
node.setAttribute(attributeName, 'true');
|
node.setAttribute(attributeName, 'true');
|
||||||
// Not white or transparent or grey
|
// Not white or transparent or grey
|
||||||
if (
|
if (isValidWaveColor(waveColor)) {
|
||||||
waveColor &&
|
|
||||||
waveColor !== '#ffffff' &&
|
|
||||||
waveColor !== 'rgb(255, 255, 255)' &&
|
|
||||||
isNotGrey(waveColor) &&
|
|
||||||
!/rgba\((?:\d*, ){3}0\)/.test(waveColor) && // any transparent rgba color
|
|
||||||
waveColor !== 'transparent'
|
|
||||||
) {
|
|
||||||
extraNode.style.borderColor = waveColor;
|
extraNode.style.borderColor = waveColor;
|
||||||
|
|
||||||
const nodeRoot = node.getRootNode?.() || node.ownerDocument;
|
const nodeRoot = node.getRootNode?.() || node.ownerDocument;
|
||||||
@ -167,10 +185,7 @@ class InternalWave extends React.Component<WaveProps> {
|
|||||||
}
|
}
|
||||||
this.resetEffect(node);
|
this.resetEffect(node);
|
||||||
// Get wave color from target
|
// Get wave color from target
|
||||||
const waveColor =
|
const waveColor = getTargetWaveColor(node);
|
||||||
getComputedStyle(node).getPropertyValue('border-top-color') || // Firefox Compatible
|
|
||||||
getComputedStyle(node).getPropertyValue('border-color') ||
|
|
||||||
getComputedStyle(node).getPropertyValue('background-color');
|
|
||||||
this.clickWaveTimeoutId = window.setTimeout(() => this.onClick(node, waveColor), 0);
|
this.clickWaveTimeoutId = window.setTimeout(() => this.onClick(node, waveColor), 0);
|
||||||
|
|
||||||
raf.cancel(this.animationStartId);
|
raf.cancel(this.animationStartId);
|
||||||
|
@ -15,7 +15,7 @@ interface WaveToken extends AliasToken {
|
|||||||
const genWaveStyle: GenerateStyle<WaveToken> = token => {
|
const genWaveStyle: GenerateStyle<WaveToken> = token => {
|
||||||
const waveEffect = new Keyframes('waveEffect', {
|
const waveEffect = new Keyframes('waveEffect', {
|
||||||
'100%': {
|
'100%': {
|
||||||
boxShadow: `0 0 0 6px ${token.colorPrimary}`,
|
boxShadow: `0 0 0 6px var(--antd-wave-shadow-color)`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -29,6 +29,8 @@ const genWaveStyle: GenerateStyle<WaveToken> = token => {
|
|||||||
{
|
{
|
||||||
[`${token.clickAnimatingWithoutExtraNodeTrue},
|
[`${token.clickAnimatingWithoutExtraNodeTrue},
|
||||||
${token.clickAnimatingTrue}`]: {
|
${token.clickAnimatingTrue}`]: {
|
||||||
|
'--antd-wave-shadow-color': token.colorPrimary,
|
||||||
|
'--scroll-bar': 0,
|
||||||
position: 'relative',
|
position: 'relative',
|
||||||
},
|
},
|
||||||
[`${token.clickAnimatingWithoutExtraNodeTrueAfter},
|
[`${token.clickAnimatingWithoutExtraNodeTrueAfter},
|
||||||
@ -40,16 +42,20 @@ const genWaveStyle: GenerateStyle<WaveToken> = token => {
|
|||||||
bottom: 0,
|
bottom: 0,
|
||||||
display: 'block',
|
display: 'block',
|
||||||
borderRadius: 'inherit',
|
borderRadius: 'inherit',
|
||||||
boxShadow: `0 0 0 0 ${token.colorPrimary}`,
|
boxShadow: `0 0 0 0 var(--antd-wave-shadow-color)`,
|
||||||
opacity: 0.2,
|
opacity: 0.2,
|
||||||
animation: `${fadeEffect.getName(token.hashId)} 2s ${
|
animation: {
|
||||||
token.motionEaseOutCirc
|
_skip_check_: true,
|
||||||
}, ${waveEffect.getName(token.hashId)} 0.4s ${token.motionEaseOutCirc}`,
|
value: `${fadeEffect.getName(token.hashId)} 2s ${
|
||||||
|
token.motionEaseOutCirc
|
||||||
|
}, ${waveEffect.getName(token.hashId)} 0.4s ${token.motionEaseOutCirc}`,
|
||||||
|
},
|
||||||
animationFillMode: 'forwards',
|
animationFillMode: 'forwards',
|
||||||
content: '""',
|
content: '""',
|
||||||
pointerEvents: 'none',
|
pointerEvents: 'none',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{},
|
||||||
waveEffect,
|
waveEffect,
|
||||||
fadeEffect,
|
fadeEffect,
|
||||||
];
|
];
|
||||||
|
Loading…
Reference in New Issue
Block a user