App.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use client";
  2. import React, { useContext } from 'react';
  3. import classNames from 'classnames';
  4. import { devUseWarning } from '../_util/warning';
  5. import { ConfigContext } from '../config-provider';
  6. import useMessage from '../message/useMessage';
  7. import useModal from '../modal/useModal';
  8. import useNotification from '../notification/useNotification';
  9. import AppContext, { AppConfigContext } from './context';
  10. import useStyle from './style';
  11. const App = props => {
  12. const {
  13. prefixCls: customizePrefixCls,
  14. children,
  15. className,
  16. rootClassName,
  17. message,
  18. notification,
  19. style,
  20. component = 'div'
  21. } = props;
  22. const {
  23. direction,
  24. getPrefixCls
  25. } = useContext(ConfigContext);
  26. const prefixCls = getPrefixCls('app', customizePrefixCls);
  27. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
  28. const customClassName = classNames(hashId, prefixCls, className, rootClassName, cssVarCls, {
  29. [`${prefixCls}-rtl`]: direction === 'rtl'
  30. });
  31. const appConfig = useContext(AppConfigContext);
  32. const mergedAppConfig = React.useMemo(() => ({
  33. message: Object.assign(Object.assign({}, appConfig.message), message),
  34. notification: Object.assign(Object.assign({}, appConfig.notification), notification)
  35. }), [message, notification, appConfig.message, appConfig.notification]);
  36. const [messageApi, messageContextHolder] = useMessage(mergedAppConfig.message);
  37. const [notificationApi, notificationContextHolder] = useNotification(mergedAppConfig.notification);
  38. const [ModalApi, ModalContextHolder] = useModal();
  39. const memoizedContextValue = React.useMemo(() => ({
  40. message: messageApi,
  41. notification: notificationApi,
  42. modal: ModalApi
  43. }), [messageApi, notificationApi, ModalApi]);
  44. // https://github.com/ant-design/ant-design/issues/48802#issuecomment-2097813526
  45. devUseWarning('App')(!(cssVarCls && component === false), 'usage', 'When using cssVar, ensure `component` is assigned a valid React component string.');
  46. // ============================ Render ============================
  47. const Component = component === false ? React.Fragment : component;
  48. const rootProps = {
  49. className: customClassName,
  50. style
  51. };
  52. return wrapCSSVar(/*#__PURE__*/React.createElement(AppContext.Provider, {
  53. value: memoizedContextValue
  54. }, /*#__PURE__*/React.createElement(AppConfigContext.Provider, {
  55. value: mergedAppConfig
  56. }, /*#__PURE__*/React.createElement(Component, Object.assign({}, component === false ? undefined : rootProps), ModalContextHolder, messageContextHolder, notificationContextHolder, children))));
  57. };
  58. if (process.env.NODE_ENV !== 'production') {
  59. App.displayName = 'App';
  60. }
  61. export default App;