Skip to content

Commit

Permalink
Backport PR jupyterlab#575: Reflect theme changes without a refresh (j…
Browse files Browse the repository at this point in the history
…upyterlab#599)

* Reflect theme changes without a refresh

By leveraging `IThemeManager.themeChanged`, we can listen on theme change signals
and rebuild the theme object in response. This allows CSS variable changes to reflect
in the MUI theme without having to refresh the page.

* update yarn.lock

* pass themeManager as a prop instead of using top-level scope

* remove theme hack added in jupyterlab#192

---------

Co-authored-by: gchow <[email protected]>
Co-authored-by: David L. Qiu <[email protected]>
(cherry picked from commit ca03e2c)

Co-authored-by: Garson R Chow <[email protected]>
  • Loading branch information
dlqqq and garsonbyte authored Jan 23, 2024
1 parent 660231b commit f83b576
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
4 changes: 3 additions & 1 deletion packages/jupyter-ai/src/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Button, IconButton, Stack } from '@mui/material';
import SettingsIcon from '@mui/icons-material/Settings';
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import type { Awareness } from 'y-protocols/awareness';
import type { IThemeManager } from '@jupyterlab/apputils';

import { JlThemeProvider } from './jl-theme-provider';
import { ChatMessages } from './chat-messages';
Expand Down Expand Up @@ -178,6 +179,7 @@ export type ChatProps = {
selectionWatcher: SelectionWatcher;
chatHandler: ChatHandler;
globalAwareness: Awareness | null;
themeManager: IThemeManager | null;
chatView?: ChatView;
};

Expand All @@ -190,7 +192,7 @@ export function Chat(props: ChatProps): JSX.Element {
const [view, setView] = useState<ChatView>(props.chatView || ChatView.Chat);

return (
<JlThemeProvider>
<JlThemeProvider themeManager={props.themeManager}>
<SelectionContextProvider selectionWatcher={props.selectionWatcher}>
<CollaboratorsContextProvider globalAwareness={props.globalAwareness}>
<Box
Expand Down
6 changes: 5 additions & 1 deletion packages/jupyter-ai/src/components/jl-theme-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { useState, useEffect } from 'react';

import type { IThemeManager } from '@jupyterlab/apputils';
import { Theme, ThemeProvider, createTheme } from '@mui/material/styles';

import { getJupyterLabTheme } from '../theme-provider';

export function JlThemeProvider(props: {
themeManager: IThemeManager | null;
children: React.ReactNode;
}): JSX.Element {
const [theme, setTheme] = useState<Theme>(createTheme());
Expand All @@ -12,7 +14,9 @@ export function JlThemeProvider(props: {
async function setJlTheme() {
setTheme(await getJupyterLabTheme());
}

setJlTheme();
props.themeManager?.themeChanged.connect(setJlTheme);
}, []);

return <ThemeProvider theme={theme}>{props.children}</ThemeProvider>;
Expand Down
16 changes: 11 additions & 5 deletions packages/jupyter-ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
ILayoutRestorer
} from '@jupyterlab/application';

import { IWidgetTracker, ReactWidget } from '@jupyterlab/apputils';
import {
IWidgetTracker,
ReactWidget,
IThemeManager
} from '@jupyterlab/apputils';
import { IDocumentWidget } from '@jupyterlab/docregistry';
import { IGlobalAwareness } from '@jupyterlab/collaboration';
import type { Awareness } from 'y-protocols/awareness';
Expand All @@ -21,11 +25,12 @@ export type DocumentTracker = IWidgetTracker<IDocumentWidget>;
const plugin: JupyterFrontEndPlugin<void> = {
id: 'jupyter_ai:plugin',
autoStart: true,
optional: [IGlobalAwareness, ILayoutRestorer],
optional: [IGlobalAwareness, ILayoutRestorer, IThemeManager],
activate: async (
app: JupyterFrontEnd,
globalAwareness: Awareness | null,
restorer: ILayoutRestorer
restorer: ILayoutRestorer | null,
themeManager: IThemeManager | null
) => {
/**
* Initialize selection watcher singleton
Expand All @@ -43,10 +48,11 @@ const plugin: JupyterFrontEndPlugin<void> = {
chatWidget = buildChatSidebar(
selectionWatcher,
chatHandler,
globalAwareness
globalAwareness,
themeManager
);
} catch (e) {
chatWidget = buildErrorWidget();
chatWidget = buildErrorWidget(themeManager);
}

/**
Expand Down
8 changes: 1 addition & 7 deletions packages/jupyter-ai/src/theme-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export async function pollUntilReady(): Promise<void> {
export async function getJupyterLabTheme(): Promise<Theme> {
await pollUntilReady();
const light = document.body.getAttribute('data-jp-theme-light');
const primaryFontColor = getCSSVariable('--jp-ui-font-color1');
return createTheme({
spacing: 4,
components: {
Expand Down Expand Up @@ -113,7 +112,7 @@ export async function getJupyterLabTheme(): Promise<Theme> {
dark: getCSSVariable('--jp-success-color0')
},
text: {
primary: primaryFontColor,
primary: getCSSVariable('--jp-ui-font-color1'),
secondary: getCSSVariable('--jp-ui-font-color2'),
disabled: getCSSVariable('--jp-ui-font-color3')
}
Expand All @@ -127,11 +126,6 @@ export async function getJupyterLabTheme(): Promise<Theme> {
htmlFontSize: 16,
button: {
textTransform: 'capitalize'
},
// this is undocumented as of the time of writing.
// https://stackoverflow.com/a/62950304/12548458
allVariants: {
color: primaryFontColor
}
}
});
Expand Down
9 changes: 6 additions & 3 deletions packages/jupyter-ai/src/widgets/chat-error.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import React from 'react';
import { ReactWidget } from '@jupyterlab/apputils';
import type { IThemeManager } from '@jupyterlab/apputils';
import { Alert, Box } from '@mui/material';

import { chatIcon } from '../icons';
import { Alert, Box } from '@mui/material';
import { JlThemeProvider } from '../components/jl-theme-provider';

export function buildErrorWidget(): ReactWidget {
export function buildErrorWidget(
themeManager: IThemeManager | null
): ReactWidget {
const ErrorWidget = ReactWidget.create(
<JlThemeProvider>
<JlThemeProvider themeManager={themeManager}>
<Box
sx={{
width: '100%',
Expand Down
5 changes: 4 additions & 1 deletion packages/jupyter-ai/src/widgets/chat-sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { ReactWidget } from '@jupyterlab/apputils';
import type { IThemeManager } from '@jupyterlab/apputils';
import type { Awareness } from 'y-protocols/awareness';

import { Chat } from '../components/chat';
Expand All @@ -10,13 +11,15 @@ import { ChatHandler } from '../chat_handler';
export function buildChatSidebar(
selectionWatcher: SelectionWatcher,
chatHandler: ChatHandler,
globalAwareness: Awareness | null
globalAwareness: Awareness | null,
themeManager: IThemeManager | null
): ReactWidget {
const ChatWidget = ReactWidget.create(
<Chat
selectionWatcher={selectionWatcher}
chatHandler={chatHandler}
globalAwareness={globalAwareness}
themeManager={themeManager}
/>
);
ChatWidget.id = 'jupyter-ai::chat';
Expand Down

0 comments on commit f83b576

Please sign in to comment.