@nil-/doc 0.2.41 → 0.2.42

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @nil-/doc
2
2
 
3
+ ## 0.2.42
4
+
5
+ ### Patch Changes
6
+
7
+ - [doc][new] added theme store in sveltekit to retain used theme ([#67](https://github.com/njaldea/mono/pull/67))
8
+
3
9
  ## 0.2.41
4
10
 
5
11
  ### Patch Changes
@@ -97,6 +97,13 @@ const parentTheme = getTheme();
97
97
  const dark = initTheme();
98
98
  $:
99
99
  $dark = theme === void 0 ? $parentTheme : "dark" === theme;
100
+ const toggle = () => {
101
+ if (theme !== void 0) {
102
+ theme = $dark ? "light" : "dark";
103
+ } else {
104
+ $dark = !$dark;
105
+ }
106
+ };
100
107
  </script>
101
108
  <!--
102
109
  @component
@@ -105,7 +112,7 @@ $:
105
112
  <div class="layout" class:dark={$dark}>
106
113
  <div class="top">
107
114
  <slot name="title"><span>@nil-/doc</span></slot>
108
- <div class="icon" on:click={() => ($dark = !$dark)} on:keypress={null}>
115
+ <div class="icon" on:click={toggle} on:keypress={null}>
109
116
  <ThemeIcon bind:dark={$dark} />
110
117
  </div>
111
118
  <div
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nil-/doc",
3
- "version": "0.2.41",
3
+ "version": "0.2.42",
4
4
  "author": {
5
5
  "email": "njaldea@gmail.com",
6
6
  "name": "Neil Aldea"
@@ -8,15 +8,15 @@
8
8
  "license": "ISC",
9
9
  "devDependencies": {
10
10
  "@sveltejs/adapter-vercel": "^1.0.0",
11
- "@sveltejs/kit": "^1.0.1",
12
- "@sveltejs/package": "^1.0.1",
11
+ "@sveltejs/kit": "^1.0.5",
12
+ "@sveltejs/package": "^1.0.2",
13
13
  "@vitest/coverage-c8": "^0.26.3",
14
14
  "mdsvex": "^0.10.6",
15
15
  "remark-admonitions": "^1.2.1",
16
16
  "svelte-check": "^2.10.3",
17
17
  "tslib": "^2.4.1",
18
18
  "typescript": "^4.9.4",
19
- "vite": "^4.0.3",
19
+ "vite": "^4.0.4",
20
20
  "vitest": "^0.26.3"
21
21
  },
22
22
  "peerDependencies": {
@@ -1,5 +1,6 @@
1
- import { type Readable } from "svelte/store";
2
- type Routes = {
1
+ import { type Readable, type Writable } from "svelte/store";
2
+ import type { Theme } from "../components/context";
3
+ type Settings = {
3
4
  /**
4
5
  * List of routes
5
6
  */
@@ -13,12 +14,17 @@ type Routes = {
13
14
  * @param e - event that contains detail about the target url
14
15
  */
15
16
  navigate: (e: CustomEvent<string>) => Promise<void>;
17
+ /**
18
+ * A store that is responsible in keeping the theme in localStorage
19
+ * Default is "dark"
20
+ */
21
+ theme: Writable<Exclude<Theme, undefined>>;
16
22
  };
17
23
  /**
18
24
  * Dedicated helper method to be used for sveltekit
19
25
  * @param detail - vite's `import.meta.glob(..., { eager: true })`
20
26
  * @param prefix - only use when layout is not in the root route
21
- * @returns Routes
27
+ * @returns Settings
22
28
  */
23
- export declare const sveltekit: (detail: Record<string, unknown>, prefix?: string | null) => Routes;
29
+ export declare const sveltekit: (detail: Record<string, unknown>, prefix?: string | null) => Settings;
24
30
  export {};
@@ -1,6 +1,7 @@
1
- import { derived } from "svelte/store";
1
+ import { derived, writable } from "svelte/store";
2
2
  import { page } from "$app/stores";
3
3
  import { goto } from "$app/navigation";
4
+ import { browser } from "$app/environment";
4
5
  const toRoute = (p) => p.substring(1, p.lastIndexOf("/"));
5
6
  const routeHasLayoutGroup = /\(.*\)/;
6
7
  const collapseLayout = (p) => p
@@ -14,22 +15,30 @@ const isRouteDynamic = (p) => null == routeIsDynamic.exec(p);
14
15
  * Dedicated helper method to be used for sveltekit
15
16
  * @param detail - vite's `import.meta.glob(..., { eager: true })`
16
17
  * @param prefix - only use when layout is not in the root route
17
- * @returns Routes
18
+ * @returns Settings
18
19
  */
19
- export const sveltekit = (detail, prefix = null) => ({
20
- data: Object.keys(detail)
21
- .map(toRoute)
22
- .map(collapseLayout)
23
- .filter(isNotRoot)
24
- .filter(isRouteDynamic),
25
- current: derived(page, ($page) => {
26
- if ($page.route.id) {
27
- if (prefix) {
28
- return collapseLayout($page.route.id.substring(prefix.length));
20
+ export const sveltekit = (detail, prefix = null) => {
21
+ const key = "@nil-/doc/theme";
22
+ const initialValue = browser && "light" === localStorage.getItem(key) ? "light" : "dark";
23
+ const theme = writable(initialValue);
24
+ theme.subscribe((v) => browser && localStorage.setItem(key, v));
25
+ const result = {
26
+ data: Object.keys(detail)
27
+ .map(toRoute)
28
+ .map(collapseLayout)
29
+ .filter(isNotRoot)
30
+ .filter(isRouteDynamic),
31
+ current: derived(page, ($page) => {
32
+ if ($page.route.id) {
33
+ if (prefix) {
34
+ return collapseLayout($page.route.id.substring(prefix.length));
35
+ }
36
+ return collapseLayout($page.route.id);
29
37
  }
30
- return collapseLayout($page.route.id);
31
- }
32
- return null;
33
- }),
34
- navigate: prefix ? (e) => goto(`${prefix}${e.detail}`) : (e) => goto(e.detail)
35
- });
38
+ return null;
39
+ }),
40
+ navigate: prefix ? (e) => goto(`${prefix}${e.detail}`) : (e) => goto(e.detail),
41
+ theme
42
+ };
43
+ return result;
44
+ };