Skip to content

Commit 79692fc

Browse files
authored
feat: useJupyterReactColormode (#394)
1 parent 3eeb603 commit 79692fc

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

packages/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@datalayer/jupyter-react",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "Jupyter React - React.js components 100% compatible with Jupyter.",
55
"license": "MIT",
66
"main": "lib/index.js",

packages/react/src/theme/JupyterReactTheme.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* MIT License
55
*/
66

7+
import React, { createContext, useContext } from 'react';
78
import { BaseStyles, ThemeProvider } from '@primer/react';
89
import { Colormode, JupyterLabCss, jupyterLabTheme } from '../theme';
910

@@ -19,6 +20,22 @@ import '@primer/primitives/dist/css/functional/size/size.css';
1920
import '@primer/primitives/dist/css/functional/size/viewport.css';
2021
import '@primer/primitives/dist/css/functional/typography/typography.css';
2122

23+
// Create context for colormode
24+
const JupyterReactColormodeContext = createContext<Colormode | undefined>(
25+
undefined
26+
);
27+
28+
// Hook to access the colormode from the context
29+
export function useJupyterReactColormode(): Colormode {
30+
const colormode = useContext(JupyterReactColormodeContext);
31+
if (colormode === undefined) {
32+
throw new Error(
33+
'useJupyterReactColormode must be used within a JupyterReactTheme provider'
34+
);
35+
}
36+
return colormode;
37+
}
38+
2239
type IJupyterLabThemeProps = {
2340
colormode?: Colormode;
2441
loadJupyterLabCss?: boolean;
@@ -35,7 +52,7 @@ export function JupyterReactTheme(
3552
theme = jupyterLabTheme,
3653
} = props;
3754
return (
38-
<>
55+
<JupyterReactColormodeContext.Provider value={colormode}>
3956
{loadJupyterLabCss && <JupyterLabCss colormode={colormode} />}
4057
<ThemeProvider
4158
theme={theme}
@@ -53,7 +70,7 @@ export function JupyterReactTheme(
5370
{children}
5471
</BaseStyles>
5572
</ThemeProvider>
56-
</>
73+
</JupyterReactColormodeContext.Provider>
5774
);
5875
}
5976

0 commit comments

Comments
 (0)