Complete "Reacting to light/dark theme changes" pattern documentation
Reference existing JSDoc examples instead of maintaining a separate one in `docs/patterns.ts`: - Vanilla JS: `applyDocumentTheme_fromHostContext` from `styles.examples.ts` - React: `useDocumentTheme_conditionalRender` for reactive theme reading Expand `applyDocumentTheme_fromHostContext` example to show both the `onhostcontextchanged` handler and initial theme application after `connect()`. Note in the docs that `useHostStyles()` applies theme automatically, so React apps don't need additional setup unless they want to read the theme reactively for conditional rendering. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
fb5e96a719
commit
77306d1aaf
1 changed files with 20 additions and 20 deletions
|
|
@ -21,7 +21,7 @@ import {
|
|||
function getDocumentTheme_checkCurrent() {
|
||||
//#region getDocumentTheme_checkCurrent
|
||||
const theme = getDocumentTheme();
|
||||
const isDark = theme === "dark";
|
||||
document.body.classList.toggle("dark", theme === "dark");
|
||||
//#endregion getDocumentTheme_checkCurrent
|
||||
}
|
||||
|
||||
|
|
@ -30,11 +30,20 @@ function getDocumentTheme_checkCurrent() {
|
|||
*/
|
||||
function applyDocumentTheme_fromHostContext(app: App) {
|
||||
//#region applyDocumentTheme_fromHostContext
|
||||
// Apply when host context changes
|
||||
app.onhostcontextchanged = (params) => {
|
||||
if (params.theme) {
|
||||
applyDocumentTheme(params.theme);
|
||||
}
|
||||
};
|
||||
|
||||
// Apply initial theme after connecting
|
||||
app.connect().then(() => {
|
||||
const ctx = app.getHostContext();
|
||||
if (ctx?.theme) {
|
||||
applyDocumentTheme(ctx.theme);
|
||||
}
|
||||
});
|
||||
//#endregion applyDocumentTheme_fromHostContext
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue