|
| 1 | +import type { VNode } from 'vue'; |
| 2 | +import { shallowRef, computed, defineComponent } from 'vue'; |
| 3 | +import { useNotification as useVcNotification } from '../vc-notification'; |
| 4 | +import type { NotificationAPI } from '../vc-notification'; |
| 5 | +import type { |
| 6 | + NotificationInstance, |
| 7 | + ArgsProps, |
| 8 | + NotificationPlacement, |
| 9 | + NotificationConfig, |
| 10 | +} from './interface'; |
| 11 | + |
| 12 | +import useStyle from './style'; |
| 13 | +import { getCloseIcon, PureContent } from './PurePanel'; |
| 14 | +import { getMotion, getPlacementStyle } from './util'; |
| 15 | +import useConfigInject from '../config-provider/hooks/useConfigInject'; |
| 16 | +import classNames from '../_util/classNames'; |
| 17 | +import type { Key } from '../_util/type'; |
| 18 | + |
| 19 | +const DEFAULT_OFFSET = 24; |
| 20 | +const DEFAULT_DURATION = 4.5; |
| 21 | + |
| 22 | +// ============================================================================== |
| 23 | +// == Holder == |
| 24 | +// ============================================================================== |
| 25 | +type HolderProps = NotificationConfig & { |
| 26 | + onAllRemoved?: VoidFunction; |
| 27 | + getPopupContainer?: () => HTMLElement; |
| 28 | +}; |
| 29 | + |
| 30 | +interface HolderRef extends NotificationAPI { |
| 31 | + prefixCls: string; |
| 32 | + hashId: string; |
| 33 | +} |
| 34 | + |
| 35 | +const Holder = defineComponent({ |
| 36 | + name: 'Holder', |
| 37 | + inheritAttrs: false, |
| 38 | + props: ['prefixCls', 'class', 'type', 'icon', 'content', 'onAllRemoved'], |
| 39 | + setup(props: HolderProps, { expose }) { |
| 40 | + const { getPrefixCls, getPopupContainer } = useConfigInject('notification', props); |
| 41 | + const prefixCls = computed(() => props.prefixCls || getPrefixCls('notification')); |
| 42 | + // =============================== Style =============================== |
| 43 | + const getStyles = (placement: NotificationPlacement) => |
| 44 | + getPlacementStyle(placement, props.top ?? DEFAULT_OFFSET, props.bottom ?? DEFAULT_OFFSET); |
| 45 | + |
| 46 | + // Style |
| 47 | + const [, hashId] = useStyle(prefixCls); |
| 48 | + |
| 49 | + const getClassName = () => classNames(hashId.value, { [`${prefixCls.value}-rtl`]: props.rtl }); |
| 50 | + |
| 51 | + // ============================== Motion =============================== |
| 52 | + const getNotificationMotion = () => getMotion(prefixCls.value); |
| 53 | + |
| 54 | + // ============================== Origin =============================== |
| 55 | + const [api, holder] = useVcNotification({ |
| 56 | + prefixCls: prefixCls.value, |
| 57 | + getStyles, |
| 58 | + getClassName, |
| 59 | + motion: getNotificationMotion, |
| 60 | + closable: true, |
| 61 | + closeIcon: getCloseIcon(prefixCls.value), |
| 62 | + duration: DEFAULT_DURATION, |
| 63 | + getContainer: () => |
| 64 | + props.getPopupContainer?.() || getPopupContainer.value?.() || document.body, |
| 65 | + maxCount: props.maxCount, |
| 66 | + hashId: hashId.value, |
| 67 | + onAllRemoved: props.onAllRemoved, |
| 68 | + }); |
| 69 | + |
| 70 | + // ================================ Ref ================================ |
| 71 | + expose({ |
| 72 | + ...api, |
| 73 | + prefixCls: prefixCls.value, |
| 74 | + hashId, |
| 75 | + }); |
| 76 | + return holder; |
| 77 | + }, |
| 78 | +}); |
| 79 | + |
| 80 | +// ============================================================================== |
| 81 | +// == Hook == |
| 82 | +// ============================================================================== |
| 83 | +export function useInternalNotification( |
| 84 | + notificationConfig?: HolderProps, |
| 85 | +): readonly [NotificationInstance, () => VNode] { |
| 86 | + const holderRef = shallowRef<HolderRef>(null); |
| 87 | + |
| 88 | + // ================================ API ================================ |
| 89 | + const wrapAPI = computed(() => { |
| 90 | + // Wrap with notification content |
| 91 | + |
| 92 | + // >>> Open |
| 93 | + const open = (config: ArgsProps) => { |
| 94 | + if (!holderRef.value) { |
| 95 | + return; |
| 96 | + } |
| 97 | + const { open: originOpen, prefixCls, hashId } = holderRef.value; |
| 98 | + const noticePrefixCls = `${prefixCls}-notice`; |
| 99 | + |
| 100 | + const { message, description, icon, type, btn, className, ...restConfig } = config; |
| 101 | + return originOpen({ |
| 102 | + placement: 'topRight', |
| 103 | + ...restConfig, |
| 104 | + content: ( |
| 105 | + <PureContent |
| 106 | + prefixCls={noticePrefixCls} |
| 107 | + icon={icon} |
| 108 | + type={type} |
| 109 | + message={message} |
| 110 | + description={description} |
| 111 | + btn={btn} |
| 112 | + /> |
| 113 | + ), |
| 114 | + // @ts-ignore |
| 115 | + class: classNames(type && `${noticePrefixCls}-${type}`, hashId, className), |
| 116 | + }); |
| 117 | + }; |
| 118 | + |
| 119 | + // >>> destroy |
| 120 | + const destroy = (key?: Key) => { |
| 121 | + if (key !== undefined) { |
| 122 | + holderRef.value?.close(key); |
| 123 | + } else { |
| 124 | + holderRef.value?.destroy(); |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + const clone = { |
| 129 | + open, |
| 130 | + destroy, |
| 131 | + } as NotificationInstance; |
| 132 | + |
| 133 | + const keys = ['success', 'info', 'warning', 'error'] as const; |
| 134 | + keys.forEach(type => { |
| 135 | + clone[type] = config => |
| 136 | + open({ |
| 137 | + ...config, |
| 138 | + type, |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + return clone; |
| 143 | + }); |
| 144 | + |
| 145 | + // ============================== Return =============================== |
| 146 | + return [ |
| 147 | + wrapAPI.value, |
| 148 | + () => <Holder key="notification-holder" {...notificationConfig} ref={holderRef} />, |
| 149 | + ] as const; |
| 150 | +} |
| 151 | + |
| 152 | +export default function useNotification(notificationConfig?: NotificationConfig) { |
| 153 | + return useInternalNotification(notificationConfig); |
| 154 | +} |
0 commit comments