svelte-os-themes 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -120,3 +120,13 @@ npm install svelte-os-themes
120
120
  - `value`
121
121
 
122
122
  Returns the current theme when used as a getter and sets the theme when used as a setter.
123
+
124
+ ### parseTheme
125
+
126
+ `parseTheme` is a helper function that parses any value into a valid theme. See example below
127
+
128
+ ```js
129
+ console.log(parseTheme('LIGHT')); // 'light'
130
+ console.log(parseTheme('invalid')); // undefined
131
+ console.log(parseTheme('invalid', 'dark')); // 'dark'
132
+ ```
@@ -24,7 +24,8 @@ export interface CreateThemeContextConfig {
24
24
  }
25
25
  export type CreateThemeContextReturn = ReturnType<typeof createThemeContext>;
26
26
  export declare function createThemeContext(config?: CreateThemeContextConfig): {
27
- theme: Theme;
27
+ get theme(): Theme;
28
+ set theme(value: Theme | null | undefined);
28
29
  readonly effects: {
29
30
  setup(): void;
30
31
  themeChanged(): void;
@@ -84,7 +84,7 @@ export function createThemeContext(config) {
84
84
  return theme;
85
85
  },
86
86
  set theme(value) {
87
- theme = value;
87
+ theme = value ?? fallback;
88
88
  },
89
89
  get effects() {
90
90
  return effects;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { parseTheme } from './parse-theme.js';
1
2
  export { default as ThemeProvider } from './theme-provider.svelte';
2
3
  export type { Theme } from './types.js';
3
4
  export { useTheme } from './use-theme.svelte.js';
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
+ export { parseTheme } from './parse-theme.js';
1
2
  export { default as ThemeProvider } from './theme-provider.svelte';
2
3
  export { useTheme } from './use-theme.svelte.js';
@@ -1,3 +1,13 @@
1
1
  import type { Theme } from './types.js';
2
+ /**
3
+ * Parses string to theme
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * console.log(parseTheme('LIGHT')); // 'light'
8
+ * console.log(parseTheme('invalid')); // undefined
9
+ * console.log(parseTheme('invalid', 'dark')); // 'dark'
10
+ * ```
11
+ */
2
12
  export declare function parseTheme(value: unknown, fallback: Theme): Theme;
3
13
  export declare function parseTheme(value: unknown): Theme | undefined;
@@ -37,5 +37,6 @@ import type { Theme } from './types.js';
37
37
  * ```
38
38
  */
39
39
  export declare function useTheme(): {
40
- value: Theme;
40
+ get value(): Theme;
41
+ set value(theme: Theme | null | undefined);
41
42
  };
@@ -1,4 +1,5 @@
1
1
  import { getContext } from 'svelte';
2
+ import { parseTheme } from './parse-theme.js';
2
3
  /**
3
4
  * @example
4
5
  * ```svelte
@@ -44,7 +45,10 @@ export function useTheme() {
44
45
  },
45
46
  set value(theme) {
46
47
  if (context) {
47
- context.theme = theme;
48
+ const v = parseTheme(theme);
49
+ if (v) {
50
+ context.theme = v;
51
+ }
48
52
  }
49
53
  },
50
54
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "svelte-os-themes",
3
3
  "type": "module",
4
4
  "license": "MIT",
5
- "version": "0.1.0",
5
+ "version": "0.3.0",
6
6
  "svelte": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
8
  "exports": {