2024-08-21 00:00:02 +08:00
|
|
|
import React 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';
|
2024-08-20 17:34:10 +08:00
|
|
|
import { useEvent } from 'rc-util';
|
2022-09-23 14:31:16 +08:00
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2024-08-09 22:22:45 +08:00
|
|
|
import { useZIndex } from '../_util/hooks/useZIndex';
|
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';
|
2024-08-20 17:34:10 +08:00
|
|
|
import type { FloatButtonGroupProps } from './interface';
|
2022-09-23 14:31:16 +08:00
|
|
|
import useStyle from './style';
|
|
|
|
|
2024-08-21 00:00:02 +08:00
|
|
|
const FloatButtonGroup: React.FC<Readonly<FloatButtonGroupProps>> = (props) => {
|
2022-09-23 14:31:16 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
style,
|
|
|
|
shape = 'circle',
|
|
|
|
type = 'default',
|
2024-08-20 10:16:07 +08:00
|
|
|
placement = 'top',
|
2022-09-23 14:31:16 +08:00
|
|
|
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,
|
2024-08-20 17:34:10 +08:00
|
|
|
onClick: onTriggerButtonClick,
|
2023-08-08 18:05:16 +08:00
|
|
|
...floatButtonProps
|
2022-09-23 14:31:16 +08:00
|
|
|
} = props;
|
|
|
|
|
2024-03-19 17:14:22 +08:00
|
|
|
const { direction, getPrefixCls, floatButtonGroup } =
|
2024-08-21 00:00:02 +08:00
|
|
|
React.useContext<ConfigConsumerProps>(ConfigContext);
|
2024-03-19 17:14:22 +08:00
|
|
|
|
|
|
|
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);
|
2024-08-20 10:16:07 +08:00
|
|
|
|
2022-09-23 14:31:16 +08:00
|
|
|
const groupPrefixCls = `${prefixCls}-group`;
|
|
|
|
|
2024-08-20 10:16:07 +08:00
|
|
|
const isMenuMode = trigger && ['click', 'hover'].includes(trigger);
|
|
|
|
const isValidPlacement = placement && ['top', 'left', 'right', 'bottom'].includes(placement);
|
|
|
|
|
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,
|
2024-08-20 10:16:07 +08:00
|
|
|
[`${groupPrefixCls}-${shape}-shadow`]: !isMenuMode,
|
|
|
|
[`${groupPrefixCls}-${placement}`]: isMenuMode && isValidPlacement, // 只有菜单模式才支持弹出方向
|
2022-09-23 14:31:16 +08:00
|
|
|
});
|
|
|
|
|
2024-08-09 22:22:45 +08:00
|
|
|
// ============================ zIndex ============================
|
|
|
|
const [zIndex] = useZIndex('FloatButton', style?.zIndex as number);
|
|
|
|
|
|
|
|
const mergedStyle: React.CSSProperties = { ...style, zIndex };
|
|
|
|
|
2022-09-23 14:31:16 +08:00
|
|
|
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
|
|
|
|
2024-08-20 17:34:10 +08:00
|
|
|
// ========================== Open ==========================
|
|
|
|
const hoverTrigger = trigger === 'hover';
|
|
|
|
const clickTrigger = trigger === 'click';
|
|
|
|
|
|
|
|
const triggerOpen = useEvent((nextOpen: boolean) => {
|
|
|
|
if (open !== nextOpen) {
|
|
|
|
setOpen(nextOpen);
|
|
|
|
onOpenChange?.(nextOpen);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// ===================== Trigger: Hover =====================
|
2024-08-21 00:00:02 +08:00
|
|
|
const onMouseEnter: React.MouseEventHandler<HTMLDivElement> = () => {
|
2024-08-20 17:34:10 +08:00
|
|
|
if (hoverTrigger) {
|
|
|
|
triggerOpen(true);
|
|
|
|
}
|
2022-12-14 13:51:57 +08:00
|
|
|
};
|
|
|
|
|
2024-08-21 00:00:02 +08:00
|
|
|
const onMouseLeave: React.MouseEventHandler<HTMLDivElement> = () => {
|
2024-08-20 17:34:10 +08:00
|
|
|
if (hoverTrigger) {
|
|
|
|
triggerOpen(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ===================== Trigger: Click =====================
|
|
|
|
const onInternalTriggerButtonClick: FloatButtonGroupProps['onClick'] = (e) => {
|
|
|
|
if (clickTrigger) {
|
|
|
|
triggerOpen(!open);
|
|
|
|
}
|
|
|
|
onTriggerButtonClick?.(e);
|
2022-12-14 13:51:57 +08:00
|
|
|
};
|
|
|
|
|
2024-08-20 17:34:10 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (clickTrigger) {
|
|
|
|
const onDocClick = (e: MouseEvent) => {
|
|
|
|
// Skip if click on the group
|
|
|
|
if (floatButtonGroupRef.current?.contains(e.target as Node)) {
|
|
|
|
return;
|
2022-12-14 13:51:57 +08:00
|
|
|
}
|
2024-08-20 17:34:10 +08:00
|
|
|
triggerOpen(false);
|
2022-12-14 13:51:57 +08:00
|
|
|
};
|
2024-08-21 00:00:02 +08:00
|
|
|
document.addEventListener('click', onDocClick, { capture: true });
|
|
|
|
return () => document.removeEventListener('click', onDocClick, { capture: true });
|
2022-12-14 13:51:57 +08:00
|
|
|
}
|
2024-08-20 17:34:10 +08:00
|
|
|
}, [clickTrigger]);
|
2022-09-23 14:31:16 +08:00
|
|
|
|
2024-08-20 17:34:10 +08:00
|
|
|
// ======================== Warning =========================
|
2023-06-12 17:20:55 +08:00
|
|
|
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`',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-20 17:34:10 +08:00
|
|
|
// ========================= Render =========================
|
2023-11-15 11:46:17 +08:00
|
|
|
return wrapCSSVar(
|
2022-09-23 14:31:16 +08:00
|
|
|
<FloatButtonGroupProvider value={shape}>
|
2024-08-20 17:34:10 +08:00
|
|
|
<div
|
|
|
|
ref={floatButtonGroupRef}
|
|
|
|
className={groupCls}
|
2024-08-21 00:00:02 +08:00
|
|
|
style={mergedStyle}
|
2024-08-20 17:34:10 +08:00
|
|
|
// Hover trigger
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
>
|
2024-08-20 10:16:07 +08:00
|
|
|
{isMenuMode ? (
|
2022-09-23 14:31:16 +08:00
|
|
|
<>
|
|
|
|
<CSSMotion visible={open} motionName={`${groupPrefixCls}-wrap`}>
|
|
|
|
{({ className: motionClassName }) => (
|
|
|
|
<div className={classNames(motionClassName, wrapperCls)}>{children}</div>
|
|
|
|
)}
|
|
|
|
</CSSMotion>
|
|
|
|
<FloatButton
|
|
|
|
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']}
|
2024-08-14 09:17:56 +08:00
|
|
|
className={`${groupPrefixCls}-trigger`}
|
2024-08-20 17:34:10 +08:00
|
|
|
onClick={onInternalTriggerButtonClick}
|
2023-08-08 18:05:16 +08:00
|
|
|
{...floatButtonProps}
|
2022-09-23 14:31:16 +08:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
children
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</FloatButtonGroupProvider>,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-08-21 00:00:02 +08:00
|
|
|
export default FloatButtonGroup;
|