GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — docs/content/plugins/theme-switcher.md at docs/linux · NativeScript/docs

Expand file tree

/

theme-switcher.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
189 lines (131 loc) · 6.04 KB
/

theme-switcher.md

Copy path

File metadata and controls

189 lines (131 loc) · 6.04 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions
title Theme Switcher
editUrl https://github.com/NativeScript/plugins/tree/main/packages/theme-switcher/README.md

@nativescript/theme-switcher

Contents

  • Installation
  • Usage
    • Register the Themes
    • Switch Themes
    • Switch multiple themes simultaneously
  • API
    • initThemes()
    • switchTheme()
    • loadDefaultTheme()
  • Demo App
  • License

Installation

npm install @nativescript/theme-switcher

Usage

Register the Themes

Use the initThemes() function to register the themes to switch from.

Call this function in the app's app.ts or main.ts file, before the app starts.

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'),
})

Switch Themes

To switch themes, call the [switchTheme()] method passing it the name of the theme to switch to.

switchTheme('red')
switchTheme('green')

:::tip Note

The theme-loader! prefix is used to apply a custom loader that prevents the styles from being auto-applied, and instead applied on-demand by the theme switcher. It requires @nativescript/webpack@5+ to work properly.

:::

:::tip Note for TypeScript users

TypeScript does not know about theme-loader or css/scss files, in order to supress the type errors when calling import('theme-loader!./path/to/theme.css') you can add

/// <reference path="./node_modules/@nativescript/theme-switcher/shims.d.ts" />

To your references.d.ts, or use // @ts-ignore to supress the errors locally.

:::


Switch multiple themes simultaneously

If you need to switch multiple themes simultaneously, 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

  1. switcher1 switches button styles
  2. switcher2 switches font styles
  3. 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()

initThemes(themes, options)

Register the themes to be switched from.

Parameter Type Description
themes ThemeDefinition 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 initially, if set as a theme.
options ThemeSwitcherOptions Optional: Themes initialization settings.

ThemeDefinition

interface ThemeDefinition {
  [name: string]: () => any
}

ThemeSwitcherOptions

Option Type Description
persistent boolean Optional: Defaults to true. If persistent is enabled, the last selected theme will be saved to ApplicationSettings and automatically restored when initThemes() is called.
persistenceKey string Optional: The key to the selected theme under on the device storage. Defaults to __theme_switcher_default

switchTheme()

switchTheme(themeName)

Used to switch from the current theme to the specified one.

Parameter Type Description
themeName string The name of the theme to switch to.

loadDefaultTheme()

import { loadDefaultTheme } from '@nativescript/theme-switcher'

loadDefaultTheme()

Loads the last selected theme if persistence is enabled, otherwise loads "default" if it exists.


Demo App

Try out the plugin's demo with the NativeScript Preview app at StackBlitz.

License

Apache License Version 2.0

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.