Skip to content

Commit 4a45a78

Browse files
committed
test(themes): update the testing script to include the tokens
1 parent ad6fe66 commit 4a45a78

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"build.debug": "npm run clean && stencil build --debug",
8282
"build.docs.json": "stencil build --docs-json dist/docs.json",
8383
"build.tokens": "npx build.tokens --config scripts/tokens/index.mjs && npm run prettier.tokens",
84+
"build.newTokens": "esbuild src/themes/*/*.tokens.ts --bundle --format=esm --platform=browser --target=es2017 --outdir=www/themes --outbase=src/themes",
8485
"clean": "node scripts/clean.js",
8586
"css.minify": "cleancss -O2 -o ./css/ionic.bundle.css ./css/ionic.bundle.css",
8687
"css.sass": "sass --embed-sources --style compressed src/css:./css",
@@ -94,7 +95,7 @@
9495
"prerender.e2e": "node scripts/testing/prerender.js",
9596
"prettier": "prettier \"./src/**/*.{html,ts,tsx,js,jsx,scss}\"",
9697
"prettier.tokens": "prettier \"./src/foundations/*.{scss, html}\" --write --cache",
97-
"start": "npm run build.css && stencil build --dev --watch --serve",
98+
"start": "npm run build.css && npm run build.newTokens && stencil build --dev --watch --serve",
9899
"test": "npm run test.spec && npm run test.e2e",
99100
"test.spec": "stencil test --spec --max-workers=2",
100101
"test.e2e": "npx playwright test",

core/scripts/testing/scripts.js

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,15 @@
1515
}
1616

1717
/**
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.
3620
*/
3721
const themeQuery = window.location.search.match(/ionic:theme=([a-z]+)/);
3822
const themeAttr = document.documentElement.getAttribute('theme');
23+
const themeName = themeQuery?.[1] || themeAttr || 'md';
3924

25+
// TODO(): Remove this when the tokens are working for all components
26+
// and the themes all use the same bundle
4027
if ((themeQuery && themeQuery[1] === 'ionic') || themeAttr === 'ionic') {
4128
const ionicThemeLinkTag = document.querySelector('link[href*="css/ionic/bundle.ionic.css"]');
4229

@@ -54,6 +41,51 @@
5441
}
5542
}
5643

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+
5789
window.Ionic = window.Ionic || {};
5890
window.Ionic.config = window.Ionic.config || {};
5991

core/src/global/ionic-global.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { applyGlobalTheme, getCustomTheme } from '@utils/theme';
44

55
import type { IonicConfig, Mode, Theme } from '../interface';
66
import { defaultTheme as baseTheme } from '../themes/base/default.tokens';
7-
import { defaultTheme as ionicTheme } from '../themes/ionic/default.tokens';
87
import type { BaseTheme } from '../themes/themes.interfaces';
98
import { shouldUseCloseWatcher } from '../utils/hardware-back-button';
109
import { isPlatform, setupPlatforms } from '../utils/platform';
@@ -156,11 +155,7 @@ export const initialize = (userConfig: IonicConfig = {}) => {
156155
config.set('customTheme', combinedTheme);
157156
} else {
158157
applyGlobalTheme(baseTheme);
159-
}
160-
161-
// TODO(): remove this when we update the ionic theme
162-
if (defaultTheme === 'ionic') {
163-
applyGlobalTheme(ionicTheme);
158+
config.set('customTheme', baseTheme);
164159
}
165160

166161
if (config.getBoolean('_testing')) {

0 commit comments

Comments
 (0)