@vueboot/composables 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 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,99 @@
1
- # Temporary Holding Version
1
+ # @vueboot/composables — 主题 / 屏幕方向 / User-Agent
2
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.
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"` | 最终方向与判定来源 |
51
+ | `orientation` / `angle` / `orientationSupported` | `string \| undefined` / `number` / `boolean` | `screen.orientation` 原始信息 |
52
+ | `width` / `height` | `number` | viewport 宽高(CSS 像素) |
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`,并同步 `.van-theme-dark`(Vant 4 暗色变量)。
60
+ - 主题由 `useColorMode` 维护:storage key 为 `h5.color-mode`,`auto` 跟随系统。
61
+ `useColorMode` 自身还会在 `<html>` 上维护 `.light` / `.dark` class;
62
+ 如果使用方样式依赖这两个 class,不要用自定义 `onChanged` 覆盖掉它们。
63
+ - 方向判定优先级:`viewport` 可用时以实际宽高为准;与 `screen.orientation`
64
+ 一致时来源记为 `screen`,与 `window.orientation` 一致时记为
65
+ `window.orientation`,都冲突时记为 `viewport`。`viewport` 不可用才回退到
66
+ orientation API,最后兜底竖屏。原因见 `orientation.ts` 注释(iOS / 微信
67
+ WebView 的 `screen.orientation` 可能长期停在错误值)。
68
+ - 自动监听 `resize` / `orientationchange` / `pageshow` / `visualViewport.resize`;
69
+ `orientationchange` 会延迟 120ms 再补读一次尺寸。
70
+ - 模块在 SSR / node 下可安全 import:`canUseDOM` 为 false 时不访问 DOM,
71
+ 所有值退化为 0 / false / 竖屏。
72
+
73
+ ## useUserAgent
74
+
75
+ `UserAgentState` 字段分组:
76
+
77
+ | 分组 | 字段 |
78
+ | --- | --- |
79
+ | 平台 | `isAndroid` / `isIOS` / `isHarmony` / `isIPad` / `isIPhone` / `osName` / `osVersion` |
80
+ | 形态 | `isMobile` / `isTablet` / `isDesktop` / `isTouch` / `deviceBrand` |
81
+ | 浏览器 | `isChrome` / `isSafari` / `isEdge` / `isFirefox` / `browserName` / `browserVersion` |
82
+ | 内核 | `isWebKit` / `isBlink` / `isGecko` / `isChromium` / `kernelName` / `kernelVersion` |
83
+ | App 容器 | `isWeChat` / `isAlipay` / `isToutiao` / `isBaidu` / `isQQ` 及对应 `*Version` |
84
+ | 方法 | `refreshUserAgent()` / `compareVersion()` / `isVersionAtLeast()` / `isVersionKnown()` |
85
+
86
+ - 模块加载时解析一次 UA;`pageshow`(BFCache 恢复、页面返回)时重新解析。
87
+ - UA 为空时无法判断平台 / 形态 / 浏览器,但 JS 能力探查仍会保留:`isTouch` 与
88
+ `isWeChat`(来自 WeixinJSBridge)不会因为 UA 被清空而丢失。
89
+ - Android 版本被 Chromium 110 冻结为 `10` 时,会异步用 UA-CH
90
+ `getHighEntropyValues(["platformVersion"])` 补写 `osVersion`;失败静默忽略。
91
+ `refreshUserAgent()` 返回的 Promise 在补写完成后 resolve。
92
+ - 解析结果包含:系统 / 版本、设备形态(mobile / tablet / desktop)、品牌、
93
+ 浏览器品牌与版本、渲染内核(WebKit / Blink / Gecko / EdgeHTML / ArkWeb)、
94
+ App 容器(微信 / 支付宝 / 头条 / 百度 / QQ / 微博等)。
95
+ - `isChrome` / `isEdge` 等是**品牌**布尔;内核判断用 `isWebKit` / `isBlink` /
96
+ `isGecko` / `isChromium`。iOS 上 Chrome / Edge / Firefox 都是 WebKit。
97
+ - 版本工具:`compareVersion(a, b)`(分段数值比较)、`isVersionKnown(v)`
98
+ (区分「低版本」与「拿不到版本」)、`isVersionAtLeast(v, target)`
99
+ (已随入口导出,也可从 `useUserAgent()` 上取)。
@@ -0,0 +1,107 @@
1
+ import { Orientation, OrientationSource } from './orientation.js';
2
+ import * as _vueuse_core from '@vueuse/core';
3
+ import { OSName, KernelName, BrowserName, compareVersion, isVersionAtLeast, isVersionKnown } from './user-agent.js';
4
+ export { DeviceCapabilities, InAppWebViewName, UserAgentSnapshot, detectDeviceCapabilities } from './user-agent.js';
5
+
6
+ type ThemeMode = "auto" | "light" | "dark";
7
+ /**
8
+ * 初始化主题与屏幕方向。
9
+ * 主题持久化在 h5.color-mode,auto 跟随系统;Vant 暗色由 .van-theme-dark 驱动。
10
+ */
11
+ declare function setupEnvironment(): void;
12
+ declare function setThemeMode(mode: ThemeMode): void;
13
+ /** 模块级单例:主题与方向是全局事实,多处调用共享同一份响应式状态 */
14
+ declare const state$1: {
15
+ readonly colorMode: _vueuse_core.BasicColorSchema;
16
+ readonly isDark: boolean;
17
+ readonly colorTheme: "light" | "dark";
18
+ readonly isPortrait: boolean;
19
+ readonly orientation: string | undefined;
20
+ readonly resolvedOrientation: Orientation;
21
+ readonly orientationSource: OrientationSource;
22
+ readonly orientationSupported: boolean;
23
+ readonly angle: number;
24
+ readonly width: number;
25
+ readonly height: number;
26
+ readonly setThemeMode: typeof setThemeMode;
27
+ };
28
+ /** 只读环境状态,属性已经是解包后的值 */
29
+ type EnvironmentState = typeof state$1;
30
+ /**
31
+ * 主题与屏幕方向状态(只读 reactive,模板里属性自动解包)。
32
+ *
33
+ * @example
34
+ * const env = useEnvironment();
35
+ * env.colorMode; // "auto" | "light" | "dark",不需要 .value
36
+ * env.isPortrait; // boolean
37
+ * env.setThemeMode("dark"); // 写入口只有这一个
38
+ */
39
+ declare function useEnvironment(): EnvironmentState;
40
+
41
+ /**
42
+ * 重新解析 UA。
43
+ *
44
+ * 桌面/移动模式切换、BFCache 恢复后 UA 可能变化;解析完再用 UA-CH 补一次 Android 版本。
45
+ */
46
+ declare function refreshUserAgent(): Promise<void>;
47
+ /** 模块级单例:UA 是全局事实,多处调用共享同一份响应式状态 */
48
+ declare const state: {
49
+ readonly userAgent: string;
50
+ readonly isAndroid: boolean;
51
+ readonly isIOS: boolean;
52
+ readonly isHarmony: boolean;
53
+ readonly isIPad: boolean;
54
+ readonly isIPhone: boolean;
55
+ readonly osName: OSName;
56
+ readonly deviceBrand: string | null;
57
+ readonly isMobile: boolean;
58
+ readonly isTablet: boolean;
59
+ readonly isDesktop: boolean;
60
+ readonly isTouch: boolean;
61
+ readonly isChrome: boolean;
62
+ readonly isSafari: boolean;
63
+ readonly isEdge: boolean;
64
+ readonly isFirefox: boolean;
65
+ readonly isWeChat: boolean;
66
+ readonly isAlipay: boolean;
67
+ readonly isToutiao: boolean;
68
+ readonly isBaidu: boolean;
69
+ readonly isQQ: boolean;
70
+ readonly wechatVersion: string | null;
71
+ readonly alipayVersion: string | null;
72
+ readonly toutiaoVersion: string | null;
73
+ readonly baiduVersion: string | null;
74
+ readonly qqVersion: string | null;
75
+ readonly kernelName: KernelName;
76
+ readonly kernelVersion: string | null;
77
+ readonly isWebKit: boolean;
78
+ readonly isBlink: boolean;
79
+ readonly isGecko: boolean;
80
+ readonly isChromium: boolean;
81
+ readonly osVersion: string | null;
82
+ readonly browserName: BrowserName;
83
+ readonly browserVersion: string | null;
84
+ readonly refreshUserAgent: typeof refreshUserAgent;
85
+ readonly compareVersion: typeof compareVersion;
86
+ readonly isVersionAtLeast: typeof isVersionAtLeast;
87
+ readonly isVersionKnown: typeof isVersionKnown;
88
+ };
89
+ /** 只读 UA 状态,属性已经是解包后的值 */
90
+ type UserAgentState = typeof state;
91
+ /**
92
+ * 基于 UA 的运行环境状态(只读 reactive,模板里属性自动解包)。
93
+ *
94
+ * 版本号形如 "16.5" / "13",无法识别时为 null。
95
+ *
96
+ * @example
97
+ * const userAgent = useUserAgent();
98
+ * userAgent.isWeChat; // boolean,不需要 .value
99
+ * userAgent.osVersion; // string | null
100
+ * await userAgent.refreshUserAgent();
101
+ *
102
+ * // 属性是解包后的值,不要解构、不要再写 .value;
103
+ * // watch 必须传 getter:watch(() => userAgent.isWeChat, cb)
104
+ */
105
+ declare function useUserAgent(): UserAgentState;
106
+
107
+ export { BrowserName, type EnvironmentState, KernelName, OSName, Orientation, OrientationSource, type ThemeMode, type UserAgentState, compareVersion, isVersionAtLeast, isVersionKnown, setupEnvironment, useEnvironment, useUserAgent };