@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.
@@ -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 counter for SSR-safe ID generation
9
- let globalIdCounter = 0;
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
- * This implementation ensures that server-side rendered HTML
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
- // Track client-side hydration
29
- (0, _react.useEffect)(() => {
30
- setIsClient(true);
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
- // If we already have an ID, return it (important for consistency)
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 a new ID using the global counter
39
- globalIdCounter += 1;
40
- const newId = `${prefix}-${globalIdCounter}`;
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, useState } from 'react';
1
+ import { useRef, useCallback, useEffect } from 'react';
2
2
 
3
- // Global counter for SSR-safe ID generation
4
- let globalIdCounter = 0;
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
- * This implementation ensures that server-side rendered HTML
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
- // Track client-side hydration
24
- useEffect(() => {
25
- setIsClient(true);
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
- // If we already have an ID, return it (important for consistency)
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 a new ID using the global counter
35
- globalIdCounter += 1;
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 newId;
39
+ return idRef.current;
40
40
  }, [prefix]);
41
41
 
42
42
  return generateId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abstraks-dev/ui-library",
3
- "version": "1.1.19",
3
+ "version": "1.1.22",
4
4
  "description": "User Interface library",
5
5
  "main": "dist/index.js",
6
6
  "files": [