Skip to content

Commit b11330f

Browse files
authored
Adds usePluginStyle hook and PluginStyle type (#29)
1 parent 496cbc5 commit b11330f

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/client/initialize.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
PluginConfig,
44
PluginInstance,
55
PluginMessageResponse,
6+
PluginStyle,
67
WorkbookSelection,
78
WorkbookVariable,
89
} from '../types';
@@ -223,6 +224,18 @@ export function initialize<T = {}>(): PluginInstance<T> {
223224
void execPromise('wb:plugin:element:fetch-more', configId);
224225
},
225226
},
227+
228+
style: {
229+
subscribe(callback: (style: PluginStyle) => void) {
230+
on('wb:plugin:style:update', callback);
231+
return () => off('wb:plugin:style:update', callback);
232+
},
233+
234+
get() {
235+
return execPromise('wb:plugin:style:get');
236+
},
237+
},
238+
226239
destroy() {
227240
Object.keys(listeners).forEach(event => delete listeners[event]);
228241
window.removeEventListener('message', listener, false);

src/react/hooks.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
WorkbookElementData,
99
WorkbookSelection,
1010
WorkbookVariable,
11+
PluginStyle,
1112
} from '../types';
1213
import { deepEqual } from '../utils/deepEqual';
1314

@@ -246,3 +247,20 @@ export function useActionEffect(configId: string, effect: () => void) {
246247
return client.config.registerEffect(configId, effectRef.current);
247248
}, [client, configId, effect]);
248249
}
250+
251+
/**
252+
* React hook for accessing plugin style with live updates
253+
* @returns {PluginStyle | undefined} Style properties from the workbook if available
254+
*/
255+
export function usePluginStyle(): PluginStyle | undefined {
256+
const client = usePlugin();
257+
const [style, setStyle] = useState<PluginStyle | undefined>();
258+
259+
useEffect(() => {
260+
// Request initial style data on mount and subscribe to updates
261+
void client.style.get().then(response => setStyle(response));
262+
return client.style.subscribe(setStyle);
263+
}, [client]);
264+
265+
return style;
266+
}

src/types.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ export interface PluginConfig<T> {
2323
[key: string]: any;
2424
}
2525

26+
/**
27+
* Style colors available to plugins
28+
* @typedef {object} PluginStyle
29+
* @property {string} backgroundColor Background color set from workbook if any
30+
*/
31+
export interface PluginStyle {
32+
backgroundColor: string;
33+
}
34+
2635
/**
2736
* @typedef {object} WorkbookVariable
2837
* @property {string} name Name of control variable within workbook
@@ -348,6 +357,21 @@ export interface PluginInstance<T = any> {
348357
fetchMoreElementData(configId: string): void;
349358
};
350359

360+
style: {
361+
/**
362+
* Subscribe to style updates
363+
* @param callback Function to call when style updates
364+
* @returns Unsubscriber function
365+
*/
366+
subscribe(callback: (style: PluginStyle) => void): () => void;
367+
368+
/**
369+
* Request current style from workbook
370+
* @returns Promise with current style
371+
*/
372+
get(): Promise<PluginStyle>;
373+
};
374+
351375
/**
352376
* Destroys plugin instance and removes all subscribers
353377
*/

0 commit comments

Comments
 (0)