Skip to content
Merged
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
13 changes: 13 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,
PluginStyle,
WorkbookSelection,
WorkbookVariable,
} from '../types';
Expand Down Expand Up @@ -223,6 +224,18 @@ export function initialize<T = {}>(): PluginInstance<T> {
void execPromise('wb:plugin:element:fetch-more', configId);
},
},

style: {
subscribe(callback: (style: PluginStyle) => void) {
on('wb:plugin:style:update', callback);
return () => off('wb:plugin:style:update', callback);
},

get() {
return execPromise('wb:plugin:style:get');
},
},

destroy() {
Object.keys(listeners).forEach(event => delete listeners[event]);
window.removeEventListener('message', listener, false);
Expand Down
18 changes: 18 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,
PluginStyle,
} from '../types';
import { deepEqual } from '../utils/deepEqual';

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

/**
* React hook for accessing plugin style with live updates
* @returns {PluginStyle | undefined} Style properties from the workbook if available
*/
export function usePluginStyle(): PluginStyle | undefined {
const client = usePlugin();
const [style, setStyle] = useState<PluginStyle | undefined>();

useEffect(() => {
// Request initial style data on mount and subscribe to updates
void client.style.get().then(response => setStyle(response));
return client.style.subscribe(setStyle);
}, [client]);

return style;
}
24 changes: 24 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export interface PluginConfig<T> {
[key: string]: any;
}

/**
* Style colors available to plugins
* @typedef {object} PluginStyle
* @property {string} backgroundColor Background color set from workbook if any
*/
export interface PluginStyle {
backgroundColor: string;
}

/**
* @typedef {object} WorkbookVariable
* @property {string} name Name of control variable within workbook
Expand Down Expand Up @@ -348,6 +357,21 @@ export interface PluginInstance<T = any> {
fetchMoreElementData(configId: string): void;
};

style: {
/**
* Subscribe to style updates
* @param callback Function to call when style updates
* @returns Unsubscriber function
*/
subscribe(callback: (style: PluginStyle) => void): () => void;

/**
* Request current style from workbook
* @returns Promise with current style
*/
get(): Promise<PluginStyle>;
};

/**
* Destroys plugin instance and removes all subscribers
*/
Expand Down