@vueboot/composables 0.1.0 → 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 +21 -21
- package/README.md +127 -98
- package/dist/index.d.ts +45 -12
- package/dist/index.js +120 -57
- package/dist/index.js.map +1 -1
- package/dist/orientation.d.ts +1 -1
- package/dist/orientation.js +1 -1
- package/dist/orientation.js.map +1 -1
- package/dist/user-agent.d.ts +10 -1
- package/dist/user-agent.js +7 -3
- package/dist/user-agent.js.map +1 -1
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,21 +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.
|
|
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,99 +1,128 @@
|
|
|
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"` | 最终方向与判定来源 |
|
|
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
|
|
60
|
-
- 主题由 `useColorMode`
|
|
61
|
-
`
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
`
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
`
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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)`
|
|
99
128
|
(已随入口导出,也可从 `useUserAgent()` 上取)。
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,50 @@
|
|
|
1
1
|
import { Orientation, OrientationSource } from './orientation.js';
|
|
2
|
-
import * as _vueuse_core from '@vueuse/core';
|
|
3
2
|
import { OSName, KernelName, BrowserName, compareVersion, isVersionAtLeast, isVersionKnown } from './user-agent.js';
|
|
4
3
|
export { DeviceCapabilities, InAppWebViewName, UserAgentSnapshot, detectDeviceCapabilities } from './user-agent.js';
|
|
5
4
|
|
|
6
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
|
+
}
|
|
7
24
|
/**
|
|
8
25
|
* 初始化主题与屏幕方向。
|
|
9
|
-
*
|
|
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`。
|
|
10
45
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/** 模块级单例:主题与方向是全局事实,多处调用共享同一份响应式状态 */
|
|
14
|
-
declare const state$1: {
|
|
15
|
-
readonly colorMode: _vueuse_core.BasicColorSchema;
|
|
46
|
+
interface EnvironmentState {
|
|
47
|
+
readonly colorMode: ThemeMode;
|
|
16
48
|
readonly isDark: boolean;
|
|
17
49
|
readonly colorTheme: "light" | "dark";
|
|
18
50
|
readonly isPortrait: boolean;
|
|
@@ -23,10 +55,8 @@ declare const state$1: {
|
|
|
23
55
|
readonly angle: number;
|
|
24
56
|
readonly width: number;
|
|
25
57
|
readonly height: number;
|
|
26
|
-
readonly setThemeMode:
|
|
27
|
-
}
|
|
28
|
-
/** 只读环境状态,属性已经是解包后的值 */
|
|
29
|
-
type EnvironmentState = typeof state$1;
|
|
58
|
+
readonly setThemeMode: (mode: ThemeMode) => void;
|
|
59
|
+
}
|
|
30
60
|
/**
|
|
31
61
|
* 主题与屏幕方向状态(只读 reactive,模板里属性自动解包)。
|
|
32
62
|
*
|
|
@@ -35,6 +65,9 @@ type EnvironmentState = typeof state$1;
|
|
|
35
65
|
* env.colorMode; // "auto" | "light" | "dark",不需要 .value
|
|
36
66
|
* env.isPortrait; // boolean
|
|
37
67
|
* env.setThemeMode("dark"); // 写入口只有这一个
|
|
68
|
+
*
|
|
69
|
+
* 主题配置(storageKey / darkClass)请使用 `setupEnvironment(options)`,
|
|
70
|
+
* 且必须在首次调用前执行。
|
|
38
71
|
*/
|
|
39
72
|
declare function useEnvironment(): EnvironmentState;
|
|
40
73
|
|
|
@@ -104,4 +137,4 @@ type UserAgentState = typeof state;
|
|
|
104
137
|
*/
|
|
105
138
|
declare function useUserAgent(): UserAgentState;
|
|
106
139
|
|
|
107
|
-
export { BrowserName, type EnvironmentState, KernelName, OSName, Orientation, OrientationSource, type ThemeMode, type UserAgentState, compareVersion, isVersionAtLeast, isVersionKnown, setupEnvironment, useEnvironment, useUserAgent };
|
|
140
|
+
export { BrowserName, type EnvironmentOptions, type EnvironmentState, KernelName, OSName, Orientation, OrientationSource, type ThemeMode, type UserAgentState, compareVersion, isVersionAtLeast, isVersionKnown, setupEnvironment, useEnvironment, useUserAgent };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { computed,
|
|
2
|
-
import {
|
|
1
|
+
import { ref, computed, shallowRef, readonly, reactive, effectScope, watch } from 'vue';
|
|
2
|
+
import { usePreferredDark, useEventListener, useColorMode } from '@vueuse/core';
|
|
3
3
|
|
|
4
4
|
// src/useEnvironment.ts
|
|
5
5
|
|
|
@@ -36,17 +36,27 @@ function resolveOrientation(input) {
|
|
|
36
36
|
}
|
|
37
37
|
if (screenValue) return { value: screenValue, source: "screen" };
|
|
38
38
|
if (legacyValue) return { value: legacyValue, source: "window.orientation" };
|
|
39
|
-
return { value: "portrait", source: "
|
|
39
|
+
return { value: "portrait", source: "fallback" };
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
// src/useEnvironment.ts
|
|
43
|
+
var DEFAULT_STORAGE_KEY = "vueboot-color-mode";
|
|
43
44
|
var canUseDOM = typeof window !== "undefined" && typeof document !== "undefined";
|
|
45
|
+
var canUseAnimationFrame = canUseDOM && typeof window.requestAnimationFrame === "function";
|
|
46
|
+
function requestViewportFrame(cb) {
|
|
47
|
+
if (canUseAnimationFrame) return window.requestAnimationFrame(cb);
|
|
48
|
+
return setTimeout(() => cb(performance.now()), 16);
|
|
49
|
+
}
|
|
50
|
+
function cancelViewportFrame(id) {
|
|
51
|
+
if (canUseAnimationFrame) window.cancelAnimationFrame(id);
|
|
52
|
+
else clearTimeout(id);
|
|
53
|
+
}
|
|
44
54
|
function readViewportSize() {
|
|
45
55
|
if (!canUseDOM) return { width: 0, height: 0 };
|
|
46
56
|
const root = document.documentElement;
|
|
47
57
|
return {
|
|
48
|
-
width: window.innerWidth || root.clientWidth ||
|
|
49
|
-
height: window.innerHeight || root.clientHeight ||
|
|
58
|
+
width: window.innerWidth || root.clientWidth || 0,
|
|
59
|
+
height: window.innerHeight || root.clientHeight || 0
|
|
50
60
|
};
|
|
51
61
|
}
|
|
52
62
|
function readLegacyOrientation() {
|
|
@@ -73,16 +83,11 @@ function readScreenOrientation() {
|
|
|
73
83
|
}
|
|
74
84
|
return { type: void 0, angle: 0, supported: false };
|
|
75
85
|
}
|
|
76
|
-
var colorMode = useColorMode({
|
|
77
|
-
storageKey: "h5.color-mode",
|
|
78
|
-
initialValue: "auto",
|
|
79
|
-
emitAuto: true
|
|
80
|
-
});
|
|
81
86
|
var preferredDark = usePreferredDark();
|
|
82
|
-
var
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
var
|
|
87
|
+
var colorMode;
|
|
88
|
+
var isDark;
|
|
89
|
+
var darkThemeClass;
|
|
90
|
+
var lightThemeClass;
|
|
86
91
|
var viewport = ref(readViewportSize());
|
|
87
92
|
var screenOrientation = ref(
|
|
88
93
|
readScreenOrientation()
|
|
@@ -107,7 +112,9 @@ var resolvedOrientation = computed(() => resolved.value.value);
|
|
|
107
112
|
var orientationSource = computed(() => resolved.value.source);
|
|
108
113
|
var isPortrait = computed(() => resolvedOrientation.value === "portrait");
|
|
109
114
|
var refreshTimer;
|
|
115
|
+
var viewportFrame;
|
|
110
116
|
var stopEnvironmentWatch;
|
|
117
|
+
var environmentScope;
|
|
111
118
|
function refreshViewport() {
|
|
112
119
|
viewport.value = readViewportSize();
|
|
113
120
|
legacyWindowOrientation.value = readLegacyOrientation();
|
|
@@ -118,17 +125,32 @@ function refreshOrientation() {
|
|
|
118
125
|
if (refreshTimer) clearTimeout(refreshTimer);
|
|
119
126
|
refreshTimer = setTimeout(refreshViewport, 120);
|
|
120
127
|
}
|
|
128
|
+
function refreshViewportOnNextFrame() {
|
|
129
|
+
if (!canUseDOM) return;
|
|
130
|
+
if (viewportFrame !== void 0) {
|
|
131
|
+
cancelViewportFrame(viewportFrame);
|
|
132
|
+
}
|
|
133
|
+
viewportFrame = requestViewportFrame(() => {
|
|
134
|
+
viewportFrame = void 0;
|
|
135
|
+
refreshViewport();
|
|
136
|
+
});
|
|
137
|
+
}
|
|
121
138
|
if (canUseDOM) {
|
|
122
139
|
const stops = [
|
|
123
140
|
useEventListener(window, "orientationchange", refreshOrientation, {
|
|
124
141
|
passive: true
|
|
125
142
|
}),
|
|
126
|
-
useEventListener(window, "resize",
|
|
143
|
+
useEventListener(window, "resize", refreshViewportOnNextFrame, {
|
|
144
|
+
passive: true
|
|
145
|
+
}),
|
|
127
146
|
useEventListener(window, "pageshow", refreshOrientation, { passive: true }),
|
|
128
147
|
...window.visualViewport ? [
|
|
129
|
-
useEventListener(
|
|
130
|
-
|
|
131
|
-
|
|
148
|
+
useEventListener(
|
|
149
|
+
window.visualViewport,
|
|
150
|
+
"resize",
|
|
151
|
+
refreshViewportOnNextFrame,
|
|
152
|
+
{ passive: true }
|
|
153
|
+
)
|
|
132
154
|
] : []
|
|
133
155
|
];
|
|
134
156
|
const hot = import.meta.hot;
|
|
@@ -138,66 +160,103 @@ if (canUseDOM) {
|
|
|
138
160
|
clearTimeout(refreshTimer);
|
|
139
161
|
refreshTimer = void 0;
|
|
140
162
|
}
|
|
163
|
+
if (viewportFrame !== void 0) {
|
|
164
|
+
cancelViewportFrame(viewportFrame);
|
|
165
|
+
viewportFrame = void 0;
|
|
166
|
+
}
|
|
141
167
|
stopEnvironmentWatch?.();
|
|
168
|
+
environmentScope?.stop();
|
|
142
169
|
stopEnvironmentWatch = void 0;
|
|
170
|
+
environmentScope = void 0;
|
|
143
171
|
});
|
|
144
172
|
}
|
|
145
173
|
var initialized = false;
|
|
146
174
|
function applyEnvironment() {
|
|
147
|
-
if (!canUseDOM) return;
|
|
175
|
+
if (!canUseDOM || !isDark) return;
|
|
148
176
|
const root = document.documentElement || null;
|
|
149
177
|
if (root) {
|
|
150
178
|
root.dataset.theme = isDark.value ? "dark" : "light";
|
|
151
179
|
root.dataset.orientation = resolvedOrientation.value;
|
|
152
180
|
root.dataset.orientationSource = orientationSource.value;
|
|
153
181
|
root.style.colorScheme = isDark.value ? "dark" : "light";
|
|
154
|
-
|
|
182
|
+
const dark = isDark.value;
|
|
183
|
+
if (darkThemeClass) {
|
|
184
|
+
root.classList.toggle(darkThemeClass, dark);
|
|
185
|
+
}
|
|
186
|
+
if (lightThemeClass && lightThemeClass !== darkThemeClass) {
|
|
187
|
+
root.classList.toggle(lightThemeClass, !dark);
|
|
188
|
+
}
|
|
155
189
|
}
|
|
156
190
|
}
|
|
157
|
-
function ensureEnvironment() {
|
|
191
|
+
function ensureEnvironment(options) {
|
|
158
192
|
if (initialized) return;
|
|
159
193
|
initialized = true;
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
194
|
+
const storageKey = options?.storageKey ?? DEFAULT_STORAGE_KEY;
|
|
195
|
+
darkThemeClass = options?.darkClass || void 0;
|
|
196
|
+
lightThemeClass = options?.lightClass || void 0;
|
|
197
|
+
environmentScope = effectScope(true);
|
|
198
|
+
environmentScope.run(() => {
|
|
199
|
+
const mode = useColorMode({
|
|
200
|
+
storageKey,
|
|
201
|
+
initialValue: "auto",
|
|
202
|
+
emitAuto: true
|
|
203
|
+
});
|
|
204
|
+
const dark = computed(
|
|
205
|
+
() => mode.value === "auto" ? preferredDark.value : mode.value === "dark"
|
|
206
|
+
);
|
|
207
|
+
const theme = computed(
|
|
208
|
+
() => dark.value ? "dark" : "light"
|
|
209
|
+
);
|
|
210
|
+
colorMode = mode;
|
|
211
|
+
isDark = dark;
|
|
212
|
+
state = createEnvironmentState(mode, dark, theme);
|
|
213
|
+
stopEnvironmentWatch = watch(
|
|
214
|
+
[
|
|
215
|
+
mode,
|
|
216
|
+
dark,
|
|
217
|
+
theme,
|
|
218
|
+
resolved,
|
|
219
|
+
viewportWidth,
|
|
220
|
+
viewportHeight,
|
|
221
|
+
legacyWindowOrientation,
|
|
222
|
+
screenOrientation,
|
|
223
|
+
angle
|
|
224
|
+
],
|
|
225
|
+
applyEnvironment,
|
|
226
|
+
{ immediate: true }
|
|
227
|
+
);
|
|
228
|
+
});
|
|
175
229
|
}
|
|
176
|
-
function setupEnvironment() {
|
|
177
|
-
ensureEnvironment();
|
|
230
|
+
function setupEnvironment(options) {
|
|
231
|
+
ensureEnvironment(options);
|
|
178
232
|
}
|
|
179
233
|
function setThemeMode(mode) {
|
|
234
|
+
if (!colorMode) return;
|
|
180
235
|
colorMode.value = mode === "auto" ? "auto" : mode === "dark" ? "dark" : "light";
|
|
181
236
|
}
|
|
182
|
-
function createEnvironmentState() {
|
|
183
|
-
return
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
237
|
+
function createEnvironmentState(colorModeRef, isDarkRef, colorThemeRef) {
|
|
238
|
+
return readonly(
|
|
239
|
+
reactive({
|
|
240
|
+
// useColorMode 的 ref 类型比 ThemeMode 宽,这里收窄为公共类型
|
|
241
|
+
colorMode: computed(() => colorModeRef.value),
|
|
242
|
+
isDark: isDarkRef,
|
|
243
|
+
colorTheme: colorThemeRef,
|
|
244
|
+
isPortrait,
|
|
245
|
+
orientation: screenType,
|
|
246
|
+
resolvedOrientation,
|
|
247
|
+
orientationSource,
|
|
248
|
+
orientationSupported,
|
|
249
|
+
angle,
|
|
250
|
+
width: viewportWidth,
|
|
251
|
+
height: viewportHeight,
|
|
252
|
+
setThemeMode
|
|
253
|
+
})
|
|
254
|
+
);
|
|
197
255
|
}
|
|
198
|
-
var state
|
|
256
|
+
var state;
|
|
199
257
|
function useEnvironment() {
|
|
200
258
|
ensureEnvironment();
|
|
259
|
+
if (!state) throw new Error("useEnvironment \u521D\u59CB\u5316\u5931\u8D25");
|
|
201
260
|
return state;
|
|
202
261
|
}
|
|
203
262
|
|
|
@@ -262,7 +321,9 @@ var BROWSER_SKINS = [
|
|
|
262
321
|
/\bOPR\/([\d.]+)/i,
|
|
263
322
|
/\bOPiOS\/([\d.]+)/i,
|
|
264
323
|
/\bOpera\s?(?:Mini|Mobi)\/([\d.]+)/i,
|
|
265
|
-
|
|
324
|
+
// 有界匹配:真实 UA 中 Opera 与 Version/ 的间距很短,
|
|
325
|
+
// 无界 .+? 在重复 "Opera" 的长输入上会退化为 O(k·n)
|
|
326
|
+
/\bOpera\b[^]{0,128}?\bVersion\/([\d.]+)/i,
|
|
266
327
|
/\bOpera[\/ ]([\d.]+)/i
|
|
267
328
|
]
|
|
268
329
|
},
|
|
@@ -569,7 +630,7 @@ function parseUserAgent(input) {
|
|
|
569
630
|
const isAndroidTablet = isAndroid && !/Mobile/i.test(ua) && !isNarrowScreen;
|
|
570
631
|
const isTablet = isIPad || /Tablet/i.test(ua) || isAndroidTablet && !isHarmonyDesktop;
|
|
571
632
|
const isMobile = !isTablet && !isHarmonyDesktop && (isIOS || isAndroid || isHarmony || /Windows Phone|IEMobile/i.test(ua));
|
|
572
|
-
const isDesktop = !isMobile && !isTablet;
|
|
633
|
+
const isDesktop = !isMobile && !isTablet && osName !== null;
|
|
573
634
|
const isWeChat = /MicroMessenger/i.test(ua) || input.hasWeixinBridge === true;
|
|
574
635
|
const isAlipay = /AlipayClient/i.test(ua) || /AliApp\(AP\//i.test(ua);
|
|
575
636
|
const isToutiao = /NewsArticle\//i.test(ua) || /ToutiaoMicroApp\//i.test(ua);
|
|
@@ -670,7 +731,9 @@ function parseUserAgent(input) {
|
|
|
670
731
|
};
|
|
671
732
|
}
|
|
672
733
|
function readUserAgentSnapshot() {
|
|
673
|
-
if (typeof navigator === "undefined"
|
|
734
|
+
if (typeof navigator === "undefined" || typeof window === "undefined" || typeof document === "undefined") {
|
|
735
|
+
return { ...EMPTY_SNAPSHOT };
|
|
736
|
+
}
|
|
674
737
|
const capabilities = detectDeviceCapabilities();
|
|
675
738
|
return parseUserAgent({
|
|
676
739
|
userAgent: navigator.userAgent ?? "",
|