@vueboot/composables 0.0.0-stage → 0.2.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ygzhang-lab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,128 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # @vueboot/composables — 主题 / 屏幕方向 / User-Agent
2
+
3
+ 两个运行时 composable,共享同一套约定。
4
+
5
+ 依赖:`vue` 3.5+、`@vueuse/core` 15+(由 `@vueuse/core` 的 peer 依赖决定);入口为 `@vueboot/composables`。
6
+
7
+ | 入口 | 返回 | 作用 |
8
+ | --- | --- | --- |
9
+ | `useEnvironment()` | `EnvironmentState` | 主题(auto / light / dark)、横竖屏、视口尺寸 |
10
+ | `useUserAgent()` | `UserAgentState` | UA 解析出的系统 / 浏览器 / WebView / App 容器能力 |
11
+
12
+ ```ts
13
+ import { useEnvironment, useUserAgent } from "@vueboot/composables";
14
+
15
+ const env = useEnvironment();
16
+ const ua = useUserAgent();
17
+
18
+ env.isPortrait; // boolean,属性已解包
19
+ env.setThemeMode("dark");
20
+ ua.isWeChat; // boolean
21
+ ua.osVersion; // string | null
22
+ await ua.refreshUserAgent();
23
+ ```
24
+
25
+ ## 使用约定
26
+
27
+ - **模块级单例**:两个 composable 在模块加载时创建状态,多处调用返回同一个
28
+ `readonly(reactive(...))`。不要在调用方解构,也不要写 `.value`。
29
+ - **响应式读取**:`watch` 必须传 getter:`watch(() => ua.isWeChat, cb)`;
30
+ 直接 `watch(ua.isWeChat, cb)` 会丢掉响应性。
31
+ - **只读 + 单一写入口**:主题只能通过 `env.setThemeMode()` 修改;UA 只能通过
32
+ `ua.refreshUserAgent()` 重新解析。
33
+ - **SSR / 多 app**:模块级状态在同一 Node 进程内共享,不是每请求隔离。
34
+ 服务端只需要解析能力时,优先使用不依赖 Vue / DOM 的纯函数:
35
+ `parseUserAgent(input)`、`resolveOrientation(input)`、`compareVersion(a, b)`。
36
+ 其中 `parseUserAgent` / `resolveOrientation` 不在桶文件导出,需从具体文件导入:
37
+ `import { parseUserAgent } from "@vueboot/composables/user-agent"`、
38
+ `import { resolveOrientation } from "@vueboot/composables/orientation"`;
39
+ 版本工具 `compareVersion` / `isVersionKnown` / `isVersionAtLeast` 已随入口导出。
40
+
41
+ ## useEnvironment
42
+
43
+ `EnvironmentState` 主要字段:
44
+
45
+ | 字段 | 类型 | 说明 |
46
+ | --- | --- | --- |
47
+ | `colorMode` | `"auto" \| "light" \| "dark"` | 当前主题模式(`auto` 跟随系统) |
48
+ | `isDark` / `colorTheme` | `boolean` / `"light" \| "dark"` | 解析后的暗色状态 |
49
+ | `isPortrait` | `boolean` | 是否竖屏 |
50
+ | `resolvedOrientation` / `orientationSource` | `"portrait" \| "landscape"` / `"screen" \| "window.orientation" \| "viewport" \| "fallback"` | 最终方向与判定来源 |
51
+ | `orientation` / `angle` / `orientationSupported` | `string \| undefined` / `number` / `boolean` | `screen.orientation` 原始信息 |
52
+ | `width` / `height` | `number` | viewport 宽高(CSS 像素);视口不可读时为 `0`,不使用 `screen` 物理尺寸替代 |
53
+ | `setThemeMode(mode)` | `(mode: ThemeMode) => void` | 唯一主题写入口 |
54
+
55
+ 初始化与 DOM 副作用:
56
+
57
+ - 调用 `useEnvironment()` 或 `setupEnvironment()` 后安装 watcher,把结果写到
58
+ `<html>`:`data-theme`、`data-orientation`、`data-orientation-source`、
59
+ `style.color-scheme`。
60
+ - 主题由 `useColorMode` 维护,默认 storage key 为 `vueboot-color-mode`,
61
+ `auto` 跟随系统。默认只维护 `.light` / `.dark` class,**不写任何第三方 UI
62
+ 框架的亮色 / 暗色 class**;如果使用方样式依赖这两个 class,不要用自定义
63
+ `onChanged` 覆盖掉它们。
64
+ - 需要自定义 storage key 或第三方亮色 / 暗色 class 时,在**首次**
65
+ `useEnvironment()` 之前配置(之后配置固定):
66
+
67
+ ```ts
68
+ setupEnvironment({
69
+ storageKey: "my-app-color-mode", // 默认 "vueboot-color-mode"
70
+ lightClass: "van-theme-light", // 默认不写
71
+ darkClass: "van-theme-dark", // 默认不写;Vant 4 用户按需开启
72
+ });
73
+ ```
74
+
75
+ 从旧版本升级时传 `storageKey: "h5.color-mode"` 可保留用户已保存的主题;
76
+ 默认 key 变更后旧偏好不会自动迁移。
77
+ - 方向判定优先级:`viewport` 可用时以实际宽高为准;与 `screen.orientation`
78
+ 一致时来源记为 `screen`,与 `window.orientation` 一致时记为
79
+ `window.orientation`,都冲突时记为 `viewport`。`viewport` 不可用才回退到
80
+ orientation API;两者都读不到时按竖屏兜底,来源记为 `fallback`(表示猜测,
81
+ 不是真实读数)。原因见 `orientation.ts` 注释(iOS / 微信 WebView 的
82
+ `screen.orientation` 可能长期停在错误值)。
83
+ - 视口尺寸读不到(`innerWidth` 与 `clientWidth` 都是 0)时,`width` / `height`
84
+ 保持为 0,不会回退到 `screen` 的物理尺寸;方向判定仍会使用 orientation API。
85
+ - 自动监听 `resize` / `orientationchange` / `pageshow` / `visualViewport.resize`;
86
+ `resize` 用 `requestAnimationFrame` 合帧,每帧最多读取一次布局;
87
+ `orientationchange` / `pageshow` 会立即读取并延迟 120ms 再补读一次。
88
+ - 内部 watcher 使用 detached `effectScope`,生命周期属于模块而非首次调用方:
89
+ 在组件 `setup()` 里首次调用后,组件卸载(路由切换、`v-if`、HMR)不会停止
90
+ 主题与方向的 DOM 同步。
91
+ - 模块在 SSR / node 下可安全 import:`canUseDOM` 为 false 时不访问 DOM,
92
+ 所有值退化为 0 / false / 竖屏。
93
+
94
+ ## useUserAgent
95
+
96
+ `UserAgentState` 字段分组:
97
+
98
+ | 分组 | 字段 |
99
+ | --- | --- |
100
+ | 平台 | `isAndroid` / `isIOS` / `isHarmony` / `isIPad` / `isIPhone` / `osName` / `osVersion` |
101
+ | 形态 | `isMobile` / `isTablet` / `isDesktop` / `isTouch` / `deviceBrand` |
102
+ | 浏览器 | `isChrome` / `isSafari` / `isEdge` / `isFirefox` / `browserName` / `browserVersion` |
103
+ | 内核 | `isWebKit` / `isBlink` / `isGecko` / `isChromium` / `kernelName` / `kernelVersion` |
104
+ | App 容器 | `isWeChat` / `isAlipay` / `isToutiao` / `isBaidu` / `isQQ` 及对应 `*Version` |
105
+ | 方法 | `refreshUserAgent()` / `compareVersion()` / `isVersionAtLeast()` / `isVersionKnown()` |
106
+
107
+ - 模块加载时解析一次 UA;`pageshow`(BFCache 恢复、页面返回)时重新解析。
108
+ - **SSR**:`readUserAgentSnapshot()` 要求真正的浏览器环境(`window` + `document`)。
109
+ Node 21+ / Deno / Bun 虽然自带 `navigator`,但仍会返回空快照(`isDesktop` 等
110
+ 全部为 false / null),不会把 `Node.js/22` 当成桌面浏览器。服务端需要按请求
111
+ UA 判断时,显式调用 `parseUserAgent({ userAgent: req.headers["user-agent"] })`;
112
+ 首屏也不要依赖 `isMobile` 决定渲染分支,否则会出现 hydration 不一致。
113
+ - UA 为空时无法判断平台 / 形态 / 浏览器,但 JS 能力探查仍会保留:`isTouch` 与
114
+ `isWeChat`(来自 WeixinJSBridge)不会因为 UA 被清空而丢失。
115
+ - `isMobile` / `isTablet` / `isDesktop` 互斥,但**不保证覆盖所有情况**:平台无法
116
+ 识别时(`curl`、`Node.js/22`、爬虫、裁剪过的 UA)三者都为 `false`,未知不会被
117
+ 当成桌面。
118
+ - Android 版本被 Chromium 110 冻结为 `10` 时,会异步用 UA-CH
119
+ `getHighEntropyValues(["platformVersion"])` 补写 `osVersion`;失败静默忽略。
120
+ `refreshUserAgent()` 返回的 Promise 在补写完成后 resolve。
121
+ - 解析结果包含:系统 / 版本、设备形态(mobile / tablet / desktop)、品牌、
122
+ 浏览器品牌与版本、渲染内核(WebKit / Blink / Gecko / EdgeHTML / ArkWeb)、
123
+ App 容器(微信 / 支付宝 / 头条 / 百度 / QQ / 微博等)。
124
+ - `isChrome` / `isEdge` 等是**品牌**布尔;内核判断用 `isWebKit` / `isBlink` /
125
+ `isGecko` / `isChromium`。iOS 上 Chrome / Edge / Firefox 都是 WebKit。
126
+ - 版本工具:`compareVersion(a, b)`(分段数值比较)、`isVersionKnown(v)`
127
+ (区分「低版本」与「拿不到版本」)、`isVersionAtLeast(v, target)`
128
+ (已随入口导出,也可从 `useUserAgent()` 上取)。
@@ -0,0 +1,140 @@
1
+ import { Orientation, OrientationSource } from './orientation.js';
2
+ import { OSName, KernelName, BrowserName, compareVersion, isVersionAtLeast, isVersionKnown } from './user-agent.js';
3
+ export { DeviceCapabilities, InAppWebViewName, UserAgentSnapshot, detectDeviceCapabilities } from './user-agent.js';
4
+
5
+ type ThemeMode = "auto" | "light" | "dark";
6
+ interface EnvironmentOptions {
7
+ /**
8
+ * 主题持久化 key,默认 `"vueboot-color-mode"`。
9
+ * 同一 origin 上的多个应用应各自使用不同的 key,避免主题互相串。
10
+ * 需要兼容旧版本时传 `"h5.color-mode"`。
11
+ */
12
+ storageKey?: string;
13
+ /**
14
+ * 额外的第三方 UI 暗色 class(例如 Vant 4 的 `"van-theme-dark"`)。
15
+ * 默认不写任何第三方 class;`.light` / `.dark` 始终由 useColorMode 维护。
16
+ */
17
+ darkClass?: string | null;
18
+ /**
19
+ * 额外的第三方 UI 亮色 class(例如 `"van-theme-light"`)。
20
+ * 默认不写;与 `darkClass` 不应相同。
21
+ */
22
+ lightClass?: string | null;
23
+ }
24
+ /**
25
+ * 初始化主题与屏幕方向。
26
+ *
27
+ * 必须在首次 `useEnvironment()` 之前调用才能生效;首次初始化后配置固定。
28
+ * 主题默认持久化在 `vueboot-color-mode`,auto 跟随系统;
29
+ * 默认只维护 `.light` / `.dark` class,第三方 UI 的亮色 / 暗色 class
30
+ * 通过 `lightClass` / `darkClass` 配置。
31
+ *
32
+ * @example
33
+ * setupEnvironment({
34
+ * storageKey: "my-app-color-mode",
35
+ * lightClass: "van-theme-light",
36
+ * darkClass: "van-theme-dark",
37
+ * });
38
+ */
39
+ declare function setupEnvironment(options?: EnvironmentOptions): void;
40
+ /**
41
+ * 只读环境状态,属性已经是解包后的值。
42
+ *
43
+ * 与 `useUserAgent` 一致:内部 ref / computed 在属性访问时自动解包,
44
+ * 模板里直接写 `env.isPortrait`,脚本里也不要再写 `.value`。
45
+ */
46
+ interface EnvironmentState {
47
+ readonly colorMode: ThemeMode;
48
+ readonly isDark: boolean;
49
+ readonly colorTheme: "light" | "dark";
50
+ readonly isPortrait: boolean;
51
+ readonly orientation: string | undefined;
52
+ readonly resolvedOrientation: Orientation;
53
+ readonly orientationSource: OrientationSource;
54
+ readonly orientationSupported: boolean;
55
+ readonly angle: number;
56
+ readonly width: number;
57
+ readonly height: number;
58
+ readonly setThemeMode: (mode: ThemeMode) => void;
59
+ }
60
+ /**
61
+ * 主题与屏幕方向状态(只读 reactive,模板里属性自动解包)。
62
+ *
63
+ * @example
64
+ * const env = useEnvironment();
65
+ * env.colorMode; // "auto" | "light" | "dark",不需要 .value
66
+ * env.isPortrait; // boolean
67
+ * env.setThemeMode("dark"); // 写入口只有这一个
68
+ *
69
+ * 主题配置(storageKey / darkClass)请使用 `setupEnvironment(options)`,
70
+ * 且必须在首次调用前执行。
71
+ */
72
+ declare function useEnvironment(): EnvironmentState;
73
+
74
+ /**
75
+ * 重新解析 UA。
76
+ *
77
+ * 桌面/移动模式切换、BFCache 恢复后 UA 可能变化;解析完再用 UA-CH 补一次 Android 版本。
78
+ */
79
+ declare function refreshUserAgent(): Promise<void>;
80
+ /** 模块级单例:UA 是全局事实,多处调用共享同一份响应式状态 */
81
+ declare const state: {
82
+ readonly userAgent: string;
83
+ readonly isAndroid: boolean;
84
+ readonly isIOS: boolean;
85
+ readonly isHarmony: boolean;
86
+ readonly isIPad: boolean;
87
+ readonly isIPhone: boolean;
88
+ readonly osName: OSName;
89
+ readonly deviceBrand: string | null;
90
+ readonly isMobile: boolean;
91
+ readonly isTablet: boolean;
92
+ readonly isDesktop: boolean;
93
+ readonly isTouch: boolean;
94
+ readonly isChrome: boolean;
95
+ readonly isSafari: boolean;
96
+ readonly isEdge: boolean;
97
+ readonly isFirefox: boolean;
98
+ readonly isWeChat: boolean;
99
+ readonly isAlipay: boolean;
100
+ readonly isToutiao: boolean;
101
+ readonly isBaidu: boolean;
102
+ readonly isQQ: boolean;
103
+ readonly wechatVersion: string | null;
104
+ readonly alipayVersion: string | null;
105
+ readonly toutiaoVersion: string | null;
106
+ readonly baiduVersion: string | null;
107
+ readonly qqVersion: string | null;
108
+ readonly kernelName: KernelName;
109
+ readonly kernelVersion: string | null;
110
+ readonly isWebKit: boolean;
111
+ readonly isBlink: boolean;
112
+ readonly isGecko: boolean;
113
+ readonly isChromium: boolean;
114
+ readonly osVersion: string | null;
115
+ readonly browserName: BrowserName;
116
+ readonly browserVersion: string | null;
117
+ readonly refreshUserAgent: typeof refreshUserAgent;
118
+ readonly compareVersion: typeof compareVersion;
119
+ readonly isVersionAtLeast: typeof isVersionAtLeast;
120
+ readonly isVersionKnown: typeof isVersionKnown;
121
+ };
122
+ /** 只读 UA 状态,属性已经是解包后的值 */
123
+ type UserAgentState = typeof state;
124
+ /**
125
+ * 基于 UA 的运行环境状态(只读 reactive,模板里属性自动解包)。
126
+ *
127
+ * 版本号形如 "16.5" / "13",无法识别时为 null。
128
+ *
129
+ * @example
130
+ * const userAgent = useUserAgent();
131
+ * userAgent.isWeChat; // boolean,不需要 .value
132
+ * userAgent.osVersion; // string | null
133
+ * await userAgent.refreshUserAgent();
134
+ *
135
+ * // 属性是解包后的值,不要解构、不要再写 .value;
136
+ * // watch 必须传 getter:watch(() => userAgent.isWeChat, cb)
137
+ */
138
+ declare function useUserAgent(): UserAgentState;
139
+
140
+ export { BrowserName, type EnvironmentOptions, type EnvironmentState, KernelName, OSName, Orientation, OrientationSource, type ThemeMode, type UserAgentState, compareVersion, isVersionAtLeast, isVersionKnown, setupEnvironment, useEnvironment, useUserAgent };