11import { Build , getMode , setMode , getElement } from '@stencil/core' ;
22import { printIonWarning } from '@utils/logging' ;
3- import { applyGlobalTheme } from '@utils/theme' ;
3+ import { applyGlobalTheme , getCustomTheme } from '@utils/theme' ;
44
55import type { IonicConfig , Mode , Theme } from '../interface' ;
66import { defaultTheme as baseTheme } from '../themes/base/default.tokens' ;
@@ -13,60 +13,6 @@ import { config, configFromSession, configFromURL, saveConfig } from './config';
1313let defaultMode : Mode ;
1414let defaultTheme : Theme = 'md' ;
1515
16- /**
17- * Prints a warning message to the developer to inform them of
18- * an invalid configuration of mode and theme.
19- * @param mode The invalid mode configuration.
20- * @param theme The invalid theme configuration.
21- */
22- const printInvalidModeWarning = ( mode : Mode , theme : Theme , ref ?: any ) => {
23- printIonWarning (
24- `Invalid mode and theme combination provided: mode: ${ mode } , theme: ${ theme } . Fallback mode ${ getDefaultModeForTheme (
25- theme
26- ) } will be used.`,
27- ref
28- ) ;
29- } ;
30-
31- /**
32- * Validates if a mode is accepted for a theme configuration.
33- * @param mode The mode to validate.
34- * @param theme The theme the mode is being used with.
35- * @returns `true` if the mode is valid for the theme, `false` if invalid.
36- */
37- export const isModeValidForTheme = ( mode : Mode , theme : Theme ) => {
38- if ( mode === 'md' ) {
39- return theme === 'md' || theme === 'ionic' ;
40- } else if ( mode === 'ios' ) {
41- return theme === 'ios' || theme === 'ionic' ;
42- }
43- return false ;
44- } ;
45-
46- /**
47- * Returns the default mode for a specified theme.
48- * @param theme The theme to return a default mode for.
49- * @returns The default mode, either `ios` or `md`.
50- */
51- const getDefaultModeForTheme = ( theme : Theme ) : Mode => {
52- if ( theme === 'ios' ) {
53- return 'ios' ;
54- }
55- return 'md' ;
56- } ;
57-
58- /**
59- * Returns the default theme for a specified mode.
60- * @param mode The mode to return a default theme for.
61- * @returns The default theme.
62- */
63- const getDefaultThemeForMode = ( mode : Mode ) : Theme => {
64- if ( mode === 'ios' ) {
65- return 'ios' ;
66- }
67- return 'md' ;
68- } ;
69-
7016const isModeSupported = ( elmMode : string ) => [ 'ios' , 'md' ] . includes ( elmMode ) ;
7117
7218const isThemeSupported = ( theme : string ) => [ 'ios' , 'md' , 'ionic' ] . includes ( theme ) ;
@@ -75,32 +21,13 @@ const isIonicElement = (elm: HTMLElement) => elm.tagName?.startsWith('ION-');
7521
7622/**
7723 * Returns the mode value of the element reference or the closest
78- * parent with a valid mode.
24+ * parent with a valid mode. If no mode is set, then fallback
25+ * to the default mode.
7926 * @param ref The element reference to look up the mode for.
80- * @param theme Optionally can provide the theme to avoid an additional look-up.
8127 * @returns The mode value for the element reference.
8228 */
83- export const getIonMode = ( ref ?: any , theme = getIonTheme ( ref ) ) : Mode => {
84- if ( ref ?. mode && isModeValidForTheme ( ref ?. mode , theme ) ) {
85- /**
86- * If the reference already has a mode configuration,
87- * use it instead of performing a look-up.
88- */
89- return ref . mode ;
90- } else {
91- const el = getElement ( ref ) ;
92- const mode = ( el . closest ( '[mode]' ) ?. getAttribute ( 'mode' ) as Mode ) || defaultMode ;
93-
94- if ( isModeValidForTheme ( mode , theme ) ) {
95- /**
96- * The mode configuration is supported for the configured theme.
97- */
98- return mode ;
99- } else {
100- printInvalidModeWarning ( mode , theme , ref ) ;
101- }
102- }
103- return getDefaultModeForTheme ( theme ) ;
29+ export const getIonMode = ( ref ?: any ) : Mode => {
30+ return ref ?. mode || ( getElement ( ref ) . closest ( '[mode]' ) ?. getAttribute ( 'mode' ) as Mode ) || defaultMode ;
10431} ;
10532
10633/**
@@ -125,7 +52,7 @@ export const getIonTheme = (ref?: any): Theme => {
12552 const mode = ref ?. mode ?? ( el . closest ( '[mode]' ) ?. getAttribute ( 'mode' ) as Mode ) ;
12653
12754 if ( mode ) {
128- return getDefaultThemeForMode ( mode ) ;
55+ return mode ;
12956 }
13057
13158 /**
@@ -210,15 +137,7 @@ export const initialize = (userConfig: IonicConfig = {}) => {
210137 * otherwise get the theme via config settings, and fallback to md.
211138 */
212139
213- Ionic . theme = defaultTheme = config . get (
214- 'theme' ,
215- doc . documentElement . getAttribute ( 'theme' ) || getDefaultThemeForMode ( defaultMode )
216- ) ;
217-
218- if ( ! isModeValidForTheme ( defaultMode , defaultTheme ) ) {
219- printInvalidModeWarning ( defaultMode , defaultTheme , configObj ) ;
220- defaultMode = getDefaultModeForTheme ( defaultTheme ) ;
221- }
140+ Ionic . theme = defaultTheme = config . get ( 'theme' , doc . documentElement . getAttribute ( 'theme' ) || defaultMode ) ;
222141
223142 config . set ( 'mode' , defaultMode ) ;
224143 doc . documentElement . setAttribute ( 'mode' , defaultMode ) ;
@@ -228,7 +147,7 @@ export const initialize = (userConfig: IonicConfig = {}) => {
228147 doc . documentElement . setAttribute ( 'theme' , defaultTheme ) ;
229148 doc . documentElement . classList . add ( defaultTheme ) ;
230149
231- const customTheme : BaseTheme | undefined = configObj . customTheme ;
150+ const customTheme : BaseTheme | undefined = getCustomTheme ( configObj . customTheme , defaultMode ) ;
232151
233152 // Apply base theme, or combine with custom theme if provided
234153 if ( customTheme ) {
0 commit comments