2023-10-20 17:19:17 +08:00
|
|
|
import React, { memo, useCallback, useContext, useEffect } from 'react';
|
2022-09-23 14:31:16 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
|
|
|
import FileTextOutlined from '@ant-design/icons/FileTextOutlined';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import CSSMotion from 'rc-motion';
|
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
import { devUseWarning } from '../_util/warning';
|
2022-09-23 14:31:16 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2024-03-19 17:14:22 +08:00
|
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
2022-09-23 14:31:16 +08:00
|
|
|
import { FloatButtonGroupProvider } from './context';
|
2023-09-11 17:28:04 +08:00
|
|
|
import FloatButton, { floatButtonPrefixCls } from './FloatButton';
|
2023-10-20 17:19:17 +08:00
|
|
|
import type { FloatButtonGroupProps, FloatButtonRef } from './interface';
|
2022-09-23 14:31:16 +08:00
|
|
|
import useStyle from './style';
|
|
|
|
|
2022-11-18 22:20:18 +08:00
|
|
|
const FloatButtonGroup: React.FC<FloatButtonGroupProps> = (props) => {
|
2022-09-23 14:31:16 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
style,
|
|
|
|
shape = 'circle',
|
|
|
|
type = 'default',
|
|
|
|
icon = <FileTextOutlined />,
|
2024-03-19 17:14:22 +08:00
|
|
|
closeIcon,
|
2022-09-23 14:31:16 +08:00
|
|
|
description,
|
|
|
|
trigger,
|
|
|
|
children,
|
|
|
|
onOpenChange,
|
2023-08-08 18:05:16 +08:00
|
|
|
open: customOpen,
|
|
|
|
...floatButtonProps
|
2022-09-23 14:31:16 +08:00
|
|
|
} = props;
|
|
|
|
|
2024-03-19 17:14:22 +08:00
|
|
|
const { direction, getPrefixCls, floatButtonGroup } =
|
|
|
|
useContext<ConfigConsumerProps>(ConfigContext);
|
|
|
|
|
|
|
|
const mergedCloseIcon = closeIcon ?? floatButtonGroup?.closeIcon ?? <CloseOutlined />;
|
|
|
|
|
2022-09-23 14:31:16 +08:00
|
|
|
const prefixCls = getPrefixCls(floatButtonPrefixCls, customizePrefixCls);
|
2023-11-15 11:46:17 +08:00
|
|
|
const rootCls = useCSSVarCls(prefixCls);
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
|
2022-09-23 14:31:16 +08:00
|
|
|
const groupPrefixCls = `${prefixCls}-group`;
|
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const groupCls = classNames(groupPrefixCls, hashId, cssVarCls, rootCls, className, {
|
2022-09-23 14:31:16 +08:00
|
|
|
[`${groupPrefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${groupPrefixCls}-${shape}`]: shape,
|
|
|
|
[`${groupPrefixCls}-${shape}-shadow`]: !trigger,
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrapperCls = classNames(hashId, `${groupPrefixCls}-wrap`);
|
|
|
|
|
2023-08-08 18:05:16 +08:00
|
|
|
const [open, setOpen] = useMergedState(false, { value: customOpen });
|
2022-09-23 14:31:16 +08:00
|
|
|
|
2023-10-20 17:19:17 +08:00
|
|
|
const floatButtonGroupRef = React.useRef<HTMLDivElement>(null);
|
2022-09-23 14:31:16 +08:00
|
|
|
|
2023-10-20 17:19:17 +08:00
|
|
|
const floatButtonRef = React.useRef<FloatButtonRef['nativeElement']>(null);
|
|
|
|
|
|
|
|
const hoverAction = React.useMemo<React.DOMAttributes<HTMLDivElement>>(() => {
|
2022-12-14 13:51:57 +08:00
|
|
|
const hoverTypeAction = {
|
2022-09-23 14:31:16 +08:00
|
|
|
onMouseEnter() {
|
|
|
|
setOpen(true);
|
|
|
|
onOpenChange?.(true);
|
|
|
|
},
|
|
|
|
onMouseLeave() {
|
|
|
|
setOpen(false);
|
|
|
|
onOpenChange?.(false);
|
|
|
|
},
|
|
|
|
};
|
2022-12-14 13:51:57 +08:00
|
|
|
return trigger === 'hover' ? hoverTypeAction : {};
|
|
|
|
}, [trigger]);
|
|
|
|
|
|
|
|
const handleOpenChange = () => {
|
|
|
|
setOpen((prevState) => {
|
|
|
|
onOpenChange?.(!prevState);
|
|
|
|
return !prevState;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onClick = useCallback(
|
|
|
|
(e: MouseEvent) => {
|
2022-12-20 00:33:25 +08:00
|
|
|
if (floatButtonGroupRef.current?.contains(e.target as Node)) {
|
|
|
|
if (floatButtonRef.current?.contains(e.target as Node)) {
|
2022-12-14 13:51:57 +08:00
|
|
|
handleOpenChange();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setOpen(false);
|
|
|
|
onOpenChange?.(false);
|
|
|
|
},
|
|
|
|
[trigger],
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (trigger === 'click') {
|
|
|
|
document.addEventListener('click', onClick);
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('click', onClick);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, [trigger]);
|
2022-09-23 14:31:16 +08:00
|
|
|
|
2023-06-12 17:20:55 +08:00
|
|
|
// =================== Warning =====================
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('FloatButton.Group');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2023-06-12 17:20:55 +08:00
|
|
|
warning(
|
2023-08-08 18:05:16 +08:00
|
|
|
!('open' in props) || !!trigger,
|
2023-09-11 17:28:04 +08:00
|
|
|
'usage',
|
2023-06-12 17:20:55 +08:00
|
|
|
'`open` need to be used together with `trigger`',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-15 11:46:17 +08:00
|
|
|
return wrapCSSVar(
|
2022-09-23 14:31:16 +08:00
|
|
|
<FloatButtonGroupProvider value={shape}>
|
2022-12-14 13:51:57 +08:00
|
|
|
<div ref={floatButtonGroupRef} className={groupCls} style={style} {...hoverAction}>
|
2022-09-23 14:31:16 +08:00
|
|
|
{trigger && ['click', 'hover'].includes(trigger) ? (
|
|
|
|
<>
|
|
|
|
<CSSMotion visible={open} motionName={`${groupPrefixCls}-wrap`}>
|
|
|
|
{({ className: motionClassName }) => (
|
|
|
|
<div className={classNames(motionClassName, wrapperCls)}>{children}</div>
|
|
|
|
)}
|
|
|
|
</CSSMotion>
|
|
|
|
<FloatButton
|
2022-12-14 13:51:57 +08:00
|
|
|
ref={floatButtonRef}
|
2022-09-23 14:31:16 +08:00
|
|
|
type={type}
|
2024-03-19 17:14:22 +08:00
|
|
|
icon={open ? mergedCloseIcon : icon}
|
2022-09-23 14:31:16 +08:00
|
|
|
description={description}
|
2023-07-24 15:38:49 +08:00
|
|
|
aria-label={props['aria-label']}
|
2023-08-08 18:05:16 +08:00
|
|
|
{...floatButtonProps}
|
2022-09-23 14:31:16 +08:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
children
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</FloatButtonGroupProvider>,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default memo(FloatButtonGroup);
|