Theme Switcher
Usage
import { initThemes, switchTheme } from '@nativescript/theme-switcher'
// first initialize the themes
initThemes({
// default is optional - will be auto-applied after initializing (*)
default: () => import('theme-loader!./themes/default.scss'),
red: () => import('theme-loader!./themes/red.scss'),
green: () => import('theme-loader!./themes/green.scss')
})
// the later on, switch themes with
switchTheme('red')
switchTheme('green')
The |
For TypeScript users: TypeScript does not know about
To your |
If you need to switch multiple themes simultaniously, you can initialize as many switchers as you need. Each switcher will load css and persist (unless disabled) the last selected theme.
Can be useful if your app can switch different parts of the theme individually. For example
-
switcher1
switches button styles -
switcher2
switches font styles -
etc.
import { ThemeSwitcher } from '@nativescript/theme-switcher'
const switcher1 = new ThemeSwitcher('switcher1')
const switcher2 = new ThemeSwitcher('switcher2')
switcher1.initThemes({
/* ... */
})
switcher2.initThemes({
/* ... */
})
switcher1.switchTheme(/* ... */)
switcher2.switchTheme(/* ... */)
API
initThemes(themes: ThemeDefinition, options?: ThemeSwitcherOptions)
interface ThemeDefinition {
[name: string]: () => any
}
interface ThemeSwitcherOptions {
persistent?: boolean // default: true
persistenceKey?: string // default: __theme_switcher_default
}
themes
is an object with the theme name as the key, and a loader function that returns the theme css (css string, ast, optionally async).
default
will be applied if set as a theme.
If persistent
is enabled (default), the last selected theme will be saved to ApplicationSettings and automatically restored when initThemes
is called.