-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathstyles.examples.ts
More file actions
139 lines (127 loc) · 3.63 KB
/
Copy pathstyles.examples.ts
File metadata and controls
139 lines (127 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Type-checked examples for style utilities in {@link ./styles.ts `styles.ts`}.
*
* These examples are included in the API documentation via `@includeCode` tags.
* Each function's region markers define the code snippet that appears in the docs.
*
* @module
*/
import { App } from "./app.js";
import {
getDocumentTheme,
applyDocumentTheme,
applyHostStyleVariables,
applyHostFonts,
} from "./styles.js";
/**
* Example: Check current theme.
*/
function getDocumentTheme_checkCurrent() {
//#region getDocumentTheme_checkCurrent
const theme = getDocumentTheme();
document.body.classList.toggle("dark", theme === "dark");
//#endregion getDocumentTheme_checkCurrent
}
/**
* Example: Apply theme from host context.
*/
function applyDocumentTheme_fromHostContext(app: App) {
//#region applyDocumentTheme_fromHostContext
// Apply when host context changes
app.onhostcontextchanged = (ctx) => {
if (ctx.theme) {
applyDocumentTheme(ctx.theme);
}
};
// Apply initial theme after connecting
app.connect().then(() => {
const ctx = app.getHostContext();
if (ctx?.theme) {
applyDocumentTheme(ctx.theme);
}
});
//#endregion applyDocumentTheme_fromHostContext
}
/**
* Example: Apply style variables from host context.
*/
function applyHostStyleVariables_fromHostContext(app: App) {
//#region applyHostStyleVariables_fromHostContext
// Use CSS variables in your styles
document.body.style.background = "var(--color-background-primary)";
// Apply when host context changes
app.onhostcontextchanged = (ctx) => {
if (ctx.styles?.variables) {
applyHostStyleVariables(ctx.styles.variables);
}
};
// Apply initial styles after connecting
app.connect().then(() => {
const ctx = app.getHostContext();
if (ctx?.styles?.variables) {
applyHostStyleVariables(ctx.styles.variables);
}
});
//#endregion applyHostStyleVariables_fromHostContext
}
/**
* Example: Apply to a specific element.
*/
function applyHostStyleVariables_toElement(app: App) {
//#region applyHostStyleVariables_toElement
app.onhostcontextchanged = (ctx) => {
const container = document.getElementById("app-root");
if (container && ctx.styles?.variables) {
applyHostStyleVariables(ctx.styles.variables, container);
}
};
//#endregion applyHostStyleVariables_toElement
}
/**
* Example: Apply fonts from host context.
*/
function applyHostFonts_fromHostContext(app: App) {
//#region applyHostFonts_fromHostContext
// Apply when host context changes
app.onhostcontextchanged = (ctx) => {
if (ctx.styles?.css?.fonts) {
applyHostFonts(ctx.styles.css.fonts);
}
};
// Apply initial fonts after connecting
app.connect().then(() => {
const ctx = app.getHostContext();
if (ctx?.styles?.css?.fonts) {
applyHostFonts(ctx.styles.css.fonts);
}
});
//#endregion applyHostFonts_fromHostContext
}
/**
* Example: Host providing self-hosted fonts.
*/
function applyHostFonts_selfHosted() {
//#region applyHostFonts_selfHosted
// Example of what a host might provide:
const fontCss = `
@font-face {
font-family: "Anthropic Sans";
src: url("https://assets.anthropic.com/.../Regular.otf") format("opentype");
font-weight: 400;
}
`;
applyHostFonts(fontCss);
//#endregion applyHostFonts_selfHosted
}
/**
* Example: Host providing Google Fonts.
*/
function applyHostFonts_googleFonts() {
//#region applyHostFonts_googleFonts
// Example of what a host might provide:
const fontCss = `
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
`;
applyHostFonts(fontCss);
//#endregion applyHostFonts_googleFonts
}