|
15 | 15 | } |
16 | 16 |
|
17 | 17 | /** |
18 | | - * The term `palette` is used to as a param to match the |
19 | | - * Ionic docs, plus here is already a `ionic:theme` query being |
20 | | - * used for `md`, `ios`, and `ionic` themes. |
21 | | - */ |
22 | | - const palette = window.location.search.match(/palette=([a-z]+)/); |
23 | | - if (palette && palette[1] !== 'light') { |
24 | | - const linkTag = document.createElement('link'); |
25 | | - linkTag.setAttribute('rel', 'stylesheet'); |
26 | | - linkTag.setAttribute('type', 'text/css'); |
27 | | - linkTag.setAttribute('href', `/css/palettes/${palette[1]}.always.css`); |
28 | | - document.head.appendChild(linkTag); |
29 | | - } |
30 | | - |
31 | | - /** |
32 | | - * The `ionic` theme uses a different stylesheet than the `iOS` and `md` themes. |
33 | | - * This is to ensure that the `ionic` theme is loaded when the `ionic:theme=ionic` |
34 | | - * or when the HTML tag has the `theme="ionic"` attribute. This is useful for |
35 | | - * the snapshot tests, where the `ionic` theme is not loaded by default. |
| 18 | + * The `theme` query param is used to load a specific theme. |
| 19 | + * This can be `ionic`, `ios`, or `md`. Default to `md` for tests. |
36 | 20 | */ |
37 | 21 | const themeQuery = window.location.search.match(/ionic:theme=([a-z]+)/); |
38 | 22 | const themeAttr = document.documentElement.getAttribute('theme'); |
| 23 | + const themeName = themeQuery?.[1] || themeAttr || 'md'; |
39 | 24 |
|
| 25 | + // TODO(): Remove this when the tokens are working for all components |
| 26 | + // and the themes all use the same bundle |
40 | 27 | if ((themeQuery && themeQuery[1] === 'ionic') || themeAttr === 'ionic') { |
41 | 28 | const ionicThemeLinkTag = document.querySelector('link[href*="css/ionic/bundle.ionic.css"]'); |
42 | 29 |
|
|
54 | 41 | } |
55 | 42 | } |
56 | 43 |
|
| 44 | + /** |
| 45 | + * The `palette` query param is used to load a specific palette |
| 46 | + * for the theme. This can be `light`, `dark`, `high-contrast`, |
| 47 | + * or `high-contrast-dark`. Default to `light` for tests. |
| 48 | + */ |
| 49 | + const paletteQuery = window.location.search.match(/palette=([a-z]+)/); |
| 50 | + const paletteName = paletteQuery?.[1] || 'light'; |
| 51 | + |
| 52 | + // Load theme tokens dynamically instead of stylesheets |
| 53 | + if (themeName && ['ionic', 'ios', 'md'].includes(themeName)) { |
| 54 | + loadThemeTokens(themeName, paletteName); |
| 55 | + } |
| 56 | + |
| 57 | + async function loadThemeTokens(themeName, paletteName) { |
| 58 | + try { |
| 59 | + // Dynamically import the theme tokens |
| 60 | + const defaultTokens = await import(`/www/themes/${themeName}/default.tokens.js`); |
| 61 | + const theme = defaultTokens.defaultTheme; |
| 62 | + |
| 63 | + // If a specific palette is requested, modify the palette structure |
| 64 | + // to set the enabled property to 'always' |
| 65 | + if (paletteName === 'dark' && theme.palette?.dark) { |
| 66 | + theme.palette.dark.enabled = 'always'; |
| 67 | + } |
| 68 | + |
| 69 | + // Apply the theme tokens to Ionic config |
| 70 | + window.Ionic = window.Ionic || {}; |
| 71 | + window.Ionic.config = window.Ionic.config || {}; |
| 72 | + window.Ionic.config.customTheme = theme; |
| 73 | + |
| 74 | + // Re-apply the global theme |
| 75 | + if (window.Ionic.config.get && window.Ionic.config.set) { |
| 76 | + import('/www/themes/utils/theme.js').then(themeModule => { |
| 77 | + themeModule.applyGlobalTheme(theme); |
| 78 | + }).catch(() => { |
| 79 | + console.warn('Could not reapply theme - theme module not found'); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + console.info(`Loaded ${themeName} theme with palette ${paletteName}:`, theme); |
| 84 | + } catch (error) { |
| 85 | + console.warn(`Failed to load theme tokens for ${themeName}:`, error); |
| 86 | + } |
| 87 | + } |
| 88 | + |
57 | 89 | window.Ionic = window.Ionic || {}; |
58 | 90 | window.Ionic.config = window.Ionic.config || {}; |
59 | 91 |
|
|
0 commit comments