Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
12 changes: 12 additions & 0 deletions src/client/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ export function initialize<T = {}>(): PluginInstance<T> {
void execPromise('wb:plugin:element:fetch-more', configId);
},
},

style: {
subscribeToStyle(callback: (style: any) => void) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super small, can we just call these subscribe and get? Makes more sense to do style.subscribe() than style.subscribeToStyle()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change getStyle to get?

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

getStyle() {
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>(undefined);
Comment thread
alisonjlai marked this conversation as resolved.
Outdated

useEffect(() => {
// Request initial style data on mount and subscribe to updates
void client.style.getStyle().then(setStyle);
Comment thread
alisonjlai marked this conversation as resolved.
Outdated
return client.style.subscribeToStyle(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;
Comment thread
alisonjlai marked this conversation as resolved.
Outdated
}

/**
* @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
*/
subscribeToStyle(callback: (style: PluginStyle) => void): () => void;

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

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