@red-hat-developer-hub/backstage-plugin-theme 1.1.0 → 1.2.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # @red-hat-developer-hub/backstage-plugin-theme
2
2
 
3
+ ## 1.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 61f08f2: Fix the RHDH entity-page layout so content can grow beyond the viewport without
8
+ being clipped by the main content container.
9
+
10
+ ## 1.2.0
11
+
12
+ ### Minor Changes
13
+
14
+ - e8c4cd7: feat: allow customers to customize BUI components via app-config (RHIDP-14510)
15
+
16
+ Adds two new `app.branding` configuration options:
17
+
18
+ - `customCSS`: raw CSS string injected as global styles (use at your own risk)
19
+ - `theme.{light|dark}.bui.tokens`: per-theme structured token overrides mapping to BUI CSS custom properties (`--bui-*`), allowing different values for light and dark themes
20
+
21
+ ### Patch Changes
22
+
23
+ - a8731b1: Update theme workspace dependencies to Backstage 1.54.6.
24
+ - 1a55424: Offset Backstage UI dialogs such as Inspect Entity below the global header so their title and close control stay visible.
25
+
3
26
  ## 1.1.0
4
27
 
5
28
  ### Minor Changes
@@ -0,0 +1,552 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "type": "object",
4
+ "properties": {
5
+ "app": {
6
+ "type": "object",
7
+ "properties": {
8
+ "branding": {
9
+ "type": "object",
10
+ "properties": {
11
+ "theme": {
12
+ "type": "object",
13
+ "additionalProperties": {
14
+ "type": "object",
15
+ "properties": {
16
+ "variant": {
17
+ "type": "string",
18
+ "enum": [
19
+ "rhdh",
20
+ "backstage"
21
+ ],
22
+ "description": "Optional key to load different defaults. Fallbacks to the latest 'rhdh' theme if not defined."
23
+ },
24
+ "mode": {
25
+ "type": "string",
26
+ "enum": [
27
+ "light",
28
+ "dark"
29
+ ],
30
+ "description": "Light or dark theme. Automatically selects 'dark' if the theme name contains the keyword \"dark\"."
31
+ },
32
+ "palette": {
33
+ "type": "object",
34
+ "properties": {
35
+ "rhdh": {
36
+ "type": "object",
37
+ "properties": {
38
+ "general": {
39
+ "type": "object",
40
+ "properties": {
41
+ "pageInset": {
42
+ "type": "string"
43
+ },
44
+ "pageInsetBackgroundColor": {
45
+ "type": "string"
46
+ },
47
+ "disabled": {
48
+ "type": "string"
49
+ },
50
+ "disabledBackground": {
51
+ "type": "string"
52
+ },
53
+ "paperBackgroundImage": {
54
+ "type": "string"
55
+ },
56
+ "paperBorderColor": {
57
+ "type": "string"
58
+ },
59
+ "popoverBoxShadow": {
60
+ "type": "string"
61
+ },
62
+ "cardBackgroundColor": {
63
+ "type": "string"
64
+ },
65
+ "cardBorderColor": {
66
+ "type": "string"
67
+ },
68
+ "mainSectionBackgroundColor": {
69
+ "type": "string"
70
+ },
71
+ "formControlBackgroundColor": {
72
+ "type": "string"
73
+ },
74
+ "sidebarBackgroundColor": {
75
+ "type": "string"
76
+ },
77
+ "sidebarDividerColor": {
78
+ "type": "string"
79
+ },
80
+ "sidebarItemSelectedBackgroundColor": {
81
+ "type": "string"
82
+ },
83
+ "tableTitleColor": {
84
+ "type": "string"
85
+ },
86
+ "tableSubtitleColor": {
87
+ "type": "string"
88
+ },
89
+ "tableColumnTitleColor": {
90
+ "type": "string"
91
+ },
92
+ "tableRowHover": {
93
+ "type": "string"
94
+ },
95
+ "tableBorderColor": {
96
+ "type": "string"
97
+ },
98
+ "tableBackgroundColor": {
99
+ "type": "string"
100
+ },
101
+ "tabsLinkHoverBackgroundColor": {
102
+ "type": "string"
103
+ },
104
+ "contrastText": {
105
+ "type": "string"
106
+ },
107
+ "appBarBackgroundScheme": {
108
+ "type": "string",
109
+ "enum": [
110
+ "light",
111
+ "dark"
112
+ ]
113
+ },
114
+ "appBarBackgroundColor": {
115
+ "type": "string"
116
+ },
117
+ "appBarForegroundColor": {
118
+ "type": "string"
119
+ },
120
+ "appBarBackgroundImage": {
121
+ "type": "string"
122
+ },
123
+ "starredItemsColor": {
124
+ "type": "string"
125
+ }
126
+ }
127
+ },
128
+ "primary": {
129
+ "type": "object",
130
+ "properties": {
131
+ "main": {
132
+ "type": "string"
133
+ },
134
+ "focusVisibleBorder": {
135
+ "type": "string"
136
+ }
137
+ }
138
+ },
139
+ "secondary": {
140
+ "type": "object",
141
+ "properties": {
142
+ "main": {
143
+ "type": "string"
144
+ },
145
+ "focusVisibleBorder": {
146
+ "type": "string"
147
+ }
148
+ }
149
+ },
150
+ "cards": {
151
+ "type": "object",
152
+ "properties": {
153
+ "headerTextColor": {
154
+ "type": "string"
155
+ },
156
+ "headerBackgroundColor": {
157
+ "type": "string"
158
+ },
159
+ "headerBackgroundImage": {
160
+ "type": "string"
161
+ }
162
+ }
163
+ }
164
+ }
165
+ }
166
+ },
167
+ "additionalProperties": {},
168
+ "description": "Palette configuration. Accepts standard Backstage/MUI palette options plus an optional 'rhdh' key for RHDH-specific palette extensions."
169
+ },
170
+ "fontFamily": {
171
+ "type": "string",
172
+ "description": "Font family for the theme."
173
+ },
174
+ "htmlFontSize": {
175
+ "type": "number",
176
+ "description": "Base HTML font size in pixels."
177
+ },
178
+ "typography": {
179
+ "type": "object",
180
+ "properties": {
181
+ "htmlFontSize": {
182
+ "type": "number"
183
+ },
184
+ "fontFamily": {
185
+ "type": "string"
186
+ },
187
+ "h1": {
188
+ "type": "object",
189
+ "properties": {
190
+ "fontFamily": {
191
+ "type": "string"
192
+ },
193
+ "fontSize": {
194
+ "type": [
195
+ "number",
196
+ "string"
197
+ ]
198
+ },
199
+ "fontWeight": {
200
+ "type": "number"
201
+ },
202
+ "marginBottom": {
203
+ "type": "number"
204
+ }
205
+ }
206
+ },
207
+ "h2": {
208
+ "type": "object",
209
+ "properties": {
210
+ "fontFamily": {
211
+ "type": "string"
212
+ },
213
+ "fontSize": {
214
+ "type": [
215
+ "number",
216
+ "string"
217
+ ]
218
+ },
219
+ "fontWeight": {
220
+ "type": "number"
221
+ },
222
+ "marginBottom": {
223
+ "type": "number"
224
+ }
225
+ }
226
+ },
227
+ "h3": {
228
+ "type": "object",
229
+ "properties": {
230
+ "fontFamily": {
231
+ "type": "string"
232
+ },
233
+ "fontSize": {
234
+ "type": [
235
+ "number",
236
+ "string"
237
+ ]
238
+ },
239
+ "fontWeight": {
240
+ "type": "number"
241
+ },
242
+ "marginBottom": {
243
+ "type": "number"
244
+ }
245
+ }
246
+ },
247
+ "h4": {
248
+ "type": "object",
249
+ "properties": {
250
+ "fontFamily": {
251
+ "type": "string"
252
+ },
253
+ "fontSize": {
254
+ "type": [
255
+ "number",
256
+ "string"
257
+ ]
258
+ },
259
+ "fontWeight": {
260
+ "type": "number"
261
+ },
262
+ "marginBottom": {
263
+ "type": "number"
264
+ }
265
+ }
266
+ },
267
+ "h5": {
268
+ "type": "object",
269
+ "properties": {
270
+ "fontFamily": {
271
+ "type": "string"
272
+ },
273
+ "fontSize": {
274
+ "type": [
275
+ "number",
276
+ "string"
277
+ ]
278
+ },
279
+ "fontWeight": {
280
+ "type": "number"
281
+ },
282
+ "marginBottom": {
283
+ "type": "number"
284
+ }
285
+ }
286
+ },
287
+ "h6": {
288
+ "type": "object",
289
+ "properties": {
290
+ "fontFamily": {
291
+ "type": "string"
292
+ },
293
+ "fontSize": {
294
+ "type": [
295
+ "number",
296
+ "string"
297
+ ]
298
+ },
299
+ "fontWeight": {
300
+ "type": "number"
301
+ },
302
+ "marginBottom": {
303
+ "type": "number"
304
+ }
305
+ }
306
+ }
307
+ },
308
+ "description": "Typography configuration for headings and base font settings."
309
+ },
310
+ "defaultPageTheme": {
311
+ "type": "string",
312
+ "description": "Default page theme name."
313
+ },
314
+ "pageTheme": {
315
+ "type": "object",
316
+ "additionalProperties": {
317
+ "type": "object",
318
+ "properties": {
319
+ "backgroundColor": {
320
+ "anyOf": [
321
+ {
322
+ "type": "string"
323
+ },
324
+ {
325
+ "type": "array",
326
+ "items": {
327
+ "type": "string"
328
+ }
329
+ }
330
+ ]
331
+ },
332
+ "colors": {
333
+ "anyOf": [
334
+ {
335
+ "type": "string"
336
+ },
337
+ {
338
+ "type": "array",
339
+ "items": {
340
+ "type": "string"
341
+ }
342
+ }
343
+ ]
344
+ },
345
+ "shape": {
346
+ "type": "string"
347
+ },
348
+ "backgroundImage": {
349
+ "type": "string"
350
+ },
351
+ "fontColor": {
352
+ "type": "string"
353
+ }
354
+ }
355
+ },
356
+ "description": "Page theme configurations keyed by theme name."
357
+ },
358
+ "options": {
359
+ "type": "object",
360
+ "properties": {
361
+ "components": {
362
+ "type": "string",
363
+ "enum": [
364
+ "rhdh",
365
+ "backstage",
366
+ "mui"
367
+ ]
368
+ },
369
+ "rippleEffect": {
370
+ "type": "string",
371
+ "enum": [
372
+ "on",
373
+ "off"
374
+ ]
375
+ },
376
+ "paper": {
377
+ "type": "string",
378
+ "enum": [
379
+ "patternfly",
380
+ "mui"
381
+ ]
382
+ },
383
+ "buttons": {
384
+ "type": "string",
385
+ "enum": [
386
+ "patternfly",
387
+ "mui"
388
+ ]
389
+ },
390
+ "inputs": {
391
+ "type": "string",
392
+ "enum": [
393
+ "patternfly",
394
+ "mui"
395
+ ]
396
+ },
397
+ "checkbox": {
398
+ "type": "string",
399
+ "enum": [
400
+ "patternfly",
401
+ "mui"
402
+ ]
403
+ },
404
+ "accordions": {
405
+ "type": "string",
406
+ "enum": [
407
+ "patternfly",
408
+ "mui"
409
+ ]
410
+ },
411
+ "sidebars": {
412
+ "type": "string",
413
+ "enum": [
414
+ "patternfly",
415
+ "mui"
416
+ ]
417
+ },
418
+ "pages": {
419
+ "type": "string",
420
+ "enum": [
421
+ "patternfly",
422
+ "mui"
423
+ ]
424
+ },
425
+ "headers": {
426
+ "type": "string",
427
+ "enum": [
428
+ "patternfly",
429
+ "mui"
430
+ ]
431
+ },
432
+ "toolbars": {
433
+ "type": "string",
434
+ "enum": [
435
+ "patternfly",
436
+ "mui"
437
+ ]
438
+ },
439
+ "dialogs": {
440
+ "type": "string",
441
+ "enum": [
442
+ "patternfly",
443
+ "mui"
444
+ ]
445
+ },
446
+ "cards": {
447
+ "type": "string",
448
+ "enum": [
449
+ "patternfly",
450
+ "mui"
451
+ ]
452
+ },
453
+ "tables": {
454
+ "type": "string",
455
+ "enum": [
456
+ "patternfly",
457
+ "mui"
458
+ ]
459
+ },
460
+ "tabs": {
461
+ "type": "string",
462
+ "enum": [
463
+ "patternfly",
464
+ "mui"
465
+ ]
466
+ },
467
+ "appBar": {
468
+ "type": "string",
469
+ "enum": [
470
+ "patternfly",
471
+ "mui"
472
+ ]
473
+ },
474
+ "breadcrumbs": {
475
+ "type": "string",
476
+ "enum": [
477
+ "patternfly",
478
+ "mui"
479
+ ]
480
+ }
481
+ },
482
+ "description": "UI component style options."
483
+ },
484
+ "bui": {
485
+ "type": "object",
486
+ "properties": {
487
+ "tokens": {
488
+ "type": "object",
489
+ "properties": {
490
+ "primary": {
491
+ "type": "string"
492
+ },
493
+ "warning": {
494
+ "type": "string"
495
+ },
496
+ "error": {
497
+ "type": "string"
498
+ },
499
+ "success": {
500
+ "type": "string"
501
+ },
502
+ "info": {
503
+ "type": "string"
504
+ },
505
+ "fontFamily": {
506
+ "type": "string"
507
+ },
508
+ "fontFamilyMonospace": {
509
+ "type": "string"
510
+ },
511
+ "borderColor": {
512
+ "type": "string"
513
+ },
514
+ "backgroundColor": {
515
+ "type": "string"
516
+ },
517
+ "foregroundPrimary": {
518
+ "type": "string"
519
+ },
520
+ "foregroundSecondary": {
521
+ "type": "string"
522
+ },
523
+ "foregroundDisabled": {
524
+ "type": "string"
525
+ },
526
+ "focusRing": {
527
+ "type": "string"
528
+ },
529
+ "shadow": {
530
+ "type": "string"
531
+ }
532
+ }
533
+ }
534
+ },
535
+ "description": "Structured BUI token overrides mapping to --bui-* CSS custom properties."
536
+ }
537
+ }
538
+ },
539
+ "description": "Theme configuration.",
540
+ "deepVisibility": "frontend"
541
+ },
542
+ "customCSS": {
543
+ "type": "string",
544
+ "description": "Raw CSS string injected as global styles. Use at your own risk -- BUI class names may change between Backstage releases.",
545
+ "visibility": "frontend"
546
+ }
547
+ }
548
+ }
549
+ }
550
+ }
551
+ }
552
+ }
@@ -1,23 +1,48 @@
1
1
  import { jsx, jsxs } from 'react/jsx-runtime';
