2121 */
2222
2323const DEFAULT_THEME = 'md' ;
24+ const DEFAULT_PALETTE = 'light' ;
2425
2526( function ( ) {
2627
@@ -88,17 +89,33 @@ const DEFAULT_THEME = 'md';
8889 * or `high-contrast-dark`. Default to `light` for tests.
8990 */
9091 const validPalettes = [ 'light' , 'dark' , 'high-contrast' , 'high-contrast-dark' ] ;
92+
93+ const configDarkMode = window . Ionic ?. config ?. customTheme ?. palette ?. dark ?. enabled === 'always' ? 'dark' : null ;
94+ const configHighContrastMode = window . Ionic ?. config ?. customTheme ?. palette ?. highContrast ?. enabled === 'always' ? 'high-contrast' : null ;
95+ const configHighContrastDarkMode = window . Ionic ?. config ?. customTheme ?. palette ?. highContrastDark ?. enabled === 'always' ? 'high-contrast-dark' : null ;
96+ /**
97+ * Ensure window.Ionic.config is defined before importing 'testing/scripts'
98+ * in the test HTML to properly initialize the palette configuration below.
99+ *
100+ * Example:
101+ * <script>
102+ * window.Ionic = { config: { customTheme: { palette: { ... } } } };
103+ * </script>
104+ * <script src="testing/scripts.js"></script>
105+ */
106+ const configPalette = configDarkMode || configHighContrastMode || configHighContrastDarkMode ;
91107 const paletteQuery = window . location . search . match ( / p a l e t t e = ( [ a - z - ] + ) / ) ;
92108 const paletteHash = window . location . hash . match ( / p a l e t t e = ( [ a - z - ] + ) / ) ;
93109 const darkClass = document . body ?. classList . contains ( 'ion-palette-dark' ) ? 'dark' : null ;
94110 const highContrastClass = document . body ?. classList . contains ( 'ion-palette-high-contrast' ) ? 'high-contrast' : null ;
95111 const highContrastDarkClass = darkClass && highContrastClass ? 'high-contrast-dark' : null ;
112+ const paletteClass = highContrastDarkClass || highContrastClass || darkClass ;
96113
97- let paletteName = paletteQuery ?. [ 1 ] || paletteHash ?. [ 1 ] || highContrastDarkClass || darkClass || highContrastClass || 'light' ;
114+ let paletteName = configPalette || paletteQuery ?. [ 1 ] || paletteHash ?. [ 1 ] || paletteClass || DEFAULT_PALETTE ;
98115
99116 if ( ! validPalettes . includes ( paletteName ) ) {
100117 console . warn ( `Invalid palette name: '${ paletteName } '. Falling back to 'light' palette.` ) ;
101- paletteName = 'light' ;
118+ paletteName = DEFAULT_PALETTE ;
102119 }
103120
104121 // Load theme tokens if the theme is valid
@@ -119,8 +136,15 @@ const DEFAULT_THEME = 'md';
119136
120137 // If a specific palette is requested, modify the palette structure
121138 // to set the enabled property to 'always'
139+ // TODO(FW-4004): Implement dark palette
122140 if ( paletteName === 'dark' && theme . palette ?. dark ) {
123141 theme . palette . dark . enabled = 'always' ;
142+ // TODO(FW-4005): Implement high contrast palette
143+ } else if ( paletteName === 'high-contrast' && theme . palette ?. highContrast ) {
144+ theme . palette . highContrast . enabled = 'always' ;
145+ // TODO(FW-4005): Implement high contrast dark palette
146+ } else if ( paletteName === 'high-contrast-dark' && theme . palette ?. highContrastDark ) {
147+ theme . palette . highContrastDark . enabled = 'always' ;
124148 }
125149
126150 // Apply the theme tokens to Ionic config
0 commit comments