@riebeckite/plugin-color-mode 0.0.1
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 +166 -0
- package/README_ja.md +136 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +100 -0
- package/dist/components/color-mode-script.d.ts +10 -0
- package/dist/components/color-mode-toggle.d.ts +17 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +202 -0
- package/dist/src/constants.d.ts +23 -0
- package/dist/src/init.d.ts +6 -0
- package/package.json +63 -0
- package/style.css +84 -0
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 Rerurate_514
|
|
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
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# @riebeckite/plugin-color-mode
|
|
2
|
+
|
|
3
|
+
Light / dark / system color-mode switching for Riebeckite sites. The control
|
|
4
|
+
writes to `data-theme` on `<html>` at runtime, exactly the attribute the
|
|
5
|
+
themes' CSS uses to pick a palette — so it works with every built-in theme and
|
|
6
|
+
needs no JavaScript in the theme itself.
|
|
7
|
+
|
|
8
|
+
[日本語](./README_ja.md)
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
Themes derive their palette from three CSS states (see
|
|
13
|
+
[theme-system.md](../../../docs/en/theme-system.md)):
|
|
14
|
+
|
|
15
|
+
- `:root` — light
|
|
16
|
+
- `:root[data-theme="dark"]` — dark
|
|
17
|
+
- `@media (prefers-color-scheme: dark) { :root:not([data-theme]) }` — follow the OS ("system")
|
|
18
|
+
|
|
19
|
+
The plugin is a thin runtime switch over that contract:
|
|
20
|
+
|
|
21
|
+
- `ColorModeScript` — a one-line inline script for `<head>` that applies the
|
|
22
|
+
saved mode **before first paint**, preventing a flash of the wrong theme.
|
|
23
|
+
- `ColorModeToggle` — a segmented control with light / dark / system buttons.
|
|
24
|
+
- `initColorMode` — the client entry that binds the buttons, persists the
|
|
25
|
+
choice in `localStorage` and keeps the document in sync.
|
|
26
|
+
|
|
27
|
+
`colorModePlugin()` itself only registers the stylesheet and the client entry;
|
|
28
|
+
everything else is delivered through the two components you render in your site
|
|
29
|
+
shell.
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { defineConfig } from "@riebeckite/core";
|
|
35
|
+
import { colorModePlugin } from "@riebeckite/plugin-color-mode";
|
|
36
|
+
|
|
37
|
+
export default defineConfig({
|
|
38
|
+
// ...
|
|
39
|
+
plugins: [colorModePlugin()],
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`colorModePlugin()` bundles `style.css` and declares `initColorMode` as a client
|
|
44
|
+
entry.
|
|
45
|
+
|
|
46
|
+
### Render the components
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
import { ColorModeScript, ColorModeToggle } from "@riebeckite/plugin-color-mode";
|
|
50
|
+
|
|
51
|
+
// ...in your renderer
|
|
52
|
+
return (
|
|
53
|
+
<html {...themeAttributes()}>
|
|
54
|
+
<head>
|
|
55
|
+
<ColorModeScript />
|
|
56
|
+
{/* stylesheets, ... */}
|
|
57
|
+
</head>
|
|
58
|
+
<body>
|
|
59
|
+
<header>
|
|
60
|
+
<ColorModeToggle />
|
|
61
|
+
</header>
|
|
62
|
+
{children}
|
|
63
|
+
</body>
|
|
64
|
+
</html>
|
|
65
|
+
);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`<ColorModeScript />` must be rendered in `<head>` ahead of the stylesheets so
|
|
69
|
+
the mode is set before CSS is applied. `<ColorModeToggle />` can go anywhere;
|
|
70
|
+
positions in headers and navbars are typical.
|
|
71
|
+
|
|
72
|
+
### Props
|
|
73
|
+
|
|
74
|
+
`ColorModeScript`
|
|
75
|
+
|
|
76
|
+
| Prop | Description |
|
|
77
|
+
| ---- | ----------- |
|
|
78
|
+
| `storageKey` | localStorage key to read from. Defaults to `riebeckite-color-mode`. |
|
|
79
|
+
|
|
80
|
+
`ColorModeToggle`
|
|
81
|
+
|
|
82
|
+
| Prop | Description |
|
|
83
|
+
| ---- | ----------- |
|
|
84
|
+
| `storageKey` | localStorage key to persist under. Defaults to `riebeckite-color-mode`. |
|
|
85
|
+
| `modes` | Modes to offer, in order. Defaults to `["light", "dark", "system"]`. |
|
|
86
|
+
| `labels` | Per-mode `aria-label` overrides: `{ light?, dark?, system? }`. |
|
|
87
|
+
| `label` | Group `aria-label`. Defaults to `"Color mode"`. |
|
|
88
|
+
|
|
89
|
+
Anything you configure here must be reflected on both components if you change
|
|
90
|
+
the storage key.
|
|
91
|
+
|
|
92
|
+
## How the mode is decided
|
|
93
|
+
|
|
94
|
+
1. On first paint, `ColorModeScript` applies the persisted value if it is a
|
|
95
|
+
valid mode (`light`, `dark` or `system`); otherwise it leaves the
|
|
96
|
+
`data-theme` the server already emitted (from your theme's `colorMode`).
|
|
97
|
+
2. On load, `initColorMode` repeats the same logic and syncs the buttons'
|
|
98
|
+
`aria-pressed` state.
|
|
99
|
+
3. Clicking a button persists the choice and re-applies it immediately.
|
|
100
|
+
`"system"` **removes** the `data-theme` attribute, so the OS preference CSS
|
|
101
|
+
takes over.
|
|
102
|
+
|
|
103
|
+
Since themes are presentation-only, the SSRed `data-theme` comes from your
|
|
104
|
+
theme's `colorMode`. The plugin only overrides it when the visitor has chosen
|
|
105
|
+
otherwise.
|
|
106
|
+
|
|
107
|
+
## CSS hooks
|
|
108
|
+
|
|
109
|
+
| Hook | Purpose |
|
|
110
|
+
| ---- | ------- |
|
|
111
|
+
| `rr-color-mode` | Root container of the toggle. |
|
|
112
|
+
| `rr-color-mode__button` | A single mode button. |
|
|
113
|
+
| `rr-color-mode__icon` | The inline SVG icon. |
|
|
114
|
+
|
|
115
|
+
The stylesheet uses `--rb-color-*` tokens, so the control follows the active
|
|
116
|
+
theme. The root is `display: none` until `initColorMode` (or the inline script)
|
|
117
|
+
sets `data-rb-color-mode="ready"` on `<html>`: with JavaScript disabled the
|
|
118
|
+
control never appears.
|
|
119
|
+
|
|
120
|
+
## Accessibility
|
|
121
|
+
|
|
122
|
+
- The container is a `<fieldset>` whose `<legend>` names the group (rendered
|
|
123
|
+
visually hidden).
|
|
124
|
+
- Each button is `type="button"` with an `aria-label` naming its mode; the
|
|
125
|
+
active one carries `aria-pressed="true"` set by the runtime.
|
|
126
|
+
- Icons are decorative (`aria-hidden`); labels come from the buttons.
|
|
127
|
+
- Transitions are disabled under `prefers-reduced-motion`.
|
|
128
|
+
|
|
129
|
+
## Events
|
|
130
|
+
|
|
131
|
+
`initColorMode` dispatches a `CustomEvent` on `document`:
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
document.addEventListener("riebeckite:color-mode", (event) => {
|
|
135
|
+
event.detail.mode; // "light" | "dark" | "system"
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Nothing consumes it yet. It exists so integrations (for example re-rendering
|
|
140
|
+
diagrams) can follow mode changes in the future.
|
|
141
|
+
|
|
142
|
+
## Limitations
|
|
143
|
+
|
|
144
|
+
- Diagram plugins (`d2`, `mermaid`, `vega-lite`) read `data-theme` once at
|
|
145
|
+
initialization. After switching modes they keep their previous palette until
|
|
146
|
+
the page is reloaded. Listening for `riebeckite:color-mode` is the hook for a
|
|
147
|
+
future re-render.
|
|
148
|
+
- The inline script is subject to Content-Security-Policy. If your `script-src`
|
|
149
|
+
forbids inline scripts, allow it by nonce or hash.
|
|
150
|
+
- `localStorage` access is wrapped in `try/catch`; when storage is unavailable
|
|
151
|
+
the switch still works for the current page but is not persisted.
|
|
152
|
+
- Multiple toggles on one page should share the same `storageKey`.
|
|
153
|
+
|
|
154
|
+
## Exports
|
|
155
|
+
|
|
156
|
+
- `colorModePlugin()` — plugin factory
|
|
157
|
+
- `ColorModeToggle` — the switch component
|
|
158
|
+
- `ColorModeScript` — the before-paint inline script component
|
|
159
|
+
- `initColorMode` — browser init (also exported by `@riebeckite/plugin-color-mode/client`)
|
|
160
|
+
- Constants: `COLOR_MODES`, `COLOR_MODE_STORAGE_KEY`, `COLOR_MODE_EVENT` (and friends)
|
|
161
|
+
- Type: `ColorMode`
|
|
162
|
+
|
|
163
|
+
## See also
|
|
164
|
+
|
|
165
|
+
- [Theme system](../../../docs/en/theme-system.md)
|
|
166
|
+
- [`@riebeckite/plugin-ux`](../ux/README.md)
|
package/README_ja.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# @riebeckite/plugin-color-mode
|
|
2
|
+
|
|
3
|
+
Riebeckite サイト向けのライト / ダーク / システム連動のカラーモード切替プラグインです。実行時に `<html>` の `data-theme` 属性へ書き込みます。これはテーマが配色を選ぶのに使う属性そのものなので、組み込みの全テーマで動作し、テーマ側に JavaScript は一切必要ありません。
|
|
4
|
+
|
|
5
|
+
[English](./README.md)
|
|
6
|
+
|
|
7
|
+
## 概要
|
|
8
|
+
|
|
9
|
+
テーマの配色は次の 3 つの CSS 状態で決まります([theme-system.md](../../../docs/ja/theme-system.md) 参照):
|
|
10
|
+
|
|
11
|
+
- `:root` — ライト
|
|
12
|
+
- `:root[data-theme="dark"]` — ダーク
|
|
13
|
+
- `@media (prefers-color-scheme: dark) { :root:not([data-theme]) }` — OS に追随(system)
|
|
14
|
+
|
|
15
|
+
プラグインはこの契約に対する薄い実行時スイッチです:
|
|
16
|
+
|
|
17
|
+
- `ColorModeScript` — `<head>` に置く 1 行のインラインスクリプト。**最初の描画の前に**保存済みモードを適用し、テーマのちらつき(FOUC)を防ぎます。
|
|
18
|
+
- `ColorModeToggle` — ライト / ダーク / システムのセグメント型コントロール。
|
|
19
|
+
- `initColorMode` — クライアントエントリ。ボタンの結線、`localStorage` への保存、ドキュメントの状態同期を行います。
|
|
20
|
+
|
|
21
|
+
`colorModePlugin()` はスタイルシートとクライアントエントリの登録だけを行います。UI はサイトシェルで描画する 2 つのコンポーネントから届きます。
|
|
22
|
+
|
|
23
|
+
## 使い方
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { defineConfig } from "@riebeckite/core";
|
|
27
|
+
import { colorModePlugin } from "@riebeckite/plugin-color-mode";
|
|
28
|
+
|
|
29
|
+
export default defineConfig({
|
|
30
|
+
// ...
|
|
31
|
+
plugins: [colorModePlugin()],
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`colorModePlugin()` は `style.css` をバンドルし、`initColorMode` をクライアントエントリとして宣言します。
|
|
36
|
+
|
|
37
|
+
### コンポーネントの描画
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
import { ColorModeScript, ColorModeToggle } from "@riebeckite/plugin-color-mode";
|
|
41
|
+
|
|
42
|
+
// ...レンダラー内
|
|
43
|
+
return (
|
|
44
|
+
<html {...themeAttributes()}>
|
|
45
|
+
<head>
|
|
46
|
+
<ColorModeScript />
|
|
47
|
+
{/* スタイルシート, ... */}
|
|
48
|
+
</head>
|
|
49
|
+
<body>
|
|
50
|
+
<header>
|
|
51
|
+
<ColorModeToggle />
|
|
52
|
+
</header>
|
|
53
|
+
{children}
|
|
54
|
+
</body>
|
|
55
|
+
</html>
|
|
56
|
+
);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`<ColorModeScript />` は CSS が適用される前にモードが確定するよう、スタイルシートより先に `<head>` へ置いてください。`<ColorModeToggle />` はヘッダーやナビバーなど任意の場所に配置できます。
|
|
60
|
+
|
|
61
|
+
### Props
|
|
62
|
+
|
|
63
|
+
`ColorModeScript`
|
|
64
|
+
|
|
65
|
+
| Prop | 説明 |
|
|
66
|
+
| ---- | ---- |
|
|
67
|
+
| `storageKey` | 読み取り元の localStorage キー。既定値は `riebeckite-color-mode`。 |
|
|
68
|
+
|
|
69
|
+
`ColorModeToggle`
|
|
70
|
+
|
|
71
|
+
| Prop | 説明 |
|
|
72
|
+
| ---- | ---- |
|
|
73
|
+
| `storageKey` | 保存先の localStorage キー。既定値は `riebeckite-color-mode`。 |
|
|
74
|
+
| `modes` | 表示するモード(順序付き)。既定値は `["light", "dark", "system"]`。 |
|
|
75
|
+
| `labels` | モードごとの `aria-label` 上書き: `{ light?, dark?, system? }`。 |
|
|
76
|
+
| `label` | グループの先頭に置く可視非表示の `<legend>` として使われます。既定値は `"Color mode"`。 |
|
|
77
|
+
|
|
78
|
+
ストレージキーを変更する場合は、両コンポーネントで同じ値を指定してください。
|
|
79
|
+
|
|
80
|
+
## モードの決定方法
|
|
81
|
+
|
|
82
|
+
1. `ColorModeScript` が最初の描画時に保存値を適用します。値が有効なモード(`light` / `dark` / `system`)の場合のみ上書きし、それ以外はサーバーが出力した `data-theme`(テーマの `colorMode` 由来)をそのまま残します。
|
|
83
|
+
2. 読み込み時に `initColorMode` が同じ判定を行い、ボタンの `aria-pressed` を同期します。
|
|
84
|
+
3. ボタンを押すと選択を保存して即時に再適用します。`"system"` は `data-theme` 属性を**削除**し、OS の設定 CSS に委ねます。
|
|
85
|
+
|
|
86
|
+
テーマは表示のみを担当するため、SSR 時の `data-theme` はテーマの `colorMode` から生成されます。プラグインは訪問者が別のモードを選んだときだけ上書きします。
|
|
87
|
+
|
|
88
|
+
## CSS フック
|
|
89
|
+
|
|
90
|
+
| フック | 用途 |
|
|
91
|
+
| ---- | ---- |
|
|
92
|
+
| `rr-color-mode` | トグルのルートコンテナ。 |
|
|
93
|
+
| `rr-color-mode__button` | 個々のモードボタン。 |
|
|
94
|
+
| `rr-color-mode__icon` | インライン SVG アイコン。 |
|
|
95
|
+
|
|
96
|
+
スタイルシートは `--rb-color-*` トークンを使うため、アクティブなテーマに追従します。ルートは `display: none` で、`initColorMode`(またはインラインスクリプト)が `<html>` に `data-rb-color-mode="ready"` を付けるまで非表示です。JavaScript が無効なページではコントロールそのものが現れません。
|
|
97
|
+
|
|
98
|
+
## アクセシビリティ
|
|
99
|
+
|
|
100
|
+
- コンテナは `<fieldset>` で、グループ名は可視非表示の `<legend>`(`label` props)が提供します。
|
|
101
|
+
- 各ボタンは `type="button"` でモード名の `aria-label` を持ち、アクティブなボタンには実行時設定の `aria-pressed="true"` が付きます。
|
|
102
|
+
- アイコンは装飾(`aria-hidden`)。ラベルはボタンから提供されます。
|
|
103
|
+
- `prefers-reduced-motion` ではトランジションを無効化します。
|
|
104
|
+
|
|
105
|
+
## イベント
|
|
106
|
+
|
|
107
|
+
`initColorMode` は `document` 上で `CustomEvent` を発火します:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
document.addEventListener("riebeckite:color-mode", (event) => {
|
|
111
|
+
event.detail.mode; // "light" | "dark" | "system"
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
現時点で消費するものはありません。図の再描画などの連携のための拡張ポイントです。
|
|
116
|
+
|
|
117
|
+
## 制約
|
|
118
|
+
|
|
119
|
+
- 図関連プラグイン(`d2` / `mermaid` / `vega-lite`)は初期化時にしか `data-theme` を読みません。モード切替後、リロードまでは前の配色のままです。将来の再描画のためのフックが `riebeckite:color-mode` イベントです。
|
|
120
|
+
- インラインスクリプトは Content-Security-Policy の影響を受けます。`script-src` がインラインを禁止している場合は nonce や hash で許可してください。
|
|
121
|
+
- `localStorage` へのアクセスは `try/catch` で保護しています。ストレージが使えない環境でも、そのページでは切替が機能します(永続化のみされません)。
|
|
122
|
+
- 1 ページに複数のトグルを置く場合は、同じ `storageKey` を共有してください。
|
|
123
|
+
|
|
124
|
+
## Exports
|
|
125
|
+
|
|
126
|
+
- `colorModePlugin()` — プラグインファクトリ
|
|
127
|
+
- `ColorModeToggle` — 切替コンポーネント
|
|
128
|
+
- `ColorModeScript` — 描画前インラインスクリプトのコンポーネント
|
|
129
|
+
- `initColorMode` — ブラウザ初期化関数(`@riebeckite/plugin-color-mode/client` からも export)
|
|
130
|
+
- 定数: `COLOR_MODES` / `COLOR_MODE_STORAGE_KEY` / `COLOR_MODE_EVENT` ほか
|
|
131
|
+
- 型: `ColorMode`
|
|
132
|
+
|
|
133
|
+
## 関連
|
|
134
|
+
|
|
135
|
+
- [テーマシステム](../../../docs/ja/theme-system.md)
|
|
136
|
+
- [`@riebeckite/plugin-ux`](../ux/README_ja.md)
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { initColorMode } from "./src/init.js";
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
var COLOR_MODES = ["light", "dark", "system"];
|
|
3
|
+
var COLOR_MODE_STORAGE_KEY = "riebeckite-color-mode";
|
|
4
|
+
var COLOR_MODE_ROOT_ATTRIBUTE = "data-color-mode-root";
|
|
5
|
+
var COLOR_MODE_STORAGE_KEY_ATTRIBUTE = "data-color-mode-storage-key";
|
|
6
|
+
var COLOR_MODE_BUTTON_ATTRIBUTE = "data-color-mode";
|
|
7
|
+
var COLOR_MODE_READY_ATTRIBUTE = "data-rb-color-mode";
|
|
8
|
+
var COLOR_MODE_EVENT = "riebeckite:color-mode";
|
|
9
|
+
|
|
10
|
+
// src/init.ts
|
|
11
|
+
var started = false;
|
|
12
|
+
function initColorMode() {
|
|
13
|
+
if (started) return;
|
|
14
|
+
started = true;
|
|
15
|
+
const run = () => {
|
|
16
|
+
const roots = Array.from(
|
|
17
|
+
document.querySelectorAll(`[${COLOR_MODE_ROOT_ATTRIBUTE}]`)
|
|
18
|
+
);
|
|
19
|
+
apply(getCurrentMode(roots));
|
|
20
|
+
if (roots.length === 0) return;
|
|
21
|
+
for (const root of roots) {
|
|
22
|
+
const storageKey = readStorageKey(root);
|
|
23
|
+
for (const button of root.querySelectorAll(
|
|
24
|
+
`[${COLOR_MODE_BUTTON_ATTRIBUTE}]`
|
|
25
|
+
)) {
|
|
26
|
+
button.addEventListener("click", () => {
|
|
27
|
+
const mode = parseColorMode(
|
|
28
|
+
button.getAttribute(COLOR_MODE_BUTTON_ATTRIBUTE)
|
|
29
|
+
);
|
|
30
|
+
if (mode === null) return;
|
|
31
|
+
writeStoredMode(storageKey, mode);
|
|
32
|
+
apply(mode);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const storageKeys = new Set(roots.map(readStorageKey));
|
|
37
|
+
window.addEventListener("storage", (event) => {
|
|
38
|
+
if (event.key !== null && storageKeys.has(event.key)) {
|
|
39
|
+
apply(getCurrentMode(roots));
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
if (document.readyState === "loading") {
|
|
44
|
+
document.addEventListener("DOMContentLoaded", run, { once: true });
|
|
45
|
+
} else {
|
|
46
|
+
run();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function apply(mode) {
|
|
50
|
+
const element = document.documentElement;
|
|
51
|
+
if (mode === "system") {
|
|
52
|
+
element.removeAttribute("data-theme");
|
|
53
|
+
} else {
|
|
54
|
+
element.setAttribute("data-theme", mode);
|
|
55
|
+
}
|
|
56
|
+
element.setAttribute(COLOR_MODE_READY_ATTRIBUTE, "ready");
|
|
57
|
+
for (const button of document.querySelectorAll(
|
|
58
|
+
`[${COLOR_MODE_BUTTON_ATTRIBUTE}]`
|
|
59
|
+
)) {
|
|
60
|
+
button.setAttribute(
|
|
61
|
+
"aria-pressed",
|
|
62
|
+
button.getAttribute(COLOR_MODE_BUTTON_ATTRIBUTE) === mode ? "true" : "false"
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
document.dispatchEvent(
|
|
66
|
+
new CustomEvent(COLOR_MODE_EVENT, {
|
|
67
|
+
detail: { mode }
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
function getCurrentMode(roots) {
|
|
72
|
+
for (const root of roots) {
|
|
73
|
+
const stored = readStoredMode(readStorageKey(root));
|
|
74
|
+
if (stored !== null) return stored;
|
|
75
|
+
}
|
|
76
|
+
const theme = document.documentElement.getAttribute("data-theme");
|
|
77
|
+
return theme === "light" || theme === "dark" ? theme : "system";
|
|
78
|
+
}
|
|
79
|
+
function readStorageKey(root) {
|
|
80
|
+
return root.getAttribute(COLOR_MODE_STORAGE_KEY_ATTRIBUTE) ?? COLOR_MODE_STORAGE_KEY;
|
|
81
|
+
}
|
|
82
|
+
function readStoredMode(storageKey) {
|
|
83
|
+
try {
|
|
84
|
+
return parseColorMode(window.localStorage.getItem(storageKey));
|
|
85
|
+
} catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function writeStoredMode(storageKey, mode) {
|
|
90
|
+
try {
|
|
91
|
+
window.localStorage.setItem(storageKey, mode);
|
|
92
|
+
} catch {
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function parseColorMode(value) {
|
|
96
|
+
return COLOR_MODES.includes(value ?? "") ? value : null;
|
|
97
|
+
}
|
|
98
|
+
export {
|
|
99
|
+
initColorMode
|
|
100
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type ColorModeScriptProps = {
|
|
2
|
+
/** localStorage key to read the persisted choice from. Defaults to the plugin default. */
|
|
3
|
+
readonly storageKey?: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Inline script (and its `data-rb-color-mode="ready"` marker) that applies the
|
|
7
|
+
* persisted color mode before first paint, preventing a flash of the wrong
|
|
8
|
+
* theme. Render in `<head>`, ahead of the stylesheets.
|
|
9
|
+
*/
|
|
10
|
+
export default function ColorModeScript({ storageKey, }: ColorModeScriptProps): import("hono/jsx/jsx-dev-runtime").JSX.Element;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type ColorMode } from "../src/constants.js";
|
|
2
|
+
export type ColorModeToggleProps = {
|
|
3
|
+
/** localStorage key to persist the choice under. Defaults to the plugin default. */
|
|
4
|
+
readonly storageKey?: string;
|
|
5
|
+
/** Modes to offer, in order. Defaults to `["light", "dark", "system"]`. */
|
|
6
|
+
readonly modes?: readonly ColorMode[];
|
|
7
|
+
/** Per-mode `aria-label` overrides. */
|
|
8
|
+
readonly labels?: Readonly<Partial<Record<ColorMode, string>>>;
|
|
9
|
+
/** Group label, used as the `<legend>`. */
|
|
10
|
+
readonly label?: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Server-rendered control to switch between light, dark and system color
|
|
14
|
+
* modes. The active state is applied by `initColorMode` at runtime; until the
|
|
15
|
+
* client runs the control is hidden by the stylesheet.
|
|
16
|
+
*/
|
|
17
|
+
export default function ColorModeToggle({ storageKey, modes, labels, label, }: ColorModeToggleProps): import("hono/jsx/jsx-dev-runtime").JSX.Element;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { default as ColorModeScript } from "./components/color-mode-script.js";
|
|
2
|
+
export { default as ColorModeToggle } from "./components/color-mode-toggle.js";
|
|
3
|
+
export { COLOR_MODE_BUTTON_ATTRIBUTE, COLOR_MODE_EVENT, COLOR_MODE_READY_ATTRIBUTE, COLOR_MODE_ROOT_ATTRIBUTE, COLOR_MODE_STORAGE_KEY, COLOR_MODE_STORAGE_KEY_ATTRIBUTE, COLOR_MODES, type ColorMode, } from "./src/constants.js";
|
|
4
|
+
export { initColorMode } from "./src/init.js";
|
|
5
|
+
/**
|
|
6
|
+
* Registers the color-mode plugin: bundles the toggle stylesheet and declares
|
|
7
|
+
* `initColorMode` as the browser entry point that wires up toggling at runtime.
|
|
8
|
+
*/
|
|
9
|
+
export declare function colorModePlugin(): import("@riebeckite/core").RiebeckitePlugin<unknown>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import {
|
|
3
|
+
createClientEntry,
|
|
4
|
+
createStyleAsset,
|
|
5
|
+
definePlugin
|
|
6
|
+
} from "@riebeckite/core";
|
|
7
|
+
|
|
8
|
+
// src/constants.ts
|
|
9
|
+
var COLOR_MODES = ["light", "dark", "system"];
|
|
10
|
+
var COLOR_MODE_STORAGE_KEY = "riebeckite-color-mode";
|
|
11
|
+
var COLOR_MODE_ROOT_ATTRIBUTE = "data-color-mode-root";
|
|
12
|
+
var COLOR_MODE_STORAGE_KEY_ATTRIBUTE = "data-color-mode-storage-key";
|
|
13
|
+
var COLOR_MODE_BUTTON_ATTRIBUTE = "data-color-mode";
|
|
14
|
+
var COLOR_MODE_READY_ATTRIBUTE = "data-rb-color-mode";
|
|
15
|
+
var COLOR_MODE_EVENT = "riebeckite:color-mode";
|
|
16
|
+
|
|
17
|
+
// components/color-mode-script.tsx
|
|
18
|
+
import { jsx } from "hono/jsx/jsx-runtime";
|
|
19
|
+
function ColorModeScript({
|
|
20
|
+
storageKey = COLOR_MODE_STORAGE_KEY
|
|
21
|
+
}) {
|
|
22
|
+
const key = JSON.stringify(storageKey);
|
|
23
|
+
const ready = JSON.stringify(COLOR_MODE_READY_ATTRIBUTE);
|
|
24
|
+
const source = `(function(){var d=document.documentElement;var m=null;try{m=localStorage.getItem(${key})}catch(e){m=null}if(m==="light"||m==="dark"){d.setAttribute("data-theme",m)}else if(m==="system"){d.removeAttribute("data-theme")}d.setAttribute(${ready},"ready")})();`;
|
|
25
|
+
return /* @__PURE__ */ jsx("script", { dangerouslySetInnerHTML: { __html: source } });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// components/color-mode-toggle.tsx
|
|
29
|
+
import { Fragment, jsx as jsx2, jsxs } from "hono/jsx/jsx-runtime";
|
|
30
|
+
var DEFAULT_LABELS = {
|
|
31
|
+
light: "Light",
|
|
32
|
+
dark: "Dark",
|
|
33
|
+
system: "System"
|
|
34
|
+
};
|
|
35
|
+
function ColorModeToggle({
|
|
36
|
+
storageKey = COLOR_MODE_STORAGE_KEY,
|
|
37
|
+
modes = COLOR_MODES,
|
|
38
|
+
labels = {},
|
|
39
|
+
label = "Color mode"
|
|
40
|
+
}) {
|
|
41
|
+
return /* @__PURE__ */ jsxs(
|
|
42
|
+
"fieldset",
|
|
43
|
+
{
|
|
44
|
+
class: "rr-color-mode",
|
|
45
|
+
"data-color-mode-root": true,
|
|
46
|
+
"data-color-mode-storage-key": storageKey,
|
|
47
|
+
children: [
|
|
48
|
+
/* @__PURE__ */ jsx2("legend", { class: "rr-color-mode__legend", children: label }),
|
|
49
|
+
modes.map((mode) => /* @__PURE__ */ jsx2(
|
|
50
|
+
"button",
|
|
51
|
+
{
|
|
52
|
+
type: "button",
|
|
53
|
+
class: "rr-color-mode__button",
|
|
54
|
+
"data-color-mode": mode,
|
|
55
|
+
"aria-label": labels[mode] ?? DEFAULT_LABELS[mode],
|
|
56
|
+
children: /* @__PURE__ */ jsx2(
|
|
57
|
+
"svg",
|
|
58
|
+
{
|
|
59
|
+
class: "rr-color-mode__icon",
|
|
60
|
+
viewBox: "0 0 24 24",
|
|
61
|
+
width: "16",
|
|
62
|
+
height: "16",
|
|
63
|
+
fill: "none",
|
|
64
|
+
stroke: "currentColor",
|
|
65
|
+
"aria-hidden": "true",
|
|
66
|
+
children: iconShape(mode)
|
|
67
|
+
}
|
|
68
|
+
)
|
|
69
|
+
},
|
|
70
|
+
mode
|
|
71
|
+
))
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
function iconShape(mode) {
|
|
77
|
+
switch (mode) {
|
|
78
|
+
case "light":
|
|
79
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
80
|
+
/* @__PURE__ */ jsx2("circle", { cx: "12", cy: "12", r: "4" }),
|
|
81
|
+
/* @__PURE__ */ jsx2("path", { d: "M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41" })
|
|
82
|
+
] });
|
|
83
|
+
case "dark":
|
|
84
|
+
return /* @__PURE__ */ jsx2("path", { d: "M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z" });
|
|
85
|
+
case "system":
|
|
86
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
87
|
+
/* @__PURE__ */ jsx2("rect", { x: "2", y: "3", width: "20", height: "14", rx: "2" }),
|
|
88
|
+
/* @__PURE__ */ jsx2("path", { d: "M8 21h8M12 17v4" })
|
|
89
|
+
] });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/init.ts
|
|
94
|
+
var started = false;
|
|
95
|
+
function initColorMode() {
|
|
96
|
+
if (started) return;
|
|
97
|
+
started = true;
|
|
98
|
+
const run = () => {
|
|
99
|
+
const roots = Array.from(
|
|
100
|
+
document.querySelectorAll(`[${COLOR_MODE_ROOT_ATTRIBUTE}]`)
|
|
101
|
+
);
|
|
102
|
+
apply(getCurrentMode(roots));
|
|
103
|
+
if (roots.length === 0) return;
|
|
104
|
+
for (const root of roots) {
|
|
105
|
+
const storageKey = readStorageKey(root);
|
|
106
|
+
for (const button of root.querySelectorAll(
|
|
107
|
+
`[${COLOR_MODE_BUTTON_ATTRIBUTE}]`
|
|
108
|
+
)) {
|
|
109
|
+
button.addEventListener("click", () => {
|
|
110
|
+
const mode = parseColorMode(
|
|
111
|
+
button.getAttribute(COLOR_MODE_BUTTON_ATTRIBUTE)
|
|
112
|
+
);
|
|
113
|
+
if (mode === null) return;
|
|
114
|
+
writeStoredMode(storageKey, mode);
|
|
115
|
+
apply(mode);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const storageKeys = new Set(roots.map(readStorageKey));
|
|
120
|
+
window.addEventListener("storage", (event) => {
|
|
121
|
+
if (event.key !== null && storageKeys.has(event.key)) {
|
|
122
|
+
apply(getCurrentMode(roots));
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
if (document.readyState === "loading") {
|
|
127
|
+
document.addEventListener("DOMContentLoaded", run, { once: true });
|
|
128
|
+
} else {
|
|
129
|
+
run();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function apply(mode) {
|
|
133
|
+
const element = document.documentElement;
|
|
134
|
+
if (mode === "system") {
|
|
135
|
+
element.removeAttribute("data-theme");
|
|
136
|
+
} else {
|
|
137
|
+
element.setAttribute("data-theme", mode);
|
|
138
|
+
}
|
|
139
|
+
element.setAttribute(COLOR_MODE_READY_ATTRIBUTE, "ready");
|
|
140
|
+
for (const button of document.querySelectorAll(
|
|
141
|
+
`[${COLOR_MODE_BUTTON_ATTRIBUTE}]`
|
|
142
|
+
)) {
|
|
143
|
+
button.setAttribute(
|
|
144
|
+
"aria-pressed",
|
|
145
|
+
button.getAttribute(COLOR_MODE_BUTTON_ATTRIBUTE) === mode ? "true" : "false"
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
document.dispatchEvent(
|
|
149
|
+
new CustomEvent(COLOR_MODE_EVENT, {
|
|
150
|
+
detail: { mode }
|
|
151
|
+
})
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
function getCurrentMode(roots) {
|
|
155
|
+
for (const root of roots) {
|
|
156
|
+
const stored = readStoredMode(readStorageKey(root));
|
|
157
|
+
if (stored !== null) return stored;
|
|
158
|
+
}
|
|
159
|
+
const theme = document.documentElement.getAttribute("data-theme");
|
|
160
|
+
return theme === "light" || theme === "dark" ? theme : "system";
|
|
161
|
+
}
|
|
162
|
+
function readStorageKey(root) {
|
|
163
|
+
return root.getAttribute(COLOR_MODE_STORAGE_KEY_ATTRIBUTE) ?? COLOR_MODE_STORAGE_KEY;
|
|
164
|
+
}
|
|
165
|
+
function readStoredMode(storageKey) {
|
|
166
|
+
try {
|
|
167
|
+
return parseColorMode(window.localStorage.getItem(storageKey));
|
|
168
|
+
} catch {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function writeStoredMode(storageKey, mode) {
|
|
173
|
+
try {
|
|
174
|
+
window.localStorage.setItem(storageKey, mode);
|
|
175
|
+
} catch {
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function parseColorMode(value) {
|
|
179
|
+
return COLOR_MODES.includes(value ?? "") ? value : null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// index.ts
|
|
183
|
+
function colorModePlugin() {
|
|
184
|
+
return definePlugin({
|
|
185
|
+
name: "color-mode",
|
|
186
|
+
assets: [createStyleAsset("color-mode")],
|
|
187
|
+
clientEntries: [createClientEntry("color-mode", "initColorMode")]
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
export {
|
|
191
|
+
COLOR_MODES,
|
|
192
|
+
COLOR_MODE_BUTTON_ATTRIBUTE,
|
|
193
|
+
COLOR_MODE_EVENT,
|
|
194
|
+
COLOR_MODE_READY_ATTRIBUTE,
|
|
195
|
+
COLOR_MODE_ROOT_ATTRIBUTE,
|
|
196
|
+
COLOR_MODE_STORAGE_KEY,
|
|
197
|
+
COLOR_MODE_STORAGE_KEY_ATTRIBUTE,
|
|
198
|
+
ColorModeScript,
|
|
199
|
+
ColorModeToggle,
|
|
200
|
+
colorModePlugin,
|
|
201
|
+
initColorMode
|
|
202
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** The modes a site can be in. */
|
|
2
|
+
export declare const COLOR_MODES: readonly ["light", "dark", "system"];
|
|
3
|
+
export type ColorMode = (typeof COLOR_MODES)[number];
|
|
4
|
+
/** Default `localStorage` key used to persist the selected mode. */
|
|
5
|
+
export declare const COLOR_MODE_STORAGE_KEY = "riebeckite-color-mode";
|
|
6
|
+
/** Attribute on the root element rendered by `ColorModeToggle`. */
|
|
7
|
+
export declare const COLOR_MODE_ROOT_ATTRIBUTE = "data-color-mode-root";
|
|
8
|
+
/** Per-root override for the storage key. */
|
|
9
|
+
export declare const COLOR_MODE_STORAGE_KEY_ATTRIBUTE = "data-color-mode-storage-key";
|
|
10
|
+
/** Attribute on each mode button. */
|
|
11
|
+
export declare const COLOR_MODE_BUTTON_ATTRIBUTE = "data-color-mode";
|
|
12
|
+
/**
|
|
13
|
+
* Attribute set on `<html>` once the runtime is ready. The stylesheet keeps
|
|
14
|
+
* the toggle hidden until this is present, so pages with JavaScript disabled
|
|
15
|
+
* never show a non-functional control.
|
|
16
|
+
*/
|
|
17
|
+
export declare const COLOR_MODE_READY_ATTRIBUTE = "data-rb-color-mode";
|
|
18
|
+
/**
|
|
19
|
+
* `CustomEvent` dispatched on `document` whenever the mode changes, with
|
|
20
|
+
* `{ detail: { mode } }`. Present for integrations (for example, re-rendering
|
|
21
|
+
* diagrams that read `data-theme`); nothing consumes it today.
|
|
22
|
+
*/
|
|
23
|
+
export declare const COLOR_MODE_EVENT = "riebeckite:color-mode";
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@riebeckite/plugin-color-mode",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"source": "./index.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./client": {
|
|
16
|
+
"types": "./dist/client.d.ts",
|
|
17
|
+
"import": "./dist/client.js",
|
|
18
|
+
"source": "./client.ts",
|
|
19
|
+
"default": "./dist/client.js"
|
|
20
|
+
},
|
|
21
|
+
"./style.css": "./style.css"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"hono": "^4.12.25",
|
|
25
|
+
"@riebeckite/core": "0.0.1"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"README.md",
|
|
33
|
+
"README_ja.md",
|
|
34
|
+
"dist",
|
|
35
|
+
"style.css"
|
|
36
|
+
],
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "https://github.com/Rerurate514/riebeckite.git",
|
|
40
|
+
"directory": "packages/plugins/color-mode"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/Rerurate514/riebeckite#readme",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/Rerurate514/riebeckite/issues"
|
|
45
|
+
},
|
|
46
|
+
"description": "Light / dark / system color-mode switching for Riebeckite.",
|
|
47
|
+
"keywords": [
|
|
48
|
+
"riebeckite",
|
|
49
|
+
"plugin",
|
|
50
|
+
"theme",
|
|
51
|
+
"color-mode",
|
|
52
|
+
"dark-mode"
|
|
53
|
+
],
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
56
|
+
},
|
|
57
|
+
"sideEffects": [
|
|
58
|
+
"./style.css"
|
|
59
|
+
],
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "node ../../../scripts/build_package.mjs"
|
|
62
|
+
}
|
|
63
|
+
}
|
package/style.css
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
@layer components {
|
|
2
|
+
.rr-color-mode {
|
|
3
|
+
--rr-color-mode-ink: var(--rb-color-ink);
|
|
4
|
+
--rr-color-mode-muted: var(--rb-color-muted);
|
|
5
|
+
--rr-color-mode-surface: var(--rb-color-surface);
|
|
6
|
+
--rr-color-mode-surface-hover: var(--rb-color-surface-hover);
|
|
7
|
+
--rr-color-mode-border: var(--rb-color-border);
|
|
8
|
+
|
|
9
|
+
display: none;
|
|
10
|
+
align-items: center;
|
|
11
|
+
gap: 0.25rem;
|
|
12
|
+
margin: 0;
|
|
13
|
+
min-width: 0;
|
|
14
|
+
padding: 0.2rem;
|
|
15
|
+
border: 1px solid var(--rr-color-mode-border);
|
|
16
|
+
border-radius: 999px;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/* The legend names the group for assistive technology only. */
|
|
20
|
+
.rr-color-mode__legend {
|
|
21
|
+
position: absolute;
|
|
22
|
+
width: 1px;
|
|
23
|
+
height: 1px;
|
|
24
|
+
padding: 0;
|
|
25
|
+
margin: -1px;
|
|
26
|
+
overflow: hidden;
|
|
27
|
+
clip: rect(0 0 0 0);
|
|
28
|
+
white-space: nowrap;
|
|
29
|
+
border: 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* The control only appears once the runtime marked the page as ready, so
|
|
33
|
+
pages with JavaScript disabled never show a non-functional switch. */
|
|
34
|
+
html[data-rb-color-mode="ready"] .rr-color-mode {
|
|
35
|
+
display: inline-flex;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.rr-color-mode__button {
|
|
39
|
+
display: inline-flex;
|
|
40
|
+
align-items: center;
|
|
41
|
+
justify-content: center;
|
|
42
|
+
width: 2rem;
|
|
43
|
+
height: 2rem;
|
|
44
|
+
padding: 0;
|
|
45
|
+
color: var(--rr-color-mode-muted);
|
|
46
|
+
background: transparent;
|
|
47
|
+
border: 0;
|
|
48
|
+
border-radius: 999px;
|
|
49
|
+
cursor: pointer;
|
|
50
|
+
transition:
|
|
51
|
+
background-color 120ms linear,
|
|
52
|
+
color 120ms linear;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.rr-color-mode__button:hover {
|
|
56
|
+
color: var(--rr-color-mode-ink);
|
|
57
|
+
background: var(--rr-color-mode-surface-hover);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.rr-color-mode__button[aria-pressed="true"] {
|
|
61
|
+
color: var(--rr-color-mode-ink);
|
|
62
|
+
background: var(--rr-color-mode-surface);
|
|
63
|
+
box-shadow: inset 0 0 0 1px var(--rr-color-mode-border);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.rr-color-mode__icon {
|
|
67
|
+
display: block;
|
|
68
|
+
width: 1rem;
|
|
69
|
+
height: 1rem;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.rr-color-mode__icon path,
|
|
73
|
+
.rr-color-mode__icon circle,
|
|
74
|
+
.rr-color-mode__icon rect {
|
|
75
|
+
stroke-linecap: round;
|
|
76
|
+
stroke-linejoin: round;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@media (prefers-reduced-motion: reduce) {
|
|
80
|
+
.rr-color-mode__button {
|
|
81
|
+
transition: none;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|