ant-design/components/config-provider/index.tsx

625 lines
16 KiB
TypeScript
Raw Normal View History

'use client';
import * as React from 'react';
import { createTheme } from '@ant-design/cssinjs';
import IconContext from '@ant-design/icons/lib/components/Context';
import type { ValidateMessages } from 'rc-field-form/lib/interface';
import useMemo from 'rc-util/lib/hooks/useMemo';
import { merge } from 'rc-util/lib/utils/set';
import type { Options } from 'scroll-into-view-if-needed';
import warning, { WarningContext, type WarningContextProps } from '../_util/warning';
import type { RequiredMark } from '../form/Form';
2023-06-15 10:26:56 +08:00
import ValidateMessagesContext from '../form/validateMessagesContext';
import type { InputProps } from '../input';
import type { Locale } from '../locale';
import LocaleProvider, { ANT_MARK } from '../locale';
import type { LocaleContextProps } from '../locale/context';
import LocaleContext from '../locale/context';
import defaultLocale from '../locale/en_US';
import type { SpaceProps } from '../space';
import type { TabsProps } from '../tabs';
import { defaultTheme, DesignTokenContext } from '../theme/context';
import defaultSeedToken from '../theme/themes/seed';
import type {
BadgeConfig,
ButtonConfig,
ComponentStyleConfig,
ConfigConsumerProps,
CSPConfig,
DirectionType,
DrawerConfig,
FlexConfig,
ModalConfig,
PopupOverflow,
Theme,
ThemeConfig,
WaveConfig,
} from './context';
import { ConfigConsumer, ConfigContext, defaultIconPrefixCls } from './context';
2021-09-01 10:56:50 +08:00
import { registerTheme } from './cssVariables';
docs: v5 site upgrade (#38328) * build: try to use dumi as doc tool * docs: migrate demo structure to dumi way * refactor: use type export & import * docs: migrate demo previewer to dumi * docs: create empty layout & components * docs: apply custom rehype plugin * docs: create empty extra pages * docs: Add Banner component * chore: move theme tsconfig.json * docs: home page init * docs: migrate header (#37896) * docs: header * docs: update * docs: home init * clean up * test: fix site lint * chore: tsc ignore demo * chore: dumi demo migrate script * chore: cards * docs: home layout * docs: Update locale logic * docs: fix getLink logic * chore: fix ci (#37899) * chore: fix ci * ci: remove check-ts-demo * ci: preview build * test: ignore demo.tsx * chore: update script * test: update snapshot * test: update node and image test * chore: add .surgeignore * docs: layout providers (#37908) * docs: add components sidebar (#37923) * docs: sidebar * docs: update docs title * docs: update design doc * chore: code clean * docs: handle changelog page * docs: add title * docs: add subtitle * docs: active header nav * chore: code clean * docs: overview * chore: code clean * docs: update intl (#37918) * docs: update intl * chore: code clean * docs: update favicons * chore: update testPathIgnorePatterns * chore: code clean * chore: code clean * chore: copy 404.html (#37996) * docs: Home page theme picker * chore: Update migrate script * docs: home page update * docs: theme editor style * docs: theme lang * chore: update migrate.js * docs: fix demo (#38094) * chore: update migrate.js * docs: update md * docs: update demo * test: fix snapshot * chore: move debug to code attr in migrate script * chore: update md Co-authored-by: PeachScript <scdzwyxst@gmail.com> * feat: overview page * feat: Migrate `404` page (#38118) * feat: migrate IconSearch component (#37916) * feat<site/IconSearch>: copy IconDisplay from site to .dumi * feat<site/IconSearch>: change docs of icon * feat<site/IconSearch>: tweak * feat<site/IconSearch>: use useIntl instead of injectIntl * feat<site/IconSearch>: fix ts type error * feat<site/IconSearch>: use intl.formatMessage to render text * docs: Adjust home btn sizw * docs: Update doc * feat: v5 site overview page (#38131) * feat: site * fix: fix * feat: v5 site overview page * fix: fix path * fix: fix * fix: fix * docs: fix margin logic * feat: v5 site change-log page (#38137) * feat: v5 site change-log page (#38162) * docs: site redirect to home pag * docs: theme picker * docs: use react-intl from dumi (#38183) * docs: Theme Picker * docs: update dumi config * docs: home back fix * docs: picker colorful * docs: locale of it * docs: update components desc * docs: site of links * docs: update components list * docs: update desc * feat: Migrate `DemoWrapper` component (#38166) * feat: Migrate `DemoWrapper` component * feat: remove invalid comments and add comment for `key` prop * docs: FloatButton pure panel * chore: update demo * chore: update dumi config * Revert "chore: update demo" This reverts commit 028265d3ba5987df5f13c3e9d42cf76cb1812b2e. * chore: test logic adjust to support cnpm modules * chore: add locale alias * docs: /index to / * docs: add locale redirect head script * chore: adjust compact * docs: fix missing token * feat: compact switch * chore: code clean * docs: update home * docs: fix radius token * docs: hash of it * chore: adjust home page * docs: Add background map * docs: site theme bac logic * docs: avatar * docs: update logo color * docs: home banner * docs: adjust tour size * docs: purepanl update * docs: transfooter * docs: update banner gif * docs: content (#38361) * docs: title & EditButton * docs: content * chore: fix toc * docs: resource page * docs: transform resource data from hast * docs: filename & Resource Card * chore: enable prerender * chore: remove less * docs: toc style * chore: fix lint * docs: fix Layout page * docs: fix CP page * chore: update demos * docs: workaround for export dynamic html * chore: enable demo eslint * docs: table style * fix: header shadow * chore: update snapshot * fix: toc style * docs: add title * docs: Adjust site * feat: helmet * docs: site css * fix: description * feat: toc debug * docs: update config-provider * feat: use colorPanel * fix: colorPanel value * feat: anchor ink ball style * feat: apply theme editor * fix: code block style * chore: update demo * chore: fix lint * chore: code clean * chore: update snapshot * feat: ts2js * chore: description * docs: site ready for ssr includes: - move client render logic to useEffect in site theme - extract antd cssinjs to a single css file like bisheng - workaround to support react@18 pipeableStream for emotion * chore: bump testing lib * docs: font size of title * chore: remove react-sortable-hoc * chore: update snapshot * chore: update script Co-authored-by: PeachScript <scdzwyxst@gmail.com> Co-authored-by: MadCcc <1075746765@qq.com> Co-authored-by: zqran <uuxnet@gmail.com> Co-authored-by: TrickyPi <530257315@qq.com> Co-authored-by: lijianan <574980606@qq.com>
2022-11-09 12:28:04 +08:00
import type { RenderEmptyHandler } from './defaultRenderEmpty';
import { DisabledContextProvider } from './DisabledContext';
2023-02-23 15:05:18 +08:00
import useConfig from './hooks/useConfig';
import useTheme from './hooks/useTheme';
import MotionWrapper from './MotionWrapper';
import PropWarning from './PropWarning';
import type { SizeType } from './SizeContext';
import SizeContext, { SizeContextProvider } from './SizeContext';
import useStyle from './style';
2018-12-26 16:01:00 +08:00
/**
* Since too many feedback using static method like `Modal.confirm` not getting theme, we record the
* theme register info here to help developer get warning info.
*/
let existThemeConfig = false;
export const warnContext: (componentName: string) => void =
process.env.NODE_ENV !== 'production'
? (componentName: string) => {
warning(
!existThemeConfig,
componentName,
`Static function can not consume context like dynamic theme. Please use 'App' component instead.`,
);
}
: /* istanbul ignore next */
null!;
export {
ConfigConsumer,
ConfigContext,
defaultIconPrefixCls,
type ConfigConsumerProps,
docs: v5 site upgrade (#38328) * build: try to use dumi as doc tool * docs: migrate demo structure to dumi way * refactor: use type export & import * docs: migrate demo previewer to dumi * docs: create empty layout & components * docs: apply custom rehype plugin * docs: create empty extra pages * docs: Add Banner component * chore: move theme tsconfig.json * docs: home page init * docs: migrate header (#37896) * docs: header * docs: update * docs: home init * clean up * test: fix site lint * chore: tsc ignore demo * chore: dumi demo migrate script * chore: cards * docs: home layout * docs: Update locale logic * docs: fix getLink logic * chore: fix ci (#37899) * chore: fix ci * ci: remove check-ts-demo * ci: preview build * test: ignore demo.tsx * chore: update script * test: update snapshot * test: update node and image test * chore: add .surgeignore * docs: layout providers (#37908) * docs: add components sidebar (#37923) * docs: sidebar * docs: update docs title * docs: update design doc * chore: code clean * docs: handle changelog page * docs: add title * docs: add subtitle * docs: active header nav * chore: code clean * docs: overview * chore: code clean * docs: update intl (#37918) * docs: update intl * chore: code clean * docs: update favicons * chore: update testPathIgnorePatterns * chore: code clean * chore: code clean * chore: copy 404.html (#37996) * docs: Home page theme picker * chore: Update migrate script * docs: home page update * docs: theme editor style * docs: theme lang * chore: update migrate.js * docs: fix demo (#38094) * chore: update migrate.js * docs: update md * docs: update demo * test: fix snapshot * chore: move debug to code attr in migrate script * chore: update md Co-authored-by: PeachScript <scdzwyxst@gmail.com> * feat: overview page * feat: Migrate `404` page (#38118) * feat: migrate IconSearch component (#37916) * feat<site/IconSearch>: copy IconDisplay from site to .dumi * feat<site/IconSearch>: change docs of icon * feat<site/IconSearch>: tweak * feat<site/IconSearch>: use useIntl instead of injectIntl * feat<site/IconSearch>: fix ts type error * feat<site/IconSearch>: use intl.formatMessage to render text * docs: Adjust home btn sizw * docs: Update doc * feat: v5 site overview page (#38131) * feat: site * fix: fix * feat: v5 site overview page * fix: fix path * fix: fix * fix: fix * docs: fix margin logic * feat: v5 site change-log page (#38137) * feat: v5 site change-log page (#38162) * docs: site redirect to home pag * docs: theme picker * docs: use react-intl from dumi (#38183) * docs: Theme Picker * docs: update dumi config * docs: home back fix * docs: picker colorful * docs: locale of it * docs: update components desc * docs: site of links * docs: update components list * docs: update desc * feat: Migrate `DemoWrapper` component (#38166) * feat: Migrate `DemoWrapper` component * feat: remove invalid comments and add comment for `key` prop * docs: FloatButton pure panel * chore: update demo * chore: update dumi config * Revert "chore: update demo" This reverts commit 028265d3ba5987df5f13c3e9d42cf76cb1812b2e. * chore: test logic adjust to support cnpm modules * chore: add locale alias * docs: /index to / * docs: add locale redirect head script * chore: adjust compact * docs: fix missing token * feat: compact switch * chore: code clean * docs: update home * docs: fix radius token * docs: hash of it * chore: adjust home page * docs: Add background map * docs: site theme bac logic * docs: avatar * docs: update logo color * docs: home banner * docs: adjust tour size * docs: purepanl update * docs: transfooter * docs: update banner gif * docs: content (#38361) * docs: title & EditButton * docs: content * chore: fix toc * docs: resource page * docs: transform resource data from hast * docs: filename & Resource Card * chore: enable prerender * chore: remove less * docs: toc style * chore: fix lint * docs: fix Layout page * docs: fix CP page * chore: update demos * docs: workaround for export dynamic html * chore: enable demo eslint * docs: table style * fix: header shadow * chore: update snapshot * fix: toc style * docs: add title * docs: Adjust site * feat: helmet * docs: site css * fix: description * feat: toc debug * docs: update config-provider * feat: use colorPanel * fix: colorPanel value * feat: anchor ink ball style * feat: apply theme editor * fix: code block style * chore: update demo * chore: fix lint * chore: code clean * chore: update snapshot * feat: ts2js * chore: description * docs: site ready for ssr includes: - move client render logic to useEffect in site theme - extract antd cssinjs to a single css file like bisheng - workaround to support react@18 pipeableStream for emotion * chore: bump testing lib * docs: font size of title * chore: remove react-sortable-hoc * chore: update snapshot * chore: update script Co-authored-by: PeachScript <scdzwyxst@gmail.com> Co-authored-by: MadCcc <1075746765@qq.com> Co-authored-by: zqran <uuxnet@gmail.com> Co-authored-by: TrickyPi <530257315@qq.com> Co-authored-by: lijianan <574980606@qq.com>
2022-11-09 12:28:04 +08:00
type CSPConfig,
type DirectionType,
type RenderEmptyHandler,
2023-01-23 19:24:05 +08:00
type ThemeConfig,
};
2018-11-26 12:06:42 +08:00
New Component: Typography (#14250) * text with prefix * add edit style * support editable * enhance accessibility & type experience * optimize IME case * support copy * add locale * add secondary & disabled * add ellipsis shadow text * split to 3 components * update snapshot * update desc * change lines also need update ellipsis * skip aria when is in ellipsis * add ResizeObserver in _util * update snapshot * move TestBase into test file * update test case * update doc * fix typo * important => level * use rows * update demo cols to 1 * fix cssText not work in firefox * update doc * add miss point * support extendable * update snapshot * fix doc * copyable support string * update snapshot * update doc * update doc desc * adjust style * full test * reset after test * rename * update snapshot * fix compile * adjust style * update desc * update prefixCls * update margin * adjust * nest wrap of tag content * adjust style * update comment * rm % * one more thing * tmp of measure * merge string as children * update snapshot * update testcase * remove comment * use internal variable for configProvider passing * update snapshot * use expandable instead of extendable * less variable it * update demo * update less * adjust code & mark style * remove mark padding * update measure logic * support nest element style * use childNode.textContent to fix react 15 error * update css * popout Typography * add link style * adjust doc * use ellipsis instead of rows & expandable * update doc * update doc * update doc & style * fix typo * add css ellipsis support * client render * update snapshot * enhance copyable * support onExpand * update test case * add test of css ellipsis * fix logic in react 15 * rename onChange -> onSave * use tagName of article * fix lint
2019-02-19 11:42:05 +08:00
export const configConsumerProps = [
'getTargetContainer',
New Component: Typography (#14250) * text with prefix * add edit style * support editable * enhance accessibility & type experience * optimize IME case * support copy * add locale * add secondary & disabled * add ellipsis shadow text * split to 3 components * update snapshot * update desc * change lines also need update ellipsis * skip aria when is in ellipsis * add ResizeObserver in _util * update snapshot * move TestBase into test file * update test case * update doc * fix typo * important => level * use rows * update demo cols to 1 * fix cssText not work in firefox * update doc * add miss point * support extendable * update snapshot * fix doc * copyable support string * update snapshot * update doc * update doc desc * adjust style * full test * reset after test * rename * update snapshot * fix compile * adjust style * update desc * update prefixCls * update margin * adjust * nest wrap of tag content * adjust style * update comment * rm % * one more thing * tmp of measure * merge string as children * update snapshot * update testcase * remove comment * use internal variable for configProvider passing * update snapshot * use expandable instead of extendable * less variable it * update demo * update less * adjust code & mark style * remove mark padding * update measure logic * support nest element style * use childNode.textContent to fix react 15 error * update css * popout Typography * add link style * adjust doc * use ellipsis instead of rows & expandable * update doc * update doc * update doc & style * fix typo * add css ellipsis support * client render * update snapshot * enhance copyable * support onExpand * update test case * add test of css ellipsis * fix logic in react 15 * rename onChange -> onSave * use tagName of article * fix lint
2019-02-19 11:42:05 +08:00
'getPopupContainer',
'rootPrefixCls',
'getPrefixCls',
'renderEmpty',
'csp',
'autoInsertSpaceInButton',
'locale',
'pageHeader',
New Component: Typography (#14250) * text with prefix * add edit style * support editable * enhance accessibility & type experience * optimize IME case * support copy * add locale * add secondary & disabled * add ellipsis shadow text * split to 3 components * update snapshot * update desc * change lines also need update ellipsis * skip aria when is in ellipsis * add ResizeObserver in _util * update snapshot * move TestBase into test file * update test case * update doc * fix typo * important => level * use rows * update demo cols to 1 * fix cssText not work in firefox * update doc * add miss point * support extendable * update snapshot * fix doc * copyable support string * update snapshot * update doc * update doc desc * adjust style * full test * reset after test * rename * update snapshot * fix compile * adjust style * update desc * update prefixCls * update margin * adjust * nest wrap of tag content * adjust style * update comment * rm % * one more thing * tmp of measure * merge string as children * update snapshot * update testcase * remove comment * use internal variable for configProvider passing * update snapshot * use expandable instead of extendable * less variable it * update demo * update less * adjust code & mark style * remove mark padding * update measure logic * support nest element style * use childNode.textContent to fix react 15 error * update css * popout Typography * add link style * adjust doc * use ellipsis instead of rows & expandable * update doc * update doc * update doc & style * fix typo * add css ellipsis support * client render * update snapshot * enhance copyable * support onExpand * update test case * add test of css ellipsis * fix logic in react 15 * rename onChange -> onSave * use tagName of article * fix lint
2019-02-19 11:42:05 +08:00
];
// These props is used by `useContext` directly in sub component
const PASSED_PROPS: Exclude<
keyof ConfigConsumerProps,
'rootPrefixCls' | 'getPrefixCls' | 'warning'
>[] = [
'getTargetContainer',
'getPopupContainer',
'renderEmpty',
'pageHeader',
'input',
'pagination',
'form',
'select',
'button',
];
export interface ConfigProviderProps {
2022-10-06 18:53:06 +08:00
getTargetContainer?: () => HTMLElement | Window;
getPopupContainer?: (triggerNode?: HTMLElement) => HTMLElement;
prefixCls?: string;
iconPrefixCls?: string;
children?: React.ReactNode;
2018-12-26 16:01:00 +08:00
renderEmpty?: RenderEmptyHandler;
csp?: CSPConfig;
autoInsertSpaceInButton?: boolean;
form?: ComponentStyleConfig & {
validateMessages?: ValidateMessages;
requiredMark?: RequiredMark;
colon?: boolean;
scrollToFirstError?: Options | boolean;
};
input?: ComponentStyleConfig & {
classNames?: InputProps['classNames'];
styles?: InputProps['styles'];
autoComplete?: string;
};
select?: ComponentStyleConfig & {
showSearch?: boolean;
};
pagination?: ComponentStyleConfig & { showSizeChanger?: boolean };
locale?: Locale;
pageHeader?: {
ghost: boolean;
};
componentSize?: SizeType;
componentDisabled?: boolean;
direction?: DirectionType;
space?: {
size?: SizeType | number;
className?: SpaceProps['className'];
classNames?: SpaceProps['classNames'];
style?: SpaceProps['style'];
styles?: SpaceProps['styles'];
};
virtual?: boolean;
/** @deprecated Please use `popupMatchSelectWidth` instead */
dropdownMatchSelectWidth?: boolean;
popupMatchSelectWidth?: boolean;
popupOverflow?: PopupOverflow;
theme?: ThemeConfig;
warning?: WarningContextProps;
alert?: ComponentStyleConfig;
anchor?: ComponentStyleConfig;
button?: ButtonConfig;
calendar?: ComponentStyleConfig;
carousel?: ComponentStyleConfig;
cascader?: ComponentStyleConfig;
collapse?: ComponentStyleConfig;
divider?: ComponentStyleConfig;
drawer?: DrawerConfig;
typography?: ComponentStyleConfig;
skeleton?: ComponentStyleConfig;
spin?: ComponentStyleConfig;
segmented?: ComponentStyleConfig;
statistic?: ComponentStyleConfig;
steps?: ComponentStyleConfig;
image?: ComponentStyleConfig;
layout?: ComponentStyleConfig;
list?: ComponentStyleConfig;
mentions?: ComponentStyleConfig;
modal?: ModalConfig;
progress?: ComponentStyleConfig;
result?: ComponentStyleConfig;
slider?: ComponentStyleConfig;
breadcrumb?: ComponentStyleConfig;
menu?: ComponentStyleConfig;
checkbox?: ComponentStyleConfig;
descriptions?: ComponentStyleConfig;
empty?: ComponentStyleConfig;
badge?: BadgeConfig;
radio?: ComponentStyleConfig;
rate?: ComponentStyleConfig;
switch?: ComponentStyleConfig;
transfer?: ComponentStyleConfig;
avatar?: ComponentStyleConfig;
message?: ComponentStyleConfig;
tag?: ComponentStyleConfig;
table?: ComponentStyleConfig;
card?: ComponentStyleConfig;
tabs?: ComponentStyleConfig & Pick<TabsProps, 'indicatorSize'>;
timeline?: ComponentStyleConfig;
timePicker?: ComponentStyleConfig;
upload?: ComponentStyleConfig;
notification?: ComponentStyleConfig;
tree?: ComponentStyleConfig;
colorPicker?: ComponentStyleConfig;
datePicker?: ComponentStyleConfig;
flex?: FlexConfig;
/**
* Wave is special component which only patch on the effect of component interaction.
*/
wave?: WaveConfig;
}
interface ProviderChildrenProps extends ConfigProviderProps {
parentContext: ConfigConsumerProps;
legacyLocale: Locale;
}
export const defaultPrefixCls = 'ant';
let globalPrefixCls: string;
let globalIconPrefixCls: string;
let globalTheme: ThemeConfig;
2021-09-01 10:56:50 +08:00
function getGlobalPrefixCls() {
return globalPrefixCls || defaultPrefixCls;
}
function getGlobalIconPrefixCls() {
return globalIconPrefixCls || defaultIconPrefixCls;
}
function isLegacyTheme(theme: Theme | ThemeConfig): theme is Theme {
return Object.keys(theme).some((key) => key.endsWith('Color'));
}
const setGlobalConfig = ({
prefixCls,
iconPrefixCls,
2021-09-01 10:56:50 +08:00
theme,
}: Pick<ConfigProviderProps, 'prefixCls' | 'iconPrefixCls'> & { theme?: Theme | ThemeConfig }) => {
if (prefixCls !== undefined) {
globalPrefixCls = prefixCls;
}
if (iconPrefixCls !== undefined) {
globalIconPrefixCls = iconPrefixCls;
}
2021-09-01 10:56:50 +08:00
if (theme) {
if (isLegacyTheme(theme)) {
warning(
false,
'ConfigProvider',
'`config` of css variable theme is not work in v5. Please use new `theme` config instead.',
);
registerTheme(getGlobalPrefixCls(), theme);
} else {
globalTheme = theme;
}
2021-09-01 10:56:50 +08:00
}
};
export const globalConfig = () => ({
getPrefixCls: (suffixCls?: string, customizePrefixCls?: string) => {
if (customizePrefixCls) {
return customizePrefixCls;
}
return suffixCls ? `${getGlobalPrefixCls()}-${suffixCls}` : getGlobalPrefixCls();
},
getIconPrefixCls: getGlobalIconPrefixCls,
getRootPrefixCls: () => {
// If Global prefixCls provided, use this
if (globalPrefixCls) {
return globalPrefixCls;
}
// Fallback to default prefixCls
return getGlobalPrefixCls();
},
getTheme: () => globalTheme,
});
const ProviderChildren: React.FC<ProviderChildrenProps> = (props) => {
const {
children,
csp: customCsp,
autoInsertSpaceInButton,
alert,
anchor,
form,
locale,
componentSize,
direction,
space,
virtual,
dropdownMatchSelectWidth,
popupMatchSelectWidth,
popupOverflow,
legacyLocale,
parentContext,
iconPrefixCls: customIconPrefixCls,
theme,
componentDisabled,
segmented,
statistic,
spin,
calendar,
carousel,
cascader,
collapse,
typography,
checkbox,
descriptions,
divider,
drawer,
skeleton,
steps,
image,
layout,
list,
mentions,
modal,
progress,
result,
slider,
breadcrumb,
menu,
pagination,
input,
empty,
badge,
radio,
rate,
switch: SWITCH,
transfer,
avatar,
message,
tag,
table,
card,
tabs,
timeline,
timePicker,
upload,
notification,
tree,
colorPicker,
datePicker,
flex,
wave,
warning: warningConfig,
} = props;
// =================================== Context ===================================
2021-01-13 13:02:56 +08:00
const getPrefixCls = React.useCallback(
(suffixCls: string, customizePrefixCls?: string) => {
const { prefixCls } = props;
if (customizePrefixCls) {
return customizePrefixCls;
}
2021-01-13 13:02:56 +08:00
const mergedPrefixCls = prefixCls || parentContext.getPrefixCls('');
2021-01-13 13:02:56 +08:00
return suffixCls ? `${mergedPrefixCls}-${suffixCls}` : mergedPrefixCls;
},
[parentContext.getPrefixCls, props.prefixCls],
2021-01-13 13:02:56 +08:00
);
const iconPrefixCls = customIconPrefixCls || parentContext.iconPrefixCls || defaultIconPrefixCls;
const csp = customCsp || parentContext.csp;
useStyle(iconPrefixCls, csp);
const mergedTheme = useTheme(theme, parentContext.theme);
if (process.env.NODE_ENV !== 'production') {
existThemeConfig = existThemeConfig || !!mergedTheme;
}
const baseConfig = {
2021-01-15 23:11:11 +08:00
csp,
autoInsertSpaceInButton,
alert,
anchor,
2021-01-15 23:11:11 +08:00
locale: locale || legacyLocale,
direction,
space,
virtual,
popupMatchSelectWidth: popupMatchSelectWidth ?? dropdownMatchSelectWidth,
popupOverflow,
2021-01-15 23:11:11 +08:00
getPrefixCls,
iconPrefixCls,
theme: mergedTheme,
segmented,
statistic,
spin,
calendar,
carousel,
cascader,
collapse,
typography,
checkbox,
descriptions,
divider,
drawer,
skeleton,
steps,
image,
input,
layout,
list,
mentions,
modal,
progress,
result,
slider,
breadcrumb,
menu,
pagination,
empty,
badge,
radio,
rate,
switch: SWITCH,
transfer,
avatar,
message,
tag,
table,
card,
tabs,
timeline,
timePicker,
upload,
notification,
tree,
colorPicker,
datePicker,
flex,
wave,
warning: warningConfig,
};
const config = {
...parentContext,
};
Object.keys(baseConfig).forEach((key: keyof typeof baseConfig) => {
if (baseConfig[key] !== undefined) {
(config as any)[key] = baseConfig[key];
}
});
// Pass the props used by `useContext` directly with child component.
// These props should merged into `config`.
PASSED_PROPS.forEach((propName) => {
2022-10-06 18:53:06 +08:00
const propValue = props[propName];
if (propValue) {
(config as any)[propName] = propValue;
}
});
2021-01-12 21:02:53 +08:00
// https://github.com/ant-design/ant-design/issues/27617
const memoedConfig = useMemo(
2021-01-13 13:02:56 +08:00
() => config,
config,
2022-10-06 18:53:06 +08:00
(prevConfig, currentConfig) => {
const prevKeys = Object.keys(prevConfig) as Array<keyof typeof config>;
const currentKeys = Object.keys(currentConfig) as Array<keyof typeof config>;
return (
prevKeys.length !== currentKeys.length ||
prevKeys.some((key) => prevConfig[key] !== currentConfig[key])
);
},
2021-01-12 21:02:53 +08:00
);
const memoIconContextValue = React.useMemo(
() => ({ prefixCls: iconPrefixCls, csp }),
2022-03-08 14:01:53 +08:00
[iconPrefixCls, csp],
);
let childNode = (
<>
<PropWarning dropdownMatchSelectWidth={dropdownMatchSelectWidth} />
{children}
</>
);
const validateMessages = React.useMemo(
() =>
merge(
defaultLocale.Form?.defaultValidateMessages || {},
memoedConfig.locale?.Form?.defaultValidateMessages || {},
memoedConfig.form?.validateMessages || {},
form?.validateMessages || {},
),
[memoedConfig, form?.validateMessages],
);
if (Object.keys(validateMessages).length > 0) {
childNode = (
<ValidateMessagesContext.Provider value={validateMessages}>
{childNode}
</ValidateMessagesContext.Provider>
);
}
if (locale) {
childNode = (
2021-01-12 21:02:53 +08:00
<LocaleProvider locale={locale} _ANT_MARK__={ANT_MARK}>
{childNode}
</LocaleProvider>
);
}
2022-03-08 14:01:53 +08:00
if (iconPrefixCls || csp) {
childNode = (
<IconContext.Provider value={memoIconContextValue}>{childNode}</IconContext.Provider>
);
}
if (componentSize) {
childNode = <SizeContextProvider size={componentSize}>{childNode}</SizeContextProvider>;
}
// =================================== Motion ===================================
childNode = <MotionWrapper>{childNode}</MotionWrapper>;
// ================================ Dynamic theme ================================
const memoTheme = React.useMemo(() => {
const { algorithm, token, components, ...rest } = mergedTheme || {};
const themeObj =
algorithm && (!Array.isArray(algorithm) || algorithm.length > 0)
? createTheme(algorithm)
: defaultTheme;
const parsedComponents: any = {};
Object.entries(components || {}).forEach(([componentName, componentToken]) => {
const parsedToken: typeof componentToken & { theme?: typeof defaultTheme } = {
...componentToken,
};
if ('algorithm' in parsedToken) {
if (parsedToken.algorithm === true) {
parsedToken.theme = themeObj;
} else if (
Array.isArray(parsedToken.algorithm) ||
typeof parsedToken.algorithm === 'function'
) {
parsedToken.theme = createTheme(parsedToken.algorithm);
}
delete parsedToken.algorithm;
}
parsedComponents[componentName] = parsedToken;
});
const mergedToken = {
...defaultSeedToken,
...token,
};
return {
...rest,
theme: themeObj,
token: mergedToken,
components: parsedComponents,
override: {
override: mergedToken,
...parsedComponents,
},
};
}, [mergedTheme]);
if (theme) {
childNode = (
<DesignTokenContext.Provider value={memoTheme}>{childNode}</DesignTokenContext.Provider>
);
}
// ================================== Warning ===================================
if (memoedConfig.warning) {
childNode = (
<WarningContext.Provider value={memoedConfig.warning}>{childNode}</WarningContext.Provider>
);
}
// =================================== Render ===================================
if (componentDisabled !== undefined) {
childNode = (
<DisabledContextProvider disabled={componentDisabled}>{childNode}</DisabledContextProvider>
);
}
return <ConfigContext.Provider value={memoedConfig}>{childNode}</ConfigContext.Provider>;
};
const ConfigProvider: React.FC<ConfigProviderProps> & {
/** @private internal Usage. do not use in your production */
ConfigContext: typeof ConfigContext;
/** @deprecated Please use `ConfigProvider.useConfig().componentSize` instead */
SizeContext: typeof SizeContext;
config: typeof setGlobalConfig;
useConfig: typeof useConfig;
} = (props) => {
const context = React.useContext<ConfigConsumerProps>(ConfigContext);
const antLocale = React.useContext<LocaleContextProps | undefined>(LocaleContext);
return <ProviderChildren parentContext={context} legacyLocale={antLocale!} {...props} />;
};
2018-11-26 12:06:42 +08:00
ConfigProvider.ConfigContext = ConfigContext;
ConfigProvider.SizeContext = SizeContext;
ConfigProvider.config = setGlobalConfig;
ConfigProvider.useConfig = useConfig;
Object.defineProperty(ConfigProvider, 'SizeContext', {
get: () => {
warning(
false,
'ConfigProvider',
'ConfigProvider.SizeContext is deprecated. Please use `ConfigProvider.useConfig().componentSize` instead.',
);
return SizeContext;
},
});
if (process.env.NODE_ENV !== 'production') {
ConfigProvider.displayName = 'ConfigProvider';
}
2018-12-07 20:02:01 +08:00
export default ConfigProvider;