Skip to content

Commit 7a3e178

Browse files
committed
chore: readme
1 parent 147577f commit 7a3e178

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

packages/theme-switcher/README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,72 @@ ns plugin add @nativescript/theme-switcher
66

77
## Usage
88

9-
// TODO
9+
```ts
10+
11+
import { initThemes, switchTheme } from '@nativescript/theme-switcher'
12+
13+
14+
// first initialize the themes
15+
initThemes({
16+
// default is optional - will be auto-applied after initializing (*)
17+
default: () => import('theme-loader!./themes/default.scss'),
18+
red: () => import('theme-loader!./themes/red.scss'),
19+
green: () => import('theme-loader!./themes/green.scss'),
20+
})
21+
22+
// the later on, switch themes with
23+
switchTheme('red');
24+
switchTheme('green');
25+
```
26+
27+
> **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.
28+
29+
---
30+
31+
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.
32+
33+
Can be useful if your app can switch different parts of the theme individually. For example
34+
1. `switcher1` switches button styles
35+
2. `switcher2` switches font styles
36+
3. etc.
37+
38+
```ts
39+
import { ThemeSwitcher } from '@nativescript/theme-switcher'
40+
41+
const switcher1 = new ThemeSwitcher('switcher1');
42+
const switcher2 = new ThemeSwitcher('switcher1');
43+
44+
switcher1.initThemes({ /* ... */})
45+
switcher2.initThemes({ /* ... */})
46+
47+
switcher1.switchTheme( /* ... */ )
48+
switcher2.switchTheme( /* ... */ )
49+
```
50+
51+
## API
52+
53+
### initThemes(themes: ThemeDefinition, options?: ThemeSwitcherOptions)
54+
55+
```ts
56+
interface ThemeDefinition {
57+
[name: string]: () => any;
58+
}
59+
60+
interface ThemeSwitcherOptions {
61+
persistent?: boolean; // default: true
62+
persistenceKey?: string; // default: __theme_switcher_default
63+
}
64+
```
65+
66+
`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).
67+
68+
`default` will be applied if set as a theme.
69+
70+
If `persistent` is enabled (default), the last selected theme will be saved to ApplicationSettings and automatically restored when `initThemes` is called.
71+
72+
### switchTheme(themeName: string)
73+
74+
Used to switch the current theme.
1075

1176
## License
1277

packages/theme-switcher/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { addTaggedAdditionalCSS, removeTaggedAdditionalCSS, Application, ApplicationSettings } from '@nativescript/core';
22

33
export interface ThemeDefinition {
4-
[name: string]: () => string | any;
4+
[name: string]: () => any;
55
}
66

77
export interface ThemeSwitcherOptions {
@@ -45,7 +45,7 @@ export class ThemeSwitcher {
4545
});
4646
}
4747

48-
async switchTheme(themeName) {
48+
async switchTheme(themeName: string) {
4949
console.log('SWITCHING THEME TO ', themeName);
5050
if (!this.themes.has(themeName)) {
5151
if (themeName !== 'default') {

0 commit comments

Comments
 (0)