Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/client/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
PluginConfig,
PluginInstance,
PluginMessageResponse,
PluginThemeColors,
WorkbookSelection,
WorkbookVariable,
} from '../types';
Expand Down Expand Up @@ -68,6 +69,10 @@ export function initialize<T = {}>(): PluginInstance<T> {
effect();
});

on('wb:plugin:theme:update', (themeColors: PluginThemeColors) => {
pluginConfig.themeColors = themeColors;
});

function on(event: string, listener: Function) {
listeners[event] = listeners[event] || [];
listeners[event].push(listener);
Expand Down Expand Up @@ -106,6 +111,15 @@ export function initialize<T = {}>(): PluginInstance<T> {
return pluginConfig.screenshot;
},

get themeColors() {
return pluginConfig.themeColors;
},

onThemeChange(callback: (themeColors: PluginThemeColors) => void) {
on('wb:plugin:theme:update', callback);
return () => off('wb:plugin:theme:update', callback);
},

config: {
// @ts-ignore
getKey(key) {
Expand Down
19 changes: 19 additions & 0 deletions src/react/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
WorkbookElementData,
WorkbookSelection,
WorkbookVariable,
PluginThemeColors,
} from '../types';
import { deepEqual } from '../utils/deepEqual';

Expand Down Expand Up @@ -246,3 +247,21 @@ export function useActionEffect(configId: string, effect: () => void) {
return client.config.registerEffect(configId, effectRef.current);
}, [client, configId, effect]);
}

/**
* React hook for accessing workbook theme colors with live updates
* @returns {PluginThemeColors | undefined} Theme colors from the workbook if available
*/
export function useThemeColors(): PluginThemeColors | undefined {
const client = usePlugin();
const [themeColors, setThemeColors] = useState(client.themeColors);

useEffect(() => {
// Initial sync
setThemeColors(client.themeColors);

return client.onThemeChange(setThemeColors);
}, [client]);

return themeColors;
}
25 changes: 25 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@ export interface PluginConfig<T> {
id: string;
config: T;
screenshot: boolean;
themeColors?: PluginThemeColors;
[key: string]: any;
}

/**
* Theme colors available to plugins
* @typedef {object} PluginThemeColors
* @property {string} backgroundColor Background color from workbook theme
* @property {string} textColor Primary text color from workbook theme
*/
export interface PluginThemeColors {
backgroundColor: string;
textColor: string;
}

/**
* @typedef {object} WorkbookVariable
* @property {string} name Name of control variable within workbook
Expand Down Expand Up @@ -189,6 +201,19 @@ export type CustomPluginConfigOptions =
export interface PluginInstance<T = any> {
sigmaEnv: 'author' | 'viewer' | 'explorer';

/**
* Theme colors from the workbook
* @returns {PluginThemeColors | undefined} Theme colors if available
*/
themeColors?: PluginThemeColors | undefined;

/**
* Listen to theme color changes
* @param {Function} callback Function to call when theme colors change
* @returns {Function} Unsubscriber function
*/
onThemeChange(callback: (themeColors: PluginThemeColors) => void): () => void;

config: {
/**
* Getter for entire Plugin Config
Expand Down