|
| 1 | +import { defineComponent, computed } from 'vue'; |
| 2 | +import type { App as TypeApp, Plugin } from 'vue'; |
| 3 | +import { initDefaultProps } from '../_util/props-util'; |
| 4 | +import classNames from '../_util/classNames'; |
| 5 | +import { objectType } from '../_util/type'; |
| 6 | +import useConfigInject from '../config-provider/hooks/useConfigInject'; |
| 7 | +import useMessage from '../message/useMessage'; |
| 8 | +import useModal from '../modal/useModal'; |
| 9 | +import useNotification from '../notification/useNotification'; |
| 10 | +import type { AppConfig } from './context'; |
| 11 | +import { |
| 12 | + useProvideAppConfigContext, |
| 13 | + useInjectAppConfigContext, |
| 14 | + useProvideAppContext, |
| 15 | + useInjectAppContext, |
| 16 | +} from './context'; |
| 17 | +import useStyle from './style'; |
| 18 | + |
| 19 | +export const AppProps = () => { |
| 20 | + return { |
| 21 | + rootClassName: String, |
| 22 | + message: objectType<AppConfig['message']>(), |
| 23 | + notification: objectType<AppConfig['notification']>(), |
| 24 | + }; |
| 25 | +}; |
| 26 | + |
| 27 | +const useApp = () => { |
| 28 | + return useInjectAppContext(); |
| 29 | +}; |
| 30 | + |
| 31 | +const App = defineComponent({ |
| 32 | + name: 'AApp', |
| 33 | + props: initDefaultProps(AppProps(), {}), |
| 34 | + setup(props, { slots }) { |
| 35 | + const { prefixCls } = useConfigInject('app', props); |
| 36 | + const [wrapSSR, hashId] = useStyle(prefixCls); |
| 37 | + const customClassName = computed(() => { |
| 38 | + return classNames(hashId.value, prefixCls.value, props.rootClassName); |
| 39 | + }); |
| 40 | + |
| 41 | + const appConfig = useInjectAppConfigContext(); |
| 42 | + const mergedAppConfig = computed(() => ({ |
| 43 | + message: { ...appConfig.message, ...props.message }, |
| 44 | + notification: { ...appConfig.notification, ...props.notification }, |
| 45 | + })); |
| 46 | + useProvideAppConfigContext(mergedAppConfig.value); |
| 47 | + |
| 48 | + const [messageApi, messageContextHolder] = useMessage(mergedAppConfig.value.message); |
| 49 | + const [notificationApi, notificationContextHolder] = useNotification( |
| 50 | + mergedAppConfig.value.notification, |
| 51 | + ); |
| 52 | + const [ModalApi, ModalContextHolder] = useModal(); |
| 53 | + |
| 54 | + const memoizedContextValue = computed(() => ({ |
| 55 | + message: messageApi, |
| 56 | + notification: notificationApi, |
| 57 | + modal: ModalApi, |
| 58 | + })); |
| 59 | + useProvideAppContext(memoizedContextValue.value); |
| 60 | + |
| 61 | + return () => { |
| 62 | + return wrapSSR( |
| 63 | + <div class={customClassName.value}> |
| 64 | + {ModalContextHolder()} |
| 65 | + {messageContextHolder()} |
| 66 | + {notificationContextHolder()} |
| 67 | + {slots.default?.()} |
| 68 | + </div>, |
| 69 | + ); |
| 70 | + }; |
| 71 | + }, |
| 72 | +}); |
| 73 | + |
| 74 | +App.useApp = useApp; |
| 75 | + |
| 76 | +App.install = function (app: TypeApp) { |
| 77 | + app.component(App.name, App); |
| 78 | +}; |
| 79 | + |
| 80 | +export default App as typeof App & |
| 81 | + Plugin & { |
| 82 | + readonly useApp: typeof useApp; |
| 83 | + }; |
0 commit comments