ant-design/components/float-button/FloatButtonGroup.tsx
MadCcc 1ac0bcaa60
Some checks are pending
Publish Any Commit / build (push) Waiting to run
✅ test v6 / lint (push) Waiting to run
✅ test v6 / test-react-legacy (18, 1/2) (push) Waiting to run
✅ test v6 / test-react-legacy (18, 2/2) (push) Waiting to run
✅ test v6 / test-node (push) Waiting to run
✅ test v6 / test-react-latest (dom, 1/2) (push) Waiting to run
✅ test v6 / test-react-latest (dom, 2/2) (push) Waiting to run
✅ test v6 / test-react-latest-dist (dist, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist, 2/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 1/2) (push) Blocked by required conditions
✅ test v6 / test-react-latest-dist (dist-min, 2/2) (push) Blocked by required conditions
✅ test v6 / test-coverage (push) Blocked by required conditions
✅ test v6 / build (push) Waiting to run
✅ test v6 / test lib/es module (es, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (es, 2/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 1/2) (push) Waiting to run
✅ test v6 / test lib/es module (lib, 2/2) (push) Waiting to run
👁️ Visual Regression Persist Start / test image (push) Waiting to run
feat: use cssVar by default (#52671)
* feat: use cssVar by default

* chore: update deps

* chore: update snapshot

* chore: update snapshot

* chore: update snapshot

* chore: test

* chore: update

* chore: update scripts

* chore: UPDATE TEST

* feat: add root

* chore: update snapshot

* chore: fix test case

* chore: fix cycle deps

* feat: rm legacy css variables configuration

* chore: update test case

* chore: update

* chore: fix test

* chore: update overrides

* chore: bump cssinjs

* chore: add test case
2025-02-24 15:35:29 +08:00

166 lines
5.3 KiB
TypeScript

import React from 'react';
import CloseOutlined from '@ant-design/icons/CloseOutlined';
import FileTextOutlined from '@ant-design/icons/FileTextOutlined';
import CSSMotion from '@rc-component/motion';
import useEvent from '@rc-component/util/lib/hooks/useEvent';
import useMergedState from '@rc-component/util/lib/hooks/useMergedState';
import classNames from 'classnames';
import { useZIndex } from '../_util/hooks/useZIndex';
import { devUseWarning } from '../_util/warning';
import { useComponentConfig } from '../config-provider/context';
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
import { FloatButtonGroupProvider } from './context';
import FloatButton, { floatButtonPrefixCls } from './FloatButton';
import type { FloatButtonGroupProps } from './interface';
import useStyle from './style';
const FloatButtonGroup: React.FC<Readonly<FloatButtonGroupProps>> = (props) => {
const {
prefixCls: customizePrefixCls,
className,
style,
shape = 'circle',
type = 'default',
placement = 'top',
icon = <FileTextOutlined />,
closeIcon,
description,
trigger,
children,
onOpenChange,
open: customOpen,
onClick: onTriggerButtonClick,
...floatButtonProps
} = props;
const {
direction,
getPrefixCls,
closeIcon: contextCloseIcon,
} = useComponentConfig('floatButtonGroup');
const mergedCloseIcon = closeIcon ?? contextCloseIcon ?? <CloseOutlined />;
const prefixCls = getPrefixCls(floatButtonPrefixCls, customizePrefixCls);
const rootCls = useCSSVarCls(prefixCls);
const [hashId, cssVarCls] = useStyle(prefixCls, rootCls);
const groupPrefixCls = `${prefixCls}-group`;
const isMenuMode = trigger && ['click', 'hover'].includes(trigger);
const isValidPlacement = placement && ['top', 'left', 'right', 'bottom'].includes(placement);
const groupCls = classNames(groupPrefixCls, hashId, cssVarCls, rootCls, className, {
[`${groupPrefixCls}-rtl`]: direction === 'rtl',
[`${groupPrefixCls}-${shape}`]: shape,
[`${groupPrefixCls}-${shape}-shadow`]: !isMenuMode,
[`${groupPrefixCls}-${placement}`]: isMenuMode && isValidPlacement, // 只有菜单模式才支持弹出方向
});
// ============================ zIndex ============================
const [zIndex] = useZIndex('FloatButton', style?.zIndex as number);
const mergedStyle: React.CSSProperties = { ...style, zIndex };
const wrapperCls = classNames(hashId, `${groupPrefixCls}-wrap`);
const [open, setOpen] = useMergedState(false, { value: customOpen });
const floatButtonGroupRef = React.useRef<HTMLDivElement>(null);
// ========================== Open ==========================
const hoverTrigger = trigger === 'hover';
const clickTrigger = trigger === 'click';
const triggerOpen = useEvent((nextOpen: boolean) => {
if (open !== nextOpen) {
setOpen(nextOpen);
onOpenChange?.(nextOpen);
}
});
// ===================== Trigger: Hover =====================
const onMouseEnter: React.MouseEventHandler<HTMLDivElement> = () => {
if (hoverTrigger) {
triggerOpen(true);
}
};
const onMouseLeave: React.MouseEventHandler<HTMLDivElement> = () => {
if (hoverTrigger) {
triggerOpen(false);
}
};
// ===================== Trigger: Click =====================
const onInternalTriggerButtonClick: FloatButtonGroupProps['onClick'] = (e) => {
if (clickTrigger) {
triggerOpen(!open);
}
onTriggerButtonClick?.(e);
};
React.useEffect(() => {
if (clickTrigger) {
const onDocClick = (e: MouseEvent) => {
// Skip if click on the group
if (floatButtonGroupRef.current?.contains(e.target as Node)) {
return;
}
triggerOpen(false);
};
document.addEventListener('click', onDocClick, { capture: true });
return () => document.removeEventListener('click', onDocClick, { capture: true });
}
}, [clickTrigger]);
// ======================== Warning =========================
if (process.env.NODE_ENV !== 'production') {
const warning = devUseWarning('FloatButton.Group');
warning(
!('open' in props) || !!trigger,
'usage',
'`open` need to be used together with `trigger`',
);
}
// ========================= Render =========================
return (
<FloatButtonGroupProvider value={shape}>
<div
ref={floatButtonGroupRef}
className={groupCls}
style={mergedStyle}
// Hover trigger
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
{isMenuMode ? (
<>
<CSSMotion visible={open} motionName={`${groupPrefixCls}-wrap`}>
{({ className: motionClassName }) => (
<div className={classNames(motionClassName, wrapperCls)}>{children}</div>
)}
</CSSMotion>
<FloatButton
type={type}
icon={open ? mergedCloseIcon : icon}
description={description}
aria-label={props['aria-label']}
className={`${groupPrefixCls}-trigger`}
onClick={onInternalTriggerButtonClick}
{...floatButtonProps}
/>
</>
) : (
children
)}
</div>
</FloatButtonGroupProvider>
);
};
export default FloatButtonGroup;