Skip to content

Commit 578de0b

Browse files
mgadewollcee-chen
authored andcommitted
docs: ensure high contrast mode works in EUI+
- enables workspace EUI version for docusaurus-theme - fixes theme vs colorMode setting on EuiProvider in EUI+
1 parent 03229db commit 578de0b

4 files changed

Lines changed: 33 additions & 67 deletions

File tree

packages/docusaurus-theme/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@docusaurus/theme-common": "^3.5.2",
3535
"@docusaurus/utils-validation": "^3.5.2",
3636
"@elastic/datemath": "^5.0.3",
37-
"@elastic/eui": "97.2.0",
37+
"@elastic/eui": "workspace:^",
3838
"@elastic/eui-docgen": "workspace:^",
3939
"@emotion/css": "^11.11.2",
4040
"@emotion/react": "^11.11.4",

packages/docusaurus-theme/src/components/theme_context/index.tsx

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import { EUI_THEMES, EuiProvider, EuiThemeColorMode } from '@elastic/eui';
99

1010
import { EuiThemeOverrides } from './theme_overrides';
1111

12-
const EUI_THEME_NAMES = EUI_THEMES.map(
13-
({ value }) => value
14-
) as EuiThemeColorMode[];
12+
const EUI_THEME_NAMES = EUI_THEMES.map(({ value }) => value);
1513

1614
const defaultState = {
17-
theme: EUI_THEME_NAMES[0] as EuiThemeColorMode,
15+
theme: EUI_THEME_NAMES[0] as string,
16+
colorMode: 'LIGHT' as EuiThemeColorMode,
1817
changeTheme: (themeValue: EuiThemeColorMode) => {},
18+
changeColorMode: (colorMode: EuiThemeColorMode) => {},
1919
};
2020