2
+ import { useMemo } from 'react';
2
3
  import { useApi, appThemeApiRef } from '@backstage/core-plugin-api';
3
4
  import { UnifiedThemeProvider } from '@backstage/theme';
4
5
  import { StyledEngineProvider, ThemeProvider as ThemeProvider$1 } from '@mui/material/styles';
6
+ import GlobalStyles from '@mui/material/GlobalStyles';
5
7
  import { useTheme } from '../hooks/useTheme.esm.js';
6
8
  import { useThemeConfig } from '../hooks/useThemeConfig.esm.js';
9
+ import { useBranding } from '../hooks/useBranding.esm.js';
10
+ import { buiTokensToCSS } from '../utils/buiTokensToCSS.esm.js';
7
11
 
8
12
  const ThemeProvider = ({
9
13
  themeName,
10
14
  theme,
11
15
  children
12
16
  }) => {
17
+ const branding = useBranding();
13
18
  const mui5Theme = theme.getTheme("v5");
14
19
  const secondary = mui5Theme.palette.text.secondary;
20
+ const buiTokenStyles = useMemo(() => {
21
+ const themes = branding?.theme;
22
+ if (!themes) return null;
23
+ const styles = {};
24
+ for (const [themeName2, themeConfig] of Object.entries(themes)) {
25
+ const tokens = themeConfig?.bui?.tokens;
26
+ if (!tokens) continue;
27
+ const cssMap = buiTokensToCSS(tokens);
28
+ if (Object.keys(cssMap).length === 0) continue;
29
+ const mode = themeConfig.mode ?? (themeName2.includes("dark") ? "dark" : "light");
30
+ styles[`[data-theme-mode='${mode}']`] = {
31
+ ...styles[`[data-theme-mode='${mode}']`],
32
+ ...cssMap
33
+ };
34
+ }
35
+ return Object.keys(styles).length > 0 ? styles : null;
36
+ }, [branding?.theme]);
37
+ const customCSS = branding?.customCSS ?? "";
15
38
  return /* @__PURE__ */ jsx(UnifiedThemeProvider, { themeName, theme, children: /* @__PURE__ */ jsx(StyledEngineProvider, { injectFirst: true, children: /* @__PURE__ */ jsxs(ThemeProvider$1, { theme: mui5Theme, children: [
16
39
  /* @__PURE__ */ jsx("style", { children: `
17
40
  :root, [data-theme-mode='light'], [data-theme-mode='dark'] {
18
41
  --bui-fg-secondary: ${secondary} !important;
19
42
  }
20
43
  ` }),
44
+ buiTokenStyles && /* @__PURE__ */ jsx(GlobalStyles, { styles: buiTokenStyles }),
45
+ customCSS && /* @__PURE__ */ jsx("style", { children: customCSS }),
21
46
  children
22
47
  ] }) }) });
23
48
  };
