2021-03-19 16:23:25 +08:00
|
|
|
import { updateCSS } from 'rc-util/lib/Dom/dynamicCSS';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { composeRef, supportRef } from 'rc-util/lib/ref';
|
|
|
|
import * as React from 'react';
|
2022-06-27 16:57:34 +08:00
|
|
|
import { forwardRef } from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ConfigConsumerProps, CSPConfig } from '../config-provider';
|
|
|
|
import { ConfigConsumer, ConfigContext } from '../config-provider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import raf from './raf';
|
2020-11-16 16:46:46 +08:00
|
|
|
import { cloneElement } from './reactNode';
|
2018-08-11 14:57:41 +08:00
|
|
|
|
2020-10-07 02:09:08 +08:00
|
|
|
let styleForPseudo: HTMLStyleElement | null;
|
2018-08-31 17:38:02 +08:00
|
|
|
|
2018-11-08 12:33:56 +08:00
|
|
|
// Where el is the DOM element you'd like to test for visibility
|
|
|
|
function isHidden(element: HTMLElement) {
|
2018-11-12 12:38:02 +08:00
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-10 12:16:06 +08:00
|
|
|
return !element || element.offsetParent === null || element.hidden;
|
2018-11-08 12:33:56 +08:00
|
|
|
}
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
function isNotGrey(color: string) {
|
2019-08-06 15:02:05 +08:00
|
|
|
// eslint-disable-next-line no-useless-escape
|
2020-04-03 15:02:11 +08:00
|
|
|
const match = (color || '').match(/rgba?\((\d*), (\d*), (\d*)(, [\d.]*)?\)/);
|
2019-08-05 18:38:10 +08:00
|
|
|
if (match && match[1] && match[2] && match[3]) {
|
|
|
|
return !(match[1] === match[2] && match[2] === match[3]);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:31:42 +08:00
|
|
|
export interface WaveProps {
|
|
|
|
insertExtraNode?: boolean;
|
|
|
|
disabled?: boolean;
|
2022-04-08 22:55:42 +08:00
|
|
|
children?: React.ReactNode;
|
2021-09-14 15:31:42 +08:00
|
|
|
}
|
|
|
|
|
2022-06-27 16:57:34 +08:00
|
|
|
class InternalWave extends React.Component<WaveProps> {
|
2020-05-26 10:32:35 +08:00
|
|
|
static contextType = ConfigContext;
|
|
|
|
|
2018-08-11 14:57:41 +08:00
|
|
|
private instance?: {
|
|
|
|
cancel: () => void;
|
|
|
|
};
|
|
|
|
|
2020-11-16 16:46:46 +08:00
|
|
|
private containerRef = React.createRef<HTMLDivElement>();
|
|
|
|
|
2018-08-20 23:55:35 +08:00
|
|
|
private extraNode: HTMLDivElement;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2018-08-20 23:55:35 +08:00
|
|
|
private clickWaveTimeoutId: number;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2018-11-12 20:31:58 +08:00
|
|
|
private animationStartId: number;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2018-11-12 20:31:58 +08:00
|
|
|
private animationStart: boolean = false;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-03-19 19:27:02 +08:00
|
|
|
private destroyed: boolean = false;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-01-09 20:15:37 +08:00
|
|
|
private csp?: CSPConfig;
|
2018-08-11 14:57:41 +08:00
|
|
|
|
2020-05-26 10:32:35 +08:00
|
|
|
context: ConfigConsumerProps;
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
componentDidMount() {
|
2022-06-02 18:39:32 +08:00
|
|
|
this.destroyed = false;
|
2020-11-16 16:46:46 +08:00
|
|
|
const node = this.containerRef.current as HTMLDivElement;
|
2019-08-05 18:38:10 +08:00
|
|
|
if (!node || node.nodeType !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.instance = this.bindAnimationEvent(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.instance) {
|
|
|
|
this.instance.cancel();
|
2018-08-11 14:57:41 +08:00
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
if (this.clickWaveTimeoutId) {
|
|
|
|
clearTimeout(this.clickWaveTimeoutId);
|
|
|
|
}
|
|
|
|
|
2020-03-19 19:27:02 +08:00
|
|
|
this.destroyed = true;
|
2018-08-11 14:57:41 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 23:55:35 +08:00
|
|
|
onClick = (node: HTMLElement, waveColor: string) => {
|
2021-09-14 15:31:42 +08:00
|
|
|
const { insertExtraNode, disabled } = this.props;
|
|
|
|
|
|
|
|
if (disabled || !node || isHidden(node) || node.className.indexOf('-leave') >= 0) {
|
2018-08-11 15:46:45 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-09-14 15:31:42 +08:00
|
|
|
|
2018-08-20 23:55:35 +08:00
|
|
|
this.extraNode = document.createElement('div');
|
2019-08-05 18:38:10 +08:00
|
|
|
const { extraNode } = this;
|
2020-05-26 10:32:35 +08:00
|
|
|
const { getPrefixCls } = this.context;
|
|
|
|
extraNode.className = `${getPrefixCls('')}-click-animating-node`;
|
2018-08-20 23:55:35 +08:00
|
|
|
const attributeName = this.getAttributeName();
|
2018-08-11 14:57:41 +08:00
|
|
|
node.setAttribute(attributeName, 'true');
|
2020-10-07 02:09:08 +08:00
|
|
|
// Not white or transparent or grey
|
2018-12-07 20:02:01 +08:00
|
|
|
if (
|
|
|
|
waveColor &&
|
|
|
|
waveColor !== '#ffffff' &&
|
|
|
|
waveColor !== 'rgb(255, 255, 255)' &&
|
2019-08-05 18:38:10 +08:00
|
|
|
isNotGrey(waveColor) &&
|
2020-04-03 15:02:11 +08:00
|
|
|
!/rgba\((?:\d*, ){3}0\)/.test(waveColor) && // any transparent rgba color
|
2018-12-07 20:02:01 +08:00
|
|
|
waveColor !== 'transparent'
|
|
|
|
) {
|
2018-08-11 14:57:41 +08:00
|
|
|
extraNode.style.borderColor = waveColor;
|
2021-03-19 16:23:25 +08:00
|
|
|
|
2021-03-25 11:57:49 +08:00
|
|
|
const nodeRoot = node.getRootNode?.() || node.ownerDocument;
|
2021-04-03 22:31:13 +08:00
|
|
|
const nodeBody: Element =
|
|
|
|
nodeRoot instanceof Document ? nodeRoot.body : (nodeRoot.firstChild as Element) ?? nodeRoot;
|
|
|
|
|
2021-03-19 16:23:25 +08:00
|
|
|
styleForPseudo = updateCSS(
|
|
|
|
`
|
2020-05-26 10:32:35 +08:00
|
|
|
[${getPrefixCls('')}-click-animating-without-extra-node='true']::after, .${getPrefixCls(
|
2021-03-19 16:23:25 +08:00
|
|
|
'',
|
|
|
|
)}-click-animating-node {
|
2019-07-29 16:29:54 +08:00
|
|
|
--antd-wave-shadow-color: ${waveColor};
|
2021-03-19 16:23:25 +08:00
|
|
|
}`,
|
|
|
|
'antd-wave',
|
2021-03-25 11:57:49 +08:00
|
|
|
{ csp: this.csp, attachTo: nodeBody },
|
2021-03-19 16:23:25 +08:00
|
|
|
);
|
2018-08-11 14:57:41 +08:00
|
|
|
}
|
|
|
|
if (insertExtraNode) {
|
|
|
|
node.appendChild(extraNode);
|
|
|
|
}
|
2020-12-04 19:36:16 +08:00
|
|
|
['transition', 'animation'].forEach(name => {
|
|
|
|
node.addEventListener(`${name}start`, this.onTransitionStart);
|
|
|
|
node.addEventListener(`${name}end`, this.onTransitionEnd);
|
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-08-11 14:57:41 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
onTransitionStart = (e: AnimationEvent) => {
|
2020-03-19 19:27:02 +08:00
|
|
|
if (this.destroyed) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-11-16 16:46:46 +08:00
|
|
|
const node = this.containerRef.current as HTMLDivElement;
|
2020-03-19 19:27:02 +08:00
|
|
|
if (!e || e.target !== node || this.animationStart) {
|
2019-08-05 18:38:10 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-03-19 19:27:02 +08:00
|
|
|
this.resetEffect(node);
|
2019-08-05 18:38:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
onTransitionEnd = (e: AnimationEvent) => {
|
|
|
|
if (!e || e.animationName !== 'fadeEffect') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.resetEffect(e.target as HTMLElement);
|
|
|
|
};
|
|
|
|
|
|
|
|
getAttributeName() {
|
2020-05-26 10:32:35 +08:00
|
|
|
const { getPrefixCls } = this.context;
|
2019-08-05 18:38:10 +08:00
|
|
|
const { insertExtraNode } = this.props;
|
2020-05-26 10:32:35 +08:00
|
|
|
return insertExtraNode
|
|
|
|
? `${getPrefixCls('')}-click-animating`
|
|
|
|
: `${getPrefixCls('')}-click-animating-without-extra-node`;
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
|
2018-08-11 14:57:41 +08:00
|
|
|
bindAnimationEvent = (node: HTMLElement) => {
|
2018-12-07 20:02:01 +08:00
|
|
|
if (
|
|
|
|
!node ||
|
|
|
|
!node.getAttribute ||
|
|
|
|
node.getAttribute('disabled') ||
|
|
|
|
node.className.indexOf('disabled') >= 0
|
|
|
|
) {
|
2018-08-11 14:57:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const onClick = (e: MouseEvent) => {
|
|
|
|
// Fix radio button click twice
|
2018-11-08 12:33:56 +08:00
|
|
|
if ((e.target as HTMLElement).tagName === 'INPUT' || isHidden(e.target as HTMLElement)) {
|
2018-08-11 14:57:41 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-08-20 23:55:35 +08:00
|
|
|
this.resetEffect(node);
|
|
|
|
// Get wave color from target
|
|
|
|
const waveColor =
|
|
|
|
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);
|
2018-11-12 20:31:58 +08:00
|
|
|
|
|
|
|
raf.cancel(this.animationStartId);
|
|
|
|
this.animationStart = true;
|
|
|
|
|
|
|
|
// Render to trigger transition event cost 3 frames. Let's delay 10 frames to reset this.
|
|
|
|
this.animationStartId = raf(() => {
|
|
|
|
this.animationStart = false;
|
|
|
|
}, 10);
|
2018-08-11 14:57:41 +08:00
|
|
|
};
|
|
|
|
node.addEventListener('click', onClick, true);
|
|
|
|
return {
|
|
|
|
cancel: () => {
|
|
|
|
node.removeEventListener('click', onClick, true);
|
|
|
|
},
|
|
|
|
};
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-08-11 14:57:41 +08:00
|
|
|
|
2018-08-20 23:55:35 +08:00
|
|
|
resetEffect(node: HTMLElement) {
|
2018-12-04 18:26:47 +08:00
|
|
|
if (!node || node === this.extraNode || !(node instanceof Element)) {
|
2018-08-27 11:43:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-08-20 23:55:35 +08:00
|
|
|
const { insertExtraNode } = this.props;
|
|
|
|
const attributeName = this.getAttributeName();
|
2019-01-22 16:23:59 +08:00
|
|
|
node.setAttribute(attributeName, 'false'); // edge has bug on `removeAttribute` #14466
|
2018-08-20 23:55:35 +08:00
|
|
|
|
2020-10-07 02:09:08 +08:00
|
|
|
if (styleForPseudo) {
|
|
|
|
styleForPseudo.innerHTML = '';
|
2018-08-11 14:57:41 +08:00
|
|
|
}
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
if (insertExtraNode && this.extraNode && node.contains(this.extraNode)) {
|
|
|
|
node.removeChild(this.extraNode);
|
2018-08-20 23:55:35 +08:00
|
|
|
}
|
2020-12-04 19:36:16 +08:00
|
|
|
['transition', 'animation'].forEach(name => {
|
|
|
|
node.removeEventListener(`${name}start`, this.onTransitionStart);
|
|
|
|
node.removeEventListener(`${name}end`, this.onTransitionEnd);
|
|
|
|
});
|
2018-08-11 14:57:41 +08:00
|
|
|
}
|
|
|
|
|
2019-01-09 20:15:37 +08:00
|
|
|
renderWave = ({ csp }: ConfigConsumerProps) => {
|
|
|
|
const { children } = this.props;
|
|
|
|
this.csp = csp;
|
|
|
|
|
2020-11-16 16:46:46 +08:00
|
|
|
if (!React.isValidElement(children)) return children;
|
|
|
|
|
|
|
|
let ref: React.Ref<any> = this.containerRef;
|
|
|
|
if (supportRef(children)) {
|
|
|
|
ref = composeRef((children as any).ref, this.containerRef as any);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cloneElement(children, { ref });
|
2019-01-09 20:15:37 +08:00
|
|
|
};
|
|
|
|
|
2018-08-11 14:57:41 +08:00
|
|
|
render() {
|
2019-01-09 20:15:37 +08:00
|
|
|
return <ConfigConsumer>{this.renderWave}</ConfigConsumer>;
|
2018-08-11 14:57:41 +08:00
|
|
|
}
|
|
|
|
}
|
2022-06-27 16:57:34 +08:00
|
|
|
|
|
|
|
const Wave = forwardRef<InternalWave, WaveProps>((props, ref) => (
|
|
|
|
<InternalWave ref={ref} {...props} />
|
|
|
|
));
|
|
|
|
|
|
|
|
export default Wave;
|