@gottheflag/nova 0.1.0-beta.3 → 0.1.0-beta.4
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/LICENSE +201 -0
- package/README.md +4 -0
- package/dist/adapters/index.d.ts +1 -0
- package/dist/adapters/index.js +9 -1
- package/dist/adapters/index.js.map +1 -1
- package/dist/core/index.d.ts +39 -87
- package/dist/core/index.js +25 -1
- package/dist/core/index.js.map +1 -1
- package/dist/storage/index.d.ts +87 -0
- package/dist/storage/index.js +88 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage-n1c3g6vy.d.ts +51 -0
- package/package.json +1 -1
- package/dist/chunk-NYMA6GFH.js +0 -34
- package/dist/chunk-NYMA6GFH.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
package/dist/adapters/index.d.ts
CHANGED
package/dist/adapters/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
2
3
|
|
|
3
4
|
// src/core/adapter.ts
|
|
4
5
|
function defineAdapter(definition) {
|
|
@@ -6,6 +7,13 @@ function defineAdapter(definition) {
|
|
|
6
7
|
}
|
|
7
8
|
__name(defineAdapter, "defineAdapter");
|
|
8
9
|
|
|
10
|
+
// src/core/utils.ts
|
|
11
|
+
var DEFAULT_PREFIX = "theme:";
|
|
12
|
+
function parseValue(value, prefix) {
|
|
13
|
+
return value.startsWith(prefix) ? value.slice(prefix.length) : value;
|
|
14
|
+
}
|
|
15
|
+
__name(parseValue, "parseValue");
|
|
16
|
+
|
|
9
17
|
// src/adapters/button.ts
|
|
10
18
|
var Button = defineAdapter({
|
|
11
19
|
name: "button",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/core/adapter.ts","../../src/adapters/button.ts","../../src/adapters/radio.ts","../../src/adapters/select.ts"],"names":["defineAdapter","definition","Button","name","defaults","prefix","DEFAULT_PREFIX","animate","enabled","duration","easing","selector","o","bind","ctl","el","addEventListener","event","theme","parseValue","value","state","document","startViewTransition","set","clientX","x","clientY","y","radius","Math","hypot","max","innerWidth","innerHeight","transition","ready","documentElement","clipPath","pseudoElement","Radio","checked","sync","active","Select","option","options","selectedIndex","selected"],"mappings":";;;AA6CO,SAASA,cAIfC,UAAAA,EAAmC;AAEnC,EAAA,OAAOA,UAAAA;AACR;AAPgBD,MAAAA,CAAAA,aAAAA,EAAAA,eAAAA,CAAAA;;;ACpBT,IAAME,SAASF,aAAAA,CAAgD;EAClEG,IAAAA,EAAM,QAAA;EAENC,QAAAA,EAAU;IACNC,MAAAA,EAAQC,cAAAA;IACRC,OAAAA,EAAS;MACLC,OAAAA,EAAS,KAAA;MACTC,QAAAA,EAAU,GAAA;MACVC,MAAAA,EAAQ;AACZ;AACJ,GAAA;AAEAC,EAAAA,QAAAA,0BAAWC,CAAAA,KAAM,CAAA,eAAA,EAAkBA,CAAAA,CAAEP,MAAAA,IAAUC,cAAAA,CAAAA,EAAAA,CAAAA,EAArC,UAAA,CAAA;AAEVO,EAAAA,IAAAA,CAAKC,KAAKC,EAAAA,EAAI,EAAEV,MAAAA,GAASC,cAAAA,EAAgBC,SAAO,EAAE;AAC9CQ,IAAAA,EAAAA,CAAGC,gBAAAA,CAAiB,OAAA,EAAS,OAAOC,KAAAA,KAAAA;AAChC,MAAA,MAAMC,KAAAA,GAAQC,UAAAA,CAAWJ,EAAAA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,gBAAAA,CAAiB,UAAU,MAAA;AAC1B,MAAA,MAAM6B,MAAAA,GAAS9B,EAAAA,CAAG+B,OAAAA,CAAS/B,EAAAA,CAAGgC,aAAa,CAAA;AAC3C,MAAA,MAAM7B,KAAAA,GAAQC,UAAAA,CAAW0B,MAAAA,CAAOzB,KAAAA,EAAOf,MAAAA,CAAAA;AAEvC,MAAA,IAAIa,KAAAA,KAAUJ,IAAIO,KAAAA,EAAO;AAEzBP,MAAAA,GAAAA,CAAIU,IAAIN,KAAAA,CAAAA;IACZ,CAAA,CAAA;AACJ,EAAA,CAAA;AAEAwB,EAAAA,IAAAA,CAAK5B,GAAAA,EAAKC,EAAAA,EAAI,EAAEV,MAAAA,GAASC,gBAAc,EAAE;AACrC,IAAA,MAAMqC,MAAAA,GAAS7B,IAAI6B,MAAAA,EAAM;AAEzB,IAAA,KAAA,MAAWE,MAAAA,IAAU9B,GAAG+B,OAAAA,EAAS;AAC7BD,MAAAA,MAAAA,CAAOG,QAAAA,GAAW7B,UAAAA,CAAW0B,MAAAA,CAAOzB,KAAAA,EAAOf,MAAAA,CAAAA,KAAYsC,MAAAA;AAC3D,IAAA;AACJ,EAAA;AACJ,CAAA","file":"index.js","sourcesContent":["/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport type { Controller } from \"./controller.js\";\r\n\r\nexport interface AdapterDefinition<\r\n\tE extends Element,\r\n\tO = void\r\n> {\r\n\tname: string;\r\n\r\n\t/** Default options — merged with options passed to `ctl.use()`. */\r\n\tdefaults?: Partial<O>;\r\n\r\n\t/** CSS selector string, or a function that returns one given resolved options. */\r\n\tselector: string | ((options: O) => string);\r\n\r\n\t/**\r\n\t * Attach event listeners to a single element.\r\n\t * Called once per element — at `use()` time, or when a new element is observed.\r\n\t */\r\n\tbind?(ctl: Controller, element: E, options: O): void;\r\n\r\n\t/**\r\n\t * Sync element UI state with the controller's current theme.\r\n\t * Called after every `ctl.set()` and on initial `use()`.\r\n\t */\r\n\tsync?(ctl: Controller, element: E, options: O): void;\r\n}\r\n\r\n/**\r\n * Type helper for defining adapters with full inference.\r\n * Zero runtime cost — returns the definition unchanged.\r\n * \r\n * @example\r\n * export const MyAdapter = defineAdapter<HTMLButtonElement, MyOptions>({\r\n * name: \"my-adapter\",\r\n * defaults: { prefix: \"theme:\" },\r\n * selector: (o) => `button[value=\"${o.prefix}\"]`,\r\n * bind(ctl, el, options) { ... },\r\n * sync(ctl, el, options) { ... },\r\n * });\r\n */\r\nexport function defineAdapter<\r\n\tE extends Element,\r\n\tO = void\r\n>(\r\n\tdefinition: AdapterDefinition<E, O>\r\n): AdapterDefinition<E, O> {\r\n\treturn definition;\r\n}","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { defineAdapter } from \"../core/adapter.js\";\r\nimport { State } from \"../core/types.js\";\r\nimport { DEFAULT_PREFIX, parseValue } from \"../core/utils.js\";\r\n\r\ntype Easing = \"linear\" | \"ease\" | \"ease-in\" | \"ease-out\" | \"ease-in-out\";\r\n\r\ninterface ButtonOptions {\r\n prefix?: string;\r\n animate?: {\r\n enabled?: boolean;\r\n duration?: number;\r\n easing?: Easing;\r\n }\r\n}\r\n\r\n/**\r\n * @example\r\n * <button value=\"theme:light\">Light</button>\r\n * <button value=\"theme:dark\">Dark</button>\r\n */\r\nexport const Button = defineAdapter<HTMLButtonElement, ButtonOptions>({\r\n name: \"button\",\r\n\r\n defaults: {\r\n prefix: DEFAULT_PREFIX,\r\n animate: {\r\n enabled: false,\r\n duration: 600,\r\n easing: \"ease-in-out\"\r\n }\r\n },\r\n\r\n selector: (o) => `button[value^=\"${o.prefix ?? DEFAULT_PREFIX}\"]`,\r\n\r\n bind(ctl, el, { prefix = DEFAULT_PREFIX, animate }) {\r\n el.addEventListener(\"click\", async (event) => {\r\n const theme = parseValue(el.value, prefix) as State;\r\n\r\n if (theme === ctl.state) return;\r\n\r\n if (!animate?.enabled || !document.startViewTransition) {\r\n ctl.set(theme);\r\n return;\r\n }\r\n\r\n const { clientX: x, clientY: y } = event;\r\n\r\n const radius = Math.hypot(\r\n Math.max(x, innerWidth - x),\r\n Math.max(y, innerHeight - y)\r\n );\r\n\r\n const transition = document.startViewTransition(() => ctl.set(theme));\r\n\r\n await transition.ready;\r\n\r\n document.documentElement.animate(\r\n {\r\n clipPath: [\r\n `circle(0px at ${x}px ${y}px)`,\r\n `circle(${radius}px at ${x}px ${y}px)`\r\n ]\r\n },\r\n {\r\n duration: animate.duration ?? 500,\r\n easing: animate.easing ?? \"ease-in-out\",\r\n pseudoElement: \"::view-transition-new(root)\"\r\n }\r\n );\r\n });\r\n },\r\n});\r\n","// /**\r\n// * Copyright (c) 2026 GTF\r\n// * SPDX-License-Identifier: Apache-2.0\r\n// */\r\n\r\nimport { defineAdapter } from \"../core/adapter.js\";\r\nimport { State } from \"../core/types.js\";\r\nimport { DEFAULT_PREFIX, parseValue } from \"../core/utils.js\";\r\n\r\ninterface RadioOptions {\r\n prefix?: string;\r\n}\r\n\r\n/**\r\n * @example\r\n * <input type=\"radio\" name=\"theme\" value=\"theme:light\">\r\n * <input type=\"radio\" name=\"theme\" value=\"theme:dark\">\r\n * <input type=\"radio\" name=\"theme\" value=\"theme:system\">\r\n */\r\nexport const Radio = defineAdapter<HTMLInputElement, RadioOptions>({\r\n name: \"radio\",\r\n\r\n defaults: {\r\n prefix: DEFAULT_PREFIX,\r\n },\r\n\r\n selector: (o) => `input[type=\"radio\"][value^=\"${o.prefix}\"]`,\r\n\r\n bind(ctl, el, { prefix = DEFAULT_PREFIX }) {\r\n el.addEventListener(\"change\", () => {\r\n if (!el.checked) return;\r\n\r\n const theme = parseValue(el.value, prefix) as State;\r\n\r\n if (theme === ctl.state) return;\r\n\r\n ctl.set(theme);\r\n });\r\n },\r\n\r\n sync(ctl, el, { prefix = DEFAULT_PREFIX }) {\r\n el.checked = parseValue(el.value, prefix) === ctl.active();\r\n }\r\n});","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { defineAdapter } from \"../core/adapter.js\";\r\nimport { State } from \"../core/types.js\";\r\nimport { DEFAULT_PREFIX, parseValue } from \"../core/utils.js\";\r\n\r\ninterface SelectOptions {\r\n prefix?: string;\r\n}\r\n\r\n/**\r\n * @example\r\n * ```html\r\n * <select>\r\n * <option value=\"theme:light\">Light</option>\r\n * <option value=\"theme:system\">System</option>\r\n * <option value=\"theme:dark\">Dark</option>\r\n * </select>\r\n * ```\r\n */\r\nexport const Select = defineAdapter<HTMLSelectElement, SelectOptions>({\r\n name: \"select\",\r\n\r\n defaults: {\r\n prefix: DEFAULT_PREFIX,\r\n },\r\n \r\n selector: (o) => `select:has(option[value^=\"${o.prefix}\"])`,\r\n\r\n bind(ctl, el, { prefix = DEFAULT_PREFIX }) {\r\n el.addEventListener(\"change\", () => {\r\n const option = el.options[ el.selectedIndex ];\r\n const theme = parseValue(option.value, prefix) as State;\r\n\r\n if (theme === ctl.state) return;\r\n\r\n ctl.set(theme);\r\n });\r\n },\r\n\r\n sync(ctl, el, { prefix = DEFAULT_PREFIX }) {\r\n const active = ctl.active();\r\n\r\n for (const option of el.options) {\r\n option.selected = parseValue(option.value, prefix) === active;\r\n }\r\n }\r\n});"]}
|
|
1
|
+
{"version":3,"sources":["../../src/core/adapter.ts","../../src/core/utils.ts","../../src/adapters/button.ts","../../src/adapters/radio.ts","../../src/adapters/select.ts"],"names":["defineAdapter","definition","DEFAULT_PREFIX","parseValue","value","prefix","startsWith","slice","length","Button","name","defaults","animate","enabled","duration","easing","selector","o","bind","ctl","el","addEventListener","event","theme","state","document","startViewTransition","set","clientX","x","clientY","y","radius","Math","hypot","max","innerWidth","innerHeight","transition","ready","documentElement","clipPath","pseudoElement","Radio","checked","sync","active","Select","option","options","selectedIndex","selected"],"mappings":";;;;AA6CO,SAASA,cAIfC,UAAAA,EAAmC;AAEnC,EAAA,OAAOA,UAAAA;AACR;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GAAAA,CAAIQ,IAAIJ,KAAAA,CAAAA;IACZ,CAAA,CAAA;AACJ,EAAA,CAAA;AAEAsB,EAAAA,IAAAA,CAAK1B,GAAAA,EAAKC,EAAAA,EAAI,EAAEf,MAAAA,GAASH,gBAAc,EAAE;AACrC,IAAA,MAAM4C,MAAAA,GAAS3B,IAAI2B,MAAAA,EAAM;AAEzB,IAAA,KAAA,MAAWE,MAAAA,IAAU5B,GAAG6B,OAAAA,EAAS;AAC7BD,MAAAA,MAAAA,CAAOG,QAAAA,GAAWhD,UAAAA,CAAW6C,MAAAA,CAAO5C,KAAAA,EAAOC,MAAAA,CAAAA,KAAYyC,MAAAA;AAC3D,IAAA;AACJ,EAAA;AACJ,CAAA","file":"index.js","sourcesContent":["/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport type { Controller } from \"./controller.js\";\r\n\r\nexport interface AdapterDefinition<\r\n\tE extends Element,\r\n\tO = void\r\n> {\r\n\tname: string;\r\n\r\n\t/** Default options — merged with options passed to `ctl.use()`. */\r\n\tdefaults?: Partial<O>;\r\n\r\n\t/** CSS selector string, or a function that returns one given resolved options. */\r\n\tselector: string | ((options: O) => string);\r\n\r\n\t/**\r\n\t * Attach event listeners to a single element.\r\n\t * Called once per element — at `use()` time, or when a new element is observed.\r\n\t */\r\n\tbind?(ctl: Controller, element: E, options: O): void;\r\n\r\n\t/**\r\n\t * Sync element UI state with the controller's current theme.\r\n\t * Called after every `ctl.set()` and on initial `use()`.\r\n\t */\r\n\tsync?(ctl: Controller, element: E, options: O): void;\r\n}\r\n\r\n/**\r\n * Type helper for defining adapters with full inference.\r\n * Zero runtime cost — returns the definition unchanged.\r\n * \r\n * @example\r\n * export const MyAdapter = defineAdapter<HTMLButtonElement, MyOptions>({\r\n * name: \"my-adapter\",\r\n * defaults: { prefix: \"theme:\" },\r\n * selector: (o) => `button[value=\"${o.prefix}\"]`,\r\n * bind(ctl, el, options) { ... },\r\n * sync(ctl, el, options) { ... },\r\n * });\r\n */\r\nexport function defineAdapter<\r\n\tE extends Element,\r\n\tO = void\r\n>(\r\n\tdefinition: AdapterDefinition<E, O>\r\n): AdapterDefinition<E, O> {\r\n\treturn definition;\r\n}","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { ResolvedConfig } from \"./config.js\";\r\nimport { System } from \"./system.js\";\r\nimport { State, type Name } from \"./types.js\";\r\n\r\nexport const DEFAULT_PREFIX = \"theme:\";\r\n\r\n/**\r\n * Resolve a theme state to an effective theme name.\r\n * \r\n * @see {@link State}\r\n * \r\n * @param theme Theme state\r\n * @param system System instance.\r\n * @param config Resolved set of configuration.\r\n * @returns \r\n */\r\nexport function resolveStateOf(state: State, system: System, config: ResolvedConfig): Name | null {\r\n\tif (state === config.system) {\r\n\t\treturn system.prefers;\r\n\t}\r\n\r\n\tif (state === config.light || state === config.dark) {\r\n\t\treturn state;\r\n\t}\r\n\r\n\treturn typeof state === \"string\" ? state : null;\r\n}\r\n\r\n/**\r\n * Resolve a root element from a given value.\r\n * \r\n * @description\r\n * This function takes a root value and returns the corresponding\r\n * HTMLElement. If the value is already an HTMLElement, it is returned\r\n * directly. If the value is a Document, it returns the documentElement.\r\n * If the value is a ShadowRoot, it returns the host element.\r\n * \r\n * @param root Root value\r\n */\r\nexport function resolveRoot(root: unknown): HTMLElement {\r\n\tif (root instanceof HTMLElement) {\r\n\t\treturn root\r\n\t} else if (root instanceof Document) {\r\n\t\treturn root.documentElement\r\n\t} else if (root instanceof ShadowRoot && root.host instanceof HTMLElement) {\r\n\t\treturn root.host\r\n\t};\r\n\r\n\treturn document.documentElement;\r\n}\r\n\r\n/**\r\n * Parse a value with a prefix.\r\n * \r\n * @description\r\n * This function takes a value and a prefix. If the value starts with\r\n * the prefix, it is sliced from the beginning. Otherwise, the value is\r\n * returned as-is.\r\n * \r\n * @param value Value to parse\r\n * @param prefix Prefix to check\r\n */\r\nexport function parseValue(value: string, prefix: string): string {\r\n\treturn value.startsWith(prefix)\r\n\t\t? value.slice(prefix.length)\r\n\t\t: value;\r\n}","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { defineAdapter } from \"../core/adapter.js\";\r\nimport { State } from \"../core/types.js\";\r\nimport { DEFAULT_PREFIX, parseValue } from \"../core/utils.js\";\r\n\r\ntype Easing = \"linear\" | \"ease\" | \"ease-in\" | \"ease-out\" | \"ease-in-out\";\r\n\r\ninterface ButtonOptions {\r\n prefix?: string;\r\n animate?: {\r\n enabled?: boolean;\r\n duration?: number;\r\n easing?: Easing;\r\n }\r\n}\r\n\r\n/**\r\n * @example\r\n * <button value=\"theme:light\">Light</button>\r\n * <button value=\"theme:dark\">Dark</button>\r\n */\r\nexport const Button = defineAdapter<HTMLButtonElement, ButtonOptions>({\r\n name: \"button\",\r\n\r\n defaults: {\r\n prefix: DEFAULT_PREFIX,\r\n animate: {\r\n enabled: false,\r\n duration: 600,\r\n easing: \"ease-in-out\"\r\n }\r\n },\r\n\r\n selector: (o) => `button[value^=\"${o.prefix ?? DEFAULT_PREFIX}\"]`,\r\n\r\n bind(ctl, el, { prefix = DEFAULT_PREFIX, animate }) {\r\n el.addEventListener(\"click\", async (event) => {\r\n const theme = parseValue(el.value, prefix) as State;\r\n\r\n if (theme === ctl.state) return;\r\n\r\n if (!animate?.enabled || !document.startViewTransition) {\r\n ctl.set(theme);\r\n return;\r\n }\r\n\r\n const { clientX: x, clientY: y } = event;\r\n\r\n const radius = Math.hypot(\r\n Math.max(x, innerWidth - x),\r\n Math.max(y, innerHeight - y)\r\n );\r\n\r\n const transition = document.startViewTransition(() => ctl.set(theme));\r\n\r\n await transition.ready;\r\n\r\n document.documentElement.animate(\r\n {\r\n clipPath: [\r\n `circle(0px at ${x}px ${y}px)`,\r\n `circle(${radius}px at ${x}px ${y}px)`\r\n ]\r\n },\r\n {\r\n duration: animate.duration ?? 500,\r\n easing: animate.easing ?? \"ease-in-out\",\r\n pseudoElement: \"::view-transition-new(root)\"\r\n }\r\n );\r\n });\r\n },\r\n});\r\n","// /**\r\n// * Copyright (c) 2026 GTF\r\n// * SPDX-License-Identifier: Apache-2.0\r\n// */\r\n\r\nimport { defineAdapter } from \"../core/adapter.js\";\r\nimport { State } from \"../core/types.js\";\r\nimport { DEFAULT_PREFIX, parseValue } from \"../core/utils.js\";\r\n\r\ninterface RadioOptions {\r\n prefix?: string;\r\n}\r\n\r\n/**\r\n * @example\r\n * <input type=\"radio\" name=\"theme\" value=\"theme:light\">\r\n * <input type=\"radio\" name=\"theme\" value=\"theme:dark\">\r\n * <input type=\"radio\" name=\"theme\" value=\"theme:system\">\r\n */\r\nexport const Radio = defineAdapter<HTMLInputElement, RadioOptions>({\r\n name: \"radio\",\r\n\r\n defaults: {\r\n prefix: DEFAULT_PREFIX,\r\n },\r\n\r\n selector: (o) => `input[type=\"radio\"][value^=\"${o.prefix}\"]`,\r\n\r\n bind(ctl, el, { prefix = DEFAULT_PREFIX }) {\r\n el.addEventListener(\"change\", () => {\r\n if (!el.checked) return;\r\n\r\n const theme = parseValue(el.value, prefix) as State;\r\n\r\n if (theme === ctl.state) return;\r\n\r\n ctl.set(theme);\r\n });\r\n },\r\n\r\n sync(ctl, el, { prefix = DEFAULT_PREFIX }) {\r\n el.checked = parseValue(el.value, prefix) === ctl.active();\r\n }\r\n});","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { defineAdapter } from \"../core/adapter.js\";\r\nimport { State } from \"../core/types.js\";\r\nimport { DEFAULT_PREFIX, parseValue } from \"../core/utils.js\";\r\n\r\ninterface SelectOptions {\r\n prefix?: string;\r\n}\r\n\r\n/**\r\n * @example\r\n * ```html\r\n * <select>\r\n * <option value=\"theme:light\">Light</option>\r\n * <option value=\"theme:system\">System</option>\r\n * <option value=\"theme:dark\">Dark</option>\r\n * </select>\r\n * ```\r\n */\r\nexport const Select = defineAdapter<HTMLSelectElement, SelectOptions>({\r\n name: \"select\",\r\n\r\n defaults: {\r\n prefix: DEFAULT_PREFIX,\r\n },\r\n \r\n selector: (o) => `select:has(option[value^=\"${o.prefix}\"])`,\r\n\r\n bind(ctl, el, { prefix = DEFAULT_PREFIX }) {\r\n el.addEventListener(\"change\", () => {\r\n const option = el.options[ el.selectedIndex ];\r\n const theme = parseValue(option.value, prefix) as State;\r\n\r\n if (theme === ctl.state) return;\r\n\r\n ctl.set(theme);\r\n });\r\n },\r\n\r\n sync(ctl, el, { prefix = DEFAULT_PREFIX }) {\r\n const active = ctl.active();\r\n\r\n for (const option of el.options) {\r\n option.selected = parseValue(option.value, prefix) === active;\r\n }\r\n }\r\n});"]}
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,89 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Copyright (c) 2026 GTF
|
|
3
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
interface AdapterDefinition<E extends Element, O = void> {
|
|
7
|
-
name: string;
|
|
8
|
-
/** Default options — merged with options passed to `ctl.use()`. */
|
|
9
|
-
defaults?: Partial<O>;
|
|
10
|
-
/** CSS selector string, or a function that returns one given resolved options. */
|
|
11
|
-
selector: string | ((options: O) => string);
|
|
12
|
-
/**
|
|
13
|
-
* Attach event listeners to a single element.
|
|
14
|
-
* Called once per element — at `use()` time, or when a new element is observed.
|
|
15
|
-
*/
|
|
16
|
-
bind?(ctl: Controller, element: E, options: O): void;
|
|
17
|
-
/**
|
|
18
|
-
* Sync element UI state with the controller's current theme.
|
|
19
|
-
* Called after every `ctl.set()` and on initial `use()`.
|
|
20
|
-
*/
|
|
21
|
-
sync?(ctl: Controller, element: E, options: O): void;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Type helper for defining adapters with full inference.
|
|
25
|
-
* Zero runtime cost — returns the definition unchanged.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* export const MyAdapter = defineAdapter<HTMLButtonElement, MyOptions>({
|
|
29
|
-
* name: "my-adapter",
|
|
30
|
-
* defaults: { prefix: "theme:" },
|
|
31
|
-
* selector: (o) => `button[value="${o.prefix}"]`,
|
|
32
|
-
* bind(ctl, el, options) { ... },
|
|
33
|
-
* sync(ctl, el, options) { ... },
|
|
34
|
-
* });
|
|
35
|
-
*/
|
|
36
|
-
declare function defineAdapter<E extends Element, O = void>(definition: AdapterDefinition<E, O>): AdapterDefinition<E, O>;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Copyright (c) 2026 GTF
|
|
40
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
41
|
-
*/
|
|
42
|
-
/**
|
|
43
|
-
* Effective theme name (e.g. `day`, `night`, `mint`).
|
|
44
|
-
*/
|
|
45
|
-
type Name = string;
|
|
46
|
-
/**
|
|
47
|
-
* Theme state (`<dark>`, `<light>`, `<system>`).
|
|
48
|
-
* Different between state themes and effective themes:
|
|
49
|
-
* - State themes are the raw values used as identifiers (`<dark>`, `<light>`, `<system>`).
|
|
50
|
-
* - Effective themes are the resolved values (e.g. `<dark>`, `<light>`, `mint`).
|
|
51
|
-
*/
|
|
52
|
-
type State = "system" | "light" | "dark";
|
|
53
|
-
/**
|
|
54
|
-
* Emitted when the theme state changes.
|
|
55
|
-
*/
|
|
56
|
-
interface ChangeEvent {
|
|
57
|
-
from: State | null;
|
|
58
|
-
to: State;
|
|
59
|
-
theme: Name;
|
|
60
|
-
}
|
|
61
|
-
interface ApplyEvent {
|
|
62
|
-
theme: Name;
|
|
63
|
-
}
|
|
64
|
-
type NEvents = {
|
|
65
|
-
change: ChangeEvent;
|
|
66
|
-
apply: ApplyEvent;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Copyright (c) 2026 GTF
|
|
71
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
72
|
-
*/
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* A storage interface for persisting the theme state.
|
|
76
|
-
*
|
|
77
|
-
* @description
|
|
78
|
-
* The theme state is persisted in a storage mechanism that is
|
|
79
|
-
* specific to the application. For example, a web application might
|
|
80
|
-
* use cookies, local storage, or a database.
|
|
81
|
-
*/
|
|
82
|
-
interface StateStorage {
|
|
83
|
-
get(): State | null;
|
|
84
|
-
set(state: State): void;
|
|
85
|
-
remove(): void;
|
|
86
|
-
}
|
|
1
|
+
import { S as StateStorage, N as Name, b as NEvents, a as State } from '../storage-n1c3g6vy.js';
|
|
87
2
|
|
|
88
3
|
/**
|
|
89
4
|
* Copyright (c) 2026 GTF
|
|
@@ -297,4 +212,41 @@ declare class Controller extends EventEmitter<NEvents> {
|
|
|
297
212
|
private bindNode;
|
|
298
213
|
}
|
|
299
214
|
|
|
300
|
-
|
|
215
|
+
/**
|
|
216
|
+
* Copyright (c) 2026 GTF
|
|
217
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
218
|
+
*/
|
|
219
|
+
|
|
220
|
+
interface AdapterDefinition<E extends Element, O = void> {
|
|
221
|
+
name: string;
|
|
222
|
+
/** Default options — merged with options passed to `ctl.use()`. */
|
|
223
|
+
defaults?: Partial<O>;
|
|
224
|
+
/** CSS selector string, or a function that returns one given resolved options. */
|
|
225
|
+
selector: string | ((options: O) => string);
|
|
226
|
+
/**
|
|
227
|
+
* Attach event listeners to a single element.
|
|
228
|
+
* Called once per element — at `use()` time, or when a new element is observed.
|
|
229
|
+
*/
|
|
230
|
+
bind?(ctl: Controller, element: E, options: O): void;
|
|
231
|
+
/**
|
|
232
|
+
* Sync element UI state with the controller's current theme.
|
|
233
|
+
* Called after every `ctl.set()` and on initial `use()`.
|
|
234
|
+
*/
|
|
235
|
+
sync?(ctl: Controller, element: E, options: O): void;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Type helper for defining adapters with full inference.
|
|
239
|
+
* Zero runtime cost — returns the definition unchanged.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* export const MyAdapter = defineAdapter<HTMLButtonElement, MyOptions>({
|
|
243
|
+
* name: "my-adapter",
|
|
244
|
+
* defaults: { prefix: "theme:" },
|
|
245
|
+
* selector: (o) => `button[value="${o.prefix}"]`,
|
|
246
|
+
* bind(ctl, el, options) { ... },
|
|
247
|
+
* sync(ctl, el, options) { ... },
|
|
248
|
+
* });
|
|
249
|
+
*/
|
|
250
|
+
declare function defineAdapter<E extends Element, O = void>(definition: AdapterDefinition<E, O>): AdapterDefinition<E, O>;
|
|
251
|
+
|
|
252
|
+
export { type AdapterDefinition, Controller, EventEmitter, Name, type ResolvedConfig, State, defineAdapter };
|
package/dist/core/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
2
3
|
|
|
3
4
|
// src/storage/local.ts
|
|
4
5
|
var LocalStorage = class {
|
|
@@ -110,6 +111,29 @@ var System = class {
|
|
|
110
111
|
}
|
|
111
112
|
};
|
|
112
113
|
|
|
114
|
+
// src/core/utils.ts
|
|
115
|
+
function resolveStateOf(state, system, config) {
|
|
116
|
+
if (state === config.system) {
|
|
117
|
+
return system.prefers;
|
|
118
|
+
}
|
|
119
|
+
if (state === config.light || state === config.dark) {
|
|
120
|
+
return state;
|
|
121
|
+
}
|
|
122
|
+
return typeof state === "string" ? state : null;
|
|
123
|
+
}
|
|
124
|
+
__name(resolveStateOf, "resolveStateOf");
|
|
125
|
+
function resolveRoot(root) {
|
|
126
|
+
if (root instanceof HTMLElement) {
|
|
127
|
+
return root;
|
|
128
|
+
} else if (root instanceof Document) {
|
|
129
|
+
return root.documentElement;
|
|
130
|
+
} else if (root instanceof ShadowRoot && root.host instanceof HTMLElement) {
|
|
131
|
+
return root.host;
|
|
132
|
+
}
|
|
133
|
+
return document.documentElement;
|
|
134
|
+
}
|
|
135
|
+
__name(resolveRoot, "resolveRoot");
|
|
136
|
+
|
|
113
137
|
// src/core/controller.ts
|
|
114
138
|
var Controller = class extends EventEmitter {
|
|
115
139
|
static {
|
package/dist/core/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/storage/local.ts","../../src/core/event.ts","../../src/core/system.ts","../../src/core/controller.ts"],"names":["LocalStorage","key","options","get","localStorage","getItem","set","state","setItem","console","warn","remove","removeItem","EventEmitter","_map","Map","on","event","listener","Set","add","off","delete","emit","payload","forEach","System","mql","handler","ctl","prefers","matchMedia","matches","config","dark","light","prefersDark","prefersLight","start","onChange","e","addEventListener","stop","removeEventListener","undefined","Controller","system","_adapters","_observer","_root","document","cfg","attribute","storage","initial","observe","syncSystemListener","startObserver","root","resolveRoot","active","resolved","theme","resolveStateOf","use","def","defaults","selector","el","querySelectorAll","bind","sync","push","from","write","apply","syncAdapters","to","destroy","disconnect","setAttribute","MutationObserver","mutations","mu","node","addedNodes","Element","bindNode","childList","subtree"],"mappings":";;;AAoBO,IAAMA,eAAN,MAAMA;EApBb;;;AAqBUC,EAAAA,GAAAA;EAET,WAAA,CAAYC,OAAAA,GAA+B,EAAC,EAAG;AAC9C,IAAA,IAAA,CAAKD,GAAAA,GAAMC,QAAQD,GAAAA,IAAO,OAAA;AAC3B,EAAA;EAEAE,GAAAA,GAAoB;AACnB,IAAA,IAAI;AACH,MAAA,OAAOC,YAAAA,CAAaC,OAAAA,CAAQ,IAAA,CAAKJ,GAAG,CAAA;IACrC,CAAA,CAAA,MAAQ;AACP,MAAA,OAAO,IAAA;AACR,IAAA;AACD,EAAA;AAEAK,EAAAA,GAAAA,CAAIC,KAAAA,EAAoB;AACvB,IAAA,IAAI;AACHH,MAAAA,YAAAA,CAAaI,OAAAA,CAAQ,IAAA,CAAKP,GAAAA,EAAKM,KAAAA,CAAAA;IAChC,CAAA,CAAA,MAAQ;AACPE,MAAAA,OAAAA,CAAQC,KAAK,sCAAA,CAAA;AACd,IAAA;AACD,EAAA;EAEAC,MA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,EAASiE,QAAAA,EAAQ,IAAM,KAAKpB,SAAAA,EAAW;AACxD,MAAA,IAAIqC,IAAAA,CAAKpD,OAAAA,CAAQmC,QAAAA,CAAAA,EAAW;AAC3BF,QAAAA,GAAAA,CAAIK,IAAAA,GAAO,IAAA,EAAMc,IAAAA,EAAMlF,OAAAA,CAAAA;AACvB+D,QAAAA,GAAAA,CAAIM,IAAAA,GAAO,IAAA,EAAMa,IAAAA,EAAMlF,OAAAA,CAAAA;AACxB,MAAA;AAEA,MAAA,KAAA,MAAWkE,EAAAA,IAAMgB,IAAAA,CAAKf,gBAAAA,CAAiBF,QAAAA,CAAAA,EAAW;AACjDF,QAAAA,GAAAA,CAAIK,IAAAA,GAAO,IAAA,EAAMF,EAAAA,EAAIlE,OAAAA,CAAAA;AACrB+D,QAAAA,GAAAA,CAAIM,IAAAA,GAAO,IAAA,EAAMH,EAAAA,EAAIlE,OAAAA,CAAAA;AACtB,MAAA;AACD,IAAA;AACD,EAAA;AACD","file":"index.js","sourcesContent":["/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { StateStorage } from \"../core/storage.js\";\r\nimport { State } from \"../core/types.js\";\r\n\r\nexport interface LocalStorageOptions {\r\n\t/**\r\n\t * Local storage key.\r\n\t * \r\n\t * @default \"theme\"\r\n\t */\r\n\tkey?: string;\r\n}\r\n\r\n/**\r\n * Simple local storage implementation that uses the browser's native `localStorage`.\r\n */\r\nexport class LocalStorage implements StateStorage {\r\n\treadonly key: string;\r\n\t\r\n\tconstructor(options: LocalStorageOptions = {}) {\r\n\t\tthis.key = options.key ?? \"theme\";\r\n\t}\r\n\t\r\n\tget(): State | null {\r\n\t\ttry {\r\n\t\t\treturn localStorage.getItem(this.key) as State | null;\r\n\t\t} catch {\r\n\t\t\treturn null;\r\n\t\t}\r\n\t}\r\n\r\n\tset(state: State): void {\r\n\t\ttry {\r\n\t\t\tlocalStorage.setItem(this.key, state);\r\n\t\t} catch {\r\n\t\t\tconsole.warn(\"[LOCAL] Failed to write theme state.\");\r\n\t\t}\r\n\t}\r\n\r\n\tremove(): void {\r\n\t\ttry {\r\n\t\t\tlocalStorage.removeItem(this.key);\r\n\t\t} catch {}\r\n\t}\r\n}","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nexport type Listener<T> = (event: T) => void;\r\n\r\n/**\r\n * A simple event emitter implementation.\r\n * \r\n * @description\r\n * This class is suitable for use in a web application that wants to\r\n * listen for changes in the theme state.\r\n */\r\nexport class EventEmitter<M extends Record<string, unknown>> {\r\n\tprivate _map = new Map<keyof M, Set<Listener<any>>>();\r\n\r\n\ton<K extends keyof M>(event: K, listener: Listener<M[ K ]>): () => void {\r\n\t\tlet set = this._map.get(event);\r\n\t\tif (!set) this._map.set(event, set = new Set());\r\n\r\n\t\tset.add(listener);\r\n\r\n\t\treturn () => this.off(event, listener);\r\n\t}\r\n\r\n\toff<K extends keyof M>(event: K, listener: Listener<M[ K ]>): void {\r\n\t\tthis._map.get(event)?.delete(listener);\r\n\t}\r\n\r\n\temit<K extends keyof M>(event: K, payload: M[ K ]): void {\r\n\t\tthis._map.get(event)?.forEach(listener => listener(payload));\r\n\t}\r\n}","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { Controller } from \"./controller.js\";\r\nimport { Name, State } from \"./types.js\";\r\n\r\n/**\r\n * System theme proxy.\r\n * \r\n * Used to detect system theme preference and listen for changes.\r\n */\r\nexport class System {\r\n\t/**\r\n\t * Media query list instance.\r\n\t */\r\n\tprivate mql?: MediaQueryList;\r\n\t/**\r\n\t * Handles the media query list listener.\r\n\t */\r\n\tprivate handler?: (e: MediaQueryListEvent) => void;\r\n\t/**\r\n\t * Controller instance.\r\n\t */\r\n\r\n\tconstructor(private ctl: Controller) { }\r\n\r\n\t/**\r\n\t * Get the effective theme by system preference.\r\n\t */\r\n\tget prefers(): Name {\r\n\t\treturn matchMedia?.(\"(prefers-color-scheme: dark)\").matches\r\n\t\t\t? this.ctl.config.dark\r\n\t\t\t: this.ctl.config.light;\r\n\t}\r\n\r\n\t/**\r\n\t * Check if the effective theme is dark.\r\n\t */\r\n\tget prefersDark(): boolean {\r\n\t\treturn this.prefers === this.ctl.config.dark;\r\n\t}\r\n\r\n\t/**\r\n\t * Check if the effective theme is light.\r\n\t */\r\n\tget prefersLight(): boolean {\r\n\t\treturn this.prefers === this.ctl.config.light;\r\n\t}\r\n\r\n\t/**\r\n\t * Start listening for system theme changes.\r\n\t * \r\n\t * @param onChange Callback to be called when the system theme changes.\r\n\t */\r\n\tstart(onChange: (state: Exclude<State, \"system\">) => void) {\r\n\t\tif (this.handler) return;\r\n\r\n\t\tthis.mql = matchMedia(\"(prefers-color-scheme: dark)\");\r\n\t\tthis.handler = e => onChange(e.matches ? \"dark\" : \"light\");\r\n\r\n\t\tthis.mql.addEventListener?.(\"change\", this.handler);\r\n\t}\r\n\r\n\t/**\r\n\t * Stop listening for system theme changes.\r\n\t */\r\n\tstop() {\r\n\t\tif (!this.mql || !this.handler) return;\r\n\r\n\t\tthis.mql.removeEventListener?.(\"change\", this.handler);\r\n\t\t\r\n\t\tthis.mql = this.handler = undefined;\r\n\t}\r\n}","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { LocalStorage } from \"../storage/local.js\";\r\nimport { AdapterDefinition } from \"./adapter.js\";\r\nimport { RawConfig, ResolvedConfig } from \"./config.js\";\r\nimport { EventEmitter } from \"./event.js\";\r\nimport { System } from \"./system.js\";\r\nimport { Name, NEvents, State } from \"./types.js\";\r\nimport { resolveRoot, resolveStateOf } from \"./utils.js\";\r\n\r\ninterface RegisteredAdapter {\r\n\tdef: AdapterDefinition<any, any>;\r\n\toptions: any;\r\n\tselector: string;\r\n}\r\n\r\n/**\r\n * Theme controller logic.\r\n */\r\nexport class Controller extends EventEmitter<NEvents> {\r\n\t/**\r\n\t * Resolved configuration.\r\n\t */\r\n\tconfig: ResolvedConfig;\r\n\t/**\r\n\t * System theme proxy.\r\n\t */\r\n\tprivate system: System;\r\n\r\n\tprivate _adapters: RegisteredAdapter[] = [];\r\n\tprivate _observer?: MutationObserver;\r\n\r\n\tconstructor(\r\n\t\tprivate _root: Document | ParentNode = document,\r\n\t\tcfg: RawConfig = {}\r\n\t) {\r\n\t\tsuper();\r\n\t\t\r\n\t\tthis.config = {\r\n\t\t\tattribute: cfg.attribute ?? \"data-theme\",\r\n\t\t\tstorage: cfg.storage ?? new LocalStorage(),\r\n\t\t\tsystem: cfg.system ?? \"system\",\r\n\t\t\tlight: cfg.light ?? \"light\",\r\n\t\t\tdark: cfg.dark ?? \"dark\",\r\n\t\t\tinitial: cfg.initial,\r\n\t\t\tobserve: cfg.observe ?? false\r\n\t\t};\r\n\r\n\t\tthis.system = new System(this);\r\n\r\n\t\tthis.syncSystemListener();\r\n\r\n\t\tif (this.config.observe) {\r\n\t\t\tthis.startObserver();\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * The root element to apply theme to.\r\n\t */\r\n\tget root(): HTMLElement {\r\n\t\treturn resolveRoot(this._root);\r\n\t}\r\n\r\n\t/**\r\n\t * @param resolved resolve state and effective themes to their values.\r\n\t * \r\n\t * @example\r\n\t * \r\n\t * system = config.system;\r\n\t * light = config.light;\r\n\t * dark = config.dark;\r\n\t * \r\n\t * resolved = true;\r\n\t * [\r\n\t * \t\t\"system\": light | dark,\r\n\t * \t\t\"light\": light,\r\n\t * \t\t\"dark\": dark\r\n\t * ]\r\n\t * resolved = false;\r\n\t * [\r\n\t * \t\t\"system\": system,\r\n\t * \t\t\"light\": light,\r\n\t * \t\t\"dark\": dark\r\n\t * ]\r\n\t */\r\n\tactive(resolved: boolean = false): State | Name | null {\r\n\t\tconst state = this.state;\r\n\r\n\t\tif (!state) {\r\n\t\t\treturn this.config.initial || null;\r\n\t\t}\r\n\r\n\t\tconst theme = resolveStateOf(\r\n\t\t\tstate,\r\n\t\t\tthis.system,\r\n\t\t\tthis.config\r\n\t\t);\r\n\r\n\t\tif (!theme) return null;\r\n\r\n\t\treturn resolved\r\n\t\t\t? theme\r\n\t\t\t: state;\r\n\t}\r\n\r\n\t/**\r\n\t * Register an adapter.\r\n\t * \r\n\t * @description\r\n\t * Adapters are toys used to change themes.\r\n\t * They define where themes lives and how they work with the controller.\r\n\t * \r\n\t * @param adapter Adapter instance\r\n\t * @param options Adapter options\r\n\t */\r\n\tuse<O>(\r\n\t\tdef: AdapterDefinition<any, O>,\r\n\t\toptions?: Partial<O>\r\n\t): this {\r\n\t\tconst resolved = { ...def.defaults, ...options } as O;\r\n\r\n\t\tconst selector = typeof def.selector === \"function\"\r\n\t\t\t? def.selector(resolved)\r\n\t\t\t: def.selector;\r\n\t\t\r\n\t\tfor (const el of this.root.querySelectorAll(selector)) {\r\n\t\t\tdef.bind?.(this, el, resolved);\r\n\t\t\tdef.sync?.(this, el, resolved);\r\n\t\t}\r\n\r\n\t\tthis._adapters.push({\r\n\t\t\tdef,\r\n\t\t\toptions: resolved,\r\n\t\t\tselector\r\n\t\t});\r\n\t\t\r\n\t\treturn this;\r\n\t}\r\n\t\r\n\t/**\r\n\t * Set the effective theme from a theme state.\r\n\t * \r\n\t * @see {@link State}\r\n\t * @param state Theme State\r\n\t */\r\n\tset(state: State) {\r\n\t\tconst from = this.state;\r\n\t\tconst theme = resolveStateOf(state, this.system, this.config);\r\n\r\n\t\tif (!theme) return this;\r\n\r\n\t\tthis.write(state);\r\n\t\tthis.syncSystemListener();\r\n\t\tthis.apply(theme);\r\n\t\tthis.syncAdapters();\r\n\r\n\t\tthis.emit(\"change\", {\r\n\t\t\tfrom,\r\n\t\t\tto: state,\r\n\t\t\ttheme\r\n\t\t});\r\n\r\n\t\treturn this;\r\n\t}\r\n\r\n\t/**\r\n\t * Read the stored theme state from local storage.\r\n\t */\r\n\tget state(): State | null {\r\n\t\treturn this.config.storage.get();\r\n\t}\r\n\r\n\t/**\r\n\t * Destroy the controller.\r\n\t * \r\n\t * @description\r\n\t * This method disconnects the observer, stops the system listener.\r\n\t */\r\n\tdestroy(): void {\r\n\t\tthis._observer?.disconnect();\r\n\t\tthis.system.stop();\r\n\t}\r\n\r\n\t/**\r\n\t * Sync all adapters.\r\n\t */\r\n\tprivate syncAdapters(): void {\r\n\t\tfor (const { def, options, selector } of this._adapters) {\r\n\t\t\tfor (const el of this.root.querySelectorAll(selector)) {\r\n\t\t\t\tdef.sync?.(this, el, options);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Sync the system listener.\r\n\t */\r\n\tprivate syncSystemListener() {\r\n\t\tconst state = this.active();\r\n\r\n\t\tif (state === this.config.system) {\r\n\t\t\tthis.system.start(() => {\r\n\t\t\t\tconst theme = resolveStateOf(\r\n\t\t\t\t\t\"system\",\r\n\t\t\t\t\tthis.system,\r\n\t\t\t\t\tthis.config\r\n\t\t\t\t);\r\n\t\t\t\tif (!theme) return;\r\n\r\n\t\t\t\tthis.apply(theme);\r\n\r\n\t\t\t\tthis.syncAdapters();\r\n\t\t\t});\r\n\t\t} else {\r\n\t\t\tthis.system.stop();\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Applies the effective theme name to the root.\r\n\t * \r\n\t * @param theme Effective theme name (e.g. `<dark>`, `<light>`, `mint`)\r\n\t */\r\n\tprivate apply(theme: Name) {\r\n\t\tthis.root.setAttribute(this.config.attribute, theme);\r\n\t\tthis.emit(\"apply\", { theme });\r\n\t}\r\n\r\n\t/**\r\n\t * Write theme state to local storage.\r\n\t * \r\n\t * @param theme Theme state\r\n\t */\r\n\tprivate write(theme: State) {\r\n\t\tthis.config.storage.set(theme);\r\n\t}\r\n\r\n\t/**\r\n\t * Start the DOM elements observer.\r\n\t * \r\n\t * @description\r\n\t * This method starts a MutationObserver that watches for changes to\r\n\t * the DOM elements that are registered as adapters. When a change\r\n\t * is detected, the controller will bind the element to the adapter.\r\n\t */\r\n\tprivate startObserver(): void {\r\n\t\tthis._observer = new MutationObserver((mutations) => {\r\n\t\t\tfor (const mu of mutations) {\r\n\t\t\t\tfor (const node of mu.addedNodes) {\r\n\t\t\t\t\tif (node instanceof Element) {\r\n\t\t\t\t\t\tthis.bindNode(node);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\tthis._observer.observe(this.root, { childList: true, subtree: true });\r\n\t}\r\n\r\n\t/**\r\n\t * Bind a single DOM element to an adapter.\r\n\t * \r\n\t * @description\r\n\t * This method checks if the element matches the adapter's selector.\r\n\t * If it does, the adapter's `bind` method is called with the element\r\n\t * and the adapter's options. If the adapter has a `sync` method, it\r\n\t * is also called with the element and the adapter's options.\r\n\t */\r\n\tprivate bindNode(node: Element): void {\r\n\t\tfor (const { def, options, selector } of this._adapters) {\r\n\t\t\tif (node.matches(selector)) {\r\n\t\t\t\tdef.bind?.(this, node, options);\r\n\t\t\t\tdef.sync?.(this, node, options);\r\n\t\t\t}\r\n\r\n\t\t\tfor (const el of node.querySelectorAll(selector)) {\r\n\t\t\t\tdef.bind?.(this, el, options);\r\n\t\t\t\tdef.sync?.(this, el, options);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}"]}
|
|
1
|
+
{"version":3,"sources":["../../src/storage/local.ts","../../src/core/event.ts","../../src/core/system.ts","../../src/core/utils.ts","../../src/core/controller.ts"],"names":["LocalStorage","key","options","get","localStorage","getItem","set","state","setItem","console","warn","remove","removeItem","EventEmitter","_map","Map","on","event","listener","Set","add","off","delete","emit","payload","forEach","System","mql","handler","ctl","prefers","matchMedia","matches","config","dark","light","prefersDark","prefersLight","start","onChange","e","addEventListener","stop","removeEventListener","undefined","resolveStateOf","system","resolveRoot","root","HTMLElement","Document","documentElement","ShadowRoot","host","document","Controller","_adapters","_observer","_root","cfg","attribute","storage","initial","observe","syncSystemListener","startObserver","active","resolved","theme","use","def","defaults","selector","el","querySelectorAll","bind","sync","push","from","write","apply","syncAdapters","to","destroy","disconnect","setAttribute","MutationObserver","mutations","mu","node","addedNodes","Element","bindNode","childList","subtree"],"mappings":";;;;AAoBO,IAAMA,eAAN,MAAMA;EApBb;;;AAqBUC,EAAAA,GAAAA;EAET,WAAA,CAAYC,OAAAA,GAA+B,EAAC,EAAG;AAC9C,IAAA,IAAA,CAAKD,GAAAA,GAAMC,QAAQD,GAAAA,IAAO,OAAA;AAC3B,EAAA;EAEAE,GAAAA,GAAoB;AACnB,IAAA,IAAI;AACH,MAAA,OAAOC,YAAAA,CAAaC,OAAAA,CAAQ,IAAA,CAAKJ,GAAG,CAAA;IACrC,CAAA,CAAA,MAAQ;AACP,MAAA,OAAO,IAAA;AACR,IAAA;AACD,EAAA;AAEAK,EAAAA,GAAAA,CAAIC,KAAAA,EAAoB;AACvB,IAAA,IAAI;AACHH,MAAAA,YAAAA,CAAaI,OAAAA,CAAQ,IAAA,CAAKP,GAAAA,EAAKM,KAAAA,CAAAA;IAChC,CAAA,CAAA,MAAQ;AACPE,MAAAA,OAAAA,CAAQC,KAAK,sCAAA,CAAA;AACd,IAAA;AACD,EAAA;EAEAC,MAAAA,GAAe;AACd,IAAA,IAAI;AACHP,MAAAA,YAAAA,CAAaQ,UAAAA,CAAW,KAAKX,GAAG,CAAA;IACjC,CAAA,CAAA,MAAQ;AAAC,IAAA;AACV,EAAA;AACD,CAAA;;;AClCO,IAAMY,eAAN,MAAMA;EAdb;;;AAeSC,EAAAA,IAAAA,uBAAWC,GAAAA,EAAAA;AAEnBC,EAAAA,EAAAA,CAAsBC,OAAUC,QAAAA,EAAwC;AACvE,IAAA,IAAIZ,GAAAA,GAAM,IAAA,CAAKQ,IAAAA,CAAKX,GAAAA,CAAIc,KAAAA,CAAAA;AACxB,IAAA,IAAI,CAACX,KAAK,IAAA,CAAKQ,IAAAA,CAAKR,IAAIW,KAAAA,EAAOX,GAAAA,mBAAM,IAAIa,GAAAA,EAAAA,CAAAA;AAEzCb,IAAAA,GAAAA,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A,EAAY;AACjC,UAAA,IAAID,gBAAgBE,OAAAA,EAAS;AAC5B,YAAA,IAAA,CAAKC,SAASH,IAAAA,CAAAA;AACf,UAAA;AACD,QAAA;AACD,MAAA;IACD,CAAA,CAAA;AAEA,IAAA,IAAA,CAAKhC,SAAAA,CAAUM,OAAAA,CAAQ,IAAA,CAAKf,IAAAA,EAAM;MAAE6C,SAAAA,EAAW,IAAA;MAAMC,OAAAA,EAAS;KAAK,CAAA;AACpE,EAAA;;;;;;;;;;AAWQF,EAAAA,QAAAA,CAASH,IAAAA,EAAqB;AACrC,IAAA,KAAA,MAAW,EAAEnB,GAAAA,EAAKpE,OAAAA,EAASsE,QAAAA,EAAQ,IAAM,KAAKhB,SAAAA,EAAW;AACxD,MAAA,IAAIiC,IAAAA,CAAKzD,OAAAA,CAAQwC,QAAAA,CAAAA,EAAW;AAC3BF,QAAAA,GAAAA,CAAIK,IAAAA,GAAO,IAAA,EAAMc,IAAAA,EAAMvF,OAAAA,CAAAA;AACvBoE,QAAAA,GAAAA,CAAIM,IAAAA,GAAO,IAAA,EAAMa,IAAAA,EAAMvF,OAAAA,CAAAA;AACxB,MAAA;AAEA,MAAA,KAAA,MAAWuE,EAAAA,IAAMgB,IAAAA,CAAKf,gBAAAA,CAAiBF,QAAAA,CAAAA,EAAW;AACjDF,QAAAA,GAAAA,CAAIK,IAAAA,GAAO,IAAA,EAAMF,EAAAA,EAAIvE,OAAAA,CAAAA;AACrBoE,QAAAA,GAAAA,CAAIM,IAAAA,GAAO,IAAA,EAAMH,EAAAA,EAAIvE,OAAAA,CAAAA;AACtB,MAAA;AACD,IAAA;AACD,EAAA;AACD","file":"index.js","sourcesContent":["/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { StateStorage } from \"../core/storage.js\";\r\nimport { State } from \"../core/types.js\";\r\n\r\nexport interface LocalStorageOptions {\r\n\t/**\r\n\t * Local storage key.\r\n\t * \r\n\t * @default \"theme\"\r\n\t */\r\n\tkey?: string;\r\n}\r\n\r\n/**\r\n * Simple local storage implementation that uses the browser's native `localStorage`.\r\n */\r\nexport class LocalStorage implements StateStorage {\r\n\treadonly key: string;\r\n\t\r\n\tconstructor(options: LocalStorageOptions = {}) {\r\n\t\tthis.key = options.key ?? \"theme\";\r\n\t}\r\n\t\r\n\tget(): State | null {\r\n\t\ttry {\r\n\t\t\treturn localStorage.getItem(this.key) as State | null;\r\n\t\t} catch {\r\n\t\t\treturn null;\r\n\t\t}\r\n\t}\r\n\r\n\tset(state: State): void {\r\n\t\ttry {\r\n\t\t\tlocalStorage.setItem(this.key, state);\r\n\t\t} catch {\r\n\t\t\tconsole.warn(\"[LOCAL] Failed to write theme state.\");\r\n\t\t}\r\n\t}\r\n\r\n\tremove(): void {\r\n\t\ttry {\r\n\t\t\tlocalStorage.removeItem(this.key);\r\n\t\t} catch {}\r\n\t}\r\n}","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nexport type Listener<T> = (event: T) => void;\r\n\r\n/**\r\n * A simple event emitter implementation.\r\n * \r\n * @description\r\n * This class is suitable for use in a web application that wants to\r\n * listen for changes in the theme state.\r\n */\r\nexport class EventEmitter<M extends Record<string, unknown>> {\r\n\tprivate _map = new Map<keyof M, Set<Listener<any>>>();\r\n\r\n\ton<K extends keyof M>(event: K, listener: Listener<M[ K ]>): () => void {\r\n\t\tlet set = this._map.get(event);\r\n\t\tif (!set) this._map.set(event, set = new Set());\r\n\r\n\t\tset.add(listener);\r\n\r\n\t\treturn () => this.off(event, listener);\r\n\t}\r\n\r\n\toff<K extends keyof M>(event: K, listener: Listener<M[ K ]>): void {\r\n\t\tthis._map.get(event)?.delete(listener);\r\n\t}\r\n\r\n\temit<K extends keyof M>(event: K, payload: M[ K ]): void {\r\n\t\tthis._map.get(event)?.forEach(listener => listener(payload));\r\n\t}\r\n}","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { Controller } from \"./controller.js\";\r\nimport { Name, State } from \"./types.js\";\r\n\r\n/**\r\n * System theme proxy.\r\n * \r\n * Used to detect system theme preference and listen for changes.\r\n */\r\nexport class System {\r\n\t/**\r\n\t * Media query list instance.\r\n\t */\r\n\tprivate mql?: MediaQueryList;\r\n\t/**\r\n\t * Handles the media query list listener.\r\n\t */\r\n\tprivate handler?: (e: MediaQueryListEvent) => void;\r\n\t/**\r\n\t * Controller instance.\r\n\t */\r\n\r\n\tconstructor(private ctl: Controller) { }\r\n\r\n\t/**\r\n\t * Get the effective theme by system preference.\r\n\t */\r\n\tget prefers(): Name {\r\n\t\treturn matchMedia?.(\"(prefers-color-scheme: dark)\").matches\r\n\t\t\t? this.ctl.config.dark\r\n\t\t\t: this.ctl.config.light;\r\n\t}\r\n\r\n\t/**\r\n\t * Check if the effective theme is dark.\r\n\t */\r\n\tget prefersDark(): boolean {\r\n\t\treturn this.prefers === this.ctl.config.dark;\r\n\t}\r\n\r\n\t/**\r\n\t * Check if the effective theme is light.\r\n\t */\r\n\tget prefersLight(): boolean {\r\n\t\treturn this.prefers === this.ctl.config.light;\r\n\t}\r\n\r\n\t/**\r\n\t * Start listening for system theme changes.\r\n\t * \r\n\t * @param onChange Callback to be called when the system theme changes.\r\n\t */\r\n\tstart(onChange: (state: Exclude<State, \"system\">) => void) {\r\n\t\tif (this.handler) return;\r\n\r\n\t\tthis.mql = matchMedia(\"(prefers-color-scheme: dark)\");\r\n\t\tthis.handler = e => onChange(e.matches ? \"dark\" : \"light\");\r\n\r\n\t\tthis.mql.addEventListener?.(\"change\", this.handler);\r\n\t}\r\n\r\n\t/**\r\n\t * Stop listening for system theme changes.\r\n\t */\r\n\tstop() {\r\n\t\tif (!this.mql || !this.handler) return;\r\n\r\n\t\tthis.mql.removeEventListener?.(\"change\", this.handler);\r\n\t\t\r\n\t\tthis.mql = this.handler = undefined;\r\n\t}\r\n}","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { ResolvedConfig } from \"./config.js\";\r\nimport { System } from \"./system.js\";\r\nimport { State, type Name } from \"./types.js\";\r\n\r\nexport const DEFAULT_PREFIX = \"theme:\";\r\n\r\n/**\r\n * Resolve a theme state to an effective theme name.\r\n * \r\n * @see {@link State}\r\n * \r\n * @param theme Theme state\r\n * @param system System instance.\r\n * @param config Resolved set of configuration.\r\n * @returns \r\n */\r\nexport function resolveStateOf(state: State, system: System, config: ResolvedConfig): Name | null {\r\n\tif (state === config.system) {\r\n\t\treturn system.prefers;\r\n\t}\r\n\r\n\tif (state === config.light || state === config.dark) {\r\n\t\treturn state;\r\n\t}\r\n\r\n\treturn typeof state === \"string\" ? state : null;\r\n}\r\n\r\n/**\r\n * Resolve a root element from a given value.\r\n * \r\n * @description\r\n * This function takes a root value and returns the corresponding\r\n * HTMLElement. If the value is already an HTMLElement, it is returned\r\n * directly. If the value is a Document, it returns the documentElement.\r\n * If the value is a ShadowRoot, it returns the host element.\r\n * \r\n * @param root Root value\r\n */\r\nexport function resolveRoot(root: unknown): HTMLElement {\r\n\tif (root instanceof HTMLElement) {\r\n\t\treturn root\r\n\t} else if (root instanceof Document) {\r\n\t\treturn root.documentElement\r\n\t} else if (root instanceof ShadowRoot && root.host instanceof HTMLElement) {\r\n\t\treturn root.host\r\n\t};\r\n\r\n\treturn document.documentElement;\r\n}\r\n\r\n/**\r\n * Parse a value with a prefix.\r\n * \r\n * @description\r\n * This function takes a value and a prefix. If the value starts with\r\n * the prefix, it is sliced from the beginning. Otherwise, the value is\r\n * returned as-is.\r\n * \r\n * @param value Value to parse\r\n * @param prefix Prefix to check\r\n */\r\nexport function parseValue(value: string, prefix: string): string {\r\n\treturn value.startsWith(prefix)\r\n\t\t? value.slice(prefix.length)\r\n\t\t: value;\r\n}","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { LocalStorage } from \"../storage/local.js\";\r\nimport { AdapterDefinition } from \"./adapter.js\";\r\nimport { RawConfig, ResolvedConfig } from \"./config.js\";\r\nimport { EventEmitter } from \"./event.js\";\r\nimport { System } from \"./system.js\";\r\nimport { Name, NEvents, State } from \"./types.js\";\r\nimport { resolveRoot, resolveStateOf } from \"./utils.js\";\r\n\r\ninterface RegisteredAdapter {\r\n\tdef: AdapterDefinition<any, any>;\r\n\toptions: any;\r\n\tselector: string;\r\n}\r\n\r\n/**\r\n * Theme controller logic.\r\n */\r\nexport class Controller extends EventEmitter<NEvents> {\r\n\t/**\r\n\t * Resolved configuration.\r\n\t */\r\n\tconfig: ResolvedConfig;\r\n\t/**\r\n\t * System theme proxy.\r\n\t */\r\n\tprivate system: System;\r\n\r\n\tprivate _adapters: RegisteredAdapter[] = [];\r\n\tprivate _observer?: MutationObserver;\r\n\r\n\tconstructor(\r\n\t\tprivate _root: Document | ParentNode = document,\r\n\t\tcfg: RawConfig = {}\r\n\t) {\r\n\t\tsuper();\r\n\t\t\r\n\t\tthis.config = {\r\n\t\t\tattribute: cfg.attribute ?? \"data-theme\",\r\n\t\t\tstorage: cfg.storage ?? new LocalStorage(),\r\n\t\t\tsystem: cfg.system ?? \"system\",\r\n\t\t\tlight: cfg.light ?? \"light\",\r\n\t\t\tdark: cfg.dark ?? \"dark\",\r\n\t\t\tinitial: cfg.initial,\r\n\t\t\tobserve: cfg.observe ?? false\r\n\t\t};\r\n\r\n\t\tthis.system = new System(this);\r\n\r\n\t\tthis.syncSystemListener();\r\n\r\n\t\tif (this.config.observe) {\r\n\t\t\tthis.startObserver();\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * The root element to apply theme to.\r\n\t */\r\n\tget root(): HTMLElement {\r\n\t\treturn resolveRoot(this._root);\r\n\t}\r\n\r\n\t/**\r\n\t * @param resolved resolve state and effective themes to their values.\r\n\t * \r\n\t * @example\r\n\t * \r\n\t * system = config.system;\r\n\t * light = config.light;\r\n\t * dark = config.dark;\r\n\t * \r\n\t * resolved = true;\r\n\t * [\r\n\t * \t\t\"system\": light | dark,\r\n\t * \t\t\"light\": light,\r\n\t * \t\t\"dark\": dark\r\n\t * ]\r\n\t * resolved = false;\r\n\t * [\r\n\t * \t\t\"system\": system,\r\n\t * \t\t\"light\": light,\r\n\t * \t\t\"dark\": dark\r\n\t * ]\r\n\t */\r\n\tactive(resolved: boolean = false): State | Name | null {\r\n\t\tconst state = this.state;\r\n\r\n\t\tif (!state) {\r\n\t\t\treturn this.config.initial || null;\r\n\t\t}\r\n\r\n\t\tconst theme = resolveStateOf(\r\n\t\t\tstate,\r\n\t\t\tthis.system,\r\n\t\t\tthis.config\r\n\t\t);\r\n\r\n\t\tif (!theme) return null;\r\n\r\n\t\treturn resolved\r\n\t\t\t? theme\r\n\t\t\t: state;\r\n\t}\r\n\r\n\t/**\r\n\t * Register an adapter.\r\n\t * \r\n\t * @description\r\n\t * Adapters are toys used to change themes.\r\n\t * They define where themes lives and how they work with the controller.\r\n\t * \r\n\t * @param adapter Adapter instance\r\n\t * @param options Adapter options\r\n\t */\r\n\tuse<O>(\r\n\t\tdef: AdapterDefinition<any, O>,\r\n\t\toptions?: Partial<O>\r\n\t): this {\r\n\t\tconst resolved = { ...def.defaults, ...options } as O;\r\n\r\n\t\tconst selector = typeof def.selector === \"function\"\r\n\t\t\t? def.selector(resolved)\r\n\t\t\t: def.selector;\r\n\t\t\r\n\t\tfor (const el of this.root.querySelectorAll(selector)) {\r\n\t\t\tdef.bind?.(this, el, resolved);\r\n\t\t\tdef.sync?.(this, el, resolved);\r\n\t\t}\r\n\r\n\t\tthis._adapters.push({\r\n\t\t\tdef,\r\n\t\t\toptions: resolved,\r\n\t\t\tselector\r\n\t\t});\r\n\t\t\r\n\t\treturn this;\r\n\t}\r\n\t\r\n\t/**\r\n\t * Set the effective theme from a theme state.\r\n\t * \r\n\t * @see {@link State}\r\n\t * @param state Theme State\r\n\t */\r\n\tset(state: State) {\r\n\t\tconst from = this.state;\r\n\t\tconst theme = resolveStateOf(state, this.system, this.config);\r\n\r\n\t\tif (!theme) return this;\r\n\r\n\t\tthis.write(state);\r\n\t\tthis.syncSystemListener();\r\n\t\tthis.apply(theme);\r\n\t\tthis.syncAdapters();\r\n\r\n\t\tthis.emit(\"change\", {\r\n\t\t\tfrom,\r\n\t\t\tto: state,\r\n\t\t\ttheme\r\n\t\t});\r\n\r\n\t\treturn this;\r\n\t}\r\n\r\n\t/**\r\n\t * Read the stored theme state from local storage.\r\n\t */\r\n\tget state(): State | null {\r\n\t\treturn this.config.storage.get();\r\n\t}\r\n\r\n\t/**\r\n\t * Destroy the controller.\r\n\t * \r\n\t * @description\r\n\t * This method disconnects the observer, stops the system listener.\r\n\t */\r\n\tdestroy(): void {\r\n\t\tthis._observer?.disconnect();\r\n\t\tthis.system.stop();\r\n\t}\r\n\r\n\t/**\r\n\t * Sync all adapters.\r\n\t */\r\n\tprivate syncAdapters(): void {\r\n\t\tfor (const { def, options, selector } of this._adapters) {\r\n\t\t\tfor (const el of this.root.querySelectorAll(selector)) {\r\n\t\t\t\tdef.sync?.(this, el, options);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Sync the system listener.\r\n\t */\r\n\tprivate syncSystemListener() {\r\n\t\tconst state = this.active();\r\n\r\n\t\tif (state === this.config.system) {\r\n\t\t\tthis.system.start(() => {\r\n\t\t\t\tconst theme = resolveStateOf(\r\n\t\t\t\t\t\"system\",\r\n\t\t\t\t\tthis.system,\r\n\t\t\t\t\tthis.config\r\n\t\t\t\t);\r\n\t\t\t\tif (!theme) return;\r\n\r\n\t\t\t\tthis.apply(theme);\r\n\r\n\t\t\t\tthis.syncAdapters();\r\n\t\t\t});\r\n\t\t} else {\r\n\t\t\tthis.system.stop();\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Applies the effective theme name to the root.\r\n\t * \r\n\t * @param theme Effective theme name (e.g. `<dark>`, `<light>`, `mint`)\r\n\t */\r\n\tprivate apply(theme: Name) {\r\n\t\tthis.root.setAttribute(this.config.attribute, theme);\r\n\t\tthis.emit(\"apply\", { theme });\r\n\t}\r\n\r\n\t/**\r\n\t * Write theme state to local storage.\r\n\t * \r\n\t * @param theme Theme state\r\n\t */\r\n\tprivate write(theme: State) {\r\n\t\tthis.config.storage.set(theme);\r\n\t}\r\n\r\n\t/**\r\n\t * Start the DOM elements observer.\r\n\t * \r\n\t * @description\r\n\t * This method starts a MutationObserver that watches for changes to\r\n\t * the DOM elements that are registered as adapters. When a change\r\n\t * is detected, the controller will bind the element to the adapter.\r\n\t */\r\n\tprivate startObserver(): void {\r\n\t\tthis._observer = new MutationObserver((mutations) => {\r\n\t\t\tfor (const mu of mutations) {\r\n\t\t\t\tfor (const node of mu.addedNodes) {\r\n\t\t\t\t\tif (node instanceof Element) {\r\n\t\t\t\t\t\tthis.bindNode(node);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\tthis._observer.observe(this.root, { childList: true, subtree: true });\r\n\t}\r\n\r\n\t/**\r\n\t * Bind a single DOM element to an adapter.\r\n\t * \r\n\t * @description\r\n\t * This method checks if the element matches the adapter's selector.\r\n\t * If it does, the adapter's `bind` method is called with the element\r\n\t * and the adapter's options. If the adapter has a `sync` method, it\r\n\t * is also called with the element and the adapter's options.\r\n\t */\r\n\tprivate bindNode(node: Element): void {\r\n\t\tfor (const { def, options, selector } of this._adapters) {\r\n\t\t\tif (node.matches(selector)) {\r\n\t\t\t\tdef.bind?.(this, node, options);\r\n\t\t\t\tdef.sync?.(this, node, options);\r\n\t\t\t}\r\n\r\n\t\t\tfor (const el of node.querySelectorAll(selector)) {\r\n\t\t\t\tdef.bind?.(this, el, options);\r\n\t\t\t\tdef.sync?.(this, el, options);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}"]}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { S as StateStorage, a as State } from '../storage-n1c3g6vy.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Copyright (c) 2026 GTF
|
|
5
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
interface LocalStorageOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Local storage key.
|
|
11
|
+
*
|
|
12
|
+
* @default "theme"
|
|
13
|
+
*/
|
|
14
|
+
key?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Simple local storage implementation that uses the browser's native `localStorage`.
|
|
18
|
+
*/
|
|
19
|
+
declare class LocalStorage implements StateStorage {
|
|
20
|
+
readonly key: string;
|
|
21
|
+
constructor(options?: LocalStorageOptions);
|
|
22
|
+
get(): State | null;
|
|
23
|
+
set(state: State): void;
|
|
24
|
+
remove(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Copyright (c) 2026 GTF
|
|
29
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
interface CookieStorageOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Cookie name.
|
|
35
|
+
*
|
|
36
|
+
* @default "theme"
|
|
37
|
+
*/
|
|
38
|
+
key?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Cookie path.
|
|
41
|
+
*
|
|
42
|
+
* @default "/"
|
|
43
|
+
*/
|
|
44
|
+
path?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Cookie domain.
|
|
47
|
+
*
|
|
48
|
+
* Example: ".nova.sa"
|
|
49
|
+
*/
|
|
50
|
+
domain?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Cookie lifetime in seconds.
|
|
53
|
+
*
|
|
54
|
+
* @default 31536000 (1 year)
|
|
55
|
+
*/
|
|
56
|
+
maxAge?: number;
|
|
57
|
+
/**
|
|
58
|
+
* SameSite policy.
|
|
59
|
+
*
|
|
60
|
+
* @default "Lax"
|
|
61
|
+
*/
|
|
62
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
63
|
+
/**
|
|
64
|
+
* Marks the cookie as secure.
|
|
65
|
+
*
|
|
66
|
+
* @default true if the page is served over HTTPS.
|
|
67
|
+
*/
|
|
68
|
+
secure?: boolean;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A storage implementation that uses cookies to store the theme state.
|
|
72
|
+
*
|
|
73
|
+
* @description
|
|
74
|
+
* This storage implementation is suitable for use in a web application that
|
|
75
|
+
* wants to persist the theme state across sub-domains or across multiple
|
|
76
|
+
* applications on the same domain.
|
|
77
|
+
*/
|
|
78
|
+
declare class CookieStorage implements StateStorage {
|
|
79
|
+
private readonly options;
|
|
80
|
+
readonly key: string;
|
|
81
|
+
constructor(options?: CookieStorageOptions);
|
|
82
|
+
get(): State | null;
|
|
83
|
+
set(state: State): void;
|
|
84
|
+
remove(): void;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { CookieStorage, LocalStorage };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/storage/local.ts
|
|
5
|
+
var LocalStorage = class {
|
|
6
|
+
static {
|
|
7
|
+
__name(this, "LocalStorage");
|
|
8
|
+
}
|
|
9
|
+
key;
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.key = options.key ?? "theme";
|
|
12
|
+
}
|
|
13
|
+
get() {
|
|
14
|
+
try {
|
|
15
|
+
return localStorage.getItem(this.key);
|
|
16
|
+
} catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
set(state) {
|
|
21
|
+
try {
|
|
22
|
+
localStorage.setItem(this.key, state);
|
|
23
|
+
} catch {
|
|
24
|
+
console.warn("[LOCAL] Failed to write theme state.");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
remove() {
|
|
28
|
+
try {
|
|
29
|
+
localStorage.removeItem(this.key);
|
|
30
|
+
} catch {
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// src/storage/cookie.ts
|
|
36
|
+
var CookieStorage = class {
|
|
37
|
+
static {
|
|
38
|
+
__name(this, "CookieStorage");
|
|
39
|
+
}
|
|
40
|
+
options;
|
|
41
|
+
key;
|
|
42
|
+
constructor(options = {}) {
|
|
43
|
+
this.options = options;
|
|
44
|
+
this.key = options.key ?? "theme";
|
|
45
|
+
}
|
|
46
|
+
get() {
|
|
47
|
+
const prefix = `${encodeURIComponent(this.key)}=`;
|
|
48
|
+
for (const cookie of document.cookie.split(";")) {
|
|
49
|
+
const c = cookie.trim();
|
|
50
|
+
if (c.startsWith(prefix)) {
|
|
51
|
+
return decodeURIComponent(c.slice(prefix.length));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
set(state) {
|
|
57
|
+
const { path = "/", domain, maxAge = 60 * 60 * 24 * 365, sameSite = "Lax", secure = location.protocol === "https:" } = this.options;
|
|
58
|
+
const parts = [
|
|
59
|
+
`${encodeURIComponent(this.key)}=${encodeURIComponent(state)}`,
|
|
60
|
+
`Path=${path}`,
|
|
61
|
+
`Max-Age=${maxAge}`,
|
|
62
|
+
`SameSite=${sameSite}`
|
|
63
|
+
];
|
|
64
|
+
if (domain) {
|
|
65
|
+
parts.push(`Domain=${domain}`);
|
|
66
|
+
}
|
|
67
|
+
if (secure) {
|
|
68
|
+
parts.push(`Secure`);
|
|
69
|
+
}
|
|
70
|
+
document.cookie = parts.join("; ");
|
|
71
|
+
}
|
|
72
|
+
remove() {
|
|
73
|
+
const { path = "/", domain } = this.options;
|
|
74
|
+
const parts = [
|
|
75
|
+
`${encodeURIComponent(this.key)}=`,
|
|
76
|
+
`Max-Age=0`,
|
|
77
|
+
`Path=${path}`
|
|
78
|
+
];
|
|
79
|
+
if (domain) {
|
|
80
|
+
parts.push(`Domain=${domain}`);
|
|
81
|
+
}
|
|
82
|
+
document.cookie = parts.join("; ");
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export { CookieStorage, LocalStorage };
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
88
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/storage/local.ts","../../src/storage/cookie.ts"],"names":["LocalStorage","key","options","get","localStorage","getItem","set","state","setItem","console","warn","remove","removeItem","CookieStorage","prefix","encodeURIComponent","cookie","document","split","c","trim","startsWith","decodeURIComponent","slice","length","path","domain","maxAge","sameSite","secure","location","protocol","parts","push","join"],"mappings":";;;;AAoBO,IAAMA,eAAN,MAAMA;EApBb;;;AAqBUC,EAAAA,GAAAA;EAET,WAAA,CAAYC,OAAAA,GAA+B,EAAC,EAAG;AAC9C,IAAA,IAAA,CAAKD,GAAAA,GAAMC,QAAQD,GAAAA,IAAO,OAAA;AAC3B,EAAA;EAEAE,GAAAA,GAAoB;AACnB,IAAA,IAAI;AACH,MAAA,OAAOC,YAAAA,CAAaC,OAAAA,CAAQ,IAAA,CAAKJ,GAAG,CAAA;IACrC,CAAA,CAAA,MAAQ;AACP,MAAA,OAAO,IAAA;AACR,IAAA;AACD,EAAA;AAEAK,EAAAA,GAAAA,CAAIC,KAAAA,EAAoB;AACvB,IAAA,IAAI;AACHH,MAAAA,YAAAA,CAAaI,OAAAA,CAAQ,IAAA,CAAKP,GAAAA,EAAKM,KAAAA,CAAAA;IAChC,CAAA,CAAA,MAAQ;AACPE,MAAAA,OAAAA,CAAQC,KAAK,sCAAA,CAAA;AACd,IAAA;AACD,EAAA;EAEAC,MAAAA,GAAe;AACd,IAAA,IAAI;AACHP,MAAAA,YAAAA,CAAaQ,UAAAA,CAAW,KAAKX,GAAG,CAAA;IACjC,CAAA,CAAA,MAAQ;AAAC,IAAA;AACV,EAAA;AACD;;;ACYO,IAAMY,gBAAN,MAAMA;EA5Db;;;;AA6DUZ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,CAAAA;;AAGb,IAAA,IAAIF,MAAAA,EAAQ;AACXM,MAAAA,KAAAA,CAAMC,IAAAA,CAAK,CAAA,OAAA,EAAUP,MAAAA,CAAAA,CAAQ,CAAA;AAC9B,IAAA;AAEA,IAAA,IAAIG,MAAAA,EAAQ;AACXG,MAAAA,KAAAA,CAAMC,KAAK,CAAA,MAAA,CAAQ,CAAA;AACpB,IAAA;AAEAhB,IAAAA,QAAAA,CAASD,MAAAA,GAASgB,KAAAA,CAAME,IAAAA,CAAK,IAAA,CAAA;AAC9B,EAAA;EAEAvB,MAAAA,GAAe;AACd,IAAA,MAAM,EAAEc,IAAAA,GAAO,GAAA,EAAKC,MAAAA,KAAW,IAAA,CAAKxB,OAAAA;AAEpC,IAAA,MAAM8B,KAAAA,GAAQ;MACb,CAAA,EAAGjB,kBAAAA,CAAmB,IAAA,CAAKd,GAAG,CAAA,CAAA,CAAA,CAAA;AAC9B,MAAA,CAAA,SAAA,CAAA;AACA,MAAA,CAAA,KAAA,EAAQwB,IAAAA,CAAAA;;AAGT,IAAA,IAAIC,MAAAA,EAAQ;AACXM,MAAAA,KAAAA,CAAMC,IAAAA,CAAK,CAAA,OAAA,EAAUP,MAAAA,CAAAA,CAAQ,CAAA;AAC9B,IAAA;AAEAT,IAAAA,QAAAA,CAASD,MAAAA,GAASgB,KAAAA,CAAME,IAAAA,CAAK,IAAA,CAAA;AAC9B,EAAA;AACD","file":"index.js","sourcesContent":["/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { StateStorage } from \"../core/storage.js\";\r\nimport { State } from \"../core/types.js\";\r\n\r\nexport interface LocalStorageOptions {\r\n\t/**\r\n\t * Local storage key.\r\n\t * \r\n\t * @default \"theme\"\r\n\t */\r\n\tkey?: string;\r\n}\r\n\r\n/**\r\n * Simple local storage implementation that uses the browser's native `localStorage`.\r\n */\r\nexport class LocalStorage implements StateStorage {\r\n\treadonly key: string;\r\n\t\r\n\tconstructor(options: LocalStorageOptions = {}) {\r\n\t\tthis.key = options.key ?? \"theme\";\r\n\t}\r\n\t\r\n\tget(): State | null {\r\n\t\ttry {\r\n\t\t\treturn localStorage.getItem(this.key) as State | null;\r\n\t\t} catch {\r\n\t\t\treturn null;\r\n\t\t}\r\n\t}\r\n\r\n\tset(state: State): void {\r\n\t\ttry {\r\n\t\t\tlocalStorage.setItem(this.key, state);\r\n\t\t} catch {\r\n\t\t\tconsole.warn(\"[LOCAL] Failed to write theme state.\");\r\n\t\t}\r\n\t}\r\n\r\n\tremove(): void {\r\n\t\ttry {\r\n\t\t\tlocalStorage.removeItem(this.key);\r\n\t\t} catch {}\r\n\t}\r\n}","/**\r\n * Copyright (c) 2026 GTF\r\n * SPDX-License-Identifier: Apache-2.0\r\n */\r\n\r\nimport { StateStorage } from \"../core/storage.js\";\r\nimport { State } from \"../core/types.js\";\r\n\r\nexport interface CookieStorageOptions {\r\n\t/**\r\n\t * Cookie name.\r\n\t * \r\n\t * @default \"theme\"\r\n\t */\r\n\tkey?: string;\r\n\r\n\t/**\r\n\t * Cookie path.\r\n\t * \r\n\t * @default \"/\"\r\n\t */\r\n\tpath?: string;\r\n\r\n\t/**\r\n\t * Cookie domain.\r\n\t * \r\n\t * Example: \".nova.sa\"\r\n\t */\r\n\tdomain?: string;\r\n\r\n\t/**\r\n\t * Cookie lifetime in seconds.\r\n\t * \r\n\t * @default 31536000 (1 year)\r\n\t */\r\n\tmaxAge?: number;\r\n\r\n\t/**\r\n\t * SameSite policy.\r\n\t * \r\n\t * @default \"Lax\"\r\n\t */\r\n\tsameSite?: \"Strict\" | \"Lax\" | \"None\";\r\n\r\n\t/**\r\n\t * Marks the cookie as secure.\r\n\t *\r\n\t * @default true if the page is served over HTTPS.\r\n\t */\r\n\tsecure?: boolean;\r\n}\r\n\r\n/**\r\n * A storage implementation that uses cookies to store the theme state.\r\n * \r\n * @description\r\n * This storage implementation is suitable for use in a web application that\r\n * wants to persist the theme state across sub-domains or across multiple\r\n * applications on the same domain.\r\n */\r\nexport class CookieStorage implements StateStorage {\r\n\treadonly key: string;\r\n\t\r\n\tconstructor(\r\n\t\tprivate readonly options: CookieStorageOptions = {}\r\n\t) {\r\n\t\tthis.key = options.key ?? \"theme\";\r\n\t}\r\n\r\n\tget(): State | null {\r\n\t\tconst prefix = `${encodeURIComponent(this.key)}=`;\r\n\r\n\t\tfor (const cookie of document.cookie.split(\";\")) {\r\n\t\t\tconst c = cookie.trim();\r\n\t\t\t\r\n\t\t\tif (c.startsWith(prefix)) {\r\n\t\t\t\treturn decodeURIComponent(c.slice(prefix.length)) as State;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn null;\r\n\t}\r\n\r\n\tset(state: State): void {\r\n\t\tconst {\r\n\t\t\tpath = '/',\r\n\t\t\tdomain,\r\n\t\t\tmaxAge = 60 * 60 * 24 * 365,\r\n\t\t\tsameSite = \"Lax\",\r\n\t\t\tsecure = location.protocol === \"https:\"\r\n\t\t} = this.options;\r\n\r\n\t\tconst parts = [\r\n\t\t\t`${encodeURIComponent(this.key)}=${encodeURIComponent(state)}`,\r\n\t\t\t`Path=${path}`,\r\n\t\t\t`Max-Age=${maxAge}`,\r\n\t\t\t`SameSite=${sameSite}`,\r\n\t\t];\r\n\r\n\t\tif (domain) {\r\n\t\t\tparts.push(`Domain=${domain}`);\r\n\t\t}\r\n\r\n\t\tif (secure) {\r\n\t\t\tparts.push(`Secure`);\r\n\t\t}\r\n\r\n\t\tdocument.cookie = parts.join(\"; \");\r\n\t}\r\n\r\n\tremove(): void {\r\n\t\tconst { path = '/', domain } = this.options;\r\n\r\n\t\tconst parts = [\r\n\t\t\t`${encodeURIComponent(this.key)}=`,\r\n\t\t\t`Max-Age=0`,\r\n\t\t\t`Path=${path}`,\r\n\t\t];\r\n\r\n\t\tif (domain) {\r\n\t\t\tparts.push(`Domain=${domain}`);\r\n\t\t}\r\n\r\n\t\tdocument.cookie = parts.join(\"; \");\r\n\t}\r\n}"]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 GTF
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Effective theme name (e.g. `day`, `night`, `mint`).
|
|
7
|
+
*/
|
|
8
|
+
type Name = string;
|
|
9
|
+
/**
|
|
10
|
+
* Theme state (`<dark>`, `<light>`, `<system>`).
|
|
11
|
+
* Different between state themes and effective themes:
|
|
12
|
+
* - State themes are the raw values used as identifiers (`<dark>`, `<light>`, `<system>`).
|
|
13
|
+
* - Effective themes are the resolved values (e.g. `<dark>`, `<light>`, `mint`).
|
|
14
|
+
*/
|
|
15
|
+
type State = "system" | "light" | "dark";
|
|
16
|
+
/**
|
|
17
|
+
* Emitted when the theme state changes.
|
|
18
|
+
*/
|
|
19
|
+
interface ChangeEvent {
|
|
20
|
+
from: State | null;
|
|
21
|
+
to: State;
|
|
22
|
+
theme: Name;
|
|
23
|
+
}
|
|
24
|
+
interface ApplyEvent {
|
|
25
|
+
theme: Name;
|
|
26
|
+
}
|
|
27
|
+
type NEvents = {
|
|
28
|
+
change: ChangeEvent;
|
|
29
|
+
apply: ApplyEvent;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Copyright (c) 2026 GTF
|
|
34
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A storage interface for persisting the theme state.
|
|
39
|
+
*
|
|
40
|
+
* @description
|
|
41
|
+
* The theme state is persisted in a storage mechanism that is
|
|
42
|
+
* specific to the application. For example, a web application might
|
|
43
|
+
* use cookies, local storage, or a database.
|
|
44
|
+
*/
|
|
45
|
+
interface StateStorage {
|
|
46
|
+
get(): State | null;
|
|
47
|
+
set(state: State): void;
|
|
48
|
+
remove(): void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type { Name as N, StateStorage as S, State as a, NEvents as b };
|
package/package.json
CHANGED
package/dist/chunk-NYMA6GFH.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
|
|
4
|
-
// src/core/utils.ts
|
|
5
|
-
var DEFAULT_PREFIX = "theme:";
|
|
6
|
-
function resolveStateOf(state, system, config) {
|
|
7
|
-
if (state === config.system) {
|
|
8
|
-
return system.prefers;
|
|
9
|
-
}
|
|
10
|
-
if (state === config.light || state === config.dark) {
|
|
11
|
-
return state;
|
|
12
|
-
}
|
|
13
|
-
return typeof state === "string" ? state : null;
|
|
14
|
-
}
|
|
15
|
-
__name(resolveStateOf, "resolveStateOf");
|
|
16
|
-
function resolveRoot(root) {
|
|
17
|
-
if (root instanceof HTMLElement) {
|
|
18
|
-
return root;
|
|
19
|
-
} else if (root instanceof Document) {
|
|
20
|
-
return root.documentElement;
|
|
21
|
-
} else if (root instanceof ShadowRoot && root.host instanceof HTMLElement) {
|
|
22
|
-
return root.host;
|
|
23
|
-
}
|
|
24
|
-
return document.documentElement;
|
|
25
|
-
}
|
|
26
|
-
__name(resolveRoot, "resolveRoot");
|
|
27
|
-
function parseValue(value, prefix) {
|
|
28
|
-
return value.startsWith(prefix) ? value.slice(prefix.length) : value;
|
|
29
|
-
}
|
|
30
|
-
__name(parseValue, "parseValue");
|
|
31
|
-
|
|
32
|
-
export { DEFAULT_PREFIX, __name, parseValue, resolveRoot, resolveStateOf };
|
|
33
|
-
//# sourceMappingURL=chunk-NYMA6GFH.js.map
|
|
34
|
-
//# sourceMappingURL=chunk-NYMA6GFH.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/core/utils.ts"],"names":["DEFAULT_PREFIX","resolveStateOf","state","system","config","prefers","light","dark","resolveRoot","root","HTMLElement","Document","documentElement","ShadowRoot","host","document","parseValue","value","prefix","startsWith","slice","length"],"mappings":";;;;A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ifier: Apache-2.0\r\n */\r\n\r\nimport { ResolvedConfig } from \"./config.js\";\r\nimport { System } from \"./system.js\";\r\nimport { State, type Name } from \"./types.js\";\r\n\r\nexport const DEFAULT_PREFIX = \"theme:\";\r\n\r\n/**\r\n * Resolve a theme state to an effective theme name.\r\n * \r\n * @see {@link State}\r\n * \r\n * @param theme Theme state\r\n * @param system System instance.\r\n * @param config Resolved set of configuration.\r\n * @returns \r\n */\r\nexport function resolveStateOf(state: State, system: System, config: ResolvedConfig): Name | null {\r\n\tif (state === config.system) {\r\n\t\treturn system.prefers;\r\n\t}\r\n\r\n\tif (state === config.light || state === config.dark) {\r\n\t\treturn state;\r\n\t}\r\n\r\n\treturn typeof state === \"string\" ? state : null;\r\n}\r\n\r\n/**\r\n * Resolve a root element from a given value.\r\n * \r\n * @description\r\n * This function takes a root value and returns the corresponding\r\n * HTMLElement. If the value is already an HTMLElement, it is returned\r\n * directly. If the value is a Document, it returns the documentElement.\r\n * If the value is a ShadowRoot, it returns the host element.\r\n * \r\n * @param root Root value\r\n */\r\nexport function resolveRoot(root: unknown): HTMLElement {\r\n\tif (root instanceof HTMLElement) {\r\n\t\treturn root\r\n\t} else if (root instanceof Document) {\r\n\t\treturn root.documentElement\r\n\t} else if (root instanceof ShadowRoot && root.host instanceof HTMLElement) {\r\n\t\treturn root.host\r\n\t};\r\n\r\n\treturn document.documentElement;\r\n}\r\n\r\n/**\r\n * Parse a value with a prefix.\r\n * \r\n * @description\r\n * This function takes a value and a prefix. If the value starts with\r\n * the prefix, it is sliced from the beginning. Otherwise, the value is\r\n * returned as-is.\r\n * \r\n * @param value Value to parse\r\n * @param prefix Prefix to check\r\n */\r\nexport function parseValue(value: string, prefix: string): string {\r\n\treturn value.startsWith(prefix)\r\n\t\t? value.slice(prefix.length)\r\n\t\t: value;\r\n}"]}
|