useid-polyfill 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Dermot Hughes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # useid-polyfill
2
+
3
+ A React `useId` polyfill for React 16 and 17, with SSR support.
4
+
5
+ React 18 introduced `useId()` for generating unique, stable IDs — critical for accessibility (ARIA attributes, label associations). This package detects React 18+ and delegates to the native `useId`. On React 16/17, it provides a fallback that avoids hydration mismatches.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install useid-polyfill
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```tsx
16
+ import { useId } from 'useid-polyfill';
17
+
18
+ function FormField({ label }: { label: string }) {
19
+ const id = useId();
20
+
21
+ return (
22
+ <div>
23
+ <label htmlFor={id}>{label}</label>
24
+ <input id={id} />
25
+ </div>
26
+ );
27
+ }
28
+ ```
29
+
30
+ ## API
31
+
32
+ ### `useId(): string | undefined`
33
+
34
+ Returns a unique, stable ID string. On React 18+, delegates to the native `React.useId()`.
35
+
36
+ On React 16/17 during SSR, returns `undefined` on the initial server render and hydration pass, then resolves to a string ID after a layout effect. This avoids hydration mismatches.
37
+
38
+ ## SSR Caveats
39
+
40
+ On React 16/17, the fallback returns `undefined` during server rendering and the hydration pass. The ID is set synchronously before paint via `useLayoutEffect`. This means:
41
+
42
+ - Elements will briefly have no ID during hydration
43
+ - After the first client-side effect cycle, IDs are assigned immediately (no `undefined` phase)
44
+ - On React 18+, the native `useId` is used and there are no caveats
45
+
46
+ If you conditionally apply the ID to DOM attributes, handle the `undefined` case:
47
+
48
+ ```tsx
49
+ const id = useId();
50
+ return <input id={id ?? undefined} aria-describedby={id ? `${id}-help` : undefined} />;
51
+ ```
52
+
53
+ ## How It Works
54
+
55
+ 1. **React 18+ detection**: If `React.useId` exists, it is used directly
56
+ 2. **Fallback (React 16/17)**:
57
+ - Server: `useState` initializes with `undefined` (no ID in HTML)
58
+ - Hydration: also `undefined` (matches server output — no mismatch)
59
+ - `useLayoutEffect` fires before paint, sets the ID via `genId()`
60
+ - `useEffect` marks handoff complete so subsequent mounts get IDs immediately
61
+
62
+ ## License
63
+
64
+ MIT
@@ -0,0 +1,3 @@
1
+ declare const useId: () => string | undefined;
2
+
3
+ export { useId };
@@ -0,0 +1,3 @@
1
+ declare const useId: () => string | undefined;
2
+
3
+ export { useId };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var a=Object.create;var d=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var I=Object.getPrototypeOf,g=Object.prototype.hasOwnProperty;var l=(e,t)=>{for(var n in t)d(e,n,{get:t[n],enumerable:!0})},r=(e,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of m(t))!g.call(e,s)&&s!==n&&d(e,s,{get:()=>t[s],enumerable:!(i=p(t,s))||i.enumerable});return e};var x=(e,t,n)=>(n=e!=null?a(I(e)):{},r(t||!e||!e.__esModule?d(n,"default",{value:e,enumerable:!0}):n,e)),y=e=>r(d({},"__esModule",{value:!0}),e);var $={};l($,{useId:()=>c});module.exports=y($);var E=x(require("react")),o=require("react"),R={...E},h=0,w=Math.random().toString(36).slice(2,6),u=()=>`uid-${w}${h++}`,L=typeof window!="undefined"?o.useLayoutEffect:o.useEffect,f=!1;function S(){let[e,t]=(0,o.useState)(()=>f?u():void 0);return L(()=>{e===void 0&&t(u())},[]),(0,o.useEffect)(()=>{f||(f=!0)},[]),e}var c=R.useId||S;0&&(module.exports={useId});
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import*as d from"react";import{useState as f,useEffect as o,useLayoutEffect as i}from"react";var r={...d},u=0,c=Math.random().toString(36).slice(2,6),n=()=>`uid-${c}${u++}`,a=typeof window!="undefined"?i:o,e=!1;function p(){let[t,s]=f(()=>e?n():void 0);return a(()=>{t===void 0&&s(n())},[]),o(()=>{e||(e=!0)},[]),t}var m=r.useId||p;export{m as useId};
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "useid-polyfill",
3
+ "version": "1.0.0",
4
+ "description": "React useId polyfill for React 16/17 with SSR support",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup",
20
+ "test": "vitest run",
21
+ "typecheck": "tsc --noEmit"
22
+ },
23
+ "peerDependencies": {
24
+ "react": ">=16.8.0"
25
+ },
26
+ "devDependencies": {
27
+ "@testing-library/react": "^16.3.2",
28
+ "@types/react": "^18.3.28",
29
+ "@types/react-dom": "^18.3.7",
30
+ "jsdom": "^28.1.0",
31
+ "react": "^18.3.1",
32
+ "react-dom": "^18.3.1",
33
+ "tsup": "^8.5.1",
34
+ "typescript": "^5.9.3",
35
+ "vitest": "^4.0.18"
36
+ },
37
+ "keywords": [
38
+ "react",
39
+ "useId",
40
+ "polyfill",
41
+ "accessibility",
42
+ "a11y",
43
+ "ssr"
44
+ ],
45
+ "license": "MIT",
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "https://github.com/dermothughes/useid-polyfill.git"
49
+ }
50
+ }