antd
Version:
An enterprise-class UI design language and React components implementation
138 lines • 5.21 kB
JavaScript
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import classNames from 'classnames';
import { useNotification as useRcNotification } from 'rc-notification';
import * as React from 'react';
import warning from '../_util/warning';
import { ConfigContext } from '../config-provider';
import { PureContent, getCloseIcon } from './PurePanel';
import useStyle from './style';
import { getMotion, getPlacementStyle } from './util';
const DEFAULT_OFFSET = 24;
const DEFAULT_DURATION = 4.5;
const Holder = /*#__PURE__*/React.forwardRef((props, ref) => {
const {
top,
bottom,
prefixCls: staticPrefixCls,
getContainer: staticGetContainer,
maxCount,
rtl,
onAllRemoved
} = props;
const {
getPrefixCls,
getPopupContainer
} = React.useContext(ConfigContext);
const prefixCls = staticPrefixCls || getPrefixCls('notification');
// =============================== Style ===============================
const getStyle = placement => getPlacementStyle(placement, top !== null && top !== void 0 ? top : DEFAULT_OFFSET, bottom !== null && bottom !== void 0 ? bottom : DEFAULT_OFFSET);
// Style
const [, hashId] = useStyle(prefixCls);
const getClassName = () => classNames(hashId, {
[`${prefixCls}-rtl`]: rtl
});
// ============================== Motion ===============================
const getNotificationMotion = () => getMotion(prefixCls);
// ============================== Origin ===============================
const [api, holder] = useRcNotification({
prefixCls,
style: getStyle,
className: getClassName,
motion: getNotificationMotion,
closable: true,
closeIcon: getCloseIcon(prefixCls),
duration: DEFAULT_DURATION,
getContainer: () => (staticGetContainer === null || staticGetContainer === void 0 ? void 0 : staticGetContainer()) || (getPopupContainer === null || getPopupContainer === void 0 ? void 0 : getPopupContainer()) || document.body,
maxCount,
onAllRemoved
});
// ================================ Ref ================================
React.useImperativeHandle(ref, () => Object.assign(Object.assign({}, api), {
prefixCls,
hashId
}));
return holder;
});
// ==============================================================================
// == Hook ==
// ==============================================================================
export function useInternalNotification(notificationConfig) {
const holderRef = React.useRef(null);
// ================================ API ================================
const wrapAPI = React.useMemo(() => {
// Wrap with notification content
// >>> Open
const open = config => {
if (!holderRef.current) {
process.env.NODE_ENV !== "production" ? warning(false, 'Notification', 'You are calling notice in render which will break in React 18 concurrent mode. Please trigger in effect instead.') : void 0;
return;
}
const {
open: originOpen,
prefixCls,
hashId
} = holderRef.current;
const noticePrefixCls = `${prefixCls}-notice`;
const {
message,
description,
icon,
type,
btn,
className,
role = 'alert'
} = config,
restConfig = __rest(config, ["message", "description", "icon", "type", "btn", "className", "role"]);
return originOpen(Object.assign(Object.assign({
placement: 'topRight'
}, restConfig), {
content: /*#__PURE__*/React.createElement(PureContent, {
prefixCls: noticePrefixCls,
icon: icon,
type: type,
message: message,
description: description,
btn: btn,
role: role
}),
className: classNames(type && `${noticePrefixCls}-${type}`, hashId, className)
}));
};
// >>> destroy
const destroy = key => {
var _a, _b;
if (key !== undefined) {
(_a = holderRef.current) === null || _a === void 0 ? void 0 : _a.close(key);
} else {
(_b = holderRef.current) === null || _b === void 0 ? void 0 : _b.destroy();
}
};
const clone = {
open,
destroy
};
const keys = ['success', 'info', 'warning', 'error'];
keys.forEach(type => {
clone[type] = config => open(Object.assign(Object.assign({}, config), {
type
}));
});
return clone;
}, []);
// ============================== Return ===============================
return [wrapAPI, /*#__PURE__*/React.createElement(Holder, Object.assign({
key: "notification-holder"
}, notificationConfig, {
ref: holderRef
}))];
}
export default function useNotification(notificationConfig) {
return useInternalNotification(notificationConfig);
}