-
Notifications
You must be signed in to change notification settings - Fork 220
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Describe the bug
useHostStyles internally uses useHostStyleVariables() and useHostFonts()
Inside useHostStyleVariables and useHostFonts are assigning different callback to app.onhostcontextchanged which causes overriding while using both of methods together inside useHostStyles:
// useHostFonts
app.onhostcontextchanged = (params) => {
if (params.styles?.css?.fonts) {
applyHostFonts(params.styles.css.fonts);
}
};
// useHostStyleVariables
app.onhostcontextchanged = (params) => {
if (params.theme) {
applyDocumentTheme(params.theme);
}
if (params.styles?.variables) {
applyHostStyleVariables(params.styles.variables);
}
};
// useHostStyles
export function useHostStyles(
app: App | null,
initialContext?: McpUiHostContext | null,
): void {
useHostStyleVariables(app, initialContext);
useHostFonts(app, initialContext);
}Overriding happens due to onhostcontextchanged implementation which is based on setNotificationHandler.
typescript-sdk mentions that previous handlers will be overwritten:
/**
* Registers a handler to invoke when this protocol object receives a notification with the given method.
*
* Note that this will replace any previous notification handler for the same method.
*/
setNotificationHandler<M extends NotificationMethod>(
method: M,
handler: (notification: NotificationTypeMap[M]) => void | Promise<void>
)To Reproduce
Steps to reproduce the behavior:
Try to track host style variables changes after using useHostStyles, it wont we tracked
Expected behavior
useHostStyles() works w/o overriding onhostcontextchanged callbacks
Proposal
- Instead of reusing
useHostStyleVariablesanduseHostFontsinsideuseHostStylesjust put all needed code into single callback and assign it toonhostcontextchanged.
OR
- Draft PR: feat: implement subscribe/unsubscribe methods for multiple event handlers #552
Implementsubscribe(eventName)/unsubscribe(eventName)methods for multiple handlers. ThenuseHostStyleVariablesanduseHostFontswould look like:
//useHostFonts
const unsubscribe = app.subscribe("hostcontextchanged", (params) => {
if (params.styles?.css?.fonts) {
applyHostFonts(params.styles.css.fonts);
}
});
return unsubscribe;
//useHostStyleVariables
const unsubscribe = app.subscribe("hostcontextchanged", (params) => {
if (params.theme) {
applyDocumentTheme(params.theme);
}
if (params.styles?.variables) {
applyHostStyleVariables(params.styles.variables);
}
});
return unsubscribe;Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working