@@ -1 +1 @@
1
- {"version":3,"file":"ThemeProvider.esm.js","sources":["../../src/components/ThemeProvider.tsx"],"sourcesContent":["/*\n * Copyright Red Hat, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { ReactNode } from 'react';\nimport {\n type AppTheme,\n appThemeApiRef,\n type AppThemeApi,\n useApi,\n} from '@backstage/core-plugin-api';\nimport { UnifiedTheme, UnifiedThemeProvider } from '@backstage/theme';\n\nimport {\n StyledEngineProvider,\n ThemeProvider as Mui5Provider,\n Theme as Mui5Theme,\n} from '@mui/material/styles';\n\nimport { useTheme } from '../hooks/useTheme';\nimport { useThemeConfig } from '../hooks/useThemeConfig';\nimport { ThemeConfig } from '../types';\n\n/**\n * Uses the UnifiedThemeProvider to style MUI v4 and v5 components.\n *\n * This component duplicated the StyledEngineProvider and MUI v5 ThemeProvider\n * to solve an issue that the defaultProps from the component configurations\n * (see `utils/createComponents.ts`) wasn't picked up.\n *\n * https://github.com/backstage/backstage/blob/master/packages/theme/src/unified/UnifiedThemeProvider.tsx#L94-L100\n */\nconst ThemeProvider = ({\n themeName,\n theme,\n children,\n}: {\n themeName: string;\n theme: UnifiedTheme;\n children: ReactNode;\n}) => {\n const mui5Theme = theme.getTheme('v5') as Mui5Theme;\n const secondary = mui5Theme.palette.text.secondary;\n return (\n <UnifiedThemeProvider themeName={themeName} theme={theme}>\n <StyledEngineProvider injectFirst>\n <Mui5Provider theme={mui5Theme}>\n {/*\n Native style tag (not Emotion) so the declaration is unlayered and\n reliably overrides @backstage/ui @layer tokens for --bui-fg-secondary.\n */}\n <style>{`\n :root, [data-theme-mode='light'], [data-theme-mode='dark'] {\n --bui-fg-secondary: ${secondary} !important;\n }\n `}</style>\n {children}\n </Mui5Provider>\n </StyledEngineProvider>\n </UnifiedThemeProvider>\n );\n};\n\nexport const createThemeProvider = (\n themeName: string,\n theme: UnifiedTheme,\n): AppTheme['Provider'] =>\n function RHDHThemeProvider({ children }) {\n return (\n <ThemeProvider themeName={themeName} theme={theme}>\n {children}\n </ThemeProvider>\n );\n };\n\nexport const createThemeProviderForThemeConfig = (\n themeName: string,\n themeConfig: ThemeConfig,\n): AppTheme['Provider'] =>\n function RHDHThemeProviderForThemeConfig({ children }) {\n const theme = useTheme(themeConfig);\n return (\n <ThemeProvider themeName={themeName} theme={theme}>\n {children}\n </ThemeProvider>\n );\n };\n\nexport const createThemeProviderForThemeName = (\n themeName: string,\n themeNameForConfig = themeName,\n): AppTheme['Provider'] =>\n function RHDHThemeProviderForThemeName({ children }) {\n const themeConfig = useThemeConfig(themeNameForConfig);\n const theme = useTheme(themeConfig);\n return (\n <ThemeProvider themeName={themeName} theme={theme}>\n {children}\n </ThemeProvider>\n );\n };\n\nconst resolveActiveKey = (\n activeId: string | undefined,\n keys: string[],\n): string => {\n if (activeId && keys.includes(activeId)) {\n return activeId;\n }\n const prefersDark = activeId\n ? activeId.includes('dark')\n : window.matchMedia?.('(prefers-color-scheme: dark)')?.matches;\n return prefersDark\n ? (keys.find(n => n.includes('dark')) ?? keys[0])\n : (keys.find(n => !n.includes('dark')) ?? keys[0]);\n};\n\nconst useActiveThemeId = (): string | undefined => {\n let appThemeApi: AppThemeApi | undefined;\n try {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n appThemeApi = useApi<AppThemeApi>(appThemeApiRef); // NOSONAR\n } catch {\n // appThemeApi may not be available during createApp initialization\n }\n return appThemeApi?.getActiveThemeId();\n};\n\nexport type SharedThemeEntry = {\n name?: string;\n config?: ThemeConfig;\n theme?: UnifiedTheme;\n};\n\nexport const createSharedThemeProvider = (\n entries: Record<string, SharedThemeEntry>,\n): AppTheme['Provider'] => {\n const keys = Object.keys(entries);\n\n function RHDHSharedThemeProvider({ children }: { children: ReactNode }) {\n const activeId = useActiveThemeId();\n const key = resolveActiveKey(activeId, keys);\n const entry = entries[key];\n\n // Always call hooks unconditionally (Rules of Hooks).\n // For entries that don't need them the results are simply unused.\n const configFromName = useThemeConfig(entry.name ?? key);\n const resolvedConfig = entry.config ?? configFromName;\n const themeFromHook = useTheme(resolvedConfig);\n\n const theme = entry.theme ?? themeFromHook;\n return (\n <ThemeProvider theme={theme} themeName={entry.name ?? key}>\n {children}\n </ThemeProvider>\n );\n }\n return RHDHSharedThemeProvider;\n};\n"],"names":["Mui5Provider"],"mappings":";;;;;;;AA4CA,MAAM,gBAAgB,CAAC;AAAA,EACrB,SAAA;AAAA,EACA,KAAA;AAAA,EACA;AACF,CAAA,KAIM;AACJ,EAAA,MAAM,SAAA,GAAY,KAAA,CAAM,QAAA,CAAS,IAAI,CAAA;AACrC,EAAA,MAAM,SAAA,GAAY,SAAA,CAAU,OAAA,CAAQ,IAAA,CAAK,SAAA;AACzC,EAAA,uBACE,GAAA,CAAC,oBAAA,EAAA,EAAqB,SAAA,EAAsB,KAAA,EAC1C,QAAA,kBAAA,GAAA,CAAC,oBAAA,EAAA,EAAqB,WAAA,EAAW,IAAA,EAC/B,QAAA,kBAAA,IAAA,CAACA,eAAA,EAAA,EAAa,KAAA,EAAO,SAAA,EAKnB,QAAA,EAAA;AAAA,oBAAA,GAAA,CAAC,OAAA,EAAA,EAAO,QAAA,EAAA;AAAA;AAAA,kCAAA,EAEkB,SAAS,CAAA;AAAA;AAA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AG,CAAA;AACvD,IAAA,MAAM,cAAA,GAAiB,MAAM,MAAA,IAAU,cAAA;AACvC,IAAA,MAAM,aAAA,GAAgB,SAAS,cAAc,CAAA;AAE7C,IAAA,MAAM,KAAA,GAAQ,MAAM,KAAA,IAAS,aAAA;AAC7B,IAAA,2BACG,aAAA,EAAA,EAAc,KAAA,EAAc,WAAW,KAAA,CAAM,IAAA,IAAQ,KACnD,QAAA,EACH,CAAA;AAAA,EAEJ;AACA,EAAA,OAAO,uBAAA;AACT;;;;"}
1
+ {"version":3,"file":"ThemeProvider.esm.js","sources":["../../src/components/ThemeProvider.tsx"],"sourcesContent":["/*\n * Copyright Red Hat, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { type ReactNode, useMemo } from 'react';\nimport {\n type AppTheme,\n appThemeApiRef,\n type AppThemeApi,\n useApi,\n} from '@backstage/core-plugin-api';\nimport { UnifiedTheme, UnifiedThemeProvider } from '@backstage/theme';\n\nimport {\n StyledEngineProvider,\n ThemeProvider as Mui5Provider,\n Theme as Mui5Theme,\n} from '@mui/material/styles';\nimport GlobalStyles from '@mui/material/GlobalStyles';\n\nimport { useTheme } from '../hooks/useTheme';\nimport { useThemeConfig } from '../hooks/useThemeConfig';\nimport { useBranding } from '../hooks/useBranding';\nimport { buiTokensToCSS } from '../utils/buiTokensToCSS';\nimport { ThemeConfig } from '../types';\n\n/**\n * Uses the UnifiedThemeProvider to style MUI v4 and v5 components.\n *\n * This component duplicated the StyledEngineProvider and MUI v5 ThemeProvider\n * to solve an issue that the defaultProps from the component configurations\n * (see `utils/createComponents.ts`) wasn't picked up.\n *\n * https://github.com/backstage/backstage/blob/master/packages/theme/src/unified/UnifiedThemeProvider.tsx#L94-L100\n */\nconst ThemeProvider = ({\n themeName,\n theme,\n children,\n}: {\n themeName: string;\n theme: UnifiedTheme;\n children: ReactNode;\n}) => {\n const branding = useBranding();\n const mui5Theme = theme.getTheme('v5') as Mui5Theme;\n const secondary = mui5Theme.palette.text.secondary;\n\n const buiTokenStyles = useMemo(() => {\n const themes = branding?.theme;\n if (!themes) return null;\n const styles: Record<string, Record<string, string>> = {};\n for (const [themeName, themeConfig] of Object.entries(themes)) {\n const tokens = themeConfig?.bui?.tokens;\n if (!tokens) continue;\n const cssMap = buiTokensToCSS(tokens);\n if (Object.keys(cssMap).length === 0) continue;\n const mode =\n themeConfig.mode ?? (themeName.includes('dark') ? 'dark' : 'light');\n styles[`[data-theme-mode='${mode}']`] = {\n ...styles[`[data-theme-mode='${mode}']`],\n ...cssMap,\n };\n }\n return Object.keys(styles).length > 0 ? styles : null;\n }, [branding?.theme]);\n\n const customCSS = branding?.customCSS ?? '';\n\n return (\n <UnifiedThemeProvider themeName={themeName} theme={theme}>\n <StyledEngineProvider injectFirst>\n <Mui5Provider theme={mui5Theme}>\n {/*\n Native style tag (not Emotion) so the declaration is unlayered and\n reliably overrides @backstage/ui @layer tokens for --bui-fg-secondary.\n */}\n <style>{`\n :root, [data-theme-mode='light'], [data-theme-mode='dark'] {\n --bui-fg-secondary: ${secondary} !important;\n }\n `}</style>\n {buiTokenStyles && <GlobalStyles styles={buiTokenStyles} />}\n {customCSS && <style>{customCSS}</style>}\n {children}\n </Mui5Provider>\n </StyledEngineProvider>\n </UnifiedThemeProvider>\n );\n};\n\nexport const createThemeProvider = (\n themeName: string,\n theme: UnifiedTheme,\n): AppTheme['Provider'] =>\n function RHDHThemeProvider({ children }) {\n return (\n <ThemeProvider themeName={themeName} theme={theme}>\n {children}\n </ThemeProvider>\n );\n };\n\nexport const createThemeProviderForThemeConfig = (\n themeName: string,\n themeConfig: ThemeConfig,\n): AppTheme['Provider'] =>\n function RHDHThemeProviderForThemeConfig({ children }) {\n const theme = useTheme(themeConfig);\n return (\n <ThemeProvider themeName={themeName} theme={theme}>\n {children}\n </ThemeProvider>\n );\n };\n\nexport const createThemeProviderForThemeName = (\n themeName: string,\n themeNameForConfig = themeName,\n): AppTheme['Provider'] =>\n function RHDHThemeProviderForThemeName({ children }) {\n const themeConfig = useThemeConfig(themeNameForConfig);\n const theme = useTheme(themeConfig);\n return (\n <ThemeProvider themeName={themeName} theme={theme}>\n {children}\n </ThemeProvider>\n );\n };\n\nconst resolveActiveKey = (\n activeId: string | undefined,\n keys: string[],\n): string => {\n if (activeId && keys.includes(activeId)) {\n return activeId;\n }\n const prefersDark = activeId\n ? activeId.includes('dark')\n : window.matchMedia?.('(prefers-color-scheme: dark)')?.matches;\n return prefersDark\n ? (keys.find(n => n.includes('dark')) ?? keys[0])\n : (keys.find(n => !n.includes('dark')) ?? keys[0]);\n};\n\nconst useActiveThemeId = (): string | undefined => {\n let appThemeApi: AppThemeApi | undefined;\n try {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n appThemeApi = useApi<AppThemeApi>(appThemeApiRef); // NOSONAR\n } catch {\n // appThemeApi may not be available during createApp initialization\n }\n return appThemeApi?.getActiveThemeId();\n};\n\nexport type SharedThemeEntry = {\n name?: string;\n config?: ThemeConfig;\n theme?: UnifiedTheme;\n};\n\nexport const createSharedThemeProvider = (\n entries: Record<string, SharedThemeEntry>,\n): AppTheme['Provider'] => {\n const keys = Object.keys(entries);\n\n function RHDHSharedThemeProvider({ children }: { children: ReactNode }) {\n const activeId = useActiveThemeId();\n const key = resolveActiveKey(activeId, keys);\n const entry = entries[key];\n\n // Always call hooks unconditionally (Rules of Hooks).\n // For entries that don't need them the results are simply unused.\n const configFromName = useThemeConfig(entry.name ?? key);\n const resolvedConfig = entry.config ?? configFromName;\n const themeFromHook = useTheme(resolvedConfig);\n\n const theme = entry.theme ?? themeFromHook;\n return (\n <ThemeProvider theme={theme} themeName={entry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