Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src-docs/src/views/provider/provider_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@ export const ProviderExample = {
</EuiText>
),
},
{
title: 'Enforce usage',
text: (
<EuiText>
<p>
For complex applications with multiple mount points or template
wrappers, it may be beneficial to enable logging when components do
not have access to a parent <EuiCode>EuiProvider</EuiCode>.
</p>
<p>
<EuiCode>setEuiDevProviderWarning</EuiCode> is a function that will
enable adding logging or erroring in development mode only. It
accepts three levels:
</p>
<ul>
<li>
<EuiCode>&apos;log&apos;</EuiCode>: uses{' '}
<EuiCode>console.log</EuiCode>
</li>
<li>
<EuiCode>&apos;warn&apos;</EuiCode>: uses{' '}
<EuiCode>console.warn</EuiCode>
</li>
<li>
<EuiCode>&apos;error&apos;</EuiCode>: <EuiCode>Throw</EuiCode> an
exception
</li>
</ul>
</EuiText>
),
},
{
title: 'EuiProvider props',
wrapText: false,
Expand Down
7 changes: 6 additions & 1 deletion src/services/theme/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export const EuiModificationsContext = createContext<EuiThemeModifications>({});
export const EuiColorModeContext = createContext<EuiThemeColorModeStandard>(
DEFAULT_COLOR_MODE
);
export const defaultComputedTheme = getComputed(
EuiThemeAmsterdam,
{},
DEFAULT_COLOR_MODE
);
export const EuiThemeContext = createContext<EuiThemeComputed>(
getComputed(EuiThemeAmsterdam, {}, DEFAULT_COLOR_MODE)
defaultComputedTheme
);
26 changes: 25 additions & 1 deletion src/services/theme/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ import {
EuiThemeContext,
EuiModificationsContext,
EuiColorModeContext,
defaultComputedTheme,
} from './context';
import { getEuiDevProviderWarning } from './provider';
import {
EuiThemeColorModeStandard,
EuiThemeModifications,
EuiThemeComputed,
} from './types';

const providerMessage = `\`EuiProvider\` is missing which can result in negative effects.
Wrap your component in \`EuiProvider\`: https://ela.st/euiprovider.`;

export interface UseEuiTheme<T extends {} = {}> {
euiTheme: EuiThemeComputed<T>;
colorMode: EuiThemeColorModeStandard;
Expand All @@ -30,6 +35,25 @@ export const useEuiTheme = <T extends {} = {}>(): UseEuiTheme<T> => {
const colorMode = useContext(EuiColorModeContext);
const modifications = useContext(EuiModificationsContext);

if (process.env.NODE_ENV !== 'production') {
const isFallback = theme === defaultComputedTheme;
const warningLevel = getEuiDevProviderWarning();
if (isFallback && typeof warningLevel !== 'undefined') {
switch (warningLevel) {
case 'log':
console.log(providerMessage);
break;
case 'warn':
console.warn(providerMessage);
break;
case 'error':
throw new Error(providerMessage);
default:
break;
}
}
}

const assembledTheme = useMemo(
() => ({
euiTheme: theme as EuiThemeComputed<T>,
Expand All @@ -42,7 +66,7 @@ export const useEuiTheme = <T extends {} = {}>(): UseEuiTheme<T> => {
return assembledTheme;
};

export interface WithEuiThemeProps<P = {}> {
export interface WithEuiThemeProps<P extends {} = {}> {
theme: UseEuiTheme<P>;
}
// Provide the component props interface as the generic to allow the docs props table to populate.
Expand Down
6 changes: 5 additions & 1 deletion src/services/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export {
export type { UseEuiTheme, WithEuiThemeProps } from './hooks';
export { useEuiTheme, withEuiTheme } from './hooks';
export type { EuiThemeProviderProps } from './provider';
export { EuiThemeProvider } from './provider';
export {
EuiThemeProvider,
getEuiDevProviderWarning,
setEuiDevProviderWarning,
} from './provider';
export {
buildTheme,
computed,
Expand Down
8 changes: 7 additions & 1 deletion src/services/theme/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ import {
EuiThemeModifications,
} from './types';

type LEVELS = 'log' | 'warn' | 'error';
let providerWarning: LEVELS | undefined = undefined;
export const setEuiDevProviderWarning = (level: LEVELS) =>
(providerWarning = level);
export const getEuiDevProviderWarning = () => providerWarning;

export interface EuiThemeProviderProps<T> {
theme?: EuiThemeSystem<T>;
colorMode?: EuiThemeColorMode;
Expand Down Expand Up @@ -68,7 +74,7 @@ export const EuiThemeProvider = <T extends {} = {}>({

const [theme, setTheme] = useState(
isParentTheme.current && Object.keys(parentTheme).length
? parentTheme
? { ...parentTheme } // Intentionally create a new object to break referential equality
: getComputed(
system,
buildTheme(modifications, `_${system.key}`) as typeof system,
Expand Down
2 changes: 2 additions & 0 deletions upcoming_changelogs/6216.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Added an optional dev-mode check to log, warn, or error if a component is rendered outside of `EuiProvider`