@abstraks-dev/ui-library 1.1.19 → 1.1.22
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/utils/ssrSafeId.js +21 -21
- package/dist/utils/utils/ssrSafeId.js +22 -22
- package/package.json +1 -1
package/dist/utils/ssrSafeId.js
CHANGED
|
@@ -5,41 +5,41 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.useStableId = exports.generateStableId = exports.default = void 0;
|
|
7
7
|
var _react = require("react");
|
|
8
|
-
// Global
|
|
9
|
-
let
|
|
8
|
+
// Global state for ID generation
|
|
9
|
+
let idCounter = 0;
|
|
10
|
+
let isServer = typeof window === 'undefined';
|
|
11
|
+
|
|
12
|
+
// Reset counter on server for each request, maintain on client
|
|
13
|
+
if (isServer) {
|
|
14
|
+
// Reset counter for each server render
|
|
15
|
+
idCounter = 0;
|
|
16
|
+
}
|
|
10
17
|
|
|
11
18
|
/**
|
|
12
19
|
* A hook that provides SSR-safe ID generation.
|
|
13
|
-
*
|
|
14
|
-
* matches client-side rendered HTML, preventing hydration mismatches.
|
|
15
|
-
*
|
|
16
|
-
* The hook works by:
|
|
17
|
-
* 1. Using a global counter that's consistent across server/client
|
|
18
|
-
* 2. Generating deterministic IDs during SSR
|
|
19
|
-
* 3. Maintaining the same ID sequence during hydration
|
|
20
|
+
* Ensures consistent ID generation between server and client renders.
|
|
20
21
|
*
|
|
21
22
|
* @param {string} prefix - A prefix for the generated ID (default: 'id')
|
|
22
23
|
* @returns {function} A function that generates unique IDs with the given prefix
|
|
23
24
|
*/
|
|
24
25
|
const useStableId = (prefix = 'id') => {
|
|
25
|
-
const [isClient, setIsClient] = (0, _react.useState)(false);
|
|
26
26
|
const idRef = (0, _react.useRef)(null);
|
|
27
|
+
const assignedIdRef = (0, _react.useRef)(null);
|
|
27
28
|
|
|
28
|
-
//
|
|
29
|
-
(
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
// Assign ID on first render only
|
|
30
|
+
if (assignedIdRef.current === null) {
|
|
31
|
+
idCounter += 1;
|
|
32
|
+
assignedIdRef.current = idCounter;
|
|
33
|
+
}
|
|
32
34
|
const generateId = (0, _react.useCallback)(() => {
|
|
33
|
-
//
|
|
34
|
-
if (idRef.current) {
|
|
35
|
+
// Return cached ID if available
|
|
36
|
+
if (idRef.current !== null) {
|
|
35
37
|
return idRef.current;
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
// Generate
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
idRef.current = newId;
|
|
42
|
-
return newId;
|
|
40
|
+
// Generate ID using the assigned counter value
|
|
41
|
+
idRef.current = `${prefix}-${assignedIdRef.current}`;
|
|
42
|
+
return idRef.current;
|
|
43
43
|
}, [prefix]);
|
|
44
44
|
return generateId;
|
|
45
45
|
};
|
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import { useRef, useCallback, useEffect
|
|
1
|
+
import { useRef, useCallback, useEffect } from 'react';
|
|
2
2
|
|
|
3
|
-
// Global
|
|
4
|
-
let
|
|
3
|
+
// Global state for ID generation
|
|
4
|
+
let idCounter = 0;
|
|
5
|
+
let isServer = typeof window === 'undefined';
|
|
6
|
+
|
|
7
|
+
// Reset counter on server for each request, maintain on client
|
|
8
|
+
if (isServer) {
|
|
9
|
+
// Reset counter for each server render
|
|
10
|
+
idCounter = 0;
|
|
11
|
+
}
|
|
5
12
|
|
|
6
13
|
/**
|
|
7
14
|
* A hook that provides SSR-safe ID generation.
|
|
8
|
-
*
|
|
9
|
-
* matches client-side rendered HTML, preventing hydration mismatches.
|
|
10
|
-
*
|
|
11
|
-
* The hook works by:
|
|
12
|
-
* 1. Using a global counter that's consistent across server/client
|
|
13
|
-
* 2. Generating deterministic IDs during SSR
|
|
14
|
-
* 3. Maintaining the same ID sequence during hydration
|
|
15
|
+
* Ensures consistent ID generation between server and client renders.
|
|
15
16
|
*
|
|
16
17
|
* @param {string} prefix - A prefix for the generated ID (default: 'id')
|
|
17
18
|
* @returns {function} A function that generates unique IDs with the given prefix
|
|
18
19
|
*/
|
|
19
20
|
export const useStableId = (prefix = 'id') => {
|
|
20
|
-
const [isClient, setIsClient] = useState(false);
|
|
21
21
|
const idRef = useRef(null);
|
|
22
|
+
const assignedIdRef = useRef(null);
|
|
22
23
|
|
|
23
|
-
//
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
// Assign ID on first render only
|
|
25
|
+
if (assignedIdRef.current === null) {
|
|
26
|
+
idCounter += 1;
|
|
27
|
+
assignedIdRef.current = idCounter;
|
|
28
|
+
}
|
|
27
29
|
|
|
28
30
|
const generateId = useCallback(() => {
|
|
29
|
-
//
|
|
30
|
-
if (idRef.current) {
|
|
31
|
+
// Return cached ID if available
|
|
32
|
+
if (idRef.current !== null) {
|
|
31
33
|
return idRef.current;
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
// Generate
|
|
35
|
-
|
|
36
|
-
const newId = `${prefix}-${globalIdCounter}`;
|
|
37
|
-
idRef.current = newId;
|
|
36
|
+
// Generate ID using the assigned counter value
|
|
37
|
+
idRef.current = `${prefix}-${assignedIdRef.current}`;
|
|
38
38
|
|
|
39
|
-
return
|
|
39
|
+
return idRef.current;
|
|
40
40
|
}, [prefix]);
|
|
41
41
|
|
|
42
42
|
return generateId;
|