kempo-ui 0.0.35 → 0.0.36

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.
@@ -1,4 +1,4 @@
1
- import ShadowComponent from"./ShadowComponent.js";import{html,css}from"../lit-all.min.js";import"./Icon.js";export default class ThemeSwitcher extends ShadowComponent{static properties={currentTheme:{type:String,reflect:!0,attribute:"current-theme"}};constructor(){super(),this.currentTheme=ThemeSwitcher.getCurrentTheme()}handleClick=()=>{const e=ThemeSwitcher.getCurrentTheme();"auto"===e&&ThemeSwitcher.setTheme("light"),"light"===e&&ThemeSwitcher.setTheme("dark"),"dark"===e&&ThemeSwitcher.setTheme("auto")};handleStorageChange=e=>{"theme"===e.key&&(this.currentTheme=e.newValue||"auto",document.documentElement.setAttribute("theme",this.currentTheme))};connectedCallback(){super.connectedCallback(),window.addEventListener("storage",this.handleStorageChange)}disconnectedCallback(){super.disconnectedCallback(),window.removeEventListener("storage",this.handleStorageChange)}static styles=css`
1
+ import ShadowComponent from"./ShadowComponent.js";import{html,css}from"../lit-all.min.js";import theme from"../utils/theme.js";import"./Icon.js";export default class ThemeSwitcher extends ShadowComponent{static properties={currentTheme:{type:String,reflect:!0,attribute:"current-theme"}};constructor(){super(),this.currentTheme=theme.get()}handleClick=()=>{const e=theme.get();"auto"===e&&theme.set("light"),"light"===e&&theme.set("dark"),"dark"===e&&theme.set("auto")};connectedCallback(){super.connectedCallback(),this.unsubscribe=theme.subscribe(e=>{this.currentTheme=e})}disconnectedCallback(){super.disconnectedCallback(),this.unsubscribe&&this.unsubscribe()}static styles=css`
2
2
  :host {
3
3
  --padding: var(--spacer);
4
4
  display: flex;
@@ -20,4 +20,4 @@ import ShadowComponent from"./ShadowComponent.js";import{html,css}from"../lit-al
20
20
  button.no-btn {
21
21
  padding: var(--padding);
22
22
  }
23
- `;static setTheme(e){localStorage.setItem("theme",e),document.documentElement.setAttribute("theme",e),window.dispatchEvent(new StorageEvent("storage",{key:"theme",newValue:e}))}static getCurrentTheme(){let e=document.documentElement.getAttribute("theme");return e||(e=localStorage.getItem("theme")),e||"auto"}}const colorSchemeQuery=window.matchMedia("(prefers-color-scheme: dark)"),colorSchemeChangeHandler=e=>document.documentElement.setAttribute("auto-theme",e.matches?"dark":"light");colorSchemeQuery.addEventListener("change",colorSchemeChangeHandler),colorSchemeChangeHandler(colorSchemeQuery),ThemeSwitcher.setTheme(ThemeSwitcher.getCurrentTheme()),customElements.define("k-theme-switcher",ThemeSwitcher);
23
+ `;static setTheme(e){theme.set(e)}static getCurrentTheme(){return theme.get()}static getCalculatedCurrentTheme(){return theme.getCalculated()}}customElements.define("k-theme-switcher",ThemeSwitcher);
@@ -0,0 +1 @@
1
+ const contexts={},getContext=e=>(contexts[e]||(contexts[e]={value:void 0,subscribers:new Set}),contexts[e]);export const createContext=(e="global",t)=>{const s=getContext(e);return void 0!==t&&void 0===s.value&&(s.value=t),{get:()=>s.value,set:e=>{s.value=e,s.subscribers.forEach(t=>t(e))},subscribe:e=>(s.subscribers.add(e),e(s.value),()=>s.subscribers.delete(e)),unsubscribe:e=>s.subscribers.delete(e)}};export default createContext;
@@ -0,0 +1 @@
1
+ import createContext from"./context.js";const getInitialTheme=()=>{let e=document.documentElement.getAttribute("theme");return e||(e=localStorage.getItem("theme")),e||"auto"},themeContext=createContext("theme",getInitialTheme());themeContext.subscribe(e=>{localStorage.setItem("theme",e),document.documentElement.setAttribute("theme",e)});export const setTheme=e=>themeContext.set(e);export const getTheme=()=>themeContext.get();export const subscribeToTheme=e=>themeContext.subscribe(e);export const getCalculatedTheme=()=>{const e=getTheme();return"auto"===e?window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light":e};const colorSchemeQuery=window.matchMedia("(prefers-color-scheme: dark)"),colorSchemeChangeHandler=e=>document.documentElement.setAttribute("auto-theme",e.matches?"dark":"light");colorSchemeQuery.addEventListener("change",colorSchemeChangeHandler),colorSchemeChangeHandler(colorSchemeQuery);export default{get:getTheme,set:setTheme,subscribe:subscribeToTheme,getCalculated:getCalculatedTheme};
@@ -37,7 +37,8 @@
37
37
  </k-accordion>
38
38
 
39
39
  <h3>Description</h3>
40
- <p>The <code>ThemeSwitcher</code> component allows users to switch between different themes (auto, light, dark). It extends the <a href="./shadow-component.html">ShadowComponent</a> class and automatically saves the selected theme to local storage, applies it to the document, and synchronizes across browser tabs.</p>
40
+ <p>The <code>ThemeSwitcher</code> component provides a UI control for users to switch between different themes (auto, light, dark). It extends the <a href="./shadow-component.html">ShadowComponent</a> class and uses the <a href="../utils/theme.html">theme utility</a> internally for state management, persistence, and synchronization.</p>
41
+ <p><strong>Note:</strong> You can manage theme state without this component by directly importing the <a href="../utils/theme.html">theme utility</a>.</p>
41
42
 
42
43
  <h3 id="basicUsage"><a href="#basicUsage" class="no-link">Basic Usage</a></h3>
43
44
  <p>Use the <code>ThemeSwitcher</code> component to allow users to cycle through themes. Clicking the button cycles through auto → light → dark → auto. The component automatically detects the user's system preference when in auto mode.</p>
@@ -84,6 +85,7 @@
84
85
  <ul>
85
86
  <li><a href="./shadow-component.html">ShadowComponent</a></li>
86
87
  <li><a href="./icon.html">Icon</a></li>
88
+ <li><a href="../utils/theme.html">theme</a> utility</li>
87
89
  </ul>
88
90
 
89
91
  <h3 id="properties"><a href="#properties" class="no-link">Properties</a></h3>
@@ -101,8 +103,9 @@
101
103
  <p>The <code>ThemeSwitcher</code> class does not introduce any new public methods beyond those provided by the <a href="./shadow-component.html">ShadowComponent</a> class.</p>
102
104
 
103
105
  <h3 id="staticMethods"><a href="#staticMethods" class="no-link">Static Methods</a></h3>
106
+ <p><strong>Note:</strong> These static methods are convenience wrappers around the <a href="../utils/theme.html">theme utility</a>. For direct theme management without the component, use the theme utility directly.</p>
104
107
  <h5><code>static setTheme(theme)<i>: void</i></code></h5>
105
- <p>Sets the specified theme and saves it to local storage. Also applies the theme to the document element and dispatches a storage event for cross-tab synchronization.</p>
108
+ <p>Sets the specified theme. Internally calls the <a href="../utils/theme.html">theme utility</a> which handles persistence and DOM updates.</p>
106
109
  <ul>
107
110
  <li><strong>theme</strong> <code>string</code> - The theme to set. Must be one of: <code>"auto"</code>, <code>"light"</code>, or <code>"dark"</code></li>
108
111
  </ul>
@@ -110,20 +113,29 @@
110
113
  <span class="hljs-title class_">ThemeSwitcher</span>.<span class="hljs-title function_">setTheme</span>(<span class="hljs-string">'dark'</span>);</code></pre>
111
114
 
112
115
  <h5><code>static getCurrentTheme()<i>: string</i></code></h5>
113
- <p>Gets the current theme from the document attribute or local storage. Returns <code>"auto"</code> if no theme is set.</p>
116
+ <p>Gets the current theme. Returns <code>"auto"</code> if no theme is set. Internally calls the <a href="../utils/theme.html">theme utility</a>.</p>
114
117
  <pre><code class="hljs javascript"><span class="hljs-comment">// Get the current theme</span>
115
118
  <span class="hljs-keyword">const</span> currentTheme = <span class="hljs-title class_">ThemeSwitcher</span>.<span class="hljs-title function_">getCurrentTheme</span>();
116
119
  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(currentTheme); <span class="hljs-comment">// "auto", "light", or "dark"</span></code></pre>
120
+
121
+ <h5><code>static getCalculatedCurrentTheme()<i>: string</i></code></h5>
122
+ <p>Returns the effective theme, always <code>"dark"</code> or <code>"light"</code>. If the current theme is <code>"auto"</code>, this method uses the user's system preference (<code>prefers-color-scheme</code> media query) to determine the theme. Internally calls the <a href="../utils/theme.html">theme utility</a>.</p>
123
+ <pre><code class="hljs javascript"><span class="hljs-comment">// Get the calculated current theme</span>
124
+ <span class="hljs-keyword">const</span> effectiveTheme = <span class="hljs-title class_">ThemeSwitcher</span>.<span class="hljs-title function_">getCalculatedCurrentTheme</span>();
125
+ <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(effectiveTheme); <span class="hljs.comment">// "dark" or "light"</span></code></pre>
117
126
 
118
127
  <h3 id="themeDetection"><a href="#themeDetection" class="no-link">Automatic Theme Detection</a></h3>
119
- <p>When the theme is set to <code>"auto"</code>, the component automatically detects the user's system preference using <code>prefers-color-scheme</code> media query. The detected theme is applied to the document element as an <code>auto-theme</code> attribute.</p>
120
- <p>The component also synchronizes theme changes across browser tabs using the storage event, ensuring a consistent user experience.</p>
128
+ <p>When the theme is set to <code>"auto"</code>, the <a href="../utils/theme.html">theme utility</a> automatically detects the user's system preference using <code>prefers-color-scheme</code> media query. The detected theme is applied to the document element as an <code>auto-theme</code> attribute.</p>
129
+ <p>The theme utility handles persistence via localStorage and ensures theme changes are reflected in the DOM, providing a consistent user experience.</p>
130
+
131
+ <h3>Related</h3>
132
+ <p>See the <a href="../utils/theme.html">theme utility</a> documentation for programmatic theme management without using this component.</p>
121
133
 
122
134
  </main>
135
+ <div style="height:33vh"></div>
123
136
  <script type="module" src="../src/components/Import.js"></script>
124
137
  <script type="module" src="../src/components/ThemeSwitcher.js"></script>
125
138
  <script type="module" src="../src/components/Accordion.js"></script>
126
139
  <script type="module" src="../src/components/Card.js"></script>
127
-
128
140
  </body>
129
141
  </html>
package/docs/index.html CHANGED
@@ -207,6 +207,18 @@
207
207
 
208
208
  <h2>Utils</h2>
209
209
  <div class="row -mx">
210
+ <div class="span-12 t-span-6 d-span-4 px">
211
+ <a href="./utils/cookie.html" class="card mb no-link d-b">
212
+ <h3 class="tc-primary">cookie</h3>
213
+ <p class="tc-muted">Functions for managing browser cookies.</p>
214
+ </a>
215
+ </div>
216
+ <div class="span-12 t-span-6 d-span-4 px">
217
+ <a href="./utils/context.html" class="card mb no-link d-b">
218
+ <h3 class="tc-primary">context</h3>
219
+ <p class="tc-muted">Global or named shared state for vanilla JS components.</p>
220
+ </a>
221
+ </div>
210
222
  <div class="span-12 t-span-6 d-span-4 px">
211
223
  <a href="./utils/debounce.html" class="card mb no-link d-b">
212
224
  <h3 class="tc-primary">debounce</h3>
@@ -237,6 +249,12 @@
237
249
  <p class="tc-muted">Collection of functions for string manipulation and conversion.</p>
238
250
  </a>
239
251
  </div>
252
+ <div class="span-12 t-span-6 d-span-4 px">
253
+ <a href="./utils/theme.html" class="card mb no-link d-b">
254
+ <h3 class="tc-primary">theme</h3>
255
+ <p class="tc-muted">Centralized theme management with persistence and reactive updates.</p>
256
+ </a>
257
+ </div>
240
258
  <div class="span-12 t-span-6 d-span-4 px">
241
259
  <a href="./utils/type.html" class="card mb no-link d-b">
242
260
  <h3 class="tc-primary">type</h3>
@@ -249,12 +267,6 @@
249
267
  <p class="tc-muted">Functions for manipulating and analyzing JavaScript objects.</p>
250
268
  </a>
251
269
  </div>
252
- <div class="span-12 t-span-6 d-span-4 px">
253
- <a href="./utils/cookie.html" class="card mb no-link d-b">
254
- <h3 class="tc-primary">cookie</h3>
255
- <p class="tc-muted">Functions for managing browser cookies.</p>
256
- </a>
257
- </div>
258
270
  <div class="span-12 t-span-6 d-span-4 px">
259
271
  <a href="./utils/wait.html" class="card mb no-link d-b">
260
272
  <h3 class="tc-primary">wait</h3>
@@ -65,12 +65,14 @@
65
65
  <h5 class="mb0">Utils</h5>
66
66
  <div class="pl mb">
67
67
  <a href="../utils/cookie.html">cookie</a>
68
+ <a href="../utils/context.html">context</a>
68
69
  <a href="../utils/debounce.html">debounce</a>
69
70
  <a href="../utils/drag.html">drag</a>
70
71
  <a href="../utils/formatTimestamp.html">formatTimestamp</a>
71
72
  <a href="../utils/object.html">object</a>
72
73
  <a href="../utils/propConverters.html">propConverters</a>
73
74
  <a href="../utils/string.html">string</a>
75
+ <a href="../utils/theme.html">theme</a>
74
76
  <a href="../utils/type.html">type</a>
75
77
  <a href="../utils/wait.html">wait</a>
76
78
  </div>
package/docs/nav.inc.html CHANGED
@@ -54,6 +54,7 @@
54
54
  <a href="./components/timestamp.html">Timestamp</a>
55
55
  <a href="./components/toggle.html">Toggle</a>
56
56
  <a href="./components/tree.html">Tree</a>
57
+ <a href="./utils/context.html">Context Utility</a>
57
58
  </div>
58
59
 
59
60
  <h5 class="mb0">Base Components</h5>
@@ -66,6 +67,7 @@
66
67
  <h5 class="mb0">Utils</h5>
67
68
  <div class="pl mb">
68
69
  <a href="./utils/cookie.html">cookie</a>
70
+ <a href="./utils/context.html">context</a>
69
71
  <a href="./utils/debounce.html">debounce</a>
70
72
  <a href="./utils/drag.html">drag</a>
71
73
  <a href="./utils/formatTimestamp.html">formatTimestamp</a>
@@ -1,4 +1,4 @@
1
- import ShadowComponent from"./ShadowComponent.js";import{html,css}from"../lit-all.min.js";import"./Icon.js";export default class ThemeSwitcher extends ShadowComponent{static properties={currentTheme:{type:String,reflect:!0,attribute:"current-theme"}};constructor(){super(),this.currentTheme=ThemeSwitcher.getCurrentTheme()}handleClick=()=>{const e=ThemeSwitcher.getCurrentTheme();"auto"===e&&ThemeSwitcher.setTheme("light"),"light"===e&&ThemeSwitcher.setTheme("dark"),"dark"===e&&ThemeSwitcher.setTheme("auto")};handleStorageChange=e=>{"theme"===e.key&&(this.currentTheme=e.newValue||"auto",document.documentElement.setAttribute("theme",this.currentTheme))};connectedCallback(){super.connectedCallback(),window.addEventListener("storage",this.handleStorageChange)}disconnectedCallback(){super.disconnectedCallback(),window.removeEventListener("storage",this.handleStorageChange)}static styles=css`
1
+ import ShadowComponent from"./ShadowComponent.js";import{html,css}from"../lit-all.min.js";import theme from"../utils/theme.js";import"./Icon.js";export default class ThemeSwitcher extends ShadowComponent{static properties={currentTheme:{type:String,reflect:!0,attribute:"current-theme"}};constructor(){super(),this.currentTheme=theme.get()}handleClick=()=>{const e=theme.get();"auto"===e&&theme.set("light"),"light"===e&&theme.set("dark"),"dark"===e&&theme.set("auto")};connectedCallback(){super.connectedCallback(),this.unsubscribe=theme.subscribe(e=>{this.currentTheme=e})}disconnectedCallback(){super.disconnectedCallback(),this.unsubscribe&&this.unsubscribe()}static styles=css`
2
2
  :host {
3
3
  --padding: var(--spacer);
4
4
  display: flex;
@@ -20,4 +20,4 @@ import ShadowComponent from"./ShadowComponent.js";import{html,css}from"../lit-al
20
20
  button.no-btn {
21
21
  padding: var(--padding);
22
22
  }
23
- `;static setTheme(e){localStorage.setItem("theme",e),document.documentElement.setAttribute("theme",e),window.dispatchEvent(new StorageEvent("storage",{key:"theme",newValue:e}))}static getCurrentTheme(){let e=document.documentElement.getAttribute("theme");return e||(e=localStorage.getItem("theme")),e||"auto"}}const colorSchemeQuery=window.matchMedia("(prefers-color-scheme: dark)"),colorSchemeChangeHandler=e=>document.documentElement.setAttribute("auto-theme",e.matches?"dark":"light");colorSchemeQuery.addEventListener("change",colorSchemeChangeHandler),colorSchemeChangeHandler(colorSchemeQuery),ThemeSwitcher.setTheme(ThemeSwitcher.getCurrentTheme()),customElements.define("k-theme-switcher",ThemeSwitcher);
23
+ `;static setTheme(e){theme.set(e)}static getCurrentTheme(){return theme.get()}static getCalculatedCurrentTheme(){return theme.getCalculated()}}customElements.define("k-theme-switcher",ThemeSwitcher);
@@ -0,0 +1 @@
1
+ const contexts={},getContext=e=>(contexts[e]||(contexts[e]={value:void 0,subscribers:new Set}),contexts[e]);export const createContext=(e="global",t)=>{const s=getContext(e);return void 0!==t&&void 0===s.value&&(s.value=t),{get:()=>s.value,set:e=>{s.value=e,s.subscribers.forEach(t=>t(e))},subscribe:e=>(s.subscribers.add(e),e(s.value),()=>s.subscribers.delete(e)),unsubscribe:e=>s.subscribers.delete(e)}};export default createContext;
@@ -0,0 +1 @@
1
+ import createContext from"./context.js";const getInitialTheme=()=>{let e=document.documentElement.getAttribute("theme");return e||(e=localStorage.getItem("theme")),e||"auto"},themeContext=createContext("theme",getInitialTheme());themeContext.subscribe(e=>{localStorage.setItem("theme",e),document.documentElement.setAttribute("theme",e)});export const setTheme=e=>themeContext.set(e);export const getTheme=()=>themeContext.get();export const subscribeToTheme=e=>themeContext.subscribe(e);export const getCalculatedTheme=()=>{const e=getTheme();return"auto"===e?window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light":e};const colorSchemeQuery=window.matchMedia("(prefers-color-scheme: dark)"),colorSchemeChangeHandler=e=>document.documentElement.setAttribute("auto-theme",e.matches?"dark":"light");colorSchemeQuery.addEventListener("change",colorSchemeChangeHandler),colorSchemeChangeHandler(colorSchemeQuery);export default{get:getTheme,set:setTheme,subscribe:subscribeToTheme,getCalculated:getCalculatedTheme};
@@ -0,0 +1,160 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Context Utility - Kempo UI</title>
7
+ <link rel="icon" type="image/png" sizes="48x48" href="../media/icon48.png">
8
+ <link rel="manifest" href="../manifest.json" />
9
+ <link rel="stylesheet" href="../kempo-vars.css" />
10
+ <link rel="stylesheet" href="../kempo.min.css" />
11
+ <link rel="stylesheet" href="../kempo-hljs.css" />
12
+ <link rel="stylesheet" href="../styles.css" />
13
+ <script>window.litDisableBundleWarning = true;</script>
14
+ <script src="../init-1.js" type="module"></script>
15
+ </head>
16
+ <body>
17
+ <k-import src="../nav-1.inc.html"></k-import>
18
+ <main>
19
+ <h1>Context Utility</h1>
20
+
21
+ <k-accordion persistent-id="toc" class="b r mb">
22
+ <k-accordion-header for-panel="toc-panel">Table of Contents</k-accordion-header>
23
+ <k-accordion-panel name="toc-panel">
24
+ <div class="m ">
25
+ <h6>Usage</h6>
26
+ <a href="#basicUsage">Basic Usage</a><br />
27
+ <a href="#api">API Methods</a><br />
28
+ <a href="#examples">Examples</a><br />
29
+ </div>
30
+ </k-accordion-panel>
31
+ </k-accordion>
32
+
33
+ <h3>Description</h3>
34
+ <p>The <code>createContext</code> utility provides a simple way to create global or named shared state that components can subscribe to. It enables reactive state management with automatic subscriber notifications on value changes.</p>
35
+
36
+ <h3 id="basicUsage"><a href="#basicUsage" class="no-link">Basic Usage</a></h3>
37
+ <p>Import and create a context to manage shared state:</p>
38
+ <pre><code class="hljs javascript"><span class="hljs-keyword">import</span> createContext <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/context.js'</span>;<br /><br /><span class="hljs-comment">// Create a named context with initial value</span><br /><span class="hljs-keyword">const</span> userContext = createContext(<span class="hljs-string">'user'</span>, { <span class="hljs-attr">name</span>: <span class="hljs-string">'Guest'</span> });<br /><br /><span class="hljs-comment">// Subscribe to changes</span><br />userContext.subscribe(<span class="hljs-function">(<span class="hljs-params">user</span>) =&gt;</span> {<br /> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'User changed:'</span>, user);<br />});<br /><br /><span class="hljs-comment">// Update the context value</span><br />userContext.set({ <span class="hljs-attr">name</span>: <span class="hljs-string">'John Doe'</span> });</code></pre>
39
+
40
+ <h3 id="api"><a href="#api" class="no-link">API Methods</a></h3>
41
+
42
+ <h4><code>createContext(name, initialValue)</code></h4>
43
+ <p>Creates or retrieves a context with the specified name.</p>
44
+ <h5><code>name<i>: string</i></code> (optional)</h5>
45
+ <p>The name of the context. Defaults to <code>'global'</code>. Using the same name returns the same context instance.</p>
46
+ <h5><code>initialValue<i>: any</i></code> (optional)</h5>
47
+ <p>The initial value for the context. Only set if the context doesn't already have a value.</p>
48
+ <h5>Returns: <code>Object</code></h5>
49
+ <p>An object with the following methods:</p>
50
+
51
+ <h4><code>get()</code></h4>
52
+ <p>Returns the current value of the context.</p>
53
+ <pre><code class="hljs javascript"><span class="hljs-keyword">const</span> currentValue = userContext.get();</code></pre>
54
+
55
+ <h4><code>set(value)</code></h4>
56
+ <p>Updates the context value and notifies all subscribers.</p>
57
+ <pre><code class="hljs javascript">userContext.set({ <span class="hljs-attr">name</span>: <span class="hljs-string">'Jane Doe'</span>, <span class="hljs-attr">role</span>: <span class="hljs-string">'admin'</span> });</code></pre>
58
+
59
+ <h4><code>subscribe(callback)</code></h4>
60
+ <p>Subscribes to context changes. The callback is immediately invoked with the current value, then called again whenever the value changes.</p>
61
+ <h5><code>callback<i>: Function</i></code></h5>
62
+ <p>Function to call when the context value changes. Receives the new value as an argument.</p>
63
+ <h5>Returns: <code>Function</code></h5>
64
+ <p>An unsubscribe function to remove the subscription.</p>
65
+ <pre><code class="hljs javascript"><span class="hljs-keyword">const</span> unsubscribe = userContext.subscribe(<span class="hljs-function">(<span class="hljs-params">value</span>) =&gt;</span> {<br /> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'New value:'</span>, value);<br />});<br /><br /><span class="hljs-comment">// Later, stop listening</span><br />unsubscribe();</code></pre>
66
+
67
+ <h4><code>unsubscribe(callback)</code></h4>
68
+ <p>Removes a subscriber callback from the context.</p>
69
+ <pre><code class="hljs javascript"><span class="hljs-keyword">const</span> handler = <span class="hljs-function">(<span class="hljs-params">value</span>) =&gt;</span> <span class="hljs-built_in">console</span>.log(value);<br />userContext.subscribe(handler);<br />userContext.unsubscribe(handler);</code></pre>
70
+
71
+ <h3 id="examples"><a href="#examples" class="no-link">Examples</a></h3>
72
+
73
+ <h4>Theme Management</h4>
74
+ <pre><code class="hljs javascript"><span class="hljs-keyword">import</span> createContext <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/context.js'</span>;<br /><br /><span class="hljs-keyword">const</span> themeContext = createContext(<span class="hljs-string">'theme'</span>, <span class="hljs-string">'light'</span>);<br /><br /><span class="hljs-comment">// Components can subscribe to theme changes</span><br />themeContext.subscribe(<span class="hljs-function">(<span class="hljs-params">theme</span>) =&gt;</span> {<br /> <span class="hljs-built_in">document</span>.body.className = theme;<br />});<br /><br /><span class="hljs-comment">// Toggle theme from anywhere</span><br /><span class="hljs-keyword">const</span> toggleTheme = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {<br /> <span class="hljs-keyword">const</span> current = themeContext.get();<br /> themeContext.set(current === <span class="hljs-string">'light'</span> ? <span class="hljs-string">'dark'</span> : <span class="hljs-string">'light'</span>);<br />};</code></pre>
75
+
76
+ <h4>Component State Sharing</h4>
77
+ <pre><code class="hljs javascript"><span class="hljs-keyword">import</span> createContext <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/context.js'</span>;<br /><br /><span class="hljs-keyword">const</span> cartContext = createContext(<span class="hljs-string">'cart'</span>, []);<br /><br /><span class="hljs-comment">// Add item from one component</span><br /><span class="hljs-keyword">const</span> addToCart = <span class="hljs-function">(<span class="hljs-params">item</span>) =&gt;</span> {<br /> <span class="hljs-keyword">const</span> items = cartContext.get();<br /> cartContext.set([...items, item]);<br />};<br /><br /><span class="hljs-comment">// Display cart in another component</span><br />cartContext.subscribe(<span class="hljs-function">(<span class="hljs-params">items</span>) =&gt;</span> {<br /> <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">'.cart-count'</span>).textContent = items.length;<br />});</code></pre>
78
+
79
+ <h4>Global vs Named Contexts</h4>
80
+ <pre><code class="hljs javascript"><span class="hljs-keyword">import</span> createContext <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/context.js'</span>;<br /><br /><span class="hljs-comment">// Use default global context</span><br /><span class="hljs-keyword">const</span> globalCtx = createContext();<br />globalCtx.set({ <span class="hljs-attr">appReady</span>: <span class="hljs-literal">true</span> });<br /><br /><span class="hljs-comment">// Named contexts are isolated</span><br /><span class="hljs-keyword">const</span> userCtx = createContext(<span class="hljs-string">'user'</span>);<br /><span class="hljs-keyword">const</span> settingsCtx = createContext(<span class="hljs-string">'settings'</span>);<br /><br /><span class="hljs-comment">// Same name returns the same context instance</span><br /><span class="hljs-keyword">const</span> userCtx2 = createContext(<span class="hljs-string">'user'</span>);<br /><span class="hljs-built_in">console</span>.log(userCtx === userCtx2); <span class="hljs-comment">// true</span></code></pre>
81
+
82
+ <h4>Working Example</h4>
83
+ <div class="row -mx mb">
84
+ <div class="col d-span-6 m-span-12 px">
85
+ <k-card label="HTML & JavaScript">
86
+ <pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"context-demo"</span>&gt;</span>
87
+ <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Current count: <span class="hljs-tag">&lt;<span class="hljs-name">strong</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"count-display"</span>&gt;</span>0<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
88
+ <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"increment"</span>&gt;</span>Increment<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
89
+ <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"decrement"</span>&gt;</span>Decrement<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
90
+ <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"reset"</span>&gt;</span>Reset<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
91
+ <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
92
+
93
+ <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"module"</span>&gt;</span><span class="javascript">
94
+ <span class="hljs-keyword">import</span> createContext <span class="hljs-keyword">from</span> <span class="hljs-string">'../src/utils/context.js'</span>;
95
+
96
+ <span class="hljs-keyword">const</span> counterContext = createContext(<span class="hljs-string">'counter'</span>, <span class="hljs-number">0</span>);
97
+
98
+ <span class="hljs-comment">// Subscribe to updates</span>
99
+ counterContext.subscribe(<span class="hljs-function">(<span class="hljs-params">count</span>) =&gt;</span> {
100
+ <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'count-display'</span>).textContent = count;
101
+ });
102
+
103
+ <span class="hljs-comment">// Button handlers</span>
104
+ <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'increment'</span>)
105
+ .<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
106
+ counterContext.<span class="hljs-title function_">set</span>(counterContext.<span class="hljs-title function_">get</span>() + <span class="hljs-number">1</span>);
107
+ });
108
+
109
+ <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'decrement'</span>)
110
+ .<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
111
+ counterContext.<span class="hljs-title function_">set</span>(counterContext.<span class="hljs-title function_">get</span>() - <span class="hljs-number">1</span>);
112
+ });
113
+
114
+ <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'reset'</span>)
115
+ .<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
116
+ counterContext.<span class="hljs-title function_">set</span>(<span class="hljs-number">0</span>);
117
+ });
118
+ </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></code></pre>
119
+ </k-card>
120
+ </div>
121
+ <div class="col d-span-6 m-span-12 px">
122
+ <k-card label="Results">
123
+ <div id="context-demo">
124
+ <p>Current count: <strong id="count-display">0</strong></p>
125
+ <button id="increment" class="primary">Increment</button>
126
+ <button id="decrement">Decrement</button>
127
+ <button id="reset" class="danger">Reset</button>
128
+ </div>
129
+ </k-card>
130
+ </div>
131
+ </div>
132
+
133
+ </main>
134
+ <div style="height:33vh"></div>
135
+ <script type="module" src="../src/components/Import.js"></script>
136
+ <script type="module" src="../src/components/Accordion.js"></script>
137
+ <script type="module" src="../src/components/Card.js"></script>
138
+ <script type="module">
139
+ import createContext from '../src/utils/context.js';
140
+
141
+ const counterContext = createContext('counter-demo', 0);
142
+
143
+ counterContext.subscribe(count => {
144
+ document.getElementById('count-display').textContent = count;
145
+ });
146
+
147
+ document.getElementById('increment').addEventListener('click', () => {
148
+ counterContext.set(counterContext.get() + 1);
149
+ });
150
+
151
+ document.getElementById('decrement').addEventListener('click', () => {
152
+ counterContext.set(counterContext.get() - 1);
153
+ });
154
+
155
+ document.getElementById('reset').addEventListener('click', () => {
156
+ counterContext.set(0);
157
+ });
158
+ </script>
159
+ </body>
160
+ </html>
@@ -0,0 +1,153 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Theme Utility - Kempo UI</title>
7
+ <link rel="icon" type="image/png" sizes="48x48" href="../media/icon48.png">
8
+ <link rel="manifest" href="../manifest.json" />
9
+ <link rel="stylesheet" href="../kempo-vars.css" />
10
+ <link rel="stylesheet" href="../kempo.min.css" />
11
+ <link rel="stylesheet" href="../kempo-hljs.css" />
12
+ <link rel="stylesheet" href="../styles.css" />
13
+ <script>window.litDisableBundleWarning = true;</script>
14
+ <script src="../init-1.js" type="module"></script>
15
+ </head>
16
+ <body>
17
+ <k-import src="../nav-1.inc.html"></k-import>
18
+ <main>
19
+ <h1>Theme Utility</h1>
20
+
21
+ <k-accordion persistent-id="toc" class="b r mb">
22
+ <k-accordion-header for-panel="toc-panel">Table of Contents</k-accordion-header>
23
+ <k-accordion-panel name="toc-panel">
24
+ <div class="m ">
25
+ <h6>Usage</h6>
26
+ <a href="#basicUsage">Basic Usage</a><br />
27
+ <a href="#api">API Methods</a><br />
28
+ <a href="#examples">Examples</a><br />
29
+ </div>
30
+ </k-accordion-panel>
31
+ </k-accordion>
32
+
33
+ <h3>Description</h3>
34
+ <p>The <code>theme</code> utility provides a centralized way to manage the application's theme state. It handles theme persistence via localStorage, automatic system preference detection, and reactive updates across components. This utility works independently of the ThemeSwitcher component, allowing any part of your application to read, set, or subscribe to theme changes.</p>
35
+
36
+ <h3 id="basicUsage"><a href="#basicUsage" class="no-link">Basic Usage</a></h3>
37
+ <p>Import and use the theme utility to manage application themes:</p>
38
+ <pre><code class="hljs javascript"><span class="hljs-keyword">import</span> theme <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/theme.js'</span>;<br /><br /><span class="hljs-comment">// Get current theme</span><br /><span class="hljs-keyword">const</span> currentTheme = theme.get(); <span class="hljs-comment">// 'auto', 'light', or 'dark'</span><br /><br /><span class="hljs-comment">// Set theme</span><br />theme.set(<span class="hljs-string">'dark'</span>);<br /><br /><span class="hljs-comment">// Subscribe to theme changes</span><br /><span class="hljs-keyword">const</span> unsubscribe = theme.subscribe(<span class="hljs-function">(<span class="hljs-params">newTheme</span>) =&gt;</span> {<br /> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Theme changed to:'</span>, newTheme);<br />});</code></pre>
39
+
40
+ <h3 id="api"><a href="#api" class="no-link">API Methods</a></h3>
41
+
42
+ <h4><code>theme.get()</code></h4>
43
+ <p>Returns the current theme value.</p>
44
+ <h5>Returns: <code>string</code></h5>
45
+ <p>One of: <code>'auto'</code>, <code>'light'</code>, or <code>'dark'</code></p>
46
+ <pre><code class="hljs javascript"><span class="hljs-keyword">const</span> currentTheme = theme.get();<br /><span class="hljs-built_in">console</span>.log(currentTheme); <span class="hljs-comment">// 'auto', 'light', or 'dark'</span></code></pre>
47
+
48
+ <h4><code>theme.set(value)</code></h4>
49
+ <p>Sets the theme and automatically persists it to localStorage and updates the DOM.</p>
50
+ <h5><code>value<i>: string</i></code></h5>
51
+ <p>The theme to set. Must be one of: <code>'auto'</code>, <code>'light'</code>, or <code>'dark'</code></p>
52
+ <pre><code class="hljs javascript">theme.set(<span class="hljs-string">'dark'</span>);</code></pre>
53
+
54
+ <h4><code>theme.subscribe(callback)</code></h4>
55
+ <p>Subscribes to theme changes. The callback is immediately invoked with the current theme, then called again whenever the theme changes.</p>
56
+ <h5><code>callback<i>: Function</i></code></h5>
57
+ <p>Function to call when the theme changes. Receives the new theme value as an argument.</p>
58
+ <h5>Returns: <code>Function</code></h5>
59
+ <p>An unsubscribe function to remove the subscription.</p>
60
+ <pre><code class="hljs javascript"><span class="hljs-keyword">const</span> unsubscribe = theme.subscribe(<span class="hljs-function">(<span class="hljs-params">newTheme</span>) =&gt;</span> {<br /> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Theme is now:'</span>, newTheme);<br />});<br /><br /><span class="hljs-comment">// Later, stop listening</span><br />unsubscribe();</code></pre>
61
+
62
+ <h4><code>theme.getCalculated()</code></h4>
63
+ <p>Returns the effective theme, resolving <code>'auto'</code> to either <code>'light'</code> or <code>'dark'</code> based on the user's system preference.</p>
64
+ <h5>Returns: <code>string</code></h5>
65
+ <p>Either <code>'light'</code> or <code>'dark'</code></p>
66
+ <pre><code class="hljs javascript"><span class="hljs-keyword">const</span> effectiveTheme = theme.getCalculated();<br /><span class="hljs-built_in">console</span>.log(effectiveTheme); <span class="hljs-comment">// Always 'light' or 'dark'</span></code></pre>
67
+
68
+ <h3 id="examples"><a href="#examples" class="no-link">Examples</a></h3>
69
+
70
+ <h4>Reading Theme Without Component</h4>
71
+ <pre><code class="hljs javascript"><span class="hljs-keyword">import</span> theme <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/theme.js'</span>;<br /><br /><span class="hljs-comment">// Get theme anywhere in your app</span><br /><span class="hljs-keyword">const</span> currentTheme = theme.get();<br /><span class="hljs-keyword">const</span> isDark = theme.getCalculated() === <span class="hljs-string">'dark'</span>;<br /><br /><span class="hljs-keyword">if</span>(isDark){<br /> <span class="hljs-comment">// Apply dark-specific logic</span><br />}</code></pre>
72
+
73
+ <h4>Component Subscribing to Theme</h4>
74
+ <pre><code class="hljs javascript"><span class="hljs-keyword">import</span> { LitElement, html } <span class="hljs-keyword">from</span> <span class="hljs-string">'lit'</span>;<br /><span class="hljs-keyword">import</span> theme <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/theme.js'</span>;<br /><br /><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyComponent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_ inherited__">LitElement</span> {<br /> <span class="hljs-keyword">static</span> properties = {<br /> <span class="hljs-attr">theme</span>: { <span class="hljs-attr">type</span>: <span class="hljs-title class_">String</span> }<br /> };<br /><br /> <span class="hljs-title function_">connectedCallback</span>() {<br /> <span class="hljs-variable language_">super</span>.<span class="hljs-title function_">connectedCallback</span>();<br /> <span class="hljs-variable language_">this</span>.<span class="hljs-property">unsubscribe</span> = theme.<span class="hljs-title function_">subscribe</span>(<span class="hljs-function">(<span class="hljs-params">t</span>) =&gt;</span> {<br /> <span class="hljs-variable language_">this</span>.<span class="hljs-property">theme</span> = t;<br /> });<br /> }<br /><br /> <span class="hljs-title function_">disconnectedCallback</span>() {<br /> <span class="hljs-variable language_">super</span>.<span class="hljs-title function_">disconnectedCallback</span>();<br /> <span class="hljs-keyword">if</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">unsubscribe</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">unsubscribe</span>();<br /> }<br /><br /> <span class="hljs-title function_">render</span>() {<br /> <span class="hljs-keyword">return</span> html<span class="hljs-string">`&lt;p&gt;Current theme: ${this.theme}&lt;/p&gt;`</span>;<br /> }<br />}</code></pre>
75
+
76
+ <h4>Setting Theme Programmatically</h4>
77
+ <pre><code class="hljs javascript"><span class="hljs-keyword">import</span> theme <span class="hljs-keyword">from</span> <span class="hljs-string">'./utils/theme.js'</span>;<br /><br /><span class="hljs-comment">// Set theme based on user preference from API</span><br /><span class="hljs-keyword">const</span> userPreferences = <span class="hljs-keyword">await</span> <span class="hljs-title function_">fetchUserPreferences</span>();<br />theme.<span class="hljs-title function_">set</span>(userPreferences.<span class="hljs-property">theme</span>);<br /><br /><span class="hljs-comment">// Toggle between light and dark</span><br /><span class="hljs-keyword">const</span> <span class="hljs-title function_">toggleTheme</span> = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {<br /> <span class="hljs-keyword">const</span> current = theme.<span class="hljs-title function_">get</span>();<br /> <span class="hljs-keyword">if</span>(current === <span class="hljs-string">'dark'</span>) theme.<span class="hljs-title function_">set</span>(<span class="hljs-string">'light'</span>);<br /> <span class="hljs-keyword">else</span> theme.<span class="hljs-title function_">set</span>(<span class="hljs-string">'dark'</span>);<br />};</code></pre>
78
+
79
+ <h4>Working Example</h4>
80
+ <div class="row -mx mb">
81
+ <div class="col d-span-6 m-span-12 px">
82
+ <k-card label="HTML & JavaScript">
83
+ <pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"theme-demo"</span>&gt;</span>
84
+ <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Current theme: <span class="hljs-tag">&lt;<span class="hljs-name">strong</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"theme-display"</span>&gt;</span>auto<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
85
+ <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Calculated: <span class="hljs-tag">&lt;<span class="hljs-name">strong</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"calculated-display"</span>&gt;</span>dark<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
86
+ <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"set-auto"</span>&gt;</span>Auto<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
87
+ <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"set-light"</span>&gt;</span>Light<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
88
+ <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"set-dark"</span>&gt;</span>Dark<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
89
+ <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
90
+
91
+ <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"module"</span>&gt;</span><span class="javascript">
92
+ <span class="hljs-keyword">import</span> theme <span class="hljs-keyword">from</span> <span class="hljs-string">'../src/utils/theme.js'</span>;
93
+
94
+ <span class="hljs-comment">// Subscribe to updates</span>
95
+ theme.subscribe(<span class="hljs-function">(<span class="hljs-params">t</span>) =&gt;</span> {
96
+ <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'theme-display'</span>).textContent = t;
97
+ <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'calculated-display'</span>).textContent =
98
+ theme.<span class="hljs-title function_">getCalculated</span>();
99
+ });
100
+
101
+ <span class="hljs-comment">// Button handlers</span>
102
+ <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'set-auto'</span>)
103
+ .<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> theme.<span class="hljs-title function_">set</span>(<span class="hljs-string">'auto'</span>));
104
+ <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'set-light'</span>)
105
+ .<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> theme.<span class="hljs-title function_">set</span>(<span class="hljs-string">'light'</span>));
106
+ <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">'set-dark'</span>)
107
+ .<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> theme.<span class="hljs-title function_">set</span>(<span class="hljs-string">'dark'</span>));
108
+ </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></code></pre>
109
+ </k-card>
110
+ </div>
111
+ <div class="col d-span-6 m-span-12 px">
112
+ <k-card label="Results">
113
+ <div id="theme-demo">
114
+ <p>Current theme: <strong id="theme-display">auto</strong></p>
115
+ <p>Calculated: <strong id="calculated-display">dark</strong></p>
116
+ <button id="set-auto">Auto</button>
117
+ <button id="set-light" class="primary">Light</button>
118
+ <button id="set-dark" class="danger">Dark</button>
119
+ </div>
120
+ </k-card>
121
+ </div>
122
+ </div>
123
+
124
+ <h3>Automatic Features</h3>
125
+ <h4>localStorage Persistence</h4>
126
+ <p>The theme utility automatically saves the current theme to <code>localStorage</code> with the key <code>'theme'</code>. This means the user's theme preference persists across page reloads and browser sessions.</p>
127
+
128
+ <h4>DOM Attribute Updates & Kempo CSS Integration</h4>
129
+ <p>Whenever the theme changes, it's automatically applied to <code>document.documentElement</code> as a <code>theme</code> attribute. This sets the <code>color-scheme</code> property which enables Kempo CSS's <code>light-dark()</code> function to work correctly.</p>
130
+ <p>Kempo CSS provides theme-aware CSS custom properties that automatically adapt to light and dark modes. See the <a href="https://dustinpoissant.github.io/kempo-css/" target="_blank" rel="noopener noreferrer">Kempo CSS documentation</a> for details on available color variables and theming.</p>
131
+
132
+ <h4>System Preference Detection</h4>
133
+ <p>When the theme is set to <code>'auto'</code>, the utility monitors the system's <code>prefers-color-scheme</code> media query and updates the <code>auto-theme</code> attribute on the document element. The <code>getCalculated()</code> method resolves the actual theme based on this preference.</p>
134
+
135
+ </main>
136
+ <div style="height:33vh"></div>
137
+ <script type="module" src="../src/components/Import.js"></script>
138
+ <script type="module" src="../src/components/Accordion.js"></script>
139
+ <script type="module" src="../src/components/Card.js"></script>
140
+ <script type="module">
141
+ import theme from '../src/utils/theme.js';
142
+
143
+ theme.subscribe(t => {
144
+ document.getElementById('theme-display').textContent = t;
145
+ document.getElementById('calculated-display').textContent = theme.getCalculated();
146
+ });
147
+
148
+ document.getElementById('set-auto').addEventListener('click', () => theme.set('auto'));
149
+ document.getElementById('set-light').addEventListener('click', () => theme.set('light'));
150
+ document.getElementById('set-dark').addEventListener('click', () => theme.set('dark'));
151
+ </script>
152
+ </body>
153
+ </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kempo-ui",
3
- "version": "0.0.35",
3
+ "version": "0.0.36",
4
4
  "type": "module",
