Skip to content

Commit 0ac2bb8

Browse files
committed
Export helper to react to JLab theme change
1 parent 783de9e commit 0ac2bb8

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

packages/components/src/index.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Jupyter Development Team.
22
// Distributed under the terms of the Modified BSD License.
33

4-
import { initThemeChangeListener } from './utilities/theme/applyTheme';
4+
export { addJupyterLabThemeChangeListener } from './utilities/theme/applyTheme';
55

66
export * from './design-token';
77
export * from './jupyter-design-system';
@@ -20,13 +20,3 @@ export * from './tab-panel/index';
2020
export * from './tab/index';
2121
export * from './tabs/index';
2222
export * from './text-field/index';
23-
24-
// Add Jupyter theme change listener
25-
{
26-
let isThemeChangeInitialized = false;
27-
28-
if (!isThemeChangeInitialized) {
29-
isThemeChangeInitialized = true;
30-
initThemeChangeListener();
31-
}
32-
}

packages/components/src/utilities/theme/applyTheme.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const sum = (a, b) => {
66
return a + b;
77
};
88

9+
// FIXME
910
test('temporary test', () => {
1011
expect(sum(1, 2)).toBe(3);
1112
});

packages/components/src/utilities/theme/applyTheme.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,23 @@ import { DesignToken } from '@microsoft/fast-foundation';
2020
const THEME_NAME_BODY_ATTRIBUTE = 'data-jp-theme-name';
2121
const THEME_MODE_BODY_ATTRIBUTE = 'data-jp-theme-light';
2222

23+
/**
24+
* Flag to initialized only one listener
25+
*/
26+
let isThemeChangeInitialized = false;
27+
2328
/**
2429
* Configures a MutationObserver to watch for Jupyter theme changes and
2530
* applies the current Jupyter theme to the toolkit components.
2631
*/
27-
export function initThemeChangeListener(): void {
32+
export function addJupyterLabThemeChangeListener(): void {
33+
if (!isThemeChangeInitialized) {
34+
isThemeChangeInitialized = true;
35+
initThemeChangeListener();
36+
}
37+
}
38+
39+
function initThemeChangeListener(): void {
2840
const addObserver = () => {
2941
const observer = new MutationObserver(() => {
3042
applyCurrentTheme();
@@ -46,11 +58,26 @@ export function initThemeChangeListener(): void {
4658
}
4759
}
4860

61+
/**
62+
* JupyterLab CSS variable to Design Token converter
63+
*/
4964
interface IConverter<T> {
65+
/**
66+
* Convert the CSS variable value to design token value
67+
*/
5068
converter?: (value: string) => T | null;
69+
/**
70+
* Design token to update
71+
*/
5172
token: DesignToken<T>;
5273
}
5374

75+
/**
76+
* Convert a base color to a palette.
77+
*
78+
* @param value Color string
79+
* @returns The palette generated from the color
80+
*/
5481
const colorConverter = (value: string): Palette<Swatch> | null => {
5582
const parsedColor = parseColor(value);
5683

@@ -61,6 +88,12 @@ const colorConverter = (value: string): Palette<Swatch> | null => {
6188
: null;
6289
};
6390

91+
/**
92+
* Convert a string to an integer.
93+
*
94+
* @param value String to convert
95+
* @returns Extracted integer or null
96+
*/
6497
const intConverter = (value: string): number | null => {
6598
const parsedValue = parseInt(value, 10);
6699
return isNaN(parsedValue) ? null : parsedValue;

packages/lab-example/src/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
TextField
1212
} from '@jupyter-notebook/react-components';
1313
import {
14+
addJupyterLabThemeChangeListener,
1415
allComponents,
1516
provideJupyterDesignSystem
1617
} from '@jupyter-notebook/web-components';
@@ -23,6 +24,7 @@ import { Widget } from '@lumino/widgets';
2324
import React from 'react';
2425

2526
provideJupyterDesignSystem().register(allComponents);
27+
addJupyterLabThemeChangeListener();
2628

2729
/**
2830
* Initialization data for the jupyter-ui-demo extension.

0 commit comments

Comments
 (0)