1
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
import * as React from 'react';
|
||||
import type { NoticeConfig } from './interface';
|
||||
export interface NoticeProps extends Omit<NoticeConfig, 'onClose'> {
|
||||
prefixCls: string;
|
||||
className?: string;
|
||||
style?: React.CSSProperties;
|
||||
eventKey: React.Key;
|
||||
onClick?: React.MouseEventHandler<HTMLDivElement>;
|
||||
onNoticeClose?: (key: React.Key) => void;
|
||||
hovering?: boolean;
|
||||
}
|
||||
declare const Notify: React.ForwardRefExoticComponent<NoticeProps & {
|
||||
times?: number;
|
||||
} & React.RefAttributes<HTMLDivElement>>;
|
||||
export default Notify;
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
var _clsx = require("clsx");
|
||||
var _KeyCode = _interopRequireDefault(require("@rc-component/util/lib/KeyCode"));
|
||||
var React = _interopRequireWildcard(require("react"));
|
||||
var _pickAttrs = _interopRequireDefault(require("@rc-component/util/lib/pickAttrs"));
|
||||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
||||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
||||
const Notify = /*#__PURE__*/React.forwardRef((props, ref) => {
|
||||
const {
|
||||
prefixCls,
|
||||
style,
|
||||
className,
|
||||
duration = 4.5,
|
||||
showProgress,
|
||||
pauseOnHover = true,
|
||||
eventKey,
|
||||
content,
|
||||
closable,
|
||||
props: divProps,
|
||||
onClick,
|
||||
onNoticeClose,
|
||||
times,
|
||||
hovering: forcedHovering
|
||||
} = props;
|
||||
const [hovering, setHovering] = React.useState(false);
|
||||
const [percent, setPercent] = React.useState(0);
|
||||
const [spentTime, setSpentTime] = React.useState(0);
|
||||
const mergedHovering = forcedHovering || hovering;
|
||||
const mergedDuration = typeof duration === 'number' ? duration : 0;
|
||||
const mergedShowProgress = mergedDuration > 0 && showProgress;
|
||||
|
||||
// ======================== Close =========================
|
||||
const onInternalClose = () => {
|
||||
onNoticeClose(eventKey);
|
||||
};
|
||||
const onCloseKeyDown = e => {
|
||||
if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === _KeyCode.default.ENTER) {
|
||||
onInternalClose();
|
||||
}
|
||||
};
|
||||
|
||||
// ======================== Effect ========================
|
||||
React.useEffect(() => {
|
||||
if (!mergedHovering && mergedDuration > 0) {
|
||||
const start = Date.now() - spentTime;
|
||||
const timeout = setTimeout(() => {
|
||||
onInternalClose();
|
||||
}, mergedDuration * 1000 - spentTime);
|
||||
return () => {
|
||||
if (pauseOnHover) {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
setSpentTime(Date.now() - start);
|
||||
};
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [mergedDuration, mergedHovering, times]);
|
||||
React.useEffect(() => {
|
||||
if (!mergedHovering && mergedShowProgress && (pauseOnHover || spentTime === 0)) {
|
||||
const start = performance.now();
|
||||
let animationFrame;
|
||||
const calculate = () => {
|
||||
cancelAnimationFrame(animationFrame);
|
||||
animationFrame = requestAnimationFrame(timestamp => {
|
||||
const runtime = timestamp + spentTime - start;
|
||||
const progress = Math.min(runtime / (mergedDuration * 1000), 1);
|
||||
setPercent(progress * 100);
|
||||
if (progress < 1) {
|
||||
calculate();
|
||||
}
|
||||
});
|
||||
};
|
||||
calculate();
|
||||
return () => {
|
||||
if (pauseOnHover) {
|
||||
cancelAnimationFrame(animationFrame);
|
||||
}
|
||||
};
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [mergedDuration, spentTime, mergedHovering, mergedShowProgress, times]);
|
||||
|
||||
// ======================== Closable ========================
|
||||
const closableObj = React.useMemo(() => {
|
||||
if (typeof closable === 'object' && closable !== null) {
|
||||
return closable;
|
||||
}
|
||||
return {};
|
||||
}, [closable]);
|
||||
const ariaProps = (0, _pickAttrs.default)(closableObj, true);
|
||||
|
||||
// ======================== Progress ========================
|
||||
const validPercent = 100 - (!percent || percent < 0 ? 0 : percent > 100 ? 100 : percent);
|
||||
|
||||
// ======================== Render ========================
|
||||
const noticePrefixCls = `${prefixCls}-notice`;
|
||||
return /*#__PURE__*/React.createElement("div", _extends({}, divProps, {
|
||||
ref: ref,
|
||||
className: (0, _clsx.clsx)(noticePrefixCls, className, {
|
||||
[`${noticePrefixCls}-closable`]: closable
|
||||
}),
|
||||
style: style,
|
||||
onMouseEnter: e => {
|
||||
setHovering(true);
|
||||
divProps?.onMouseEnter?.(e);
|
||||
},
|
||||
onMouseLeave: e => {
|
||||
setHovering(false);
|
||||
divProps?.onMouseLeave?.(e);
|
||||
},
|
||||
onClick: onClick
|
||||
}), /*#__PURE__*/React.createElement("div", {
|
||||
className: `${noticePrefixCls}-content`
|
||||
}, content), closable && /*#__PURE__*/React.createElement("button", _extends({
|
||||
className: `${noticePrefixCls}-close`,
|
||||
onKeyDown: onCloseKeyDown,
|
||||
"aria-label": "Close"
|
||||
}, ariaProps, {
|
||||
onClick: e => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
onInternalClose();
|
||||
}
|
||||
}), closableObj.closeIcon ?? 'x'), mergedShowProgress && /*#__PURE__*/React.createElement("progress", {
|
||||
className: `${noticePrefixCls}-progress`,
|
||||
max: "100",
|
||||
value: validPercent
|
||||
}, validPercent + '%'));
|
||||
});
|
||||
var _default = exports.default = Notify;
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
import type { CSSProperties, FC } from 'react';
|
||||
import React from 'react';
|
||||
import type { CSSMotionProps } from '@rc-component/motion';
|
||||
import type { OpenConfig, Placement, StackConfig } from './interface';
|
||||
export interface NoticeListProps {
|
||||
configList?: OpenConfig[];
|
||||
placement?: Placement;
|
||||
prefixCls?: string;
|
||||
motion?: CSSMotionProps | ((placement: Placement) => CSSMotionProps);
|
||||
stack?: StackConfig;
|
||||
onAllNoticeRemoved?: (placement: Placement) => void;
|
||||
onNoticeClose?: (key: React.Key) => void;
|
||||
className?: string;
|
||||
style?: CSSProperties;
|
||||
}
|
||||
declare const NoticeList: FC<NoticeListProps>;
|
||||
export default NoticeList;
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
var _react = _interopRequireWildcard(require("react"));
|
||||
var _clsx = require("clsx");
|
||||
var _motion = require("@rc-component/motion");
|
||||
var _Notice = _interopRequireDefault(require("./Notice"));
|
||||
var _NotificationProvider = require("./NotificationProvider");
|
||||
var _useStack = _interopRequireDefault(require("./hooks/useStack"));
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
||||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
||||
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
||||
const NoticeList = props => {
|
||||
const {
|
||||
configList,
|
||||
placement,
|
||||
prefixCls,
|
||||
className,
|
||||
style,
|
||||
motion,
|
||||
onAllNoticeRemoved,
|
||||
onNoticeClose,
|
||||
stack: stackConfig
|
||||
} = props;
|
||||
const {
|
||||
classNames: ctxCls
|
||||
} = (0, _react.useContext)(_NotificationProvider.NotificationContext);
|
||||
const dictRef = (0, _react.useRef)({});
|
||||
const [latestNotice, setLatestNotice] = (0, _react.useState)(null);
|
||||
const [hoverKeys, setHoverKeys] = (0, _react.useState)([]);
|
||||
const keys = configList.map(config => ({
|
||||
config,
|
||||
key: String(config.key)
|
||||
}));
|
||||
const [stack, {
|
||||
offset,
|
||||
threshold,
|
||||
gap
|
||||
}] = (0, _useStack.default)(stackConfig);
|
||||
const expanded = stack && (hoverKeys.length > 0 || keys.length <= threshold);
|
||||
const placementMotion = typeof motion === 'function' ? motion(placement) : motion;
|
||||
|
||||
// Clean hover key
|
||||
(0, _react.useEffect)(() => {
|
||||
if (stack && hoverKeys.length > 1) {
|
||||
setHoverKeys(prev => prev.filter(key => keys.some(({
|
||||
key: dataKey
|
||||
}) => key === dataKey)));
|
||||
}
|
||||
}, [hoverKeys, keys, stack]);
|
||||
|
||||
// Force update latest notice
|
||||
(0, _react.useEffect)(() => {
|
||||
if (stack && dictRef.current[keys[keys.length - 1]?.key]) {
|
||||
setLatestNotice(dictRef.current[keys[keys.length - 1]?.key]);
|
||||
}
|
||||
}, [keys, stack]);
|
||||
return /*#__PURE__*/_react.default.createElement(_motion.CSSMotionList, _extends({
|
||||
key: placement,
|
||||
className: (0, _clsx.clsx)(prefixCls, `${prefixCls}-${placement}`, ctxCls?.list, className, {
|
||||
[`${prefixCls}-stack`]: !!stack,
|
||||
[`${prefixCls}-stack-expanded`]: expanded
|
||||
}),
|
||||
style: style,
|
||||
keys: keys,
|
||||
motionAppear: true
|
||||
}, placementMotion, {
|
||||
onAllRemoved: () => {
|
||||
onAllNoticeRemoved(placement);
|
||||
}
|
||||
}), ({
|
||||
config,
|
||||
className: motionClassName,
|
||||
style: motionStyle,
|
||||
index: motionIndex
|
||||
}, nodeRef) => {
|
||||
const {
|
||||
key,
|
||||
times
|
||||
} = config;
|
||||
const strKey = String(key);
|
||||
const {
|
||||
className: configClassName,
|
||||
style: configStyle,
|
||||
classNames: configClassNames,
|
||||
styles: configStyles,
|
||||
...restConfig
|
||||
} = config;
|
||||
const dataIndex = keys.findIndex(item => item.key === strKey);
|
||||
|
||||
// If dataIndex is -1, that means this notice has been removed in data, but still in dom
|
||||
// Should minus (motionIndex - 1) to get the correct index because keys.length is not the same as dom length
|
||||
const stackStyle = {};
|
||||
if (stack) {
|
||||
const index = keys.length - 1 - (dataIndex > -1 ? dataIndex : motionIndex - 1);
|
||||
const transformX = placement === 'top' || placement === 'bottom' ? '-50%' : '0';
|
||||
if (index > 0) {
|
||||
stackStyle.height = expanded ? dictRef.current[strKey]?.offsetHeight : latestNotice?.offsetHeight;
|
||||
|
||||
// Transform
|
||||
let verticalOffset = 0;
|
||||
for (let i = 0; i < index; i++) {
|
||||
verticalOffset += dictRef.current[keys[keys.length - 1 - i].key]?.offsetHeight + gap;
|
||||
}
|
||||
const transformY = (expanded ? verticalOffset : index * offset) * (placement.startsWith('top') ? 1 : -1);
|
||||
const scaleX = !expanded && latestNotice?.offsetWidth && dictRef.current[strKey]?.offsetWidth ? (latestNotice?.offsetWidth - offset * 2 * (index < 3 ? index : 3)) / dictRef.current[strKey]?.offsetWidth : 1;
|
||||
stackStyle.transform = `translate3d(${transformX}, ${transformY}px, 0) scaleX(${scaleX})`;
|
||||
} else {
|
||||
stackStyle.transform = `translate3d(${transformX}, 0, 0)`;
|
||||
}
|
||||
}
|
||||
return /*#__PURE__*/_react.default.createElement("div", {
|
||||
ref: nodeRef,
|
||||
className: (0, _clsx.clsx)(`${prefixCls}-notice-wrapper`, motionClassName, configClassNames?.wrapper),
|
||||
style: {
|
||||
...motionStyle,
|
||||
...stackStyle,
|
||||
...configStyles?.wrapper
|
||||
},
|
||||
onMouseEnter: () => setHoverKeys(prev => prev.includes(strKey) ? prev : [...prev, strKey]),
|
||||
onMouseLeave: () => setHoverKeys(prev => prev.filter(k => k !== strKey))
|
||||
}, /*#__PURE__*/_react.default.createElement(_Notice.default, _extends({}, restConfig, {
|
||||
ref: node => {
|
||||
if (dataIndex > -1) {
|
||||
dictRef.current[strKey] = node;
|
||||
} else {
|
||||
delete dictRef.current[strKey];
|
||||
}
|
||||
},
|
||||
prefixCls: prefixCls,
|
||||
classNames: configClassNames,
|
||||
styles: configStyles,
|
||||
className: (0, _clsx.clsx)(configClassName, ctxCls?.notice),
|
||||
style: configStyle,
|
||||
times: times,
|
||||
key: key,
|
||||
eventKey: key,
|
||||
onNoticeClose: onNoticeClose,
|
||||
hovering: stack && hoverKeys.length > 0
|
||||
})));
|
||||
});
|
||||
};
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
NoticeList.displayName = 'NoticeList';
|
||||
}
|
||||
var _default = exports.default = NoticeList;
|
||||
Generated
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
import type { FC } from 'react';
|
||||
import React from 'react';
|
||||
export interface NotificationContextProps {
|
||||
classNames?: {
|
||||
notice?: string;
|
||||
list?: string;
|
||||
};
|
||||
}
|
||||
export declare const NotificationContext: React.Context<NotificationContextProps>;
|
||||
export interface NotificationProviderProps extends NotificationContextProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
declare const NotificationProvider: FC<NotificationProviderProps>;
|
||||
export default NotificationProvider;
|
||||
Generated
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = exports.NotificationContext = void 0;
|
||||
var _react = _interopRequireDefault(require("react"));
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
const NotificationContext = exports.NotificationContext = /*#__PURE__*/_react.default.createContext({});
|
||||
const NotificationProvider = ({
|
||||
children,
|
||||
classNames
|
||||
}) => {
|
||||
return /*#__PURE__*/_react.default.createElement(NotificationContext.Provider, {
|
||||
value: {
|
||||
classNames
|
||||
}
|
||||
}, children);
|
||||
};
|
||||
var _default = exports.default = NotificationProvider;
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import * as React from 'react';
|
||||
import type { ReactElement } from 'react';
|
||||
import type { CSSMotionProps } from '@rc-component/motion';
|
||||
import type { OpenConfig, Placement, StackConfig } from './interface';
|
||||
export interface NotificationsProps {
|
||||
prefixCls?: string;
|
||||
motion?: CSSMotionProps | ((placement: Placement) => CSSMotionProps);
|
||||
container?: HTMLElement | ShadowRoot;
|
||||
maxCount?: number;
|
||||
className?: (placement: Placement) => string;
|
||||
style?: (placement: Placement) => React.CSSProperties;
|
||||
onAllRemoved?: VoidFunction;
|
||||
stack?: StackConfig;
|
||||
renderNotifications?: (node: ReactElement, info: {
|
||||
prefixCls: string;
|
||||
key: React.Key;
|
||||
}) => ReactElement;
|
||||
}
|
||||
export interface NotificationsRef {
|
||||
open: (config: OpenConfig) => void;
|
||||
close: (key: React.Key) => void;
|
||||
destroy: () => void;
|
||||
}
|
||||
declare const Notifications: React.ForwardRefExoticComponent<NotificationsProps & React.RefAttributes<NotificationsRef>>;
|
||||
export default Notifications;
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
var React = _interopRequireWildcard(require("react"));
|
||||
var _reactDom = require("react-dom");
|
||||
var _NoticeList = _interopRequireDefault(require("./NoticeList"));
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
||||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
||||
// ant-notification ant-notification-topRight
|
||||
const Notifications = /*#__PURE__*/React.forwardRef((props, ref) => {
|
||||
const {
|
||||
prefixCls = 'rc-notification',
|
||||
container,
|
||||
motion,
|
||||
maxCount,
|
||||
className,
|
||||
style,
|
||||
onAllRemoved,
|
||||
stack,
|
||||
renderNotifications
|
||||
} = props;
|
||||
const [configList, setConfigList] = React.useState([]);
|
||||
|
||||
// ======================== Close =========================
|
||||
const onNoticeClose = key => {
|
||||
// Trigger close event
|
||||
const config = configList.find(item => item.key === key);
|
||||
const closable = config?.closable;
|
||||
const closableObj = closable && typeof closable === 'object' ? closable : {};
|
||||
const {
|
||||
onClose: closableOnClose
|
||||
} = closableObj;
|
||||
closableOnClose?.();
|
||||
config?.onClose?.();
|
||||
setConfigList(list => list.filter(item => item.key !== key));
|
||||
};
|
||||
|
||||
// ========================= Refs =========================
|
||||
React.useImperativeHandle(ref, () => ({
|
||||
open: config => {
|
||||
setConfigList(list => {
|
||||
let clone = [...list];
|
||||
|
||||
// Replace if exist
|
||||
const index = clone.findIndex(item => item.key === config.key);
|
||||
const innerConfig = {
|
||||
...config
|
||||
};
|
||||
if (index >= 0) {
|
||||
innerConfig.times = (list[index]?.times || 0) + 1;
|
||||
clone[index] = innerConfig;
|
||||
} else {
|
||||
innerConfig.times = 0;
|
||||
clone.push(innerConfig);
|
||||
}
|
||||
if (maxCount > 0 && clone.length > maxCount) {
|
||||
clone = clone.slice(-maxCount);
|
||||
}
|
||||
return clone;
|
||||
});
|
||||
},
|
||||
close: key => {
|
||||
onNoticeClose(key);
|
||||
},
|
||||
destroy: () => {
|
||||
setConfigList([]);
|
||||
}
|
||||
}));
|
||||
|
||||
// ====================== Placements ======================
|
||||
const [placements, setPlacements] = React.useState({});
|
||||
React.useEffect(() => {
|
||||
const nextPlacements = {};
|
||||
configList.forEach(config => {
|
||||
const {
|
||||
placement = 'topRight'
|
||||
} = config;
|
||||
if (placement) {
|
||||
nextPlacements[placement] = nextPlacements[placement] || [];
|
||||
nextPlacements[placement].push(config);
|
||||
}
|
||||
});
|
||||
|
||||
// Fill exist placements to avoid empty list causing remove without motion
|
||||
Object.keys(placements).forEach(placement => {
|
||||
nextPlacements[placement] = nextPlacements[placement] || [];
|
||||
});
|
||||
setPlacements(nextPlacements);
|
||||
}, [configList]);
|
||||
|
||||
// Clean up container if all notices fade out
|
||||
const onAllNoticeRemoved = placement => {
|
||||
setPlacements(originPlacements => {
|
||||
const clone = {
|
||||
...originPlacements
|
||||
};
|
||||
const list = clone[placement] || [];
|
||||
if (!list.length) {
|
||||
delete clone[placement];
|
||||
}
|
||||
return clone;
|
||||
});
|
||||
};
|
||||
|
||||
// Effect tell that placements is empty now
|
||||
const emptyRef = React.useRef(false);
|
||||
React.useEffect(() => {
|
||||
if (Object.keys(placements).length > 0) {
|
||||
emptyRef.current = true;
|
||||
} else if (emptyRef.current) {
|
||||
// Trigger only when from exist to empty
|
||||
onAllRemoved?.();
|
||||
emptyRef.current = false;
|
||||
}
|
||||
}, [placements]);
|
||||
// ======================== Render ========================
|
||||
if (!container) {
|
||||
return null;
|
||||
}
|
||||
const placementList = Object.keys(placements);
|
||||
return /*#__PURE__*/(0, _reactDom.createPortal)( /*#__PURE__*/React.createElement(React.Fragment, null, placementList.map(placement => {
|
||||
const placementConfigList = placements[placement];
|
||||
const list = /*#__PURE__*/React.createElement(_NoticeList.default, {
|
||||
key: placement,
|
||||
configList: placementConfigList,
|
||||
placement: placement,
|
||||
prefixCls: prefixCls,
|
||||
className: className?.(placement),
|
||||
style: style?.(placement),
|
||||
motion: motion,
|
||||
onNoticeClose: onNoticeClose,
|
||||
onAllNoticeRemoved: onAllNoticeRemoved,
|
||||
stack: stack
|
||||
});
|
||||
return renderNotifications ? renderNotifications(list, {
|
||||
prefixCls,
|
||||
key: placement
|
||||
}) : list;
|
||||
})), container);
|
||||
});
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
Notifications.displayName = 'Notifications';
|
||||
}
|
||||
var _default = exports.default = Notifications;
|
||||
Generated
Vendored
+35
@@ -0,0 +1,35 @@
|
||||
import type { CSSMotionProps } from '@rc-component/motion';
|
||||
import * as React from 'react';
|
||||
import type { NotificationsProps } from '../Notifications';
|
||||
import type { OpenConfig, Placement, StackConfig } from '../interface';
|
||||
type OptionalConfig = Partial<OpenConfig>;
|
||||
export interface NotificationConfig {
|
||||
prefixCls?: string;
|
||||
/** Customize container. It will repeat call which means you should return same container element. */
|
||||
getContainer?: () => HTMLElement | ShadowRoot;
|
||||
motion?: CSSMotionProps | ((placement: Placement) => CSSMotionProps);
|
||||
closable?: boolean | ({
|
||||
closeIcon?: React.ReactNode;
|
||||
onClose?: VoidFunction;
|
||||
} & React.AriaAttributes);
|
||||
maxCount?: number;
|
||||
duration?: number | false | null;
|
||||
showProgress?: boolean;
|
||||
pauseOnHover?: boolean;
|
||||
/** @private. Config for notification holder style. Safe to remove if refactor */
|
||||
className?: (placement: Placement) => string;
|
||||
/** @private. Config for notification holder style. Safe to remove if refactor */
|
||||
style?: (placement: Placement) => React.CSSProperties;
|
||||
/** @private Trigger when all the notification closed. */
|
||||
onAllRemoved?: VoidFunction;
|
||||
stack?: StackConfig;
|
||||
/** @private Slot for style in Notifications */
|
||||
renderNotifications?: NotificationsProps['renderNotifications'];
|
||||
}
|
||||
export interface NotificationAPI {
|
||||
open: (config: OptionalConfig) => void;
|
||||
close: (key: React.Key) => void;
|
||||
destroy: () => void;
|
||||
}
|
||||
export default function useNotification(rootConfig?: NotificationConfig): [NotificationAPI, React.ReactElement];
|
||||
export {};
|
||||
Generated
Vendored
+134
@@ -0,0 +1,134 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = useNotification;
|
||||
var React = _interopRequireWildcard(require("react"));
|
||||
var _Notifications = _interopRequireDefault(require("../Notifications"));
|
||||
var _util = require("@rc-component/util");
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
||||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
||||
const defaultGetContainer = () => document.body;
|
||||
let uniqueKey = 0;
|
||||
function mergeConfig(...objList) {
|
||||
const clone = {};
|
||||
objList.forEach(obj => {
|
||||
if (obj) {
|
||||
Object.keys(obj).forEach(key => {
|
||||
const val = obj[key];
|
||||
if (val !== undefined) {
|
||||
clone[key] = val;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return clone;
|
||||
}
|
||||
function useNotification(rootConfig = {}) {
|
||||
const {
|
||||
getContainer = defaultGetContainer,
|
||||
motion,
|
||||
prefixCls,
|
||||
maxCount,
|
||||
className,
|
||||
style,
|
||||
onAllRemoved,
|
||||
stack,
|
||||
renderNotifications,
|
||||
...shareConfig
|
||||
} = rootConfig;
|
||||
const [container, setContainer] = React.useState();
|
||||
const notificationsRef = React.useRef();
|
||||
const contextHolder = /*#__PURE__*/React.createElement(_Notifications.default, {
|
||||
container: container,
|
||||
ref: notificationsRef,
|
||||
prefixCls: prefixCls,
|
||||
motion: motion,
|
||||
maxCount: maxCount,
|
||||
className: className,
|
||||
style: style,
|
||||
onAllRemoved: onAllRemoved,
|
||||
stack: stack,
|
||||
renderNotifications: renderNotifications
|
||||
});
|
||||
const [taskQueue, setTaskQueue] = React.useState([]);
|
||||
const open = (0, _util.useEvent)(config => {
|
||||
const mergedConfig = mergeConfig(shareConfig, config);
|
||||
if (mergedConfig.key === null || mergedConfig.key === undefined) {
|
||||
mergedConfig.key = `rc-notification-${uniqueKey}`;
|
||||
uniqueKey += 1;
|
||||
}
|
||||
setTaskQueue(queue => [...queue, {
|
||||
type: 'open',
|
||||
config: mergedConfig
|
||||
}]);
|
||||
});
|
||||
|
||||
// ========================= Refs =========================
|
||||
const api = React.useMemo(() => ({
|
||||
open: open,
|
||||
close: key => {
|
||||
setTaskQueue(queue => [...queue, {
|
||||
type: 'close',
|
||||
key
|
||||
}]);
|
||||
},
|
||||
destroy: () => {
|
||||
setTaskQueue(queue => [...queue, {
|
||||
type: 'destroy'
|
||||
}]);
|
||||
}
|
||||
}), []);
|
||||
|
||||
// ======================= Container ======================
|
||||
// React 18 should all in effect that we will check container in each render
|
||||
// Which means getContainer should be stable.
|
||||
React.useEffect(() => {
|
||||
setContainer(getContainer());
|
||||
});
|
||||
|
||||
// ======================== Effect ========================
|
||||
React.useEffect(() => {
|
||||
// Flush task when node ready
|
||||
if (notificationsRef.current && taskQueue.length) {
|
||||
taskQueue.forEach(task => {
|
||||
switch (task.type) {
|
||||
case 'open':
|
||||
notificationsRef.current.open(task.config);
|
||||
break;
|
||||
case 'close':
|
||||
notificationsRef.current.close(task.key);
|
||||
break;
|
||||
case 'destroy':
|
||||
notificationsRef.current.destroy();
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
// https://github.com/ant-design/ant-design/issues/52590
|
||||
// React `startTransition` will run once `useEffect` but many times `setState`,
|
||||
// So `setTaskQueue` with filtered array will cause infinite loop.
|
||||
// We cache the first match queue instead.
|
||||
let oriTaskQueue;
|
||||
let tgtTaskQueue;
|
||||
|
||||
// React 17 will mix order of effect & setState in async
|
||||
// - open: setState[0]
|
||||
// - effect[0]
|
||||
// - open: setState[1]
|
||||
// - effect setState([]) * here will clean up [0, 1] in React 17
|
||||
setTaskQueue(oriQueue => {
|
||||
if (oriTaskQueue !== oriQueue || !tgtTaskQueue) {
|
||||
oriTaskQueue = oriQueue;
|
||||
tgtTaskQueue = oriQueue.filter(task => !taskQueue.includes(task));
|
||||
}
|
||||
return tgtTaskQueue;
|
||||
});
|
||||
}
|
||||
}, [taskQueue]);
|
||||
|
||||
// ======================== Return ========================
|
||||
return [api, contextHolder];
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import type { StackConfig } from '../interface';
|
||||
type StackParams = Exclude<StackConfig, boolean>;
|
||||
type UseStack = (config?: StackConfig) => [boolean, StackParams];
|
||||
declare const useStack: UseStack;
|
||||
export default useStack;
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
const DEFAULT_OFFSET = 8;
|
||||
const DEFAULT_THRESHOLD = 3;
|
||||
const DEFAULT_GAP = 16;
|
||||
const useStack = config => {
|
||||
const result = {
|
||||
offset: DEFAULT_OFFSET,
|
||||
threshold: DEFAULT_THRESHOLD,
|
||||
gap: DEFAULT_GAP
|
||||
};
|
||||
if (config && typeof config === 'object') {
|
||||
result.offset = config.offset ?? DEFAULT_OFFSET;
|
||||
result.threshold = config.threshold ?? DEFAULT_THRESHOLD;
|
||||
result.gap = config.gap ?? DEFAULT_GAP;
|
||||
}
|
||||
return [!!config, result];
|
||||
};
|
||||
var _default = exports.default = useStack;
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
import useNotification from './hooks/useNotification';
|
||||
import Notice from './Notice';
|
||||
import type { NotificationAPI, NotificationConfig } from './hooks/useNotification';
|
||||
import NotificationProvider from './NotificationProvider';
|
||||
export { useNotification, Notice, NotificationProvider };
|
||||
export type { NotificationAPI, NotificationConfig };
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "Notice", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _Notice.default;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, "NotificationProvider", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _NotificationProvider.default;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, "useNotification", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _useNotification.default;
|
||||
}
|
||||
});
|
||||
var _useNotification = _interopRequireDefault(require("./hooks/useNotification"));
|
||||
var _Notice = _interopRequireDefault(require("./Notice"));
|
||||
var _NotificationProvider = _interopRequireDefault(require("./NotificationProvider"));
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
import type React from 'react';
|
||||
export type Placement = 'top' | 'topLeft' | 'topRight' | 'bottom' | 'bottomLeft' | 'bottomRight';
|
||||
type NoticeSemanticProps = 'wrapper';
|
||||
export interface NoticeConfig {
|
||||
content?: React.ReactNode;
|
||||
duration?: number | false | null;
|
||||
showProgress?: boolean;
|
||||
pauseOnHover?: boolean;
|
||||
closable?: boolean | ({
|
||||
closeIcon?: React.ReactNode;
|
||||
onClose?: VoidFunction;
|
||||
} & React.AriaAttributes);
|
||||
className?: string;
|
||||
style?: React.CSSProperties;
|
||||
classNames?: {
|
||||
[key in NoticeSemanticProps]?: string;
|
||||
};
|
||||
styles?: {
|
||||
[key in NoticeSemanticProps]?: React.CSSProperties;
|
||||
};
|
||||
/** @private Internal usage. Do not override in your code */
|
||||
props?: React.HTMLAttributes<HTMLDivElement> & Record<string, any>;
|
||||
onClose?: VoidFunction;
|
||||
onClick?: React.MouseEventHandler<HTMLDivElement>;
|
||||
}
|
||||
export interface OpenConfig extends NoticeConfig {
|
||||
key: React.Key;
|
||||
placement?: Placement;
|
||||
content?: React.ReactNode;
|
||||
duration?: number | false | null;
|
||||
}
|
||||
export type InnerOpenConfig = OpenConfig & {
|
||||
times?: number;
|
||||
};
|
||||
export type Placements = Partial<Record<Placement, OpenConfig[]>>;
|
||||
export type StackConfig = boolean | {
|
||||
/**
|
||||
* When number is greater than threshold, notifications will be stacked together.
|
||||
* @default 3
|
||||
*/
|
||||
threshold?: number;
|
||||
/**
|
||||
* Offset when notifications are stacked together.
|
||||
* @default 8
|
||||
*/
|
||||
offset?: number;
|
||||
/**
|
||||
* Spacing between each notification when expanded.
|
||||
*/
|
||||
gap?: number;
|
||||
};
|
||||
export {};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Reference in New Issue
Block a user