context.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as React from 'react';
  2. export const defaultPrefixCls = 'ant';
  3. export const defaultIconPrefixCls = 'anticon';
  4. export const Variants = ['outlined', 'borderless', 'filled', 'underlined'];
  5. const defaultGetPrefixCls = (suffixCls, customizePrefixCls) => {
  6. if (customizePrefixCls) {
  7. return customizePrefixCls;
  8. }
  9. return suffixCls ? `${defaultPrefixCls}-${suffixCls}` : defaultPrefixCls;
  10. };
  11. // zombieJ: 🚨 Do not pass `defaultRenderEmpty` here since it will cause circular dependency.
  12. export const ConfigContext = /*#__PURE__*/React.createContext({
  13. // We provide a default function for Context without provider
  14. getPrefixCls: defaultGetPrefixCls,
  15. iconPrefixCls: defaultIconPrefixCls
  16. });
  17. export const {
  18. Consumer: ConfigConsumer
  19. } = ConfigContext;
  20. const EMPTY_OBJECT = {};
  21. /**
  22. * Get ConfigProvider configured component props.
  23. * This help to reduce bundle size for saving `?.` operator.
  24. * Do not use as `useMemo` deps since we do not cache the object here.
  25. *
  26. * NOTE: not refactor this with `useMemo` since memo will cost another memory space,
  27. * which will waste both compare calculation & memory.
  28. */
  29. export function useComponentConfig(propName) {
  30. const context = React.useContext(ConfigContext);
  31. const {
  32. getPrefixCls,
  33. direction,
  34. getPopupContainer
  35. } = context;
  36. const propValue = context[propName];
  37. return Object.assign(Object.assign({
  38. classNames: EMPTY_OBJECT,
  39. styles: EMPTY_OBJECT
  40. }, propValue), {
  41. getPrefixCls,
  42. direction,
  43. getPopupContainer
  44. });
  45. }