igniteui-astro-components 0.0.21 → 0.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/integration.ts +1 -44
- package/src/layouts/DocsLayout/DocsLayout.astro +1 -2
- package/src/scripts/sample-widget.ts +110 -28
- package/src/styles/global/_search-shared.scss +4 -0
- package/src/styles/global/_variables.scss +4 -4
- package/src/virtual-modules.d.ts +0 -2
- package/src/components/ThemingWidget/README.md +0 -35
- package/src/components/ThemingWidget/ThemingWidget.astro +0 -143
- package/src/components/ThemingWidget/ThemingWidget.scss +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "igniteui-astro-components",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.23",
|
|
4
4
|
"description": "Reusable Astro components, layouts, and integrations for Infragistics / Ignite UI documentation sites. Replaces the Starlight defaults with project-specific implementations.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
package/src/integration.ts
CHANGED
|
@@ -72,38 +72,6 @@ import { remarkHtmlTransforms } from './plugins/remark-html-transforms';
|
|
|
72
72
|
/** Build / deployment mode. Drives env-var `DOCS_BUILD_MODE`. */
|
|
73
73
|
export type DocsMode = 'development' | 'staging' | 'production';
|
|
74
74
|
|
|
75
|
-
/**
|
|
76
|
-
* Read `themeApiUrl` and `themingWidgetVersion` from the project's
|
|
77
|
-
* environment.json at build time. Uses the same lookup order as remark-env-vars.ts
|
|
78
|
-
* so both always resolve from the same file.
|
|
79
|
-
*/
|
|
80
|
-
function readThemingEnv(sourcePath: string | undefined, envKey: string): {
|
|
81
|
-
themeApiUrl: string;
|
|
82
|
-
widgetVersion: string;
|
|
83
|
-
} {
|
|
84
|
-
if (!sourcePath) return { themeApiUrl: '', widgetVersion: 'latest' };
|
|
85
|
-
const root = path.resolve(sourcePath);
|
|
86
|
-
const parent = path.dirname(root);
|
|
87
|
-
const candidates = [
|
|
88
|
-
path.join(root, 'en', 'environment.json'),
|
|
89
|
-
path.join(root, 'environment.json'),
|
|
90
|
-
path.join(parent, 'environment.json'),
|
|
91
|
-
path.join(parent, 'en', 'environment.json'),
|
|
92
|
-
];
|
|
93
|
-
const envPath = candidates.find(c => fs.existsSync(c));
|
|
94
|
-
if (!envPath) return { themeApiUrl: '', widgetVersion: 'latest' };
|
|
95
|
-
try {
|
|
96
|
-
const data = JSON.parse(fs.readFileSync(envPath, 'utf-8'));
|
|
97
|
-
const env = data[envKey] ?? data.production ?? {};
|
|
98
|
-
return {
|
|
99
|
-
themeApiUrl: (env.themeApiUrl as string) ?? '',
|
|
100
|
-
widgetVersion: (env.themingWidgetVersion as string) ?? 'latest',
|
|
101
|
-
};
|
|
102
|
-
} catch {
|
|
103
|
-
return { themeApiUrl: '', widgetVersion: 'latest' };
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
75
|
// ---------------------------------------------------------------------------
|
|
108
76
|
// Site meta virtual module
|
|
109
77
|
// ---------------------------------------------------------------------------
|
|
@@ -259,18 +227,7 @@ export const navLang = ${JSON.stringify(navLang)};
|
|
|
259
227
|
if (id === resolvedId) return siteMetaCode;
|
|
260
228
|
if (id !== navResolvedId) return;
|
|
261
229
|
|
|
262
|
-
|
|
263
|
-
const envKey = process.env.DOCS_ENV ?? process.env.NODE_ENV ?? 'production';
|
|
264
|
-
const { themeApiUrl, widgetVersion } = readThemingEnv(process.env.DOCS_SOURCE_PATH, envKey);
|
|
265
|
-
const widgetScriptSrc = widgetVersion
|
|
266
|
-
? `https://cdn-na.infragistics.com/igniteui/theming-widget/${widgetVersion}/igniteui-theming-widget.js`
|
|
267
|
-
: '';
|
|
268
|
-
|
|
269
|
-
return [
|
|
270
|
-
`export const platform = ${JSON.stringify(platform ?? null)};`,
|
|
271
|
-
`export const themeApiUrl = ${JSON.stringify(themeApiUrl)};`,
|
|
272
|
-
`export const widgetScriptSrc = ${JSON.stringify(widgetScriptSrc)};`,
|
|
273
|
-
].join('\n');
|
|
230
|
+
return `export const platform = ${JSON.stringify(platform ?? null)};`;
|
|
274
231
|
},
|
|
275
232
|
}],
|
|
276
233
|
},
|
|
@@ -10,7 +10,7 @@ import LicenseIndicator from '../../components/LicenseIndicator/LicenseIndicator
|
|
|
10
10
|
import igLogoRaw from '../../assets/ig-logo.svg?raw';
|
|
11
11
|
import MobileSidebarToggle from '../../components/MobileSidebarToggle/MobileSidebarToggle.astro';
|
|
12
12
|
import CopyCode from '../../components/CopyCode/CopyCode.astro';
|
|
13
|
-
import {
|
|
13
|
+
import { platform as virtualPlatform } from 'virtual:docs-template/nav-html';
|
|
14
14
|
import { title as siteTitle, headEntries, navLang } from 'virtual:docs-template/site-meta';
|
|
15
15
|
import '../../styles/theme.scss';
|
|
16
16
|
import './DocsLayout.scss';
|
|
@@ -73,7 +73,6 @@ type HeadEntry = { tag: string; attrs?: Record<string, string | boolean | undefi
|
|
|
73
73
|
{keywords && <meta name="keywords" content={keywords} />}
|
|
74
74
|
<link rel="icon" href="/favicon.svg" type="image/svg+xml" />
|
|
75
75
|
<link rel="stylesheet" href="https://use.typekit.net/bng2zgw.css" />
|
|
76
|
-
{widgetScriptSrc && <script is:inline src={widgetScriptSrc} async></script>}
|
|
77
76
|
{(headEntries as HeadEntry[]).map(({ tag: Tag, attrs = {}, content }) =>
|
|
78
77
|
content ? <Tag {...attrs} set:html={content} /> : <Tag {...attrs} />
|
|
79
78
|
)}
|
|
@@ -184,15 +184,18 @@ function addFooter(
|
|
|
184
184
|
footer.appendChild(btn);
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
187
|
+
// CodeSandbox hidden, re-enable when https://github.com/codesandbox/codesandbox-client/issues/8884 is fixed.
|
|
188
|
+
// https://github.com/codesandbox/codesandbox-client/issues/8883
|
|
189
|
+
// https://github.com/codesandbox/codesandbox-client/issues/8074
|
|
190
|
+
// if ((!explicitEditor || explicitEditor === 'csb') && onCodeSandbox) {
|
|
191
|
+
// const btn = document.createElement('igc-button') as HTMLElement;
|
|
192
|
+
// btn.setAttribute('variant', 'outlined');
|
|
193
|
+
// btn.setAttribute('aria-label', 'Edit in CodeSandbox');
|
|
194
|
+
// btn.className = 'codesandbox-btn igd-edit-in-btn';
|
|
195
|
+
// btn.innerHTML = codeSandboxSvg;
|
|
196
|
+
// btn.addEventListener('click', onCodeSandbox);
|
|
197
|
+
// footer.appendChild(btn);
|
|
198
|
+
// }
|
|
196
199
|
|
|
197
200
|
const fsBtn = document.createElement('igc-icon-button') as HTMLElement;
|
|
198
201
|
fsBtn.setAttribute('name', 'open-link-blank');
|
|
@@ -218,9 +221,7 @@ class AngularCodeService {
|
|
|
218
221
|
const samplePath = getSamplePath(iframeSrc, demosBaseUrl);
|
|
219
222
|
const isDv = isDvSample(samplePath);
|
|
220
223
|
const assetDir = isDv ? 'code-viewer' : 'samples';
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
const fallbackStackblitz = isDv ? null : () => this._openStackBlitzUrl(samplePath, demosBaseUrl);
|
|
224
|
+
const fallbackStackblitz = () => this._openStackBlitzUrl(samplePath, demosBaseUrl);
|
|
224
225
|
const fallbackCodeSandbox = () => this._openCodeSandboxUrl(samplePath, demosBaseUrl);
|
|
225
226
|
|
|
226
227
|
try {
|
|
@@ -256,12 +257,15 @@ class AngularCodeService {
|
|
|
256
257
|
.forEach((file: SampleFile) =>
|
|
257
258
|
addCodeTab(igcTabs, file));
|
|
258
259
|
|
|
259
|
-
|
|
260
|
-
|
|
260
|
+
// DV samples include all boilerplate in their own JSON, sharedData is for non DV only.
|
|
261
|
+
const onStackblitz = isDv
|
|
262
|
+
? () => this._openInStackBlitz(sampleData, null)
|
|
263
|
+
: () => this._openInStackBlitz(sampleData, sharedData);
|
|
264
|
+
addFooter(widget, iframeSrc, null, onStackblitz, fallbackCodeSandbox);
|
|
261
265
|
|
|
262
266
|
} catch (err: any) {
|
|
263
267
|
console.warn('[sample-widget] Could not fetch Angular sample files:', err.message);
|
|
264
|
-
addFooter(widget, iframeSrc,
|
|
268
|
+
addFooter(widget, iframeSrc, null, fallbackStackblitz, fallbackCodeSandbox);
|
|
265
269
|
}
|
|
266
270
|
}
|
|
267
271
|
|
|
@@ -274,12 +278,12 @@ class AngularCodeService {
|
|
|
274
278
|
return;
|
|
275
279
|
}
|
|
276
280
|
const files: Record<string, string> = {};
|
|
277
|
-
|
|
278
|
-
if (f.path
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
281
|
+
const addFile = (f: any) => {
|
|
282
|
+
if (!f.path || f.path.startsWith('..')) return;
|
|
283
|
+
files[f.path.replace(/^\.\//, '')] = f.content || '';
|
|
284
|
+
};
|
|
285
|
+
(sharedData?.files || []).forEach(addFile); // shared boilerplate for non-DV samples
|
|
286
|
+
(sampleData.sampleFiles || []).forEach(addFile);
|
|
283
287
|
sdk.openProject(
|
|
284
288
|
{
|
|
285
289
|
title: 'Infragistics Angular Components',
|
|
@@ -321,8 +325,9 @@ class AngularCodeService {
|
|
|
321
325
|
const repo = isDv ? 'igniteui-angular-examples' : 'igniteui-live-editing-samples';
|
|
322
326
|
const segs = new URL(demosBaseUrl).pathname.replace(/\/$/, '').split('/').filter(Boolean);
|
|
323
327
|
const prefix = isDv ? 'samples/' : (segs.pop() || 'angular-demos') + '/';
|
|
328
|
+
const params = isDv ? '?startScript=start' : '';
|
|
324
329
|
window.open(
|
|
325
|
-
`https://stackblitz.com/github/IgniteUI/${repo}/tree/${branch}/${prefix}${samplePath}`,
|
|
330
|
+
`https://stackblitz.com/github/IgniteUI/${repo}/tree/${branch}/${prefix}${samplePath}${params}`,
|
|
326
331
|
'_blank',
|
|
327
332
|
);
|
|
328
333
|
}
|
|
@@ -346,10 +351,8 @@ class XplatCodeService {
|
|
|
346
351
|
async init(ctx: WidgetContext): Promise<void> {
|
|
347
352
|
const { widget, iframeSrc, demosBaseUrl, igcTabs, githubSrc } = ctx;
|
|
348
353
|
const samplePath = getSamplePath(iframeSrc, demosBaseUrl);
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
addFooter(widget, iframeSrc, 'csb', null, () => this._openCodeSandbox(githubSrc, demosBaseUrl));
|
|
352
|
-
};
|
|
354
|
+
const fallbackStackblitz = () => this._openStackBlitzUrl(githubSrc, demosBaseUrl);
|
|
355
|
+
const fallbackCodeSandbox = () => this._openCodeSandbox(githubSrc, demosBaseUrl);
|
|
353
356
|
|
|
354
357
|
try {
|
|
355
358
|
// Blazor's local dev server uses a self-signed cert on a non-4200 port,
|
|
@@ -371,11 +374,90 @@ class XplatCodeService {
|
|
|
371
374
|
.forEach((file: SampleFile) =>
|
|
372
375
|
addCodeTab(igcTabs, file));
|
|
373
376
|
|
|
374
|
-
|
|
377
|
+
if (this.enableLiveEditing && githubSrc) {
|
|
378
|
+
// Use sdk.openProject() to avoid the StackBlitz GitHub clone freeze bug.
|
|
379
|
+
addFooter(widget, iframeSrc, null,
|
|
380
|
+
() => this._openInStackBlitz(data),
|
|
381
|
+
fallbackCodeSandbox);
|
|
382
|
+
}
|
|
375
383
|
} catch (err: any) {
|
|
376
384
|
console.warn('[sample-widget] Could not fetch xplat sample files:', err.message);
|
|
377
|
-
|
|
385
|
+
if (this.enableLiveEditing && githubSrc) {
|
|
386
|
+
addFooter(widget, iframeSrc, null, fallbackStackblitz, fallbackCodeSandbox);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
private async _openInStackBlitz(sampleData: any): Promise<void> {
|
|
392
|
+
let sdk: any;
|
|
393
|
+
try {
|
|
394
|
+
sdk = await this._loadStackBlitzSdk();
|
|
395
|
+
} catch (e: any) {
|
|
396
|
+
console.warn('[sample-widget] StackBlitz SDK failed to load:', e.message);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const files: Record<string, string> = {};
|
|
400
|
+
const addFile = (f: any) => {
|
|
401
|
+
// Skip paths that still have the old full repo prefix (pre-fix JSONs).
|
|
402
|
+
if (!f.path || f.path.startsWith('..') || f.path.startsWith('./samples/')) return;
|
|
403
|
+
files[f.path.replace(/^\.\//, '')] = f.content || '';
|
|
404
|
+
};
|
|
405
|
+
(sampleData.sampleFiles || []).forEach(addFile);
|
|
406
|
+
|
|
407
|
+
// webpack-dev-server binds to localhost by default; StackBlitz WebContainers need 0.0.0.0.
|
|
408
|
+
if (this.platform === 'wc' && files['package.json']) {
|
|
409
|
+
try {
|
|
410
|
+
const pkg = JSON.parse(files['package.json']);
|
|
411
|
+
if (pkg.scripts) {
|
|
412
|
+
pkg.scripts['serve:dev'] =
|
|
413
|
+
'node node_modules/webpack-dev-server/bin/webpack-dev-server.js' +
|
|
414
|
+
' --mode development --config ./webpack.config.js --hot --host 0.0.0.0 --allowed-hosts all';
|
|
415
|
+
pkg.scripts['start'] = 'npm run serve:dev';
|
|
416
|
+
files['package.json'] = JSON.stringify(pkg, null, 2);
|
|
417
|
+
}
|
|
418
|
+
} catch { /* malformed package.json — leave as-is */ }
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// @babel/preset-env with useBuiltIns:"usage" auto-injects core-js polyfill imports,
|
|
422
|
+
// but core-js is absent from some samples' package.json, breaking the webpack build.
|
|
423
|
+
// StackBlitz runs in modern Chrome so polyfills are unnecessary — disable the injection.
|
|
424
|
+
if (this.platform === 'wc' && files['webpack.config.js']) {
|
|
425
|
+
files['webpack.config.js'] = files['webpack.config.js']
|
|
426
|
+
.replace('"useBuiltIns": "usage"', '"useBuiltIns": false')
|
|
427
|
+
.replace(/,?\s*"corejs":\s*3/, '');
|
|
378
428
|
}
|
|
429
|
+
|
|
430
|
+
const platformLabel = this.platform === 'react' ? 'React' : 'Web Components';
|
|
431
|
+
const mainFile = Object.keys(files).find(f => f.endsWith('index.tsx') || f.endsWith('index.ts')) || '';
|
|
432
|
+
sdk.openProject(
|
|
433
|
+
{
|
|
434
|
+
title: `Infragistics ${platformLabel} Components`,
|
|
435
|
+
description: `Auto-generated from Infragistics ${platformLabel} Docs`,
|
|
436
|
+
template: 'node',
|
|
437
|
+
files,
|
|
438
|
+
tags: [this.platform, 'igniteui', 'web', 'example'],
|
|
439
|
+
},
|
|
440
|
+
{ openFile: mainFile },
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
private _loadStackBlitzSdk(): Promise<any> {
|
|
445
|
+
if ((window as any).StackBlitzSDK) return Promise.resolve((window as any).StackBlitzSDK);
|
|
446
|
+
return new Promise((resolve, reject) => {
|
|
447
|
+
const s = document.createElement('script');
|
|
448
|
+
s.src = 'https://unpkg.com/@stackblitz/sdk/bundles/sdk.umd.js';
|
|
449
|
+
s.onload = () => resolve((window as any).StackBlitzSDK);
|
|
450
|
+
s.onerror = () => reject(new Error('Failed to load StackBlitz SDK'));
|
|
451
|
+
document.head.appendChild(s);
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
private _openStackBlitzUrl(githubSrc: string, demosBaseUrl: string): void {
|
|
456
|
+
const branch = getXplatBranch(demosBaseUrl);
|
|
457
|
+
window.open(
|
|
458
|
+
`https://stackblitz.com/github/IgniteUI/igniteui-${this.platform}-examples/tree/${branch}/samples/${githubSrc}`,
|
|
459
|
+
'_blank',
|
|
460
|
+
);
|
|
379
461
|
}
|
|
380
462
|
|
|
381
463
|
private _openCodeSandbox(githubSrc: string, demosBaseUrl: string): void {
|
|
@@ -25,6 +25,10 @@
|
|
|
25
25
|
display: grid;
|
|
26
26
|
grid-template-rows: auto 1fr;
|
|
27
27
|
overflow: auto;
|
|
28
|
+
|
|
29
|
+
// min-width: 0 is necessary to allow the content to shrink below its min-content width
|
|
30
|
+
// otherwise igc-tabs won't show the scroll buttons
|
|
31
|
+
min-width: 0;
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
// Status
|
|
@@ -60,16 +60,16 @@
|
|
|
60
60
|
--igd-updated-border: light-dark(#BAE2F5, #1E3D6A);
|
|
61
61
|
|
|
62
62
|
--igd-new-bg: light-dark(#EAFEF6, #0A2A1C);
|
|
63
|
-
--igd-new-color: light-dark(#
|
|
63
|
+
--igd-new-color: light-dark(#018450, #3DBF85);
|
|
64
64
|
--igd-new-border: light-dark(#BAF5D7, #154A30);
|
|
65
65
|
|
|
66
66
|
--igd-preview-bg: light-dark(#FEF5EA, #2D1507);
|
|
67
67
|
--igd-preview-color: light-dark(#AF390A, #E06030);
|
|
68
68
|
--igd-preview-border: light-dark(#F5C8BA, #5C2810);
|
|
69
69
|
|
|
70
|
-
--igd-premium-
|
|
71
|
-
--igd-premium-
|
|
72
|
-
--igd-premium-border: light-dark(#
|
|
70
|
+
--igd-premium-bg: light-dark(#F5EAFE, #1E1030);
|
|
71
|
+
--igd-premium-color: light-dark(#8438FF, #B585FF);
|
|
72
|
+
--igd-premium-border: light-dark(#8438FF, #5A3A99);
|
|
73
73
|
|
|
74
74
|
--igd-opensource-color: light-dark(#535353, #B5B5B5);
|
|
75
75
|
--igd-opensource-bg: light-dark(#ECECEC, #383737);
|
package/src/virtual-modules.d.ts
CHANGED
|
@@ -35,6 +35,4 @@ declare module 'virtual:docs-template/site-meta' {
|
|
|
35
35
|
|
|
36
36
|
declare module 'virtual:docs-template/nav-html' {
|
|
37
37
|
export const platform: 'igniteui' | 'angular' | 'react' | 'blazor' | 'web-components' | 'slingshot' | 'appbuilder' | null;
|
|
38
|
-
export const themeApiUrl: string;
|
|
39
|
-
export const widgetScriptSrc: string;
|
|
40
38
|
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
# ThemingWidget
|
|
2
|
-
|
|
3
|
-
Renders the `<igniteui-theming-widget>` web component that lets readers switch the component theme live inside embedded sample iframes.
|
|
4
|
-
|
|
5
|
-
## Import
|
|
6
|
-
|
|
7
|
-
```astro
|
|
8
|
-
import ThemingWidget from 'igniteui-astro-components/components/ThemingWidget.astro';
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Props
|
|
12
|
-
|
|
13
|
-
| Prop | Type | Default | Description |
|
|
14
|
-
|------|------|---------|-------------|
|
|
15
|
-
| `language` | `NavLang` | `'en'` | Locale passed to the web component (`'en'` \| `'jp'` \| `'kr'`). |
|
|
16
|
-
|
|
17
|
-
## Behaviour
|
|
18
|
-
|
|
19
|
-
- Renders nothing when `themeApiUrl` is absent from the environment config.
|
|
20
|
-
- The widget container starts hidden and is revealed client-side only when at least one themeable `<iframe>` is detected on the page.
|
|
21
|
-
- Forwards `themeChange` events to all themeable frames via `postMessage`.
|
|
22
|
-
- Re-initialises on Astro View Transitions navigation (`astro:page-load`).
|
|
23
|
-
|
|
24
|
-
## Environment variable
|
|
25
|
-
|
|
26
|
-
`themeApiUrl` is resolved at build time from `environment.json` by `siteMetaIntegration` and exposed through the `virtual:docs-template/nav-html` virtual module.
|
|
27
|
-
|
|
28
|
-
## Example
|
|
29
|
-
|
|
30
|
-
```astro
|
|
31
|
-
---
|
|
32
|
-
import ThemingWidget from 'igniteui-astro-components/components/ThemingWidget.astro';
|
|
33
|
-
---
|
|
34
|
-
<ThemingWidget language="en" />
|
|
35
|
-
```
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
/**
|
|
3
|
-
* ThemingWidget
|
|
4
|
-
*
|
|
5
|
-
* Renders the `<igniteui-theming-widget>` web component.
|
|
6
|
-
* Behaviour:
|
|
7
|
-
* - `themeApiUrl` and `widgetVersion` are resolved from environment.json at
|
|
8
|
-
* build time via the `virtual:docs-template/nav-html` virtual module.
|
|
9
|
-
* - Renders nothing when `themeApiUrl` is unavailable.
|
|
10
|
-
* - The widget container starts hidden and is revealed client-side only
|
|
11
|
-
* when at least one themeable `<iframe>` is found on the page.
|
|
12
|
-
* - Forwards `themeChange` events from the widget to every themeable frame
|
|
13
|
-
* via `postMessage`.
|
|
14
|
-
* - Re-initialises on Astro View-Transitions navigation (`astro:page-load`).
|
|
15
|
-
*
|
|
16
|
-
* Props:
|
|
17
|
-
* language – locale passed to the web component. Defaults to 'en'.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import { themeApiUrl } from 'virtual:docs-template/nav-html';
|
|
21
|
-
import type { NavLang } from '../../platform.ts';
|
|
22
|
-
import './ThemingWidget.scss';
|
|
23
|
-
|
|
24
|
-
interface Props {
|
|
25
|
-
language?: NavLang;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const { language = 'en' } = Astro.props;
|
|
29
|
-
|
|
30
|
-
// Only add the `language` attribute for japanese, which the widget currently supports. For the other languages the widget falls back to english.
|
|
31
|
-
const lang = language === 'jp' ? 'ja' : undefined;
|
|
32
|
-
---
|
|
33
|
-
|
|
34
|
-
{themeApiUrl && (
|
|
35
|
-
<>
|
|
36
|
-
{/*
|
|
37
|
-
Widget container — always visible when themeApiUrl is set.
|
|
38
|
-
The client script below wires up theme→iframe forwarding.
|
|
39
|
-
*/}
|
|
40
|
-
<div id="theming-widget-container" class="theming-widget-container" transition:persist transition:animate="none">
|
|
41
|
-
{lang
|
|
42
|
-
? <igniteui-theming-widget theme-url={themeApiUrl} language={lang} />
|
|
43
|
-
: <igniteui-theming-widget theme-url={themeApiUrl} />
|
|
44
|
-
}
|
|
45
|
-
</div>
|
|
46
|
-
</>
|
|
47
|
-
)}
|
|
48
|
-
|
|
49
|
-
{/*
|
|
50
|
-
Init script — always bundled by Astro but self-guards by checking the
|
|
51
|
-
container element. When themeApiUrl was empty at build time the container
|
|
52
|
-
is absent, so this script becomes a no-op.
|
|
53
|
-
*/}
|
|
54
|
-
<script>
|
|
55
|
-
// Module-level cleanup reference so re-init on View Transitions tears down
|
|
56
|
-
// the previous observer + event listener before creating new ones.
|
|
57
|
-
let _cleanup: (() => void) | null = null;
|
|
58
|
-
|
|
59
|
-
function initThemingWidget() {
|
|
60
|
-
if (_cleanup) { _cleanup(); _cleanup = null; }
|
|
61
|
-
|
|
62
|
-
const container = document.getElementById('theming-widget-container');
|
|
63
|
-
if (!container) return;
|
|
64
|
-
|
|
65
|
-
// Must query the widget element directly — themeChange is likely non-bubbling,
|
|
66
|
-
// so attaching to the parent container div would miss it.
|
|
67
|
-
const widget = container.querySelector('igniteui-theming-widget') as
|
|
68
|
-
(HTMLElement & { theme?: { globalTheme: string } }) | null;
|
|
69
|
-
if (!widget) return;
|
|
70
|
-
|
|
71
|
-
// ── postMessage helper ───────────────────────────────────────────────────
|
|
72
|
-
// Uses the iframe's origin (not the full URL path) as targetOrigin.
|
|
73
|
-
function postToIframe(iframe: HTMLIFrameElement, themeStyle: string) {
|
|
74
|
-
const src = iframe.src;
|
|
75
|
-
if (!src || src.startsWith('about:') || src.startsWith('javascript:')) return;
|
|
76
|
-
try {
|
|
77
|
-
const targetOrigin = new URL(src).origin;
|
|
78
|
-
iframe.contentWindow?.postMessage(
|
|
79
|
-
{ themeStyle, origin: location.origin, themeName: widget?.theme?.globalTheme ?? '' },
|
|
80
|
-
targetOrigin,
|
|
81
|
-
);
|
|
82
|
-
} catch { /* cross-origin or invalid URL — ignore */ }
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// ── Replay theme into a newly loaded iframe ──────────────────────────────
|
|
86
|
-
// When an iframe finishes loading we immediately push the persisted
|
|
87
|
-
// themeStyle into it so samples that loaded after a theme was already
|
|
88
|
-
// selected pick it up.
|
|
89
|
-
function onIframeLoad(e: Event) {
|
|
90
|
-
const iframe = e.target as HTMLIFrameElement;
|
|
91
|
-
if (iframe.classList.contains('no-theming')) return;
|
|
92
|
-
const stored = sessionStorage.getItem('themeStyle');
|
|
93
|
-
if (stored) postToIframe(iframe, stored);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function attachLoadListener(node: Element) {
|
|
97
|
-
if (node instanceof HTMLIFrameElement && !node.classList.contains('no-theming')) {
|
|
98
|
-
node.addEventListener('load', onIframeLoad, { once: true });
|
|
99
|
-
}
|
|
100
|
-
node.querySelectorAll?.('iframe:not(.no-theming)').forEach(iframe => {
|
|
101
|
-
(iframe as HTMLIFrameElement).addEventListener('load', onIframeLoad, { once: true });
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Attach to iframes already in the DOM (e.g. first load, frame 0 queued immediately).
|
|
106
|
-
document.querySelectorAll<HTMLIFrameElement>('iframe:not(.no-theming)')
|
|
107
|
-
.forEach(iframe => iframe.addEventListener('load', onIframeLoad, { once: true }));
|
|
108
|
-
|
|
109
|
-
// ── themeChange → broadcast to all loaded iframes ───────────────────────
|
|
110
|
-
function onThemeChange(e: Event) {
|
|
111
|
-
const themeStyle = (e as CustomEvent<string>).detail;
|
|
112
|
-
sessionStorage.setItem('themeStyle', themeStyle);
|
|
113
|
-
document.querySelectorAll<HTMLIFrameElement>('iframe:not(.no-theming)').forEach(iframe => {
|
|
114
|
-
if (!iframe.src || iframe.src.startsWith('about:')) return;
|
|
115
|
-
// Skip lazy iframes that haven't received a real src yet.
|
|
116
|
-
if (iframe.classList.contains('lazyload') && !iframe.classList.contains('lazyloaded')) return;
|
|
117
|
-
postToIframe(iframe, themeStyle);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
widget.addEventListener('themeChange', onThemeChange);
|
|
122
|
-
|
|
123
|
-
// ── MutationObserver: watch for iframes injected after init ──────────────
|
|
124
|
-
const observer = new MutationObserver((mutations) => {
|
|
125
|
-
for (const mut of mutations) {
|
|
126
|
-
mut.addedNodes.forEach(node => {
|
|
127
|
-
if (node instanceof Element) attachLoadListener(node);
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
observer.observe(document.body, { childList: true, subtree: true });
|
|
132
|
-
|
|
133
|
-
_cleanup = () => {
|
|
134
|
-
observer.disconnect();
|
|
135
|
-
widget.removeEventListener('themeChange', onThemeChange);
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Run on initial load and after every View-Transitions navigation.
|
|
140
|
-
document.addEventListener('astro:page-load', initThemingWidget);
|
|
141
|
-
initThemingWidget();
|
|
142
|
-
</script>
|
|
143
|
-
|