@slotjs/hooks 0.0.2 → 0.0.4

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/README.md CHANGED
@@ -1,8 +1,56 @@
1
1
  # @slotjs/hooks
2
2
 
3
+ `@slotjs/hooks` 目前提供 `useResponsive`,用于根据视口宽度动态设置根节点 `html` 的字体大小,适合基于 `rem` 的响应式布局。
4
+
5
+ ```bash
6
+ npm i @slotjs/hooks
7
+ ```
8
+
9
+ ## useResponsive
10
+
11
+ `useResponsive` 会在组件挂载后立即根据当前视口宽度设置一次 `html` 的 `font-size`,并在窗口尺寸变化时自动更新。
12
+
13
+ 计算规则如下:
14
+
3
15
  ```ts
16
+ html.style.fontSize = `${baseFontSize * Math.min(clientWidth / baseWidth, 2)}px`;
17
+ ```
18
+
19
+ 也就是说:
20
+
21
+ - 设计稿宽度等于 `baseWidth` 时,根字体大小为 `baseFontSize`
22
+ - 页面变宽时会按比例放大,但最大只放大到 `2x`
23
+ - 页面变窄时会按比例缩小
24
+
25
+ ### TypeScript 签名
26
+
27
+ ```ts
28
+ function useResponsive(baseWidth?: number, baseFontSize?: number): boolean;
29
+ ```
30
+
31
+ ### 参数
32
+
33
+ | 参数 | 类型 | 默认值 | 说明 |
34
+ | --- | --- | --- | --- |
35
+ | `baseWidth` | `number` | `1920` | 设计稿基准宽度 |
36
+ | `baseFontSize` | `number` | `16` | 设计稿基准宽度下的根字体大小 |
37
+
38
+ ### 返回值
39
+
40
+ 返回 `boolean` 类型的 `isReady`:
41
+
42
+ - `false`:初始字体大小尚未设置完成
43
+ - `true`:已经完成首次计算并设置根字体大小
44
+
45
+ 这个返回值适合用来控制首屏渲染,避免依赖 `rem` 的内容在初始化前出现尺寸闪动。
46
+
47
+ ### 基础用法
48
+
49
+ ```tsx
50
+ import { useResponsive } from "@slotjs/hooks";
51
+
4
52
  const Index: React.FC = () => {
5
- const isReady = useRootFontSize();
53
+ const isReady = useResponsive();
6
54
 
7
55
  return isReady ? (
8
56
  <ContentContainer>content</ContentContainer>
@@ -11,3 +59,29 @@ const Index: React.FC = () => {
11
59
  );
12
60
  };
13
61
  ```
62
+
63
+ ### 自定义设计稿宽度和基准字号
64
+
65
+ ```tsx
66
+ import { useResponsive } from "@slotjs/hooks";
67
+
68
+ const Page = () => {
69
+ const isReady = useResponsive(1440, 20);
70
+
71
+ if (!isReady) return null;
72
+
73
+ return <main style={{ fontSize: "1rem" }}>content</main>;
74
+ };
75
+ ```
76
+
77
+ 在上面的例子里:
78
+
79
+ - 当视口宽度为 `1440px` 时,`html` 根字体大小为 `20px`
80
+ - 当视口宽度为 `720px` 时,根字体大小为 `10px`
81
+ - 当视口宽度大于等于 `2880px` 时,根字体大小封顶为 `40px`
82
+
83
+ ### 使用建议
84
+
85
+ - 推荐配合 `rem` 单位使用,这样页面元素会随着根字体大小一起缩放
86
+ - 该 hook 依赖 `window` 和 `document`,应在浏览器环境中使用
87
+ - 如果项目存在 SSR,建议只在客户端渲染阶段调用
@@ -0,0 +1,21 @@
1
+ /**
2
+ * A React hook to set the root font size based on the viewport width.
3
+ * It scales the font size up to a maximum of 2x the base font size.
4
+ * This is useful for responsive designs where the font size needs to adapt to different screen sizes.
5
+ * It listens for window resize events to adjust the font size dynamically.
6
+ *
7
+ * @param baseWidth
8
+ * @param baseFontSize
9
+ * @example
10
+ * ```ts
11
+ * const isReady = useResponsive(1920, 16);
12
+ * ```
13
+ * // This will set the root font size based on a base width of 1920px
14
+ * // and a base font size of 16px.
15
+ * // The hook returns true once the font size has been set, allowing components to render correctly
16
+ * // with the appropriate font size.
17
+ * @returns
18
+ */
19
+ export declare function useResponsive(baseWidth?: number, baseFontSize?: number): boolean;
20
+
21
+ export { }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@slotjs/hooks",
3
3
  "private": false,
4
- "version": "0.0.2",
4
+ "version": "0.0.4",
5
5
  "type": "module",
6
6
  "description": "@slotjs/hooks for react",
7
7
  "homepage": "https://github.com/slotjs/hooks.git#readme",
@@ -16,6 +16,7 @@
16
16
  "author": "iamwjun",
17
17
  "scripts": {
18
18
  "dev": "vite",
19
+ "types": "tsc -p tsconfig.build.json",
19
20
  "build": "tsc -b && vite build",
20
21
  "lint": "eslint .",
21
22
  "preview": "vite preview",
@@ -30,6 +31,7 @@
30
31
  "@eslint/js": "^9.32.0",
31
32
  "@testing-library/jest-dom": "^6.6.4",
32
33
  "@testing-library/react": "^16.3.0",
34
+ "@types/node": "^24.3.0",
33
35
  "@types/react": "^19.1.9",
34
36
  "@types/react-dom": "^19.1.7",
35
37
  "@vitejs/plugin-react": "^4.7.0",
@@ -41,9 +43,21 @@
41
43
  "typescript": "~5.8.3",
42
44
  "typescript-eslint": "^8.39.0",
43
45
  "vite": "^7.1.0",
46
+ "vite-plugin-dts": "^4.5.4",
44
47
  "vitest": "^3.2.4"
45
48
  },
46
49
  "files": [
47
- "dist"
48
- ]
50
+ "dist",
51
+ "README.md"
52
+ ],
53
+ "main": "dist/index.umd.cjs",
54
+ "module": "dist/index.js",
55
+ "types": "dist/types/index.d.ts",
56
+ "exports": {
57
+ ".": {
58
+ "types": "./dist/types/index.d.ts",
59
+ "import": "./dist/index.js",
60
+ "require": "./dist/index.umd.cjs"
61
+ }
62
+ }
49
63
  }
package/dist/README.md DELETED
@@ -1,13 +0,0 @@
1
- # @slotjs/hooks
2
-
3
- ```ts
4
- const Index: React.FC = () => {
5
- const isReady = useRootFontSize();
6
-
7
- return isReady ? (
8
- <ContentContainer>content</ContentContainer>
9
- ) : (
10
- <FullScreenWrapper>loading</FullScreenWrapper>
11
- );
12
- };
13
- ```
File without changes
File without changes