2121
export const AppThemeContext = createContext(defaultState);
@@ -24,25 +24,42 @@ export const AppThemeProvider: FunctionComponent<PropsWithChildren> = ({
2424
children,
2525
}) => {
2626
const isBrowser = useIsBrowser();
27-
const [theme, setTheme] = useState<EuiThemeColorMode>(() => {
27+
28+
const [theme, setTheme] = useState<string>(() => {
2829
if (isBrowser) {
29-
return localStorage.getItem('theme') as EuiThemeColorMode ?? defaultState.theme;
30+
return (
31+
(localStorage.getItem('theme') as EuiThemeColorMode) ??
32+
defaultState.theme
33+
);
3034
}
3135

3236
return defaultState.theme;
3337
});
3438

39+
const [colorMode, setColorMode] = useState<EuiThemeColorMode>(() => {
40+
if (isBrowser) {
41+
return (
42+
(localStorage.getItem('colorMode') as EuiThemeColorMode) ??
43+
defaultState.colorMode
44+
);
45+
}
46+
47+
return defaultState.colorMode;
48+
});
49+
3550
return (
3651
<AppThemeContext.Provider
3752
value={{
38-
theme,
53+
theme: theme,
54+
colorMode,
3955
changeTheme: setTheme,
56+
changeColorMode: setColorMode,
4057
}}
4158
>
4259
<EuiProvider
4360
globalStyles={false}
4461
modify={EuiThemeOverrides}
45-
colorMode={theme}
62+
colorMode={colorMode}
4663
>
4764
{children}
4865
</EuiProvider>

packages/docusaurus-theme/src/theme/ColorModeToggle/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ function ColorModeToggle({
1717
onChange,
1818
...rest
1919
}: WrappedProps): JSX.Element {
20-
const { theme, changeTheme } = useContext(AppThemeContext);
20+
const { colorMode, changeColorMode } = useContext(AppThemeContext);
2121

2222
useEffect(() => {
23-
changeTheme(value);
23+
changeColorMode;
2424
}, []);
2525

26-
const handleOnChange = (themeName: EuiThemeColorMode) => {
27-
changeTheme(themeName);
28-
onChange?.(themeName);
26+
const handleOnChange = (colorMode: EuiThemeColorMode) => {
27+
changeColorMode(colorMode);
28+
onChange?.(colorMode);
2929
};
3030

3131
return (
3232
<OriginalColorModeToggle
33-
value={theme}
33+
value={colorMode}
3434
onChange={handleOnChange}
3535
{...rest}
3636
/>

yarn.lock

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4609,7 +4609,7 @@ __metadata:
46094609
"@docusaurus/types": "npm:^3.5.2"
46104610
"@docusaurus/utils-validation": "npm:^3.5.2"
46114611
"@elastic/datemath": "npm:^5.0.3"
4612-
"@elastic/eui": "npm:97.2.0"
4612+
"@elastic/eui": "workspace:^"
46134613
"@elastic/eui-docgen": "workspace:^"
46144614
"@emotion/css": "npm:^11.11.2"
46154615
"@emotion/react": "npm:^11.11.4"
@@ -4676,57 +4676,6 @@ __metadata:
46764676
languageName: unknown
46774677
linkType: soft
46784678

4679-
"@elastic/eui@npm:97.2.0":
4680-
version: 97.2.0
4681-
resolution: "@elastic/eui@npm:97.2.0"
4682-
dependencies:
4683-
"@hello-pangea/dnd": "npm:^16.6.0"
4684-
"@types/lodash": "npm:^4.14.202"
4685-
"@types/numeral": "npm:^2.0.5"
4686-
"@types/react-window": "npm:^1.8.8"
4687-
"@types/refractor": "npm:^3.4.0"
4688-
chroma-js: "npm:^2.4.2"
4689-
classnames: "npm:^2.5.1"
4690-
lodash: "npm:^4.17.21"
4691-
mdast-util-to-hast: "npm:^10.2.0"
4692-
numeral: "npm:^2.0.6"
4693-
prop-types: "npm:^15.8.1"
4694-
react-dropzone: "npm:^11.7.1"
4695-
react-element-to-jsx-string: "npm:^15.0.0"
4696-
react-focus-on: "npm:^3.9.1"
4697-
react-is: "npm:^17.0.2"
4698-
react-remove-scroll-bar: "npm:^2.3.4"
4699-
react-virtualized-auto-sizer: "npm:^1.0.24"
4700-
react-window: "npm:^1.8.10"
4701-
refractor: "npm:^3.6.0"
4702-
rehype-raw: "npm:^5.1.0"
4703-
rehype-react: "npm:^6.2.1"
4704-
rehype-stringify: "npm:^8.0.0"
4705-
remark-breaks: "npm:^2.0.2"
4706-
remark-emoji: "npm:^2.1.0"
4707-
remark-parse-no-trim: "npm:^8.0.4"
4708-
remark-rehype: "npm:^8.1.0"
4709-
tabbable: "npm:^5.3.3"
4710-
text-diff: "npm:^1.0.1"
4711-
unified: "npm:^9.2.2"
4712-
unist-util-visit: "npm:^2.0.3"
4713-
url-parse: "npm:^1.5.10"
4714-
uuid: "npm:^8.3.0"
4715-
vfile: "npm:^4.2.1"
4716-
peerDependencies:
4717-
"@elastic/datemath": ^5.0.2
4718-
"@emotion/css": 11.x
4719-
"@emotion/react": 11.x
4720-
"@types/react": ^16.9 || ^17.0 || ^18.0
4721-
"@types/react-dom": ^16.9 || ^17.0 || ^18.0
4722-
moment: ^2.13.0
4723-
react: ^16.12 || ^17.0 || ^18.0
4724-
react-dom: ^16.12 || ^17.0 || ^18.0
4725-
typescript: ~4.5.3 || ^5
4726-
checksum: 10c0/c8fed61d39cc6d06f00caf8234b97e9579ca4d11d7bf0d3cf4cb5d09a6782d8866a7db66b02d3d5eb4f2947a213aca03837b38b166020d21c9bda2e7eb20ad11
4727-
languageName: node
4728-
linkType: hard
4729-
47304679
"@elastic/eui@workspace:^, @elastic/eui@workspace:packages/eui":
47314680
version: 0.0.0-use.local
47324681
resolution: "@elastic/eui@workspace:packages/eui"

0 commit comments

Comments
 (0)