Skip to content

Commit

Permalink
Add useEuiThemeCSSVariables syntactical sugar hook
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Aug 28, 2023
1 parent 81ebd19 commit 6f4f6b2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
23 changes: 22 additions & 1 deletion src/services/theme/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
*/

import React from 'react';
import { render } from '@testing-library/react';
import { render, act } from '@testing-library/react';
import { renderHook } from '../../test/rtl';

import { EuiProvider } from '../../components';

import { setEuiDevProviderWarning } from './warning';
import {
useEuiTheme,
UseEuiTheme,
withEuiTheme,
RenderWithEuiTheme,
useEuiThemeCSSVariables,
} from './hooks';

describe('useEuiTheme', () => {
Expand Down Expand Up @@ -82,3 +85,21 @@ describe('RenderWithEuiTheme', () => {
);
});
});

describe('useEuiThemeCSSVariables', () => {
it('returns CSS variable related state setters/getters', () => {
const { result } = renderHook(useEuiThemeCSSVariables, {
wrapper: EuiProvider,
});
expect(result.current.globalCSSVariables).toBeUndefined();
expect(result.current.themeCSSVariables).toBeUndefined();

act(() => {
result.current.setNearestThemeCSSVariables({ '--hello': 'world' });
});

// In this case, the nearest theme is the global one, so it should set both
expect(result.current.globalCSSVariables).toEqual({ '--hello': 'world' });
expect(result.current.themeCSSVariables).toEqual({ '--hello': 'world' });
});
});
21 changes: 21 additions & 0 deletions src/services/theme/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
EuiModificationsContext,
EuiColorModeContext,
defaultComputedTheme,
EuiNestedThemeContext,
} from './context';
import { emitEuiProviderWarning } from './warning';
import {
Expand Down Expand Up @@ -95,3 +96,23 @@ export const RenderWithEuiTheme = <T extends {} = {}>({
const theme = useEuiTheme<T>();
return children(theme);
};

/**
* Minor syntactical sugar hook for theme CSS variables.
* Primarily meant for internal EUI usage.
*/
export const useEuiThemeCSSVariables = () => {
const {
setGlobalCSSVariables,
globalCSSVariables,
setNearestThemeCSSVariables,
themeCSSVariables,
} = useContext(EuiNestedThemeContext);

return {
setGlobalCSSVariables,
globalCSSVariables,
setNearestThemeCSSVariables,
themeCSSVariables,
};
};
7 changes: 6 additions & 1 deletion src/services/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export {
EuiColorModeContext,
} from './context';
export type { UseEuiTheme, WithEuiThemeProps } from './hooks';
export { useEuiTheme, withEuiTheme, RenderWithEuiTheme } from './hooks';
export {
useEuiTheme,
withEuiTheme,
RenderWithEuiTheme,
useEuiThemeCSSVariables,
} from './hooks';
export type { EuiThemeProviderProps } from './provider';
export { EuiThemeProvider } from './provider';
export { getEuiDevProviderWarning, setEuiDevProviderWarning } from './warning';
Expand Down
9 changes: 4 additions & 5 deletions src/services/theme/provider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Side Public License, v 1.
*/

import React, { FunctionComponent, useContext, useEffect } from 'react';
import React, { FunctionComponent, useEffect } from 'react';
import type { Meta, StoryObj } from '@storybook/react';

import { EuiNestedThemeContext } from './context';
import { useEuiThemeCSSVariables } from './hooks';
import { EuiThemeProvider, EuiThemeProviderProps } from './provider';

const meta: Meta<EuiThemeProviderProps<{}>> = {
Expand Down Expand Up @@ -79,9 +79,8 @@ const MockComponent: FunctionComponent<{
color: string;
children: any;
}> = ({ global, color, children }) => {
const { setGlobalCSSVariables, setNearestThemeCSSVariables } = useContext(
EuiNestedThemeContext
);
const { setGlobalCSSVariables, setNearestThemeCSSVariables } =
useEuiThemeCSSVariables();

useEffect(() => {
if (global) {
Expand Down

0 comments on commit 6f4f6b2

Please sign in to comment.