@abstraks-dev/ui-library 1.1.20 → 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.
@@ -3,50 +3,43 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.useStableId = exports.resetCounter = exports.generateStableId = exports.default = void 0;
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';
10
11
 
11
- // Reset mechanism to ensure consistent starting point
12
- const resetCounter = () => {
13
- globalIdCounter = 0;
14
- };
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
+ }
15
17
 
16
- // Export reset function for testing and manual resets
17
- exports.resetCounter = resetCounter;
18
18
  /**
19
19
  * A hook that provides SSR-safe ID generation.
20
- * This implementation ensures that server-side rendered HTML
21
- * matches client-side rendered HTML, preventing hydration mismatches.
22
- *
23
- * The hook works by:
24
- * 1. Using a consistent global counter
25
- * 2. Generating the same ID for each component on every render
26
- * 3. Using useRef to cache the ID once generated
20
+ * Ensures consistent ID generation between server and client renders.
27
21
  *
28
22
  * @param {string} prefix - A prefix for the generated ID (default: 'id')
29
23
  * @returns {function} A function that generates unique IDs with the given prefix
30
24
  */
31
25
  const useStableId = (prefix = 'id') => {
32
26
  const idRef = (0, _react.useRef)(null);
33
- const [isHydrated, setIsHydrated] = (0, _react.useState)(false);
27
+ const assignedIdRef = (0, _react.useRef)(null);
34
28
 
35
- // Track hydration completion
36
- (0, _react.useEffect)(() => {
37
- setIsHydrated(true);
38
- }, []);
29
+ // Assign ID on first render only
30
+ if (assignedIdRef.current === null) {
31
+ idCounter += 1;
32
+ assignedIdRef.current = idCounter;
33
+ }
39
34
  const generateId = (0, _react.useCallback)(() => {
40
- // Always return the same ID if we already have one
35
+ // Return cached ID if available
41
36
  if (idRef.current !== null) {
42
37
  return idRef.current;
43
38
  }
44
39
 
45
- // Generate new ID using global counter
46
- globalIdCounter += 1;
47
- const newId = `${prefix}-${globalIdCounter}`;
48
- idRef.current = newId;
49
- return newId;
40
+ // Generate ID using the assigned counter value
41
+ idRef.current = `${prefix}-${assignedIdRef.current}`;
42
+ return idRef.current;
50
43
  }, [prefix]);
51
44
  return generateId;
52
45
  };
@@ -1,50 +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';
5
6
 
6
- // Reset mechanism to ensure consistent starting point
7
- const resetCounter = () => {
8
- globalIdCounter = 0;
9
- };
10
-
11
- // Export reset function for testing and manual resets
12
- export { resetCounter };
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
+ }
13
12
 
14
13
  /**
15
14
  * A hook that provides SSR-safe ID generation.
16
- * This implementation ensures that server-side rendered HTML
17
- * matches client-side rendered HTML, preventing hydration mismatches.
18
- *
19
- * The hook works by:
20
- * 1. Using a consistent global counter
21
- * 2. Generating the same ID for each component on every render
22
- * 3. Using useRef to cache the ID once generated
15
+ * Ensures consistent ID generation between server and client renders.
23
16
  *
24
17
  * @param {string} prefix - A prefix for the generated ID (default: 'id')
25
18
  * @returns {function} A function that generates unique IDs with the given prefix
26
19
  */
27
20
  export const useStableId = (prefix = 'id') => {
28
21
  const idRef = useRef(null);
29
- const [isHydrated, setIsHydrated] = useState(false);
22
+ const assignedIdRef = useRef(null);
30
23
 
31
- // Track hydration completion
32
- useEffect(() => {
33
- setIsHydrated(true);
34
- }, []);
24
+ // Assign ID on first render only
25
+ if (assignedIdRef.current === null) {
26
+ idCounter += 1;
27
+ assignedIdRef.current = idCounter;
28
+ }
35
29
 
36
30
  const generateId = useCallback(() => {
37
- // Always return the same ID if we already have one
31
+ // Return cached ID if available
38
32
  if (idRef.current !== null) {
39
33
  return idRef.current;
40
34
  }
41
35
 
42
- // Generate new ID using global counter
43
- globalIdCounter += 1;
44
- const newId = `${prefix}-${globalIdCounter}`;
45
- idRef.current = newId;
36
+ // Generate ID using the assigned counter value
37
+ idRef.current = `${prefix}-${assignedIdRef.current}`;
46
38
 
47
- return newId;
39
+ return idRef.current;
48
40
  }, [prefix]);
49
41
 
50
42
  return generateId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abstraks-dev/ui-library",
3
- "version": "1.1.20",
3
+ "version": "1.1.22",
4
4
  "description": "User Interface library",
5
5
  "main": "dist/index.js",
6
6
  "files": [