2023-11-30 11:06:41 +08:00
|
|
|
import React, { useContext, useMemo } from 'react';
|
2022-10-24 23:15:39 +08:00
|
|
|
import classNames from 'classnames';
|
2023-03-20 10:28:20 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
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';
|
2023-03-20 10:28:20 +08:00
|
|
|
import Badge from '../badge';
|
2022-10-24 23:15:39 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2024-03-24 10:56:28 +08:00
|
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
2022-10-24 23:15:39 +08:00
|
|
|
import Tooltip from '../tooltip';
|
2024-04-09 16:49:47 +08:00
|
|
|
import type BackTop from './BackTop';
|
2023-03-21 14:11:39 +08:00
|
|
|
import FloatButtonGroupContext from './context';
|
2023-09-11 17:28:04 +08:00
|
|
|
import Content from './FloatButtonContent';
|
2024-04-09 16:49:47 +08:00
|
|
|
import type FloatButtonGroup from './FloatButtonGroup';
|
2022-10-24 23:15:39 +08:00
|
|
|
import type {
|
|
|
|
FloatButtonContentProps,
|
2023-11-30 11:06:41 +08:00
|
|
|
FloatButtonElement,
|
2022-10-24 23:15:39 +08:00
|
|
|
FloatButtonProps,
|
|
|
|
FloatButtonShape,
|
|
|
|
} from './interface';
|
2024-04-09 16:49:47 +08:00
|
|
|
import type PurePanel from './PurePanel';
|
2023-03-01 19:00:17 +08:00
|
|
|
import useStyle from './style';
|
2022-10-24 23:15:39 +08:00
|
|
|
|
|
|
|
export const floatButtonPrefixCls = 'float-btn';
|
|
|
|
|
2024-04-09 16:49:47 +08:00
|
|
|
const InternalFloatButton = React.forwardRef<FloatButtonElement, FloatButtonProps>((props, ref) => {
|
2022-10-24 23:15:39 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2024-08-09 22:22:45 +08:00
|
|
|
style,
|
2022-10-24 23:15:39 +08:00
|
|
|
type = 'default',
|
|
|
|
shape = 'circle',
|
|
|
|
icon,
|
|
|
|
description,
|
|
|
|
tooltip,
|
2024-09-17 17:15:26 +08:00
|
|
|
htmlType = 'button',
|
2023-03-20 10:28:20 +08:00
|
|
|
badge = {},
|
2022-10-24 23:15:39 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
const { getPrefixCls, direction } = useContext<ConfigConsumerProps>(ConfigContext);
|
|
|
|
const groupShape = useContext<FloatButtonShape | undefined>(FloatButtonGroupContext);
|
|
|
|
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-10-24 23:15:39 +08:00
|
|
|
|
2024-03-24 10:56:28 +08:00
|
|
|
const mergedShape = groupShape || shape;
|
2022-10-24 23:15:39 +08:00
|
|
|
|
|
|
|
const classString = classNames(
|
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2023-11-15 11:46:17 +08:00
|
|
|
rootCls,
|
2022-10-24 23:15:39 +08:00
|
|
|
prefixCls,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-10-24 23:15:39 +08:00
|
|
|
`${prefixCls}-${type}`,
|
2024-03-24 10:56:28 +08:00
|
|
|
`${prefixCls}-${mergedShape}`,
|
2022-10-24 23:15:39 +08:00
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2024-08-09 22:22:45 +08:00
|
|
|
// ============================ zIndex ============================
|
|
|
|
const [zIndex] = useZIndex('FloatButton', style?.zIndex as number);
|
|
|
|
|
|
|
|
const mergedStyle: React.CSSProperties = { ...style, zIndex };
|
|
|
|
|
2023-03-20 10:28:20 +08:00
|
|
|
// 虽然在 ts 中已经 omit 过了,但是为了防止多余的属性被透传进来,这里再 omit 一遍,以防万一
|
2024-08-09 22:22:45 +08:00
|
|
|
const badgeProps = omit(badge, ['title', 'children', 'status', 'text'] as any[]);
|
2023-03-20 10:28:20 +08:00
|
|
|
|
2022-10-24 23:15:39 +08:00
|
|
|
const contentProps = useMemo<FloatButtonContentProps>(
|
|
|
|
() => ({ prefixCls, description, icon, type }),
|
|
|
|
[prefixCls, description, icon, type],
|
|
|
|
);
|
|
|
|
|
2023-08-08 18:05:16 +08:00
|
|
|
let buttonNode = (
|
|
|
|
<div className={`${prefixCls}-body`}>
|
|
|
|
<Content {...contentProps} />
|
|
|
|
</div>
|
2022-10-24 23:15:39 +08:00
|
|
|
);
|
|
|
|
|
2023-08-08 18:05:16 +08:00
|
|
|
if ('badge' in props) {
|
|
|
|
buttonNode = <Badge {...badgeProps}>{buttonNode}</Badge>;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('tooltip' in props) {
|
|
|
|
buttonNode = (
|
|
|
|
<Tooltip title={tooltip} placement={direction === 'rtl' ? 'right' : 'left'}>
|
|
|
|
{buttonNode}
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:15:39 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('FloatButton');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2022-10-24 23:15:39 +08:00
|
|
|
warning(
|
|
|
|
!(shape === 'circle' && description),
|
2023-09-11 17:28:04 +08:00
|
|
|
'usage',
|
2022-10-24 23:15:39 +08:00
|
|
|
'supported only when `shape` is `square`. Due to narrow space for text, short sentence is recommended.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-15 11:46:17 +08:00
|
|
|
return wrapCSSVar(
|
2022-10-24 23:15:39 +08:00
|
|
|
props.href ? (
|
2024-08-09 22:22:45 +08:00
|
|
|
<a ref={ref} {...restProps} className={classString} style={mergedStyle}>
|
2022-10-24 23:15:39 +08:00
|
|
|
{buttonNode}
|
|
|
|
</a>
|
|
|
|
) : (
|
2024-09-17 17:15:26 +08:00
|
|
|
<button ref={ref} {...restProps} className={classString} style={mergedStyle} type={htmlType}>
|
2022-10-24 23:15:39 +08:00
|
|
|
{buttonNode}
|
|
|
|
</button>
|
|
|
|
),
|
|
|
|
);
|
2024-04-09 16:49:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
type CompoundedComponent = typeof InternalFloatButton & {
|
|
|
|
Group: typeof FloatButtonGroup;
|
|
|
|
BackTop: typeof BackTop;
|
|
|
|
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
|
|
|
|
};
|
|
|
|
|
|
|
|
const FloatButton = InternalFloatButton as CompoundedComponent;
|
2022-10-24 23:15:39 +08:00
|
|
|
|
2023-07-17 23:43:32 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-10-20 17:19:17 +08:00
|
|
|
FloatButton.displayName = 'FloatButton';
|
2023-07-17 23:43:32 +08:00
|
|
|
}
|
|
|
|
|
2023-10-20 17:19:17 +08:00
|
|
|
export default FloatButton;
|