@hezhijie0327/zjdesign 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +39 -0
- package/README.md +89 -2
- package/dist/brand/index.d.mts +19 -0
- package/dist/brand/index.mjs +18 -0
- package/dist/hooks/index.d.mts +2 -0
- package/dist/hooks/index.mjs +3 -0
- package/dist/hooks-_Gk75q-U.mjs +220 -0
- package/dist/index-B-bZqvO6.d.mts +101 -0
- package/dist/index.d.mts +256 -0
- package/dist/index.mjs +421 -0
- package/dist/paper/index.d.mts +20 -0
- package/dist/paper/index.mjs +35 -0
- package/dist/styles/index.d.mts +118 -0
- package/dist/styles/index.mjs +3 -0
- package/dist/styles-CZ-LL-aM.mjs +123 -0
- package/dist/tokens/base.css +133 -0
- package/dist/tokens/behaviors.css +115 -0
- package/dist/tokens/index.css +8 -0
- package/dist/tokens/paper.css +207 -0
- package/dist/tokens/tokens.css +316 -0
- package/package.json +95 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Apache License 2.0 with Commons Clause
|
|
2
|
+
--------------------------------------
|
|
3
|
+
|
|
4
|
+
This product includes software developed by Zhijie He.
|
|
5
|
+
|
|
6
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
you may not use this file except in compliance with the License.
|
|
8
|
+
You may obtain a copy of the License at
|
|
9
|
+
|
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
|
|
12
|
+
Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
See the License for the specific language governing permissions and
|
|
16
|
+
limitations under the License.
|
|
17
|
+
|
|
18
|
+
Commons Clause Restriction
|
|
19
|
+
--------------------------
|
|
20
|
+
|
|
21
|
+
The Software is provided to you by the Licensor under the License, as
|
|
22
|
+
defined below, subject to the following condition.
|
|
23
|
+
|
|
24
|
+
Without limiting other conditions in the License, the grant of rights
|
|
25
|
+
under the License will not include, and the License does not grant to
|
|
26
|
+
you, the right to Sell the Software.
|
|
27
|
+
|
|
28
|
+
For purposes of the foregoing, "Sell" means practicing any or all of
|
|
29
|
+
the rights granted to you under the License to provide to third parties,
|
|
30
|
+
for a fee or other consideration (including without limitation fees for
|
|
31
|
+
hosting or consulting/support services related to the Software), a
|
|
32
|
+
product or service whose value derives, entirely or substantially, from
|
|
33
|
+
the functionality of the Software. Any license notice or attribution
|
|
34
|
+
required by the License must also include this Commons Clause License
|
|
35
|
+
Condition notice.
|
|
36
|
+
|
|
37
|
+
Software: ZJ Design (@hezhijie0327/zjdesign)
|
|
38
|
+
|
|
39
|
+
License: Apache 2.0 with Commons Clause
|
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ZJDesign
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@hezhijie0327/zjdesign)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://biomejs.dev/)
|
|
7
|
+
|
|
8
|
+
ZJ 产品家族的共享设计系统:暖纸底 × 墨色文字 × 琥珀金。将 ZJBlog / ZJKVM / ZJSearch / ZJStudio 四个项目的 tokens、Tailwind 片段、主题逻辑与 React 组件收敛为一个可发布的 npm 包(架构参考 lobe-ui 的单包多入口模式)。
|
|
9
|
+
|
|
10
|
+
- npm 包:`@hezhijie0327/zjdesign`
|
|
11
|
+
- 设计规范(英文):[DESIGN.md](./DESIGN.md)
|
|
12
|
+
- 代理开发手册:[AGENTS.md](./AGENTS.md)
|
|
13
|
+
|
|
14
|
+
## 特性
|
|
15
|
+
|
|
16
|
+
- **Tokens 层(技术栈无关)**:`@property` 注册的颜色 token + 亮 / 暗 / OLED 黑三档 + 整页 0.3s 交叉淡化 + 零 webfont 字体栈,纯 CSS,任何项目可直接 import
|
|
17
|
+
- **Tailwind 片段层**:家族统一的类名片段常量(`CARD` / `BTN_PRIMARY` / `CHIP` …)+ `cn()`(clsx + tailwind-merge)
|
|
18
|
+
- **React 组件层**:Button / IconButton / Modal / Popover / Dropdown / Segmented / Switch / Slider / Kbd / Toast / Collapse / CopyButton / ProgressBar
|
|
19
|
+
- **主题层**:`useTheme`(auto / light / dark / black 四态)+ 防闪 bootstrap 脚本 + localStorage / cookie 双持久化
|
|
20
|
+
- **纸张语言**:撕纸 / 胶带(djb2 种子物理)/ 邮戳 / 荧光笔等 clip-path 装饰 + 确定性微倾角
|
|
21
|
+
- **零状态库依赖**:Toast 总线不依赖 jotai;运行时依赖仅 clsx + tailwind-merge
|
|
22
|
+
|
|
23
|
+
## 快速开始
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm add @hezhijie0327/zjdesign
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
消费方 `global.css`(Tailwind v4):
|
|
30
|
+
|
|
31
|
+
```css
|
|
32
|
+
@import "tailwindcss";
|
|
33
|
+
@import "@hezhijie0327/zjdesign/tokens"; /* tokens + base + paper + behaviors */
|
|
34
|
+
|
|
35
|
+
@custom-variant dark (&:where(.dark, .dark *));
|
|
36
|
+
|
|
37
|
+
/* 片段类在包的 JS 产物里,需要让 Tailwind 扫描它 */
|
|
38
|
+
@source "../node_modules/@hezhijie0327/zjdesign/dist";
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
防闪脚本(首绘前注入 `<head>`):
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
import { THEME_BOOTSTRAP } from "@hezhijie0327/zjdesign/hooks";
|
|
45
|
+
|
|
46
|
+
// <script dangerouslySetInnerHTML={{ __html: THEME_BOOTSTRAP }} />
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## 用法
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
import { Button, Modal, toast, Toaster, useTheme } from "@hezhijie0327/zjdesign";
|
|
53
|
+
import { Tape } from "@hezhijie0327/zjdesign/paper";
|
|
54
|
+
import { Wordmark } from "@hezhijie0327/zjdesign/brand";
|
|
55
|
+
import { CARD, CHIP } from "@hezhijie0327/zjdesign/styles";
|
|
56
|
+
|
|
57
|
+
<Button variant="primary" onClick={() => toast("Saved")}>
|
|
58
|
+
保存
|
|
59
|
+
</Button>;
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
子入口一览:
|
|
63
|
+
|
|
64
|
+
| 入口 | 内容 |
|
|
65
|
+
| ------------------------------- | ------------------------------------------- |
|
|
66
|
+
| `@hezhijie0327/zjdesign` | React 组件 + hooks(主入口) |
|
|
67
|
+
| `@hezhijie0327/zjdesign/styles` | 片段常量 + `cn()` |
|
|
68
|
+
| `@hezhijie0327/zjdesign/hooks` | 主题 / `useExitPresence` / `reducedMotion` |
|
|
69
|
+
| `@hezhijie0327/zjdesign/paper` | `Tape`(种子物理胶带) |
|
|
70
|
+
| `@hezhijie0327/zjdesign/brand` | `Wordmark`(衬线字标 + 金句点) |
|
|
71
|
+
| `@hezhijie0327/zjdesign/tokens` | CSS 入口(`/tokens/base.css` 等可单独引入) |
|
|
72
|
+
|
|
73
|
+
## 本地开发
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pnpm install
|
|
77
|
+
pnpm build # 构建 dist/
|
|
78
|
+
pnpm test # vitest(jsdom)
|
|
79
|
+
pnpm lint # biome
|
|
80
|
+
pnpm dev # playground 视觉验证
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## 文档
|
|
84
|
+
|
|
85
|
+
- [DESIGN.md](./DESIGN.md):设计语言规范(tokens / 片段 / 纸张物理 / 动效)
|
|
86
|
+
- [AGENTS.md](./AGENTS.md):代理协作约定与发布流程
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
Apache-2.0 with Commons Clause — 详见 [LICENSE](./LICENSE)。
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as react0 from "react";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/brand/Wordmark.d.ts
|
|
5
|
+
/** Family wordmark: serif logotype + the gold full stop (em-relative dot,
|
|
6
|
+
* scales with the wordmark font size — the family signature). */
|
|
7
|
+
declare function Wordmark({
|
|
8
|
+
children,
|
|
9
|
+
className,
|
|
10
|
+
dotClassName
|
|
11
|
+
}: {
|
|
12
|
+
children: ReactNode;
|
|
13
|
+
/** Typography on the logotype (size etc.); the family default is the
|
|
14
|
+
* navigation-bar scale — pass e.g. "text-xl" for compact bars */
|
|
15
|
+
className?: string;
|
|
16
|
+
dotClassName?: string;
|
|
17
|
+
}): react0.JSX.Element;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { Wordmark };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { n as BRAND_DOT, z as cn } from "../styles-CZ-LL-aM.mjs";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
//#region src/brand/Wordmark.tsx
|
|
5
|
+
/** Family wordmark: serif logotype + the gold full stop (em-relative dot,
|
|
6
|
+
* scales with the wordmark font size — the family signature). */
|
|
7
|
+
function Wordmark({ children, className, dotClassName }) {
|
|
8
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
9
|
+
className: cn("font-serif text-2xl font-semibold tracking-tight", className),
|
|
10
|
+
children: [children, /* @__PURE__ */ jsx("span", {
|
|
11
|
+
"aria-hidden": "true",
|
|
12
|
+
className: cn(BRAND_DOT, "ms-0.5", dotClassName)
|
|
13
|
+
})]
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { Wordmark };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { _ as watchSystemTheme, a as THEME_BOOTSTRAP, c as THEME_COLOR_LIGHT, d as applyThemeStyle, f as cookieThemeStorage, g as setThemeStyle, h as readThemeStyle, i as useExitPresence, l as ThemeStorage, m as localStorageThemeStorage, n as UseThemeResult, o as THEME_COLOR_BLACK, p as createThemeController, r as useTheme, s as THEME_COLOR_DARK, t as UseThemeOptions, u as ThemeStyle, v as watchThemeDark, y as reducedMotion } from "../index-B-bZqvO6.mjs";
|
|
2
|
+
export { THEME_BOOTSTRAP, THEME_COLOR_BLACK, THEME_COLOR_DARK, THEME_COLOR_LIGHT, ThemeStorage, ThemeStyle, UseThemeOptions, UseThemeResult, applyThemeStyle, cookieThemeStorage, createThemeController, localStorageThemeStorage, readThemeStyle, reducedMotion, setThemeStyle, useExitPresence, useTheme, watchSystemTheme, watchThemeDark };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { a as THEME_COLOR_DARK, c as cookieThemeStorage, d as readThemeStyle, f as setThemeStyle, h as reducedMotion, i as THEME_COLOR_BLACK, l as createThemeController, m as watchThemeDark, n as useExitPresence, o as THEME_COLOR_LIGHT, p as watchSystemTheme, r as THEME_BOOTSTRAP, s as applyThemeStyle, t as useTheme, u as localStorageThemeStorage } from "../hooks-_Gk75q-U.mjs";
|
|
2
|
+
|
|
3
|
+
export { THEME_BOOTSTRAP, THEME_COLOR_BLACK, THEME_COLOR_DARK, THEME_COLOR_LIGHT, applyThemeStyle, cookieThemeStorage, createThemeController, localStorageThemeStorage, readThemeStyle, reducedMotion, setThemeStyle, useExitPresence, useTheme, watchSystemTheme, watchThemeDark };
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/motion.ts
|
|
4
|
+
/** True when the user prefers reduced motion (the global stylesheet gate
|
|
5
|
+
* collapses animations to 0.01ms, so callers should skip exit windows and
|
|
6
|
+
* transitions entirely). */
|
|
7
|
+
function reducedMotion() {
|
|
8
|
+
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/hooks/theme.ts
|
|
13
|
+
/** theme-color (browser UI / PWA title bar) takes the page background */
|
|
14
|
+
const THEME_COLOR_LIGHT = "#faf9f6";
|
|
15
|
+
const THEME_COLOR_DARK = "#1b1a18";
|
|
16
|
+
const THEME_COLOR_BLACK = "#000000";
|
|
17
|
+
/** localStorage adapter (default; key follows the family contract). */
|
|
18
|
+
function localStorageThemeStorage(key = "zj-theme") {
|
|
19
|
+
return {
|
|
20
|
+
read: () => {
|
|
21
|
+
try {
|
|
22
|
+
return localStorage.getItem(key);
|
|
23
|
+
} catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
write: (value) => {
|
|
28
|
+
try {
|
|
29
|
+
localStorage.setItem(key, value);
|
|
30
|
+
} catch {}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** Cookie adapter (ZJSearch contract: server renders the first paint, so the
|
|
35
|
+
* preference must ride the request). Value names follow the `simple_style`
|
|
36
|
+
* cookie. */
|
|
37
|
+
function cookieThemeStorage(key = "simple_style", days = 365) {
|
|
38
|
+
return {
|
|
39
|
+
read: () => {
|
|
40
|
+
const match = document.cookie.match(/* @__PURE__ */ new RegExp(`(?:^|; )${key}=([^;]*)`));
|
|
41
|
+
return match?.[1] ? decodeURIComponent(match[1]) : null;
|
|
42
|
+
},
|
|
43
|
+
write: (value) => {
|
|
44
|
+
document.cookie = `${key}=${encodeURIComponent(value)}; max-age=${days * 86400}; path=/; samesite=lax`;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function isThemeStyle(value) {
|
|
49
|
+
return value === "light" || value === "dark" || value === "black";
|
|
50
|
+
}
|
|
51
|
+
let paletteAnimTimer;
|
|
52
|
+
function resolvedDark(style) {
|
|
53
|
+
return style === "dark" || style === "black" || style === "auto" && window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
54
|
+
}
|
|
55
|
+
/** Collapse the theme-color metas in head to the resolved value: the first drops its
|
|
56
|
+
* media attribute and gets the actual background color, the rest are removed. Idempotent —
|
|
57
|
+
* both the pre-paint bootstrap and every palette flip go through here. */
|
|
58
|
+
function syncThemeColorMeta(dark, black) {
|
|
59
|
+
const metas = document.querySelectorAll("meta[name=\"theme-color\"]");
|
|
60
|
+
const primary = metas.item(0);
|
|
61
|
+
if (!primary) return;
|
|
62
|
+
primary.removeAttribute("media");
|
|
63
|
+
primary.setAttribute("content", black ? THEME_COLOR_BLACK : dark ? THEME_COLOR_DARK : THEME_COLOR_LIGHT);
|
|
64
|
+
metas.forEach((meta, index) => {
|
|
65
|
+
if (index > 0) meta.remove();
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function createThemeController(storage = localStorageThemeStorage()) {
|
|
69
|
+
const readThemeStyle$1 = () => {
|
|
70
|
+
const value = storage.read();
|
|
71
|
+
return isThemeStyle(value) ? value : "auto";
|
|
72
|
+
};
|
|
73
|
+
const applyThemeStyle$1 = (style) => {
|
|
74
|
+
const dark = resolvedDark(style);
|
|
75
|
+
const root = document.documentElement;
|
|
76
|
+
if ((root.classList.contains("dark") !== dark || root.classList.contains("black") !== (style === "black")) && !window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
|
|
77
|
+
root.classList.add("zjs-palette-anim");
|
|
78
|
+
window.clearTimeout(paletteAnimTimer);
|
|
79
|
+
paletteAnimTimer = window.setTimeout(() => root.classList.remove("zjs-palette-anim"), 350);
|
|
80
|
+
}
|
|
81
|
+
root.classList.toggle("dark", dark);
|
|
82
|
+
root.classList.toggle("black", style === "black");
|
|
83
|
+
root.setAttribute("data-theme-mode", style);
|
|
84
|
+
syncThemeColorMeta(dark, style === "black");
|
|
85
|
+
};
|
|
86
|
+
return {
|
|
87
|
+
storage,
|
|
88
|
+
readThemeStyle: readThemeStyle$1,
|
|
89
|
+
applyThemeStyle: applyThemeStyle$1,
|
|
90
|
+
setThemeStyle(style) {
|
|
91
|
+
storage.write(style);
|
|
92
|
+
applyThemeStyle$1(style);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const defaultController = /* @__PURE__ */ createThemeController();
|
|
97
|
+
const readThemeStyle = defaultController.readThemeStyle;
|
|
98
|
+
const applyThemeStyle = defaultController.applyThemeStyle;
|
|
99
|
+
const setThemeStyle = defaultController.setThemeStyle;
|
|
100
|
+
/** In auto mode, follow the system light/dark switch in real time. */
|
|
101
|
+
function watchSystemTheme(storage = localStorageThemeStorage()) {
|
|
102
|
+
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
|
|
103
|
+
const controller = createThemeController(storage);
|
|
104
|
+
if (controller.readThemeStyle() === "auto") controller.applyThemeStyle("auto");
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/** Subscribe to the dark state (html.dark is the single source of truth; both in-site
|
|
108
|
+
* toggles and system-follow flip it). Returns an unsubscribe function. Heavy theme
|
|
109
|
+
* dependencies (giscus iframe / mermaid re-render) hook in here — do not attach your own
|
|
110
|
+
* MutationObserver. */
|
|
111
|
+
function watchThemeDark(onChange) {
|
|
112
|
+
const root = document.documentElement;
|
|
113
|
+
const observer = new MutationObserver(() => onChange(root.classList.contains("dark")));
|
|
114
|
+
observer.observe(root, {
|
|
115
|
+
attributeFilter: ["class"],
|
|
116
|
+
attributes: true
|
|
117
|
+
});
|
|
118
|
+
return () => {
|
|
119
|
+
observer.disconnect();
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** Inline script for the prerendered HTML <head>: attaches .dark / .black and
|
|
123
|
+
* data-theme-mode before the first frame to prevent light/dark and icon flashes;
|
|
124
|
+
* under an explicit theme it also collapses the theme-color meta (auto keeps the
|
|
125
|
+
* media pair and follows the system natively). localStorage contract. */
|
|
126
|
+
const THEME_BOOTSTRAP = `(function(){try{var s=localStorage.getItem("zj-theme");if(s!=="light"&&s!=="dark"&&s!=="black")s="auto";var d=s==="dark"||s==="black"||(s==="auto"&&window.matchMedia("(prefers-color-scheme: dark)").matches);var r=document.documentElement;if(d)r.classList.add("dark");if(s==="black")r.classList.add("black");r.setAttribute("data-theme-mode",s);if(s!=="auto"){var m=document.querySelectorAll('meta[name="theme-color"]');if(m.length>0){m[0].removeAttribute("media");m[0].setAttribute("content",s==="black"?"${THEME_COLOR_BLACK}":d?"${THEME_COLOR_DARK}":"${THEME_COLOR_LIGHT}");for(var i=1;i<m.length;i++)m[i].remove();}}}catch(e){}})();`;
|
|
127
|
+
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/hooks/useExitPresence.ts
|
|
130
|
+
/**
|
|
131
|
+
* Exit-animation presence for conditionally rendered surfaces (dropdown
|
|
132
|
+
* menus, dialogs, drawers, popovers): a dismissal keeps the surface mounted
|
|
133
|
+
* for the exit animation instead of snapping it out of the DOM. Returns
|
|
134
|
+
* `render` (keep the element in the tree) and `closing` (the exit phase —
|
|
135
|
+
* apply the `-out` animation class, set `inert`, and short-circuit handlers
|
|
136
|
+
* that must not re-fire, e.g. a second Escape re-opening history).
|
|
137
|
+
*
|
|
138
|
+
* The unmount is a plain timer sized to the CSS animation duration —
|
|
139
|
+
* deterministic, and it doubles as the timeout fallback for the animation
|
|
140
|
+
* itself. Reduced motion skips the window entirely (the global stylesheet
|
|
141
|
+
* guard collapses the animation to 0.01ms, so an instant unmount is what
|
|
142
|
+
* the user asked for).
|
|
143
|
+
*/
|
|
144
|
+
function useExitPresence(open, ms = 160) {
|
|
145
|
+
const [render, setRender] = useState(open);
|
|
146
|
+
const [closing, setClosing] = useState(false);
|
|
147
|
+
useEffect(() => {
|
|
148
|
+
if (open) {
|
|
149
|
+
setRender(true);
|
|
150
|
+
setClosing(false);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (!render || reducedMotion()) {
|
|
154
|
+
setRender(false);
|
|
155
|
+
setClosing(false);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
setClosing(true);
|
|
159
|
+
const timer = window.setTimeout(() => {
|
|
160
|
+
setRender(false);
|
|
161
|
+
setClosing(false);
|
|
162
|
+
}, ms);
|
|
163
|
+
return () => {
|
|
164
|
+
window.clearTimeout(timer);
|
|
165
|
+
};
|
|
166
|
+
}, [
|
|
167
|
+
open,
|
|
168
|
+
ms,
|
|
169
|
+
render
|
|
170
|
+
]);
|
|
171
|
+
return {
|
|
172
|
+
render,
|
|
173
|
+
closing
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/hooks/useTheme.ts
|
|
179
|
+
/** React binding over the theme controller: keeps component state in sync with
|
|
180
|
+
* the html classes, persists the preference, and follows the system in auto
|
|
181
|
+
* mode. Mount once near the app root. */
|
|
182
|
+
function useTheme({ storage = localStorageThemeStorage(), order = [
|
|
183
|
+
"auto",
|
|
184
|
+
"light",
|
|
185
|
+
"dark"
|
|
186
|
+
] } = {}) {
|
|
187
|
+
const controller = createThemeController(storage);
|
|
188
|
+
const [style, setStyleState] = useState(controller.readThemeStyle);
|
|
189
|
+
const [dark, setDark] = useState(() => document.documentElement.classList.contains("dark"));
|
|
190
|
+
const setStyle = useCallback((next) => {
|
|
191
|
+
controller.setThemeStyle(next);
|
|
192
|
+
setStyleState(next);
|
|
193
|
+
}, [controller]);
|
|
194
|
+
const cycle = useCallback(() => {
|
|
195
|
+
setStyle(order[(order.indexOf(style) + 1) % order.length] ?? order[0] ?? "auto");
|
|
196
|
+
}, [
|
|
197
|
+
order,
|
|
198
|
+
setStyle,
|
|
199
|
+
style
|
|
200
|
+
]);
|
|
201
|
+
useEffect(() => watchSystemTheme(storage), [storage]);
|
|
202
|
+
useEffect(() => {
|
|
203
|
+
const root = document.documentElement;
|
|
204
|
+
const observer = new MutationObserver(() => setDark(root.classList.contains("dark")));
|
|
205
|
+
observer.observe(root, {
|
|
206
|
+
attributeFilter: ["class"],
|
|
207
|
+
attributes: true
|
|
208
|
+
});
|
|
209
|
+
return () => observer.disconnect();
|
|
210
|
+
}, []);
|
|
211
|
+
return {
|
|
212
|
+
style,
|
|
213
|
+
dark,
|
|
214
|
+
setStyle,
|
|
215
|
+
cycle
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
//#endregion
|
|
220
|
+
export { THEME_COLOR_DARK as a, cookieThemeStorage as c, readThemeStyle as d, setThemeStyle as f, reducedMotion as h, THEME_COLOR_BLACK as i, createThemeController as l, watchThemeDark as m, useExitPresence as n, THEME_COLOR_LIGHT as o, watchSystemTheme as p, THEME_BOOTSTRAP as r, applyThemeStyle as s, useTheme as t, localStorageThemeStorage as u };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
//#region src/hooks/motion.d.ts
|
|
2
|
+
/** True when the user prefers reduced motion (the global stylesheet gate
|
|
3
|
+
* collapses animations to 0.01ms, so callers should skip exit windows and
|
|
4
|
+
* transitions entirely). */
|
|
5
|
+
declare function reducedMotion(): boolean;
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/hooks/theme.d.ts
|
|
8
|
+
/** Light/dark theme: class strategy (html.dark / html.black), preference kept in a
|
|
9
|
+
* pluggable storage (localStorage by default, cookie for server-rendered apps),
|
|
10
|
+
* auto follows the system. Palette transitions are handled by CSS (@property
|
|
11
|
+
* registration in tokens.css + the html transition in behaviors.css); this module
|
|
12
|
+
* only flips class names, opens the transition stand-down window, and provides the
|
|
13
|
+
* anti-flash (FOUC prevention) pre-paint inline script.
|
|
14
|
+
*
|
|
15
|
+
* Family provenance: byte-compatible with the ZJBlog/ZJKVM theme.ts contract;
|
|
16
|
+
* the .black OLED tier follows ZJSearch. */
|
|
17
|
+
type ThemeStyle = "auto" | "light" | "dark" | "black";
|
|
18
|
+
/** theme-color (browser UI / PWA title bar) takes the page background */
|
|
19
|
+
declare const THEME_COLOR_LIGHT = "#faf9f6";
|
|
20
|
+
declare const THEME_COLOR_DARK = "#1b1a18";
|
|
21
|
+
declare const THEME_COLOR_BLACK = "#000000";
|
|
22
|
+
/** Read/write adapter for the persisted preference. */
|
|
23
|
+
interface ThemeStorage {
|
|
24
|
+
read: () => string | null;
|
|
25
|
+
write: (value: string) => void;
|
|
26
|
+
}
|
|
27
|
+
/** localStorage adapter (default; key follows the family contract). */
|
|
28
|
+
declare function localStorageThemeStorage(key?: string): ThemeStorage;
|
|
29
|
+
/** Cookie adapter (ZJSearch contract: server renders the first paint, so the
|
|
30
|
+
* preference must ride the request). Value names follow the `simple_style`
|
|
31
|
+
* cookie. */
|
|
32
|
+
declare function cookieThemeStorage(key?: string, days?: number): ThemeStorage;
|
|
33
|
+
declare function createThemeController(storage?: ThemeStorage): {
|
|
34
|
+
storage: ThemeStorage;
|
|
35
|
+
readThemeStyle: () => ThemeStyle;
|
|
36
|
+
applyThemeStyle: (style: ThemeStyle) => void;
|
|
37
|
+
/** Persist the preference, then apply it (classes + meta). */
|
|
38
|
+
setThemeStyle(style: ThemeStyle): void;
|
|
39
|
+
};
|
|
40
|
+
declare const readThemeStyle: () => ThemeStyle;
|
|
41
|
+
declare const applyThemeStyle: (style: ThemeStyle) => void;
|
|
42
|
+
declare const setThemeStyle: (style: ThemeStyle) => void;
|
|
43
|
+
/** In auto mode, follow the system light/dark switch in real time. */
|
|
44
|
+
declare function watchSystemTheme(storage?: ThemeStorage): void;
|
|
45
|
+
/** Subscribe to the dark state (html.dark is the single source of truth; both in-site
|
|
46
|
+
* toggles and system-follow flip it). Returns an unsubscribe function. Heavy theme
|
|
47
|
+
* dependencies (giscus iframe / mermaid re-render) hook in here — do not attach your own
|
|
48
|
+
* MutationObserver. */
|
|
49
|
+
declare function watchThemeDark(onChange: (dark: boolean) => void): () => void;
|
|
50
|
+
/** Inline script for the prerendered HTML <head>: attaches .dark / .black and
|
|
51
|
+
* data-theme-mode before the first frame to prevent light/dark and icon flashes;
|
|
52
|
+
* under an explicit theme it also collapses the theme-color meta (auto keeps the
|
|
53
|
+
* media pair and follows the system natively). localStorage contract. */
|
|
54
|
+
declare const THEME_BOOTSTRAP = "(function(){try{var s=localStorage.getItem(\"zj-theme\");if(s!==\"light\"&&s!==\"dark\"&&s!==\"black\")s=\"auto\";var d=s===\"dark\"||s===\"black\"||(s===\"auto\"&&window.matchMedia(\"(prefers-color-scheme: dark)\").matches);var r=document.documentElement;if(d)r.classList.add(\"dark\");if(s===\"black\")r.classList.add(\"black\");r.setAttribute(\"data-theme-mode\",s);if(s!==\"auto\"){var m=document.querySelectorAll('meta[name=\"theme-color\"]');if(m.length>0){m[0].removeAttribute(\"media\");m[0].setAttribute(\"content\",s===\"black\"?\"#000000\":d?\"#1b1a18\":\"#faf9f6\");for(var i=1;i<m.length;i++)m[i].remove();}}}catch(e){}})();";
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/hooks/useExitPresence.d.ts
|
|
57
|
+
/**
|
|
58
|
+
* Exit-animation presence for conditionally rendered surfaces (dropdown
|
|
59
|
+
* menus, dialogs, drawers, popovers): a dismissal keeps the surface mounted
|
|
60
|
+
* for the exit animation instead of snapping it out of the DOM. Returns
|
|
61
|
+
* `render` (keep the element in the tree) and `closing` (the exit phase —
|
|
62
|
+
* apply the `-out` animation class, set `inert`, and short-circuit handlers
|
|
63
|
+
* that must not re-fire, e.g. a second Escape re-opening history).
|
|
64
|
+
*
|
|
65
|
+
* The unmount is a plain timer sized to the CSS animation duration —
|
|
66
|
+
* deterministic, and it doubles as the timeout fallback for the animation
|
|
67
|
+
* itself. Reduced motion skips the window entirely (the global stylesheet
|
|
68
|
+
* guard collapses the animation to 0.01ms, so an instant unmount is what
|
|
69
|
+
* the user asked for).
|
|
70
|
+
*/
|
|
71
|
+
declare function useExitPresence(open: boolean, ms?: number): {
|
|
72
|
+
render: boolean;
|
|
73
|
+
closing: boolean;
|
|
74
|
+
};
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/hooks/useTheme.d.ts
|
|
77
|
+
interface UseThemeOptions {
|
|
78
|
+
/** Persisted preference storage; defaults to localStorage("zj-theme") */
|
|
79
|
+
storage?: ThemeStorage;
|
|
80
|
+
/** Cycle order for the `cycle()` convenience (ZJKVM three-state loop;
|
|
81
|
+
* pass a four-step order to include the OLED tier) */
|
|
82
|
+
order?: ThemeStyle[];
|
|
83
|
+
}
|
|
84
|
+
interface UseThemeResult {
|
|
85
|
+
/** Current preference (auto / light / dark / black) */
|
|
86
|
+
style: ThemeStyle;
|
|
87
|
+
/** Resolved dark state (black implies dark) */
|
|
88
|
+
dark: boolean;
|
|
89
|
+
setStyle: (style: ThemeStyle) => void;
|
|
90
|
+
/** Advance to the next preference in `order` (wraps around) */
|
|
91
|
+
cycle: () => void;
|
|
92
|
+
}
|
|
93
|
+
/** React binding over the theme controller: keeps component state in sync with
|
|
94
|
+
* the html classes, persists the preference, and follows the system in auto
|
|
95
|
+
* mode. Mount once near the app root. */
|
|
96
|
+
declare function useTheme({
|
|
97
|
+
storage,
|
|
98
|
+
order
|
|
99
|
+
}?: UseThemeOptions): UseThemeResult;
|
|
100
|
+
//#endregion
|
|
101
|
+
export { watchSystemTheme as _, THEME_BOOTSTRAP as a, THEME_COLOR_LIGHT as c, applyThemeStyle as d, cookieThemeStorage as f, setThemeStyle as g, readThemeStyle as h, useExitPresence as i, ThemeStorage as l, localStorageThemeStorage as m, UseThemeResult as n, THEME_COLOR_BLACK as o, createThemeController as p, useTheme as r, THEME_COLOR_DARK as s, UseThemeOptions as t, ThemeStyle as u, watchThemeDark as v, reducedMotion as y };
|