@rspress/runtime 2.0.0-rc.1 → 2.0.0-rc.2
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/dist/hooks/useWindowSize.js +14 -5
- package/dist/hooks/useWindowSize.test.js +36 -0
- package/package.json +8 -14
- package/server.d.ts +0 -2
- package/server.js +0 -2
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
import { useLayoutEffect, useState } from "react";
|
|
2
|
+
const RESIZE_DEBOUNCE_MS = 150;
|
|
2
3
|
function useWindowSize(initialWidth, initialHeight) {
|
|
3
4
|
const [size, setSize] = useState({
|
|
4
5
|
width: initialWidth ?? 1 / 0,
|
|
5
6
|
height: initialHeight ?? 1 / 0
|
|
6
7
|
});
|
|
7
8
|
useLayoutEffect(()=>{
|
|
9
|
+
let timeoutId = null;
|
|
8
10
|
const handleResize = ()=>{
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
12
|
+
timeoutId = setTimeout(()=>{
|
|
13
|
+
setSize({
|
|
14
|
+
width: window.innerWidth,
|
|
15
|
+
height: window.innerHeight
|
|
16
|
+
});
|
|
17
|
+
}, RESIZE_DEBOUNCE_MS);
|
|
13
18
|
};
|
|
14
|
-
|
|
19
|
+
setSize({
|
|
20
|
+
width: window.innerWidth,
|
|
21
|
+
height: window.innerHeight
|
|
22
|
+
});
|
|
15
23
|
window.addEventListener('resize', handleResize);
|
|
16
24
|
return ()=>{
|
|
25
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
17
26
|
window.removeEventListener('resize', handleResize);
|
|
18
27
|
};
|
|
19
28
|
}, []);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
describe('useWindowSize', ()=>{
|
|
5
|
+
it('should have debounce implementation with RESIZE_DEBOUNCE_MS constant', ()=>{
|
|
6
|
+
const filePath = join(__dirname, 'useWindowSize.ts');
|
|
7
|
+
const fileContent = readFileSync(filePath, 'utf-8');
|
|
8
|
+
expect(fileContent).toContain('const RESIZE_DEBOUNCE_MS = 150');
|
|
9
|
+
expect(fileContent).toContain('setTimeout');
|
|
10
|
+
expect(fileContent).toContain('RESIZE_DEBOUNCE_MS');
|
|
11
|
+
});
|
|
12
|
+
it('should clear timeout with conditional check', ()=>{
|
|
13
|
+
const filePath = join(__dirname, 'useWindowSize.ts');
|
|
14
|
+
const fileContent = readFileSync(filePath, 'utf-8');
|
|
15
|
+
expect(fileContent).toContain('if (timeoutId) clearTimeout(timeoutId)');
|
|
16
|
+
expect(fileContent).not.toContain('clearTimeout(timeoutId!)');
|
|
17
|
+
});
|
|
18
|
+
it('should register and cleanup resize event listener', ()=>{
|
|
19
|
+
const filePath = join(__dirname, 'useWindowSize.ts');
|
|
20
|
+
const fileContent = readFileSync(filePath, 'utf-8');
|
|
21
|
+
expect(fileContent).toContain("addEventListener('resize'");
|
|
22
|
+
expect(fileContent).toContain("removeEventListener('resize'");
|
|
23
|
+
});
|
|
24
|
+
it('should set initial size immediately', ()=>{
|
|
25
|
+
const filePath = join(__dirname, 'useWindowSize.ts');
|
|
26
|
+
const fileContent = readFileSync(filePath, 'utf-8');
|
|
27
|
+
const setStateMatch = fileContent.match(/setSize\(\{[^}]+\}\)/g);
|
|
28
|
+
expect(setStateMatch).toBeTruthy();
|
|
29
|
+
expect(setStateMatch?.length).toBeGreaterThanOrEqual(2);
|
|
30
|
+
});
|
|
31
|
+
it('should export useWindowSize function', async ()=>{
|
|
32
|
+
const module = await import("./useWindowSize.js");
|
|
33
|
+
expect(module.useWindowSize).toBeDefined();
|
|
34
|
+
expect(typeof module.useWindowSize).toBe('function');
|
|
35
|
+
});
|
|
36
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rspress/runtime",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.2",
|
|
4
4
|
"description": "The Runtime of Rspress Documentation Framework",
|
|
5
5
|
"bugs": "https://github.com/web-infra-dev/rspress/issues",
|
|
6
6
|
"repository": {
|
|
@@ -20,29 +20,23 @@
|
|
|
20
20
|
".": {
|
|
21
21
|
"types": "./dist/index.d.ts",
|
|
22
22
|
"default": "./dist/index.js"
|
|
23
|
-
},
|
|
24
|
-
"./server": {
|
|
25
|
-
"types": "./server.d.ts",
|
|
26
|
-
"default": "./server.js"
|
|
27
23
|
}
|
|
28
24
|
},
|
|
29
25
|
"types": "./dist/index.d.ts",
|
|
30
26
|
"files": [
|
|
31
|
-
"dist"
|
|
32
|
-
"server.js",
|
|
33
|
-
"server.d.ts"
|
|
27
|
+
"dist"
|
|
34
28
|
],
|
|
35
29
|
"dependencies": {
|
|
36
30
|
"@unhead/react": "^2.0.19",
|
|
37
|
-
"react": "^19.2.
|
|
38
|
-
"react-dom": "^19.2.
|
|
39
|
-
"react-router-dom": "^
|
|
40
|
-
"@rspress/shared": "2.0.0-rc.
|
|
31
|
+
"react": "^19.2.1",
|
|
32
|
+
"react-dom": "^19.2.1",
|
|
33
|
+
"react-router-dom": "^7.10.1",
|
|
34
|
+
"@rspress/shared": "2.0.0-rc.2"
|
|
41
35
|
},
|
|
42
36
|
"devDependencies": {
|
|
43
37
|
"@rsbuild/plugin-react": "~1.4.2",
|
|
44
|
-
"@rslib/core": "0.
|
|
45
|
-
"@types/react": "^19.2.
|
|
38
|
+
"@rslib/core": "0.18.3",
|
|
39
|
+
"@types/react": "^19.2.7",
|
|
46
40
|
"@types/react-dom": "^19.2.3",
|
|
47
41
|
"rsbuild-plugin-publint": "^0.3.3",
|
|
48
42
|
"typescript": "^5.8.2",
|
package/server.d.ts
DELETED
package/server.js
DELETED