igniteui-astro-components 1.0.1 → 1.1.0
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 +2 -2
- package/src/components/ThemingWidget/README.md +131 -0
- package/src/components/ThemingWidget/ThemingWidget.astro +533 -0
- package/src/components/ThemingWidget/ThemingWidget.scss +282 -0
- package/src/components/mdx/Sample/README.md +17 -16
- package/src/components/mdx/Sample/Sample.astro +44 -1
- package/src/components/mdx/Sample/Sample.scss +30 -1
- package/src/layouts/DocsLayout/DocsLayout.scss +0 -1
- package/src/scripts/sample-widget.ts +83 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "igniteui-astro-components",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
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": {
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
"peerDependencies": {
|
|
86
86
|
"astro": "^7.0.0",
|
|
87
87
|
"igniteui-theming": "^26.0.1",
|
|
88
|
-
"igniteui-webcomponents": "^7.
|
|
88
|
+
"igniteui-webcomponents": "^7.3.0-beta.1"
|
|
89
89
|
},
|
|
90
90
|
"dependencies": {
|
|
91
91
|
"@astrojs/mdx": "^7.0.7",
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# ThemingWidget
|
|
2
|
+
|
|
3
|
+
A compact theme picker dropdown for sample viewers. The trigger shows the active
|
|
4
|
+
theme (a multi-dot color swatch + theme name + muted mode label). Opening it
|
|
5
|
+
reveals an optional **MODE** toggle (Light / Dark / System) and the list of
|
|
6
|
+
selectable themes, with the active one checked.
|
|
7
|
+
|
|
8
|
+
Self-contained: icons are inlined SVGs and behavior is wired by a small client
|
|
9
|
+
script — no `igniteui-webcomponents` registration required.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```astro
|
|
14
|
+
---
|
|
15
|
+
import ThemingWidget from 'igniteui-astro-components/components/ThemingWidget.astro';
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
<ThemingWidget target="#sample-preview" />
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
On change the widget:
|
|
22
|
+
|
|
23
|
+
- dispatches a bubbling `igd-theme-change` `CustomEvent` with
|
|
24
|
+
`{ theme, mode, modePreference }`, and
|
|
25
|
+
- reflects `data-igd-theme` / `data-igd-mode` on every element matching `target`.
|
|
26
|
+
|
|
27
|
+
The selection is not persisted — the widget resets to its initial props on every
|
|
28
|
+
page load.
|
|
29
|
+
|
|
30
|
+
## Color mode
|
|
31
|
+
|
|
32
|
+
The MODE row offers **Light**, **Dark** and **System**. `system` is not passed
|
|
33
|
+
on as-is: it is resolved against the OS `prefers-color-scheme` before it leaves
|
|
34
|
+
the widget, so `mode` in the event — and the `data-igd-mode` written onto
|
|
35
|
+
`target` — is always a concrete `light` or `dark` and consumers need no
|
|
36
|
+
media-query handling of their own. The raw selection stays available as
|
|
37
|
+
`modePreference` (and as `data-mode` on the widget root, with the resolved value
|
|
38
|
+
mirrored on `data-resolved-mode`) for anything that mirrors the UI state.
|
|
39
|
+
|
|
40
|
+
The trigger's muted mode label names the resolved mode, so a `system` selection
|
|
41
|
+
reads as "Light" or "Dark" there — which mode is actually selected stays visible
|
|
42
|
+
through the pressed state of the MODE buttons.
|
|
43
|
+
|
|
44
|
+
While `system` is selected the widget follows the OS live: flipping the system
|
|
45
|
+
appearance re-reflects `data-igd-mode`, updates that label and re-emits
|
|
46
|
+
`igd-theme-change` without any interaction.
|
|
47
|
+
|
|
48
|
+
## Panel placement
|
|
49
|
+
|
|
50
|
+
The panel is promoted to a native [popover](https://developer.mozilla.org/docs/Web/API/Popover_API)
|
|
51
|
+
when the browser supports it, so it renders in the top layer and is never
|
|
52
|
+
clipped by a scrolling or `overflow: hidden` ancestor (the sample container is
|
|
53
|
+
one). It is placed below the trigger, flips above it when the space below is too
|
|
54
|
+
small, and gets a `max-height` (with a scrolling list) when neither side fits —
|
|
55
|
+
so it always stays inside the viewport. The position is recomputed while the
|
|
56
|
+
panel is open on scroll and resize. Browsers without the Popover API keep the
|
|
57
|
+
absolutely positioned panel, which still flips.
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
document.addEventListener('igd-theme-change', (e) => {
|
|
61
|
+
// e.g. { theme: 'fluent', mode: 'dark', modePreference: 'system' }
|
|
62
|
+
const { theme, mode, modePreference } = e.detail;
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Props
|
|
67
|
+
|
|
68
|
+
| Prop | Type | Default | Description |
|
|
69
|
+
| --------------- | ------------------------------- | ----------------------------------- | ------------------------------------------------------------------------------------------- |
|
|
70
|
+
| `themes` | `ThemeOption[]` | Material, Fluent, Bootstrap, Indigo | Selectable themes. Each has `name`, optional `label`, and a `swatch` array. |
|
|
71
|
+
| `selectedTheme` | `string` | env, then first theme | Active theme `name`. See [Ambient defaults](#ambient-defaults). |
|
|
72
|
+
| `mode` | `'light' \| 'dark' \| 'system'` | env, then `'light'` | Active color mode. See [Color mode](#color-mode) and [Ambient defaults](#ambient-defaults). |
|
|
73
|
+
| `showMode` | `boolean` | `true` | Show the MODE (Light / Dark / System) toggle row. |
|
|
74
|
+
| `target` | `string` | — | CSS selector to reflect `data-igd-theme` / `data-igd-mode` onto. |
|
|
75
|
+
| `label` | `string` | `'Select theme'` | Accessible label for the trigger. |
|
|
76
|
+
| `id` | `string` | auto | Root element id. |
|
|
77
|
+
| `class` | `string` | — | Extra class(es) forwarded to the root. |
|
|
78
|
+
|
|
79
|
+
### `ThemeOption`
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
interface ThemeOption {
|
|
83
|
+
name: string; // stable id, used as the event/storage value
|
|
84
|
+
label?: string; // display label (defaults to a title-cased `name`)
|
|
85
|
+
swatch: string[]; // dot colors, left → right (the design uses three)
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Ambient defaults
|
|
90
|
+
|
|
91
|
+
`selectedTheme` and `mode` fall back to the host site's configuration before
|
|
92
|
+
they fall back to the built-in defaults, so a docs site picks its starting theme
|
|
93
|
+
once instead of at every call site. The host publishes it on
|
|
94
|
+
`Astro.locals.envVars` from its middleware — the same channel `Sample.astro`
|
|
95
|
+
already reads its demo URLs from:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
// docs host middleware
|
|
99
|
+
ctx.locals.envVars = { defaultSampleTheme: 'bootstrap', defaultSampleMode: 'light' };
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
| Key | Falls back to |
|
|
103
|
+
| -------------------- | ------------------------ |
|
|
104
|
+
| `defaultSampleTheme` | the first `themes` entry |
|
|
105
|
+
| `defaultSampleMode` | `'light'` |
|
|
106
|
+
|
|
107
|
+
An explicit prop always wins. An unrecognized `defaultSampleMode` is ignored, and
|
|
108
|
+
a `defaultSampleTheme` matching no entry in `themes` falls through to the first
|
|
109
|
+
one. Consumers without middleware get an empty `Astro.locals` and the built-in
|
|
110
|
+
defaults.
|
|
111
|
+
|
|
112
|
+
`Sample.astro` adds one step on top: when it renders the widget it derives the
|
|
113
|
+
theme from the current platform — Angular → Material, React / Web Components /
|
|
114
|
+
Blazor → Bootstrap — unless `defaultSampleTheme` is set. That default is
|
|
115
|
+
functional, not cosmetic: the picker's selection is posted into the sample
|
|
116
|
+
iframe on every load, so it decides how the demo actually renders.
|
|
117
|
+
|
|
118
|
+
## Styling
|
|
119
|
+
|
|
120
|
+
The widget exposes CSS custom properties for retuning per context — override
|
|
121
|
+
them from an ancestor or via `class`:
|
|
122
|
+
|
|
123
|
+
| Variable | Default |
|
|
124
|
+
| ------------------------ | ---------------------------- |
|
|
125
|
+
| `--igd-theming-radius` | `5px` |
|
|
126
|
+
| `--igd-theming-border` | `var(--ig-gray-300)` |
|
|
127
|
+
| `--igd-theming-bg` | `light-dark(#fff, gray-100)` |
|
|
128
|
+
| `--igd-theming-fg` | `var(--ig-gray-900)` |
|
|
129
|
+
| `--igd-theming-muted` | `var(--ig-gray-500)` |
|
|
130
|
+
| `--igd-theming-hover-bg` | `var(--ig-gray-100)` |
|
|
131
|
+
| `--igd-theming-accent` | `var(--igd-accent)` |
|
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ThemingWidget
|
|
4
|
+
*
|
|
5
|
+
* A compact theme picker dropdown used inside sample viewers. The trigger shows
|
|
6
|
+
* the active theme (a small multi-dot color swatch + the theme name and, when a
|
|
7
|
+
* mode is active, a muted label with the mode it resolves to). Opening the
|
|
8
|
+
* trigger reveals a panel with:
|
|
9
|
+
*
|
|
10
|
+
* • an optional MODE row — three icon toggles for Light / Dark / System, and
|
|
11
|
+
* • a list of selectable themes, each with its swatch; the active one is checked.
|
|
12
|
+
*
|
|
13
|
+
* The widget is self-contained (no web-component dependency): icons are inlined
|
|
14
|
+
* SVGs and all behavior is wired by a small client script. On change it:
|
|
15
|
+
* • dispatches a bubbling `igd-theme-change` CustomEvent with
|
|
16
|
+
* `{ theme, mode, modePreference }`, and
|
|
17
|
+
* • reflects `data-igd-theme` / `data-igd-mode` on the optional `target` element.
|
|
18
|
+
*
|
|
19
|
+
* `system` is resolved against the OS `prefers-color-scheme` before it leaves
|
|
20
|
+
* the widget: `mode` (and everything reflected onto `target`) is always a
|
|
21
|
+
* concrete `light` / `dark`, while `modePreference` keeps the raw selection.
|
|
22
|
+
* While `system` is selected the widget follows OS changes live, re-reflecting
|
|
23
|
+
* and re-emitting whenever the preference flips.
|
|
24
|
+
*
|
|
25
|
+
* The selection is not persisted — the widget resets to its initial props on
|
|
26
|
+
* every page load.
|
|
27
|
+
*
|
|
28
|
+
* When `selectedTheme` / `mode` are omitted the widget falls back to the docs
|
|
29
|
+
* host's ambient configuration — `defaultSampleTheme` / `defaultSampleMode` on
|
|
30
|
+
* `Astro.locals.envVars` — so a site sets its starting theme once instead of at
|
|
31
|
+
* every call site.
|
|
32
|
+
*
|
|
33
|
+
* The panel is promoted to a native popover (top layer) when the browser
|
|
34
|
+
* supports it, so it escapes any clipping/stacking ancestor — e.g. the sample
|
|
35
|
+
* container's `overflow: hidden`. Its position is computed from the trigger and
|
|
36
|
+
* flips above it when there is not enough room below; when constrained it gets
|
|
37
|
+
* a `max-height` and scrolls. Browsers without the Popover API fall back to the
|
|
38
|
+
* absolutely positioned panel, which still flips.
|
|
39
|
+
*
|
|
40
|
+
* Mirrors the "Theme-Input-Dropdown" design from the Doc Samples Figma file.
|
|
41
|
+
*/
|
|
42
|
+
import './ThemingWidget.scss';
|
|
43
|
+
|
|
44
|
+
export interface ThemeOption {
|
|
45
|
+
/** Stable identifier, also used as the value in events. */
|
|
46
|
+
name: string;
|
|
47
|
+
/** Display label. Defaults to a title-cased `name`. */
|
|
48
|
+
label?: string;
|
|
49
|
+
/** Swatch dot colors (left → right). Any length; the design uses three. */
|
|
50
|
+
swatch: string[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type ThemeMode = 'light' | 'dark' | 'system';
|
|
54
|
+
|
|
55
|
+
interface Props {
|
|
56
|
+
/** Selectable themes. Defaults to Material, Fluent, Bootstrap and Indigo. */
|
|
57
|
+
themes?: ThemeOption[];
|
|
58
|
+
/**
|
|
59
|
+
* Active theme `name`. When omitted, falls back to the host's
|
|
60
|
+
* `defaultSampleTheme` env var and then to the first theme.
|
|
61
|
+
*/
|
|
62
|
+
selectedTheme?: string;
|
|
63
|
+
/**
|
|
64
|
+
* Active color mode. When omitted, falls back to the host's
|
|
65
|
+
* `defaultSampleMode` env var and then to `'light'`.
|
|
66
|
+
*/
|
|
67
|
+
mode?: ThemeMode;
|
|
68
|
+
/** Show the MODE (Light / Dark / System) toggle row. Defaults to `true`. */
|
|
69
|
+
showMode?: boolean;
|
|
70
|
+
/** Element id for the root. Defaults to an auto-generated id. */
|
|
71
|
+
id?: string;
|
|
72
|
+
/** Extra class(es) forwarded to the root. */
|
|
73
|
+
class?: string;
|
|
74
|
+
/** Accessible label for the trigger. Defaults to `'Select theme'`. */
|
|
75
|
+
label?: string;
|
|
76
|
+
/**
|
|
77
|
+
* CSS selector for an element to reflect the selection onto via
|
|
78
|
+
* `data-igd-theme` / `data-igd-mode`. When omitted, nothing is reflected.
|
|
79
|
+
*/
|
|
80
|
+
target?: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const DEFAULT_THEMES: ThemeOption[] = [
|
|
84
|
+
{ name: 'material', label: 'Material', swatch: ['#000000', '#df1b74', '#0099ff'] },
|
|
85
|
+
{ name: 'fluent', label: 'Fluent', swatch: ['#000000', '#0078D4', '#2B88D8'] },
|
|
86
|
+
{ name: 'bootstrap', label: 'Bootstrap', swatch: ['#000000', '#0D6EFD', '#6C757D'] },
|
|
87
|
+
{ name: 'indigo', label: 'Indigo', swatch: ['#000000', '#3F51B5', '#3F51B5'] },
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
const {
|
|
91
|
+
themes = DEFAULT_THEMES,
|
|
92
|
+
selectedTheme,
|
|
93
|
+
mode: modeProp,
|
|
94
|
+
showMode = true,
|
|
95
|
+
id,
|
|
96
|
+
class: className,
|
|
97
|
+
label = 'Select theme',
|
|
98
|
+
target,
|
|
99
|
+
} = Astro.props;
|
|
100
|
+
|
|
101
|
+
const rootId = id ?? `igd-theming-${Math.random().toString(36).slice(2, 9)}`;
|
|
102
|
+
|
|
103
|
+
const titleCase = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
104
|
+
|
|
105
|
+
// Ambient defaults published by the docs host on `Astro.locals.envVars` (set
|
|
106
|
+
// there by its middleware). Reading them means a widget rendered without props
|
|
107
|
+
// still opens on the theme the surrounding site ships, instead of every call
|
|
108
|
+
// site having to pass one. `locals` is `{}` when the consumer has no
|
|
109
|
+
// middleware, so this degrades to the built-in defaults.
|
|
110
|
+
const env = Astro.locals.envVars ?? {};
|
|
111
|
+
|
|
112
|
+
const activeName = selectedTheme ?? env.defaultSampleTheme ?? themes[0]?.name;
|
|
113
|
+
const active = themes.find((t) => t.name === activeName) ?? themes[0];
|
|
114
|
+
const activeLabel = active ? (active.label ?? titleCase(active.name)) : '';
|
|
115
|
+
|
|
116
|
+
// ThemingWidget supports a "system" mode, but the docs host doesn't yet which results in an inconsistent behavior.
|
|
117
|
+
// const const MODES: ThemeMode[] = ['light', 'dark', 'system'];
|
|
118
|
+
const MODES: ThemeMode[] = ['light', 'dark'];
|
|
119
|
+
|
|
120
|
+
// The env var is free-form text, so an unrecognized value falls back to 'light'
|
|
121
|
+
// instead of reaching the markup as a bogus `data-mode`.
|
|
122
|
+
const requestedMode = modeProp ?? env.defaultSampleMode;
|
|
123
|
+
const mode: ThemeMode = MODES.includes(requestedMode as ThemeMode)
|
|
124
|
+
? (requestedMode as ThemeMode)
|
|
125
|
+
: 'light';
|
|
126
|
+
|
|
127
|
+
const modeLabels: Record<ThemeMode, string> = {
|
|
128
|
+
light: 'Light',
|
|
129
|
+
dark: 'Dark',
|
|
130
|
+
system: 'System',
|
|
131
|
+
};
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
<div
|
|
135
|
+
class:list={['igd-theming', className]}
|
|
136
|
+
id={rootId}
|
|
137
|
+
data-target={target}
|
|
138
|
+
data-mode={mode}
|
|
139
|
+
data-resolved-mode={mode === 'system' ? undefined : mode}
|
|
140
|
+
data-theme={active?.name}
|
|
141
|
+
>
|
|
142
|
+
<button
|
|
143
|
+
type="button"
|
|
144
|
+
class="igd-theming__trigger"
|
|
145
|
+
aria-haspopup="listbox"
|
|
146
|
+
aria-expanded="false"
|
|
147
|
+
aria-label={label}
|
|
148
|
+
>
|
|
149
|
+
<span class="igd-theming__swatch" data-swatch>
|
|
150
|
+
{active?.swatch.map((c) => <span class="igd-theming__dot" style={`--dot: ${c}`} />)}
|
|
151
|
+
</span>
|
|
152
|
+
<span class="igd-theming__value">
|
|
153
|
+
<span class="igd-theming__theme-name" data-theme-name>{activeLabel}</span>
|
|
154
|
+
{
|
|
155
|
+
showMode && (
|
|
156
|
+
<span class="igd-theming__mode-name" data-mode-name>
|
|
157
|
+
{modeLabels[mode === 'system' ? 'light' : mode]}
|
|
158
|
+
</span>
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
</span>
|
|
162
|
+
<svg
|
|
163
|
+
class="igd-theming__chevron"
|
|
164
|
+
width="12"
|
|
165
|
+
height="12"
|
|
166
|
+
viewBox="0 0 24 24"
|
|
167
|
+
fill="none"
|
|
168
|
+
aria-hidden="true"
|
|
169
|
+
>
|
|
170
|
+
<path
|
|
171
|
+
d="M6 9l6 6 6-6"
|
|
172
|
+
stroke="currentColor"
|
|
173
|
+
stroke-width="2"
|
|
174
|
+
stroke-linecap="round"
|
|
175
|
+
stroke-linejoin="round"></path>
|
|
176
|
+
</svg>
|
|
177
|
+
</button>
|
|
178
|
+
|
|
179
|
+
<div class="igd-theming__panel" role="dialog" hidden>
|
|
180
|
+
{
|
|
181
|
+
showMode && (
|
|
182
|
+
<div class="igd-theming__mode-row">
|
|
183
|
+
<span class="igd-theming__section-label">Mode</span>
|
|
184
|
+
<div class="igd-theming__mode-buttons" role="group" aria-label="Color mode">
|
|
185
|
+
{MODES.map((m) => (
|
|
186
|
+
<button
|
|
187
|
+
type="button"
|
|
188
|
+
class="igd-theming__mode-btn"
|
|
189
|
+
data-mode-btn={m}
|
|
190
|
+
aria-pressed={m === mode ? 'true' : 'false'}
|
|
191
|
+
aria-label={modeLabels[m]}
|
|
192
|
+
title={modeLabels[m]}
|
|
193
|
+
>
|
|
194
|
+
{m === 'light' && (
|
|
195
|
+
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" aria-hidden="true">
|
|
196
|
+
<circle cx="12" cy="12" r="4" stroke="currentColor" stroke-width="2" />
|
|
197
|
+
<path
|
|
198
|
+
d="M12 2v2M12 20v2M2 12h2M20 12h2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M19.1 4.9l-1.4 1.4M6.3 17.7l-1.4 1.4"
|
|
199
|
+
stroke="currentColor"
|
|
200
|
+
stroke-width="2"
|
|
201
|
+
stroke-linecap="round"
|
|
202
|
+
/>
|
|
203
|
+
</svg>
|
|
204
|
+
)}
|
|
205
|
+
{m === 'dark' && (
|
|
206
|
+
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" aria-hidden="true">
|
|
207
|
+
<path
|
|
208
|
+
d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8z"
|
|
209
|
+
stroke="currentColor"
|
|
210
|
+
stroke-width="2"
|
|
211
|
+
stroke-linejoin="round"
|
|
212
|
+
/>
|
|
213
|
+
</svg>
|
|
214
|
+
)}
|
|
215
|
+
{m === 'system' && (
|
|
216
|
+
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" aria-hidden="true">
|
|
217
|
+
<rect
|
|
218
|
+
x="3"
|
|
219
|
+
y="4"
|
|
220
|
+
width="18"
|
|
221
|
+
height="12"
|
|
222
|
+
rx="1.5"
|
|
223
|
+
stroke="currentColor"
|
|
224
|
+
stroke-width="2"
|
|
225
|
+
/>
|
|
226
|
+
<path
|
|
227
|
+
d="M8 20h8M12 16v4"
|
|
228
|
+
stroke="currentColor"
|
|
229
|
+
stroke-width="2"
|
|
230
|
+
stroke-linecap="round"
|
|
231
|
+
/>
|
|
232
|
+
</svg>
|
|
233
|
+
)}
|
|
234
|
+
</button>
|
|
235
|
+
))}
|
|
236
|
+
</div>
|
|
237
|
+
</div>
|
|
238
|
+
)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
<ul class="igd-theming__list" role="listbox" aria-label={label}>
|
|
242
|
+
{
|
|
243
|
+
themes.map((t) => {
|
|
244
|
+
const name = t.label ?? titleCase(t.name);
|
|
245
|
+
const isActive = t.name === active?.name;
|
|
246
|
+
return (
|
|
247
|
+
<li>
|
|
248
|
+
<button
|
|
249
|
+
type="button"
|
|
250
|
+
class="igd-theming__option"
|
|
251
|
+
role="option"
|
|
252
|
+
aria-selected={isActive ? 'true' : 'false'}
|
|
253
|
+
data-option={t.name}
|
|
254
|
+
data-label={name}
|
|
255
|
+
data-swatch={t.swatch.join(',')}
|
|
256
|
+
>
|
|
257
|
+
<span class="igd-theming__swatch">
|
|
258
|
+
{t.swatch.map((c) => (
|
|
259
|
+
<span class="igd-theming__dot" style={`--dot: ${c}`} />
|
|
260
|
+
))}
|
|
261
|
+
</span>
|
|
262
|
+
<span class="igd-theming__option-name">{name}</span>
|
|
263
|
+
<svg
|
|
264
|
+
class="igd-theming__check"
|
|
265
|
+
width="16"
|
|
266
|
+
height="16"
|
|
267
|
+
viewBox="0 0 24 24"
|
|
268
|
+
fill="none"
|
|
269
|
+
aria-hidden="true"
|
|
270
|
+
>
|
|
271
|
+
<path
|
|
272
|
+
d="M5 13l4 4L19 7"
|
|
273
|
+
stroke="currentColor"
|
|
274
|
+
stroke-width="2"
|
|
275
|
+
stroke-linecap="round"
|
|
276
|
+
stroke-linejoin="round"
|
|
277
|
+
/>
|
|
278
|
+
</svg>
|
|
279
|
+
</button>
|
|
280
|
+
</li>
|
|
281
|
+
);
|
|
282
|
+
})
|
|
283
|
+
}
|
|
284
|
+
</ul>
|
|
285
|
+
</div>
|
|
286
|
+
</div>
|
|
287
|
+
|
|
288
|
+
<script>
|
|
289
|
+
class IgdThemingWidget {
|
|
290
|
+
private root: HTMLElement;
|
|
291
|
+
private trigger: HTMLButtonElement;
|
|
292
|
+
private panel: HTMLElement;
|
|
293
|
+
/** Whether the panel was promoted to a top-layer popover. */
|
|
294
|
+
private usePopover: boolean;
|
|
295
|
+
private reposition = () => this.position();
|
|
296
|
+
/** Backs the `system` mode: the OS color-scheme preference. */
|
|
297
|
+
private scheme = window.matchMedia('(prefers-color-scheme: dark)');
|
|
298
|
+
private onSchemeChange = () => {
|
|
299
|
+
// Only `system` tracks the OS; an explicit light/dark choice is fixed.
|
|
300
|
+
if (this.preference !== 'system') return;
|
|
301
|
+
this.applyResolvedMode();
|
|
302
|
+
this.emit();
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
constructor(root: HTMLElement) {
|
|
306
|
+
this.root = root;
|
|
307
|
+
this.trigger = root.querySelector('.igd-theming__trigger') as HTMLButtonElement;
|
|
308
|
+
this.panel = root.querySelector('.igd-theming__panel') as HTMLElement;
|
|
309
|
+
|
|
310
|
+
// Promote to the top layer so ancestors that clip (the sample container
|
|
311
|
+
// uses `overflow: hidden`) or create a stacking context can't swallow the
|
|
312
|
+
// panel. `manual` — not `auto` — because light dismiss and Escape are
|
|
313
|
+
// already handled below, and `auto` would also close the panel on the
|
|
314
|
+
// very click that opened it.
|
|
315
|
+
this.usePopover = typeof this.panel.showPopover === 'function';
|
|
316
|
+
if (this.usePopover) {
|
|
317
|
+
this.panel.setAttribute('popover', 'manual');
|
|
318
|
+
// A closed popover is `display: none` by UA style, so the `hidden`
|
|
319
|
+
// attribute (the no-JS fallback) is no longer needed and would win.
|
|
320
|
+
this.panel.hidden = false;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
this.bind();
|
|
324
|
+
|
|
325
|
+
// Turn a `system` selection into a concrete mode now, and keep following
|
|
326
|
+
// the OS for as long as it stays selected.
|
|
327
|
+
this.applyResolvedMode();
|
|
328
|
+
this.scheme.addEventListener('change', this.onSchemeChange);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private bind() {
|
|
332
|
+
this.trigger.addEventListener('click', () => this.toggle());
|
|
333
|
+
|
|
334
|
+
this.root.querySelectorAll<HTMLElement>('.igd-theming__option').forEach((opt) => {
|
|
335
|
+
opt.addEventListener('click', () => {
|
|
336
|
+
this.selectTheme(opt, true);
|
|
337
|
+
this.close();
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
this.root.querySelectorAll<HTMLElement>('.igd-theming__mode-btn').forEach((btn) => {
|
|
342
|
+
btn.addEventListener('click', () => {
|
|
343
|
+
this.selectMode(btn.dataset.modeBtn || 'light', true);
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
document.addEventListener('click', (e) => {
|
|
348
|
+
if (!this.root.contains(e.target as Node)) this.close();
|
|
349
|
+
});
|
|
350
|
+
document.addEventListener('keydown', (e) => {
|
|
351
|
+
if (e.key === 'Escape' && this.isOpen()) {
|
|
352
|
+
this.close();
|
|
353
|
+
this.trigger.focus();
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
private isOpen() {
|
|
359
|
+
return this.trigger.getAttribute('aria-expanded') === 'true';
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
private toggle() {
|
|
363
|
+
if (this.isOpen()) {
|
|
364
|
+
this.close();
|
|
365
|
+
} else {
|
|
366
|
+
this.open();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
private open() {
|
|
371
|
+
this.trigger.setAttribute('aria-expanded', 'true');
|
|
372
|
+
if (this.usePopover) {
|
|
373
|
+
this.panel.showPopover();
|
|
374
|
+
} else {
|
|
375
|
+
this.panel.hidden = false;
|
|
376
|
+
}
|
|
377
|
+
this.root.classList.add('is-open');
|
|
378
|
+
this.position();
|
|
379
|
+
// The trigger can live inside a scrolling container (or the page itself
|
|
380
|
+
// can scroll), so follow it for as long as the panel is open. Capture
|
|
381
|
+
// phase catches scrolls on any ancestor, not just the document.
|
|
382
|
+
window.addEventListener('scroll', this.reposition, { capture: true, passive: true });
|
|
383
|
+
window.addEventListener('resize', this.reposition, { passive: true });
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
private close() {
|
|
387
|
+
this.trigger.setAttribute('aria-expanded', 'false');
|
|
388
|
+
if (this.usePopover) {
|
|
389
|
+
if (this.panel.matches(':popover-open')) this.panel.hidePopover();
|
|
390
|
+
} else {
|
|
391
|
+
this.panel.hidden = true;
|
|
392
|
+
}
|
|
393
|
+
this.root.classList.remove('is-open', 'is-above');
|
|
394
|
+
window.removeEventListener('scroll', this.reposition, { capture: true });
|
|
395
|
+
window.removeEventListener('resize', this.reposition);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Place the panel below the trigger, or above it when the space below is
|
|
400
|
+
* too small and there is more room above. When neither side fits the full
|
|
401
|
+
* panel, the taller side wins and the panel is capped with a `max-height`
|
|
402
|
+
* (the list scrolls) so it always stays inside the viewport.
|
|
403
|
+
*/
|
|
404
|
+
private position() {
|
|
405
|
+
if (!this.isOpen()) return;
|
|
406
|
+
|
|
407
|
+
const GAP = 4; // matches the CSS offset of the fallback panel
|
|
408
|
+
const EDGE = 8; // keep this much clearance from the viewport edges
|
|
409
|
+
|
|
410
|
+
const anchor = this.trigger.getBoundingClientRect();
|
|
411
|
+
this.panel.style.maxHeight = '';
|
|
412
|
+
const height = this.panel.offsetHeight;
|
|
413
|
+
|
|
414
|
+
const roomBelow = window.innerHeight - anchor.bottom - GAP - EDGE;
|
|
415
|
+
const roomAbove = anchor.top - GAP - EDGE;
|
|
416
|
+
const above = height > roomBelow && roomAbove > roomBelow;
|
|
417
|
+
|
|
418
|
+
const available = above ? roomAbove : roomBelow;
|
|
419
|
+
if (height > available) this.panel.style.maxHeight = `${Math.max(available, 96)}px`;
|
|
420
|
+
this.root.classList.toggle('is-above', above);
|
|
421
|
+
|
|
422
|
+
if (!this.usePopover) return;
|
|
423
|
+
|
|
424
|
+
// Top-layer popovers are positioned against the viewport, so the
|
|
425
|
+
// trigger's viewport rect can be used directly.
|
|
426
|
+
const shown = this.panel.offsetHeight; // post-cap height
|
|
427
|
+
const top = above ? Math.max(EDGE, anchor.top - GAP - shown) : anchor.bottom + GAP;
|
|
428
|
+
const maxLeft = window.innerWidth - this.panel.offsetWidth - EDGE;
|
|
429
|
+
const left = Math.max(EDGE, Math.min(anchor.left, maxLeft));
|
|
430
|
+
|
|
431
|
+
this.panel.style.top = `${top}px`;
|
|
432
|
+
this.panel.style.left = `${left}px`;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
private selectTheme(opt: HTMLElement, emit: boolean) {
|
|
436
|
+
const name = opt.dataset.option || '';
|
|
437
|
+
const label = opt.dataset.label || name;
|
|
438
|
+
const swatch = (opt.dataset.swatch || '').split(',').filter(Boolean);
|
|
439
|
+
|
|
440
|
+
this.root
|
|
441
|
+
.querySelectorAll('.igd-theming__option')
|
|
442
|
+
.forEach((o) => o.setAttribute('aria-selected', 'false'));
|
|
443
|
+
opt.setAttribute('aria-selected', 'true');
|
|
444
|
+
|
|
445
|
+
const nameEl = this.trigger.querySelector('[data-theme-name]');
|
|
446
|
+
if (nameEl) nameEl.textContent = label;
|
|
447
|
+
|
|
448
|
+
const swatchEl = this.trigger.querySelector('[data-swatch]');
|
|
449
|
+
if (swatchEl) {
|
|
450
|
+
swatchEl.innerHTML = swatch
|
|
451
|
+
.map((c) => `<span class="igd-theming__dot" style="--dot: ${c}"></span>`)
|
|
452
|
+
.join('');
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
this.root.dataset.theme = name;
|
|
456
|
+
this.reflect();
|
|
457
|
+
if (emit) this.emit();
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** The user's selection: `light`, `dark` or `system`. */
|
|
461
|
+
private get preference() {
|
|
462
|
+
return this.root.dataset.mode || 'light';
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** The concrete mode that selection maps to: `light` or `dark`. */
|
|
466
|
+
private get resolvedMode() {
|
|
467
|
+
const pref = this.preference;
|
|
468
|
+
if (pref === 'system') return this.scheme.matches ? 'dark' : 'light';
|
|
469
|
+
return pref === 'dark' ? 'dark' : 'light';
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Publish the resolved mode on the root — so consumers that read the DOM
|
|
474
|
+
* instead of listening (the sample widget does, when its iframe loads) get
|
|
475
|
+
* a concrete value — and push it out to the `target` elements.
|
|
476
|
+
*/
|
|
477
|
+
private applyResolvedMode() {
|
|
478
|
+
const resolved = this.resolvedMode;
|
|
479
|
+
this.root.dataset.resolvedMode = resolved;
|
|
480
|
+
|
|
481
|
+
// The trigger names the mode the sample actually renders in, so `system`
|
|
482
|
+
// reads as "Light" / "Dark" there — the selection itself stays visible
|
|
483
|
+
// through the pressed state of the mode buttons.
|
|
484
|
+
const modeNameEl = this.trigger.querySelector('[data-mode-name]');
|
|
485
|
+
if (modeNameEl) {
|
|
486
|
+
modeNameEl.textContent = resolved.charAt(0).toUpperCase() + resolved.slice(1);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
this.reflect();
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
private selectMode(mode: string, emit: boolean) {
|
|
493
|
+
this.root.querySelectorAll<HTMLElement>('.igd-theming__mode-btn').forEach((b) => {
|
|
494
|
+
b.setAttribute('aria-pressed', b.dataset.modeBtn === mode ? 'true' : 'false');
|
|
495
|
+
});
|
|
496
|
+
this.root.dataset.mode = mode;
|
|
497
|
+
|
|
498
|
+
this.applyResolvedMode();
|
|
499
|
+
if (emit) this.emit();
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
private reflect() {
|
|
503
|
+
const sel = this.root.dataset.target;
|
|
504
|
+
if (!sel) return;
|
|
505
|
+
document.querySelectorAll<HTMLElement>(sel).forEach((el) => {
|
|
506
|
+
el.dataset.igdTheme = this.root.dataset.theme || '';
|
|
507
|
+
el.dataset.igdMode = this.root.dataset.resolvedMode || '';
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
private emit() {
|
|
512
|
+
const detail = {
|
|
513
|
+
theme: this.root.dataset.theme,
|
|
514
|
+
// Concrete, so consumers never have to resolve `system` themselves...
|
|
515
|
+
mode: this.root.dataset.resolvedMode,
|
|
516
|
+
// ...while the raw selection stays available for mirroring the UI.
|
|
517
|
+
modePreference: this.root.dataset.mode,
|
|
518
|
+
};
|
|
519
|
+
this.root.dispatchEvent(new CustomEvent('igd-theme-change', { detail, bubbles: true }));
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function initThemingWidgets() {
|
|
524
|
+
document.querySelectorAll<HTMLElement>('.igd-theming').forEach((el) => {
|
|
525
|
+
if ((el as HTMLElement & { _igdInit?: boolean })._igdInit) return;
|
|
526
|
+
(el as HTMLElement & { _igdInit?: boolean })._igdInit = true;
|
|
527
|
+
new IgdThemingWidget(el);
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
document.addEventListener('astro:page-load', initThemingWidgets);
|
|
532
|
+
initThemingWidgets();
|
|
533
|
+
</script>
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
@use 'igniteui-theming/sass/typography' as *;
|
|
2
|
+
|
|
3
|
+
@layer components {
|
|
4
|
+
.igd-theming {
|
|
5
|
+
// Public custom properties (override from an ancestor or via `class`)
|
|
6
|
+
--igd-theming-radius: #{rem(5px)};
|
|
7
|
+
--igd-theming-border: var(--ig-gray-300);
|
|
8
|
+
--igd-theming-bg: light-dark(#fff, var(--ig-gray-100));
|
|
9
|
+
--igd-theming-fg: var(--ig-gray-900);
|
|
10
|
+
--igd-theming-muted: var(--ig-gray-500);
|
|
11
|
+
--igd-theming-hover-bg: var(--ig-gray-100);
|
|
12
|
+
--igd-theming-accent: var(--igd-accent, var(--ig-primary-500));
|
|
13
|
+
--igd-theming-selected-bg: rgb(33 33 33 / 8%);
|
|
14
|
+
--igd-theming-selected-bg: color-mix(in srgb, var(--ig-gray-900) 8%, transparent);
|
|
15
|
+
|
|
16
|
+
position: relative;
|
|
17
|
+
display: inline-block;
|
|
18
|
+
font-family: var(--ig-font-family, 'Aktiv Grotesk', var(--igd-font-sans, sans-serif));
|
|
19
|
+
font-size: #{rem(14px)};
|
|
20
|
+
line-height: #{rem(20px)};
|
|
21
|
+
color: var(--igd-theming-fg);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ── Trigger ──────────────────────────────────────────────────────────────
|
|
25
|
+
.igd-theming__trigger {
|
|
26
|
+
display: inline-flex;
|
|
27
|
+
align-items: center;
|
|
28
|
+
gap: rem(8px);
|
|
29
|
+
width: rem(203px);
|
|
30
|
+
box-sizing: border-box;
|
|
31
|
+
height: rem(40px);
|
|
32
|
+
min-height: rem(40px);
|
|
33
|
+
padding: rem(8px) rem(16px);
|
|
34
|
+
background: var(--igd-theming-bg);
|
|
35
|
+
color: var(--igd-theming-fg);
|
|
36
|
+
border: rem(1px) solid var(--igd-theming-border);
|
|
37
|
+
border-radius: var(--igd-theming-radius);
|
|
38
|
+
cursor: pointer;
|
|
39
|
+
line-height: #{rem(20px)};
|
|
40
|
+
transition:
|
|
41
|
+
border-color 0.15s ease,
|
|
42
|
+
box-shadow 0.15s ease;
|
|
43
|
+
|
|
44
|
+
&:focus-visible {
|
|
45
|
+
outline: none;
|
|
46
|
+
border-color: var(--igd-theming-accent);
|
|
47
|
+
box-shadow: 0 0 0 rem(2px) color-mix(in srgb, var(--igd-theming-accent) 25%, transparent);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.igd-theming__value {
|
|
52
|
+
display: inline-flex;
|
|
53
|
+
align-items: baseline;
|
|
54
|
+
gap: rem(4px);
|
|
55
|
+
margin-inline-end: auto;
|
|
56
|
+
white-space: nowrap;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.igd-theming__theme-name {
|
|
60
|
+
font-weight: 500;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.igd-theming__mode-name {
|
|
64
|
+
color: var(--igd-theming-muted);
|
|
65
|
+
font-weight: 400;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.igd-theming__chevron {
|
|
69
|
+
flex-shrink: 0;
|
|
70
|
+
color: var(--igd-theming-muted);
|
|
71
|
+
transition: transform 0.15s ease;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.igd-theming.is-open .igd-theming__chevron {
|
|
75
|
+
transform: rotate(180deg);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ── Swatch ───────────────────────────────────────────────────────────────
|
|
79
|
+
.igd-theming__swatch {
|
|
80
|
+
display: inline-flex;
|
|
81
|
+
flex-shrink: 0;
|
|
82
|
+
align-items: center;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.igd-theming__dot {
|
|
86
|
+
display: inline-block;
|
|
87
|
+
width: rem(12px);
|
|
88
|
+
height: rem(12px);
|
|
89
|
+
border-radius: 50%;
|
|
90
|
+
background: var(--dot);
|
|
91
|
+
box-shadow: 0 0 0 rem(3px) var(--igd-theming-bg);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Light mode shows only the two accent dots; dark mode adds the leading
|
|
95
|
+
// (surface) dot for three total. Mirrors the Figma light/dark variants.
|
|
96
|
+
.igd-theming[data-mode='light'] .igd-theming__dot:first-child {
|
|
97
|
+
display: none;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// The now-first visible dot must not keep the overlap offset.
|
|
101
|
+
.igd-theming[data-mode='light'] .igd-theming__dot:first-child + .igd-theming__dot {
|
|
102
|
+
margin-inline-start: 0;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// `system` follows the OS preference. The dark arrangement is the default,
|
|
106
|
+
// so only the light half needs a rule — expressed in CSS rather than through
|
|
107
|
+
// the script's resolved mode so the swatch is already correct on first paint.
|
|
108
|
+
@media (prefers-color-scheme: light) {
|
|
109
|
+
.igd-theming[data-mode='system'] .igd-theming__dot:first-child {
|
|
110
|
+
display: none;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.igd-theming[data-mode='system'] .igd-theming__dot:first-child + .igd-theming__dot {
|
|
114
|
+
margin-inline-start: 0;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ── Panel ────────────────────────────────────────────────────────────────
|
|
119
|
+
// Fallback placement, used when the browser has no Popover API. The panel
|
|
120
|
+
// stays in flow next to the trigger and is therefore still subject to any
|
|
121
|
+
// clipping ancestor; it can at least flip via `.is-above`.
|
|
122
|
+
.igd-theming__panel {
|
|
123
|
+
position: absolute;
|
|
124
|
+
inset-block-start: calc(100% + #{rem(4px)});
|
|
125
|
+
inset-inline-start: 0;
|
|
126
|
+
z-index: 100;
|
|
127
|
+
width: rem(203px);
|
|
128
|
+
box-sizing: border-box;
|
|
129
|
+
padding-block: 0;
|
|
130
|
+
background: var(--igd-theming-bg);
|
|
131
|
+
border: rem(1px) solid var(--igd-theming-border);
|
|
132
|
+
border-radius: var(--igd-theming-radius);
|
|
133
|
+
box-shadow: 0 rem(8px) rem(24px) rgb(0 0 0 / 12%);
|
|
134
|
+
overflow: hidden auto;
|
|
135
|
+
|
|
136
|
+
&[hidden] {
|
|
137
|
+
display: none;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
.igd-theming.is-above .igd-theming__panel {
|
|
142
|
+
inset-block-start: auto;
|
|
143
|
+
inset-block-end: calc(100% + #{rem(4px)});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Top-layer placement: the script sets `popover` on the panel and drives
|
|
147
|
+
// `top`/`left` from the trigger's viewport rect, so the UA popover centering
|
|
148
|
+
// (`margin: auto; inset: 0`) has to be cleared.
|
|
149
|
+
.igd-theming__panel:popover-open {
|
|
150
|
+
position: fixed;
|
|
151
|
+
inset: auto;
|
|
152
|
+
margin: 0;
|
|
153
|
+
overflow: hidden auto;
|
|
154
|
+
overscroll-behavior: contain;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// ── Mode row ─────────────────────────────────────────────────────────────
|
|
158
|
+
.igd-theming__mode-row {
|
|
159
|
+
display: flex;
|
|
160
|
+
align-items: center;
|
|
161
|
+
justify-content: space-between;
|
|
162
|
+
gap: rem(12px);
|
|
163
|
+
min-height: rem(40px);
|
|
164
|
+
padding-inline: rem(12px);
|
|
165
|
+
border: none;
|
|
166
|
+
margin: 0;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
.igd-theming__section-label {
|
|
170
|
+
@include type-style('overline');
|
|
171
|
+
|
|
172
|
+
color: var(--ig-gray-500);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
.igd-theming__mode-buttons {
|
|
176
|
+
display: inline-flex;
|
|
177
|
+
gap: 0;
|
|
178
|
+
margin: 0;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
.igd-theming__mode-btn {
|
|
182
|
+
display: inline-flex;
|
|
183
|
+
align-items: center;
|
|
184
|
+
justify-content: center;
|
|
185
|
+
width: rem(24px);
|
|
186
|
+
height: rem(24px);
|
|
187
|
+
padding: 0;
|
|
188
|
+
margin: 0;
|
|
189
|
+
background: transparent;
|
|
190
|
+
color: var(--igd-theming-muted);
|
|
191
|
+
border: none;
|
|
192
|
+
border-radius: 50%;
|
|
193
|
+
cursor: pointer;
|
|
194
|
+
transition:
|
|
195
|
+
background 0.15s ease,
|
|
196
|
+
color 0.15s ease;
|
|
197
|
+
|
|
198
|
+
&:hover {
|
|
199
|
+
background: var(--igd-theming-hover-bg);
|
|
200
|
+
color: var(--igd-theming-fg);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
&[aria-pressed='true'] {
|
|
204
|
+
background-color: var(--igd-theming-selected-bg);
|
|
205
|
+
color: var(--igd-theming-fg);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
&:focus-visible {
|
|
209
|
+
outline: rem(2px) solid var(--igd-theming-accent);
|
|
210
|
+
outline-offset: rem(1px);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ── Theme list ───────────────────────────────────────────────────────────
|
|
215
|
+
.igd-theming .igd-theming__list {
|
|
216
|
+
list-style: none;
|
|
217
|
+
margin: 0;
|
|
218
|
+
padding: 0;
|
|
219
|
+
border: none;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
.igd-theming__list > li {
|
|
223
|
+
margin: 0;
|
|
224
|
+
padding: 0;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
.igd-theming .igd-theming__option {
|
|
228
|
+
display: flex;
|
|
229
|
+
align-items: center;
|
|
230
|
+
justify-content: flex-start;
|
|
231
|
+
gap: rem(8px);
|
|
232
|
+
width: 100%;
|
|
233
|
+
box-sizing: border-box;
|
|
234
|
+
height: rem(40px);
|
|
235
|
+
min-height: rem(40px);
|
|
236
|
+
padding-block: 0;
|
|
237
|
+
padding-inline: rem(24px) rem(12px);
|
|
238
|
+
background: transparent;
|
|
239
|
+
color: #00142b;
|
|
240
|
+
border: none;
|
|
241
|
+
cursor: pointer;
|
|
242
|
+
text-align: start;
|
|
243
|
+
font-family: var(--ig-body-2-font-family, var(--ig-font-family, 'Aktiv Grotesk', sans-serif));
|
|
244
|
+
font-size: #{rem(14px)};
|
|
245
|
+
line-height: #{rem(20px)};
|
|
246
|
+
letter-spacing: 0.25px;
|
|
247
|
+
font-weight: 500;
|
|
248
|
+
|
|
249
|
+
&:hover {
|
|
250
|
+
background: var(--igd-theming-hover-bg);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
&:focus-visible {
|
|
254
|
+
outline: rem(2px) solid var(--igd-theming-accent);
|
|
255
|
+
outline-offset: rem(-2px);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
.igd-theming__option-name {
|
|
260
|
+
margin-inline-end: auto;
|
|
261
|
+
font-family: var(--ig-body-2-font-family, var(--ig-font-family, 'Aktiv Grotesk', sans-serif));
|
|
262
|
+
font-size: #{rem(14px)};
|
|
263
|
+
line-height: #{rem(20px)};
|
|
264
|
+
letter-spacing: 0.25px;
|
|
265
|
+
font-weight: 500;
|
|
266
|
+
color: #00142b;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
.igd-theming__option[aria-selected='true'] .igd-theming__option-name {
|
|
270
|
+
font-weight: 500;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
.igd-theming__check {
|
|
274
|
+
flex-shrink: 0;
|
|
275
|
+
color: var(--igd-theming-accent);
|
|
276
|
+
opacity: 0;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
.igd-theming__option[aria-selected='true'] .igd-theming__check {
|
|
280
|
+
opacity: 1;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
@@ -10,22 +10,23 @@ import Sample from 'igniteui-astro-components/components/mdx/Sample.astro';
|
|
|
10
10
|
|
|
11
11
|
## Props
|
|
12
12
|
|
|
13
|
-
| Prop
|
|
14
|
-
|
|
|
15
|
-
| `src`
|
|
16
|
-
| `height`
|
|
17
|
-
| `alt`
|
|
18
|
-
| `lob`
|
|
19
|
-
| `dv`
|
|
20
|
-
| `crm`
|
|
21
|
-
| `iframeOnly`
|
|
22
|
-
| `fullscreenBtn`
|
|
23
|
-
| `fitContent`
|
|
24
|
-
| `spacing`
|
|
25
|
-
| `position`
|
|
26
|
-
| `resizable`
|
|
27
|
-
| `noBorder`
|
|
28
|
-
| `noBackground`
|
|
13
|
+
| Prop | Type | Default | Description |
|
|
14
|
+
| ----------------- | -------------------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
15
|
+
| `src` | `string` | _(required)_ | Sample path relative to the demos base URL, e.g. `"/charts/data-chart/axis-crossing"`. |
|
|
16
|
+
| `height` | `number` | `150` (`50` if `fitContent`) | iframe height in pixels. With `fitContent` it is only the loading placeholder / cross-origin fallback. |
|
|
17
|
+
| `alt` | `string` | `''` | `title` attribute for the iframe (accessibility). |
|
|
18
|
+
| `lob` | `boolean` | `false` | Use `lobDemosBaseUrl` instead of `demosBaseUrl` (LOB/grid-dynamic demos). |
|
|
19
|
+
| `dv` | `boolean` | `false` | Force use of `dvDemosBaseUrl` (data-viz demos). Auto-detected for paths starting with `charts/`, `gauges/`, `maps/`, `excel/`. |
|
|
20
|
+
| `crm` | `boolean` | `false` | Use `crmDemoBaseUrl` (CRM demo samples). |
|
|
21
|
+
| `iframeOnly` | `boolean` | `false` | Render only the iframe — no navbar, no code tabs, no live-edit footer. |
|
|
22
|
+
| `fullscreenBtn` | `boolean` | `false` | When `iframeOnly={true}`, also renders a "View in full screen" button below the iframe. |
|
|
23
|
+
| `fitContent` | `boolean` | `false` | Size the iframe to fit its content — the embedded page dictates the size. Same-origin demos are measured directly; cross-origin demos must report via `postMessage`. Demos with ink overflow (shadows, glows, focus rings) must reserve room for it in their own layout, since an iframe clips at its edge. |
|
|
24
|
+
| `spacing` | `'none' \| 'sm' \| 'md' \| 'lg'` | `'none'` | Padding around the iframe inside the container: `none` = 0, `sm` = 8px, `md` = 16px, `lg` = 32px. Applied by the host container, never inside the embedded page. |
|
|
25
|
+
| `position` | `string` | — | Where the iframe sits inside the container when it doesn't fill it. Accepts a horizontal token (`start`, `center`, `end`) and/or a vertical token (`top`, `center`, `bottom`), space-separated in any order — e.g. `"center"`, `"start top"`, `"end bottom"`. |
|
|
26
|
+
| `resizable` | `boolean` | `false` | Show a drag handle on the right edge that resizes the iframe's width so responsive demos can be previewed at different widths. Left/right arrow keys resize it when focused. Combines with `fitContent`: the drag owns the width, the content keeps dictating the height. |
|
|
27
|
+
| `noBorder` | `boolean` | `true` | Drop the 1px border drawn around the iframe/container. Pass `noBorder={false}` to draw it — recommended whenever chrome (code tabs or `fullscreenBtn`) is visible. |
|
|
28
|
+
| `noBackground` | `boolean` | `true` | Hide the grid-pattern backdrop drawn behind the iframe, keeping the container fully transparent. Pass `noBackground={false}` to show it. |
|
|
29
|
+
| `whiteBackground` | `boolean` | `false` | Paint the backdrop behind the iframe pure white (`#fff`) instead of the default light gray. Only the light side changes — dark mode (the site's, or the in-sample theming widget's) is left as is. Implies a painted background, so it also overrides the transparency `noBackground` leaves behind in light mode. |
|
|
29
30
|
|
|
30
31
|
## Examples
|
|
31
32
|
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* when a separate demosBaseUrl is present in the environment config.
|
|
16
16
|
*/
|
|
17
17
|
import type { PlatformContext } from '../../../lib/types.ts';
|
|
18
|
+
import ThemingWidget from '../../ThemingWidget/ThemingWidget.astro';
|
|
18
19
|
import './Sample.scss';
|
|
19
20
|
|
|
20
21
|
interface Props {
|
|
@@ -35,6 +36,11 @@ interface Props {
|
|
|
35
36
|
iframeOnly?: boolean;
|
|
36
37
|
/** When used with iframeOnly, also render a "View in full screen" button below. */
|
|
37
38
|
fullscreenBtn?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Opt in to the in-sample theme picker for this sample. Its initial theme is
|
|
41
|
+
* resolved from the site (see `defaultTheme` below), not passed per usage.
|
|
42
|
+
*/
|
|
43
|
+
showThemePicker?: boolean;
|
|
38
44
|
/**
|
|
39
45
|
* Size the iframe to fit its content: the embedded page dictates the
|
|
40
46
|
* iframe's size. `height` serves only as the placeholder/fallback — shown
|
|
@@ -77,8 +83,21 @@ interface Props {
|
|
|
77
83
|
* Hide the grid-pattern backdrop drawn behind the iframe (default). The
|
|
78
84
|
* container is fully transparent, so a demo without a background of its own
|
|
79
85
|
* sits directly on the page.
|
|
86
|
+
*
|
|
87
|
+
* Defaults to `false` when `showThemePicker` is on: a sample whose theme —
|
|
88
|
+
* and mode — the reader can switch needs a surface of its own to switch
|
|
89
|
+
* against, otherwise a dark-mode demo would sit on the page's own light
|
|
90
|
+
* background. An explicit value always wins.
|
|
80
91
|
*/
|
|
81
92
|
noBackground?: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Paint the backdrop behind the iframe pure white (#fff) instead of the
|
|
95
|
+
* default light gray. Only the light side changes: in dark mode — the site's
|
|
96
|
+
* own, or the in-sample theming widget's — the background is left exactly as
|
|
97
|
+
* it was. Asking for a white background implies a painted one, so it also
|
|
98
|
+
* overrides the transparency `noBackground` leaves behind (in light mode).
|
|
99
|
+
*/
|
|
100
|
+
whiteBackground?: boolean;
|
|
82
101
|
}
|
|
83
102
|
|
|
84
103
|
const {
|
|
@@ -89,14 +108,19 @@ const {
|
|
|
89
108
|
crm = false,
|
|
90
109
|
iframeOnly = false,
|
|
91
110
|
fullscreenBtn = false,
|
|
111
|
+
showThemePicker = false,
|
|
92
112
|
fitContent = false,
|
|
93
113
|
spacing = 'none',
|
|
94
114
|
position,
|
|
95
115
|
resizable = false,
|
|
96
116
|
noBorder = true,
|
|
97
|
-
|
|
117
|
+
whiteBackground = false,
|
|
98
118
|
} = Astro.props;
|
|
99
119
|
|
|
120
|
+
// The theme picker implies a painted backdrop (see the prop's doc), so its
|
|
121
|
+
// default flips with `showThemePicker`. An explicitly passed value still wins.
|
|
122
|
+
const noBackground = Astro.props.noBackground ?? !showThemePicker;
|
|
123
|
+
|
|
100
124
|
// Fixed samples default to 150px. In fitContent mode `height` is only the
|
|
101
125
|
// loading placeholder, so an unspecified one defaults lower (50px) to avoid
|
|
102
126
|
// reserving a tall wall of space that collapses once the demo reports its size.
|
|
@@ -105,6 +129,18 @@ const { name, lower } = Astro.locals.platformContext as PlatformContext;
|
|
|
105
129
|
|
|
106
130
|
const env = Astro.locals.envVars ?? {};
|
|
107
131
|
|
|
132
|
+
// The in-sample theme picker opens on the theme the platform's sample browser
|
|
133
|
+
// itself ships: the Angular browser serves Material, the xplat ones (React /
|
|
134
|
+
// Web Components / Blazor) serve Bootstrap. Deriving it from the platform keeps
|
|
135
|
+
// every <Sample> in the docs correct without a per-usage prop, and a host can
|
|
136
|
+
// still override it for a whole site via `defaultSampleTheme`. This is not
|
|
137
|
+
// cosmetic — the picker's current selection is posted into the iframe on every
|
|
138
|
+
// load (sample-widget.ts), so a wrong default actively re-themes the demo.
|
|
139
|
+
const PLATFORM_DEFAULT_THEME: Partial<Record<PlatformContext['name'], string>> = {
|
|
140
|
+
Angular: 'material',
|
|
141
|
+
};
|
|
142
|
+
const defaultTheme = env.defaultSampleTheme ?? PLATFORM_DEFAULT_THEME[name] ?? 'bootstrap';
|
|
143
|
+
|
|
108
144
|
// ─ Layout options (spacing / position / resize) ────────────────────────────────
|
|
109
145
|
const spacingVal = ['none', 'sm', 'md', 'lg'].includes(spacing) ? spacing : 'none';
|
|
110
146
|
|
|
@@ -167,6 +203,7 @@ const exampleTabId = `${widgetId}-example`;
|
|
|
167
203
|
'igd-sample-container--resizable': isResizable,
|
|
168
204
|
'igd-sample-container--no-border': noBorder,
|
|
169
205
|
'igd-sample-container--no-background': noBackground,
|
|
206
|
+
'igd-sample-container--white-background': whiteBackground,
|
|
170
207
|
},
|
|
171
208
|
]}
|
|
172
209
|
data-fit-content={fitContent ? 'true' : undefined}
|
|
@@ -228,6 +265,7 @@ const exampleTabId = `${widgetId}-example`;
|
|
|
228
265
|
'igd-sample-container--resizable': isResizable,
|
|
229
266
|
'igd-sample-container--no-border': noBorder,
|
|
230
267
|
'igd-sample-container--no-background': noBackground,
|
|
268
|
+
'igd-sample-container--white-background': whiteBackground,
|
|
231
269
|
},
|
|
232
270
|
]}
|
|
233
271
|
data-fit-content={fitContent ? 'true' : undefined}
|
|
@@ -264,6 +302,11 @@ const exampleTabId = `${widgetId}-example`;
|
|
|
264
302
|
</igc-tab>
|
|
265
303
|
</igc-tabs>
|
|
266
304
|
</div>
|
|
305
|
+
{showThemePicker && (
|
|
306
|
+
<div class="igd-sample-theming" hidden>
|
|
307
|
+
<ThemingWidget selectedTheme={defaultTheme} />
|
|
308
|
+
</div>
|
|
309
|
+
)}
|
|
267
310
|
</div>
|
|
268
311
|
</>
|
|
269
312
|
)
|
|
@@ -124,6 +124,11 @@ igc-tab::part(tab-header) {
|
|
|
124
124
|
|
|
125
125
|
.igd-sample-container {
|
|
126
126
|
--sample-container-header-height: #{rem(66px)};
|
|
127
|
+
// Two selectable backgrounds: the site's own light/dark surface (default),
|
|
128
|
+
// or a dedicated dark surface used when the in-sample theming widget's mode
|
|
129
|
+
// is `dark` — see `[data-igd-mode='dark']` below.
|
|
130
|
+
--igd-sample-bg-light-dark: light-dark(var(--ig-gray-100), var(--ig-surface-500));
|
|
131
|
+
--igd-sample-bg-dark: #0a0b10;
|
|
127
132
|
|
|
128
133
|
display: grid;
|
|
129
134
|
position: relative;
|
|
@@ -131,7 +136,11 @@ igc-tab::part(tab-header) {
|
|
|
131
136
|
overflow: hidden;
|
|
132
137
|
margin-block-end: rem(26px);
|
|
133
138
|
z-index: 0;
|
|
134
|
-
background:
|
|
139
|
+
background: var(--igd-sample-bg-light-dark);
|
|
140
|
+
|
|
141
|
+
&[data-igd-mode='dark'] {
|
|
142
|
+
background: var(--igd-sample-bg-dark);
|
|
143
|
+
}
|
|
135
144
|
|
|
136
145
|
iframe {
|
|
137
146
|
display: block;
|
|
@@ -202,6 +211,21 @@ igc-tab::part(tab-header) {
|
|
|
202
211
|
}
|
|
203
212
|
}
|
|
204
213
|
|
|
214
|
+
// Pure white backdrop instead of the default light gray. Only the light half
|
|
215
|
+
// of the token is swapped, so dark mode is untouched — both the site's dark
|
|
216
|
+
// surface and the theming widget's dedicated `--igd-sample-bg-dark`.
|
|
217
|
+
&--white-background {
|
|
218
|
+
--igd-sample-bg-light-dark: light-dark(#fff, var(--ig-surface-500));
|
|
219
|
+
|
|
220
|
+
// Asking for white implies a painted surface, so it also wins over the
|
|
221
|
+
// transparent container `--no-background` leaves behind — in light mode
|
|
222
|
+
// only: dark keeps the transparency it had. The `:not()` guard leaves the
|
|
223
|
+
// theming widget's dark surface in charge whenever its mode is `dark`.
|
|
224
|
+
&.igd-sample-container--no-background:not([data-igd-mode='dark']) {
|
|
225
|
+
background: light-dark(#fff, transparent);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
205
229
|
// Wrapper around the iframe. In the default (block) layout it is transparent
|
|
206
230
|
// (fills the container just like the bare iframe did); when the container
|
|
207
231
|
// opts into flex layout it becomes the positioned/resizable element.
|
|
@@ -377,6 +401,11 @@ igc-tab::part(tab-header) {
|
|
|
377
401
|
border-block-start: rem(1px) solid var(--ig-gray-300);
|
|
378
402
|
}
|
|
379
403
|
|
|
404
|
+
// The in-sample theme picker is appended after the edit controls.
|
|
405
|
+
.igd-code-view__footer-actions .igd-sample-theming {
|
|
406
|
+
margin-inline-start: 0;
|
|
407
|
+
}
|
|
408
|
+
|
|
380
409
|
.igd-code-view__header-actions,
|
|
381
410
|
.igd-code-view__footer-actions {
|
|
382
411
|
display: flex;
|
|
@@ -256,6 +256,10 @@ function addFooter(
|
|
|
256
256
|
const footer = document.createElement('div');
|
|
257
257
|
footer.className = 'igd-code-view__footer-actions';
|
|
258
258
|
|
|
259
|
+
// Move the server-rendered theme picker (if any) into the existing footer,
|
|
260
|
+
// after the edit controls so it is the rightmost footer item.
|
|
261
|
+
const themingBar = widget.querySelector<HTMLElement>('.igd-sample-theming');
|
|
262
|
+
|
|
259
263
|
if ((!explicitEditor || explicitEditor === 'stackblitz') && onStackblitz) {
|
|
260
264
|
const btn = document.createElement('igc-button') as HTMLElement;
|
|
261
265
|
btn.setAttribute('variant', 'outlined');
|
|
@@ -279,6 +283,11 @@ function addFooter(
|
|
|
279
283
|
// footer.appendChild(btn);
|
|
280
284
|
// }
|
|
281
285
|
|
|
286
|
+
if (themingBar) {
|
|
287
|
+
themingBar.hidden = false;
|
|
288
|
+
footer.appendChild(themingBar);
|
|
289
|
+
}
|
|
290
|
+
|
|
282
291
|
const fsBtn = document.createElement('igc-icon-button') as HTMLElement;
|
|
283
292
|
fsBtn.setAttribute('name', 'open-link-blank');
|
|
284
293
|
fsBtn.setAttribute('collection', 'docs');
|
|
@@ -428,6 +437,9 @@ class XplatCodeService {
|
|
|
428
437
|
this.platform = platform;
|
|
429
438
|
this.samplesOrder = XPLAT_SAMPLES_ORDER[platform] || ['ts', 'html', 'css'];
|
|
430
439
|
this.codeBase = XPLAT_CODE_BASE[platform] || '/code-viewer/';
|
|
440
|
+
// Gates only the StackBlitz / CodeSandbox buttons — Blazor samples can't be
|
|
441
|
+
// live-edited in a browser sandbox. The footer itself is always rendered:
|
|
442
|
+
// it also carries the fullscreen button and the in-sample theme picker.
|
|
431
443
|
this.enableLiveEditing = platform !== 'blazor';
|
|
432
444
|
}
|
|
433
445
|
|
|
@@ -436,6 +448,10 @@ class XplatCodeService {
|
|
|
436
448
|
const samplePath = getSamplePath(iframeSrc, demosBaseUrl);
|
|
437
449
|
const fallbackStackblitz = () => this._openStackBlitzUrl(githubSrc, demosBaseUrl);
|
|
438
450
|
const fallbackCodeSandbox = () => this._openCodeSandbox(githubSrc, demosBaseUrl);
|
|
451
|
+
// Live editing needs both a platform that supports it and a repo path to
|
|
452
|
+
// open; without either, the footer is still built — just without the
|
|
453
|
+
// edit-in buttons.
|
|
454
|
+
const liveEdit = this.enableLiveEditing && Boolean(githubSrc);
|
|
439
455
|
|
|
440
456
|
try {
|
|
441
457
|
// Blazor's local dev server uses a self-signed cert on a non-4200 port,
|
|
@@ -460,15 +476,23 @@ class XplatCodeService {
|
|
|
460
476
|
)
|
|
461
477
|
.forEach((file: SampleFile) => addCodeTab(igcTabs, file));
|
|
462
478
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
479
|
+
// Use sdk.openProject() to avoid the StackBlitz GitHub clone freeze bug.
|
|
480
|
+
addFooter(
|
|
481
|
+
widget,
|
|
482
|
+
iframeSrc,
|
|
483
|
+
null,
|
|
484
|
+
liveEdit ? () => this._openInStackBlitz(data) : null,
|
|
485
|
+
liveEdit ? fallbackCodeSandbox : null,
|
|
486
|
+
);
|
|
467
487
|
} catch (err: any) {
|
|
468
488
|
console.warn('[sample-widget] Could not fetch xplat sample files:', err.message);
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
489
|
+
addFooter(
|
|
490
|
+
widget,
|
|
491
|
+
iframeSrc,
|
|
492
|
+
null,
|
|
493
|
+
liveEdit ? fallbackStackblitz : null,
|
|
494
|
+
liveEdit ? fallbackCodeSandbox : null,
|
|
495
|
+
);
|
|
472
496
|
}
|
|
473
497
|
}
|
|
474
498
|
|
|
@@ -902,6 +926,58 @@ export function initSampleWidgets(): void {
|
|
|
902
926
|
|
|
903
927
|
const exampleTabId = `${widget.id}-example`;
|
|
904
928
|
|
|
929
|
+
// Wire the in-sample theme picker: on change (and on each iframe load)
|
|
930
|
+
// post the selection to the sample iframe so it can re-render with the
|
|
931
|
+
// chosen theme/mode. Targets the iframe's own origin (never a wildcard
|
|
932
|
+
// when the origin is known) to avoid leaking the message to other frames.
|
|
933
|
+
const themingBar = widget.querySelector<HTMLElement>('.igd-sample-theming');
|
|
934
|
+
if (themingBar && iframe) {
|
|
935
|
+
const targetOrigin = (() => {
|
|
936
|
+
try {
|
|
937
|
+
return new URL(iframeSrc, window.location.origin).origin;
|
|
938
|
+
} catch {
|
|
939
|
+
return window.location.origin;
|
|
940
|
+
}
|
|
941
|
+
})();
|
|
942
|
+
const postTheme = (theme?: string, mode?: string) => {
|
|
943
|
+
if (!theme) return;
|
|
944
|
+
iframe.contentWindow?.postMessage(
|
|
945
|
+
{
|
|
946
|
+
// `type` is kept compatible with the existing Angular sample
|
|
947
|
+
// browser contract. `event` is an explicit alias for xplat
|
|
948
|
+
// sample browsers that use a named event discriminator.
|
|
949
|
+
type: 'igd-sample-theme',
|
|
950
|
+
event: 'igd-sample-theme',
|
|
951
|
+
theme,
|
|
952
|
+
themeName: theme,
|
|
953
|
+
mode,
|
|
954
|
+
},
|
|
955
|
+
targetOrigin,
|
|
956
|
+
);
|
|
957
|
+
};
|
|
958
|
+
const themingRoot = themingBar.querySelector<HTMLElement>('.igd-theming');
|
|
959
|
+
// Reflects the widget's mode onto the sample pane so its own background
|
|
960
|
+
// (not just the iframe content) can switch between the light-dark
|
|
961
|
+
// default and the dedicated dark surface — see `--igd-sample-bg-dark`.
|
|
962
|
+
const reflectMode = (mode?: string) => {
|
|
963
|
+
if (samplePane) samplePane.dataset.igdMode = mode || '';
|
|
964
|
+
};
|
|
965
|
+
themingBar.addEventListener('igd-theme-change', (e: Event) => {
|
|
966
|
+
const detail = (e as CustomEvent<{ theme: string; mode: string }>).detail;
|
|
967
|
+
postTheme(detail?.theme, detail?.mode);
|
|
968
|
+
reflectMode(detail?.mode);
|
|
969
|
+
});
|
|
970
|
+
// Send the current (possibly persisted) selection once the sample
|
|
971
|
+
// loads. `resolvedMode` is the widget's `system` selection already
|
|
972
|
+
// resolved against `prefers-color-scheme`; `mode` is only the fallback
|
|
973
|
+
// for a widget whose script has not initialized yet.
|
|
974
|
+
iframe.addEventListener('load', () => {
|
|
975
|
+
const mode = themingRoot?.dataset.resolvedMode || themingRoot?.dataset.mode;
|
|
976
|
+
postTheme(themingRoot?.dataset.theme, mode);
|
|
977
|
+
reflectMode(mode);
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
|
|
905
981
|
// Toggle fullscreen button visibility based on active tab.
|
|
906
982
|
igcTabs.addEventListener('igcChange', (e: Event) => {
|
|
907
983
|
const detail = (e as CustomEvent<HTMLElement>).detail;
|