5
5
  "description": "A Lit based web-component library",
6
6
  "main": "index.js",
@@ -1,5 +1,6 @@
1
1
  import ShadowComponent from './ShadowComponent.js';
2
2
  import { html, css } from '../lit-all.min.js';
3
+ import theme from '../utils/theme.js';
3
4
  import './Icon.js';
4
5
 
5
6
  export default class ThemeSwitcher extends ShadowComponent {
@@ -10,24 +11,17 @@ export default class ThemeSwitcher extends ShadowComponent {
10
11
 
11
12
  constructor() {
12
13
  super();
13
- this.currentTheme = ThemeSwitcher.getCurrentTheme();
14
+ this.currentTheme = theme.get();
14
15
  }
15
16
 
16
17
  /*
17
18
  Event Handlers
18
19
  */
19
20
  handleClick = () => {
20
- const current = ThemeSwitcher.getCurrentTheme();
21
- if(current === 'auto') ThemeSwitcher.setTheme('light');
22
- if(current === 'light') ThemeSwitcher.setTheme('dark');
23
- if(current === 'dark') ThemeSwitcher.setTheme('auto');
24
- }
25
-
26
- handleStorageChange = (event) => {
27
- if (event.key === 'theme') {
28
- this.currentTheme = event.newValue || 'auto';
29
- document.documentElement.setAttribute('theme', this.currentTheme);
30
- }
21
+ const current = theme.get();
22
+ if(current === 'auto') theme.set('light');
23
+ if(current === 'light') theme.set('dark');
24
+ if(current === 'dark') theme.set('auto');
31
25
  }
32
26
 
33
27
  /*
@@ -35,12 +29,14 @@ export default class ThemeSwitcher extends ShadowComponent {
35
29
  */
36
30
  connectedCallback() {
37
31
  super.connectedCallback();
38
- window.addEventListener('storage', this.handleStorageChange);
32
+ this.unsubscribe = theme.subscribe(t => {
33
+ this.currentTheme = t;
34
+ });
39
35
  }
40
36
 
41
37
  disconnectedCallback() {
42
38
  super.disconnectedCallback();
43
- window.removeEventListener('storage', this.handleStorageChange);
39
+ if(this.unsubscribe) this.unsubscribe();
44
40
  }
45
41
 
46
42
  /*
@@ -84,26 +80,17 @@ export default class ThemeSwitcher extends ShadowComponent {
84
80
  /*
85
81
  Static Methods
86
82
  */
87
- static setTheme(theme) {
88
- localStorage.setItem('theme', theme);
89
- document.documentElement.setAttribute('theme', theme);
90
- window.dispatchEvent(new StorageEvent('storage', { key: 'theme', newValue: theme }));
83
+ static setTheme(t) {
84
+ theme.set(t);
91
85
  }
92
86
 
93
87
  static getCurrentTheme() {
94
- let theme = document.documentElement.getAttribute('theme');
95
- if(!theme) theme = localStorage.getItem('theme');
96
- return theme || 'auto';
88
+ return theme.get();
97
89
  }
98
- }
99
90
 
100
- /*
101
- Auto Theme Detection
102
- */
103
- const colorSchemeQuery = window.matchMedia('(prefers-color-scheme: dark)');
104
- const colorSchemeChangeHandler = event => document.documentElement.setAttribute('auto-theme', event.matches ? 'dark' : 'light');
105
- colorSchemeQuery.addEventListener('change', colorSchemeChangeHandler);
106
- colorSchemeChangeHandler(colorSchemeQuery);
91
+ static getCalculatedCurrentTheme() {
92
+ return theme.getCalculated();
93
+ }
94
+ }
107
95
 
108
- ThemeSwitcher.setTheme(ThemeSwitcher.getCurrentTheme());
109
96
  customElements.define('k-theme-switcher', ThemeSwitcher);
@@ -0,0 +1,40 @@
1
+ /*
2
+ Context Utility
3
+ - Create global or named shared state
4
+ - Subscribe/unsubscribe to changes
5
+ - Set/get context values
6
+ */
7
+
8
+ const contexts = {};
9
+
10
+ const getContext = name => {
11
+ if(!contexts[name]){
12
+ contexts[name] = {
13
+ value: undefined,
14
+ subscribers: new Set()
15
+ };
16
+ }
17
+ return contexts[name];
18
+ };
19
+
20
+ export const createContext = (name = 'global', initialValue) => {
21
+ const ctx = getContext(name);
22
+ if(initialValue !== undefined && ctx.value === undefined){
23
+ ctx.value = initialValue;
24
+ }
25
+ return {
26
+ get: () => ctx.value,
27
+ set: val => {
28
+ ctx.value = val;
29
+ ctx.subscribers.forEach(fn => fn(val));
30
+ },
31
+ subscribe: fn => {
32
+ ctx.subscribers.add(fn);
33
+ fn(ctx.value);
34
+ return () => ctx.subscribers.delete(fn);
35
+ },
36
+ unsubscribe: fn => ctx.subscribers.delete(fn)
37
+ };
38
+ };
39
+
40
+ export default createContext;
@@ -0,0 +1,50 @@
1
+ /*
2
+ Theme Utility
3
+ - Manage global theme state
4
+ - Subscribe to theme changes
5
+ - Persist theme to localStorage
6
+ */
7
+
8
+ import createContext from './context.js';
9
+
10
+ const getInitialTheme = () => {
11
+ let theme = document.documentElement.getAttribute('theme');
12
+ if(!theme) theme = localStorage.getItem('theme');
13
+ return theme || 'auto';
14
+ };
15
+
16
+ const themeContext = createContext('theme', getInitialTheme());
17
+
18
+ themeContext.subscribe(theme => {
19
+ localStorage.setItem('theme', theme);
20
+ document.documentElement.setAttribute('theme', theme);
21
+ });
22
+
23
+ export const setTheme = theme => themeContext.set(theme);
24
+
25
+ export const getTheme = () => themeContext.get();
26
+
27
+ export const subscribeToTheme = callback => themeContext.subscribe(callback);
28
+
29
+ export const getCalculatedTheme = () => {
30
+ const theme = getTheme();
31
+ if(theme === 'auto'){
32
+ return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
33
+ }
34
+ return theme;
35
+ };
36
+
37
+ /*
38
+ Auto Theme Detection
39
+ */
40
+ const colorSchemeQuery = window.matchMedia('(prefers-color-scheme: dark)');
41
+ const colorSchemeChangeHandler = event => document.documentElement.setAttribute('auto-theme', event.matches ? 'dark' : 'light');
42
+ colorSchemeQuery.addEventListener('change', colorSchemeChangeHandler);
43
+ colorSchemeChangeHandler(colorSchemeQuery);
44
+
45
+ export default {
46
+ get: getTheme,
47
+ set: setTheme,
48
+ subscribe: subscribeToTheme,
49
+ getCalculated: getCalculatedTheme
50
+ };