react-a11y-auto-caption 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 +21 -0
- package/package.json +41 -0
- package/src/index.tsx +161 -0
- package/src/next-env.d.ts +2 -0
- package/src/next.tsx +154 -0
- package/tsconfig.json +19 -0
- package/tsup.config.ts +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gaeun Kim
|
|
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/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-a11y-auto-caption",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Smart React component that automatically generates alt text for images using AI.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsup && tsc --emitDeclarationOnly --declaration --declarationDir ./dist",
|
|
10
|
+
"dev": "tsup --watch"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"react",
|
|
14
|
+
"accessibility",
|
|
15
|
+
"a11y",
|
|
16
|
+
"ai",
|
|
17
|
+
"image-captioning",
|
|
18
|
+
"alt-text"
|
|
19
|
+
],
|
|
20
|
+
"author": "Gaeun Kim",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/react": "^19.2.14",
|
|
24
|
+
"@types/react-dom": "^19.2.3",
|
|
25
|
+
"react": "^19.2.5",
|
|
26
|
+
"react-dom": "^19.2.5",
|
|
27
|
+
"tsup": "^8.5.1",
|
|
28
|
+
"typescript": "^6.0.2",
|
|
29
|
+
"next": "^15.0.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": ">=18.0.0",
|
|
33
|
+
"react-dom": ">=18.0.0",
|
|
34
|
+
"next": ">=13.0.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"next": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import React, { createContext, useContext, useState, useEffect, ImgHTMLAttributes } from "react";
|
|
2
|
+
|
|
3
|
+
interface SmartImageContextProps {
|
|
4
|
+
apiEndpoint?: string;
|
|
5
|
+
disableAI?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const SmartImageContext = createContext<SmartImageContextProps | undefined>(undefined);
|
|
9
|
+
|
|
10
|
+
export const SmartImageProvider: React.FC<{
|
|
11
|
+
value: SmartImageContextProps;
|
|
12
|
+
children: React.ReactNode;
|
|
13
|
+
}> = ({ value, children }) => {
|
|
14
|
+
return <SmartImageContext.Provider value={value}>{children}</SmartImageContext.Provider>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const captionCache = new Map<string, string>();
|
|
18
|
+
const pendingRequestCache = new Map<string, Promise<string>>();
|
|
19
|
+
|
|
20
|
+
export interface SmartImageProps extends ImgHTMLAttributes<HTMLImageElement> {
|
|
21
|
+
apiEndpoint?: string;
|
|
22
|
+
fallbackAlt?: string;
|
|
23
|
+
onCaptionGenerated?: (caption: string) => void;
|
|
24
|
+
disableAI?: boolean;
|
|
25
|
+
announceLive?: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const SmartImage = ({
|
|
29
|
+
src,
|
|
30
|
+
alt,
|
|
31
|
+
apiEndpoint: propsEndpoint,
|
|
32
|
+
fallbackAlt = "Image loading or caption unavailable",
|
|
33
|
+
onCaptionGenerated,
|
|
34
|
+
disableAI: propsDisableAI,
|
|
35
|
+
announceLive = false,
|
|
36
|
+
...props
|
|
37
|
+
}: SmartImageProps) => {
|
|
38
|
+
const context = useContext(SmartImageContext);
|
|
39
|
+
|
|
40
|
+
const apiEndpoint = propsEndpoint || context?.apiEndpoint;
|
|
41
|
+
const disableAI = propsDisableAI ?? context?.disableAI ?? false;
|
|
42
|
+
|
|
43
|
+
const [generatedAlt, setGeneratedAlt] = useState("");
|
|
44
|
+
const [isGenerating, setIsGenerating] = useState(false);
|
|
45
|
+
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
if (alt) {
|
|
48
|
+
setGeneratedAlt(alt);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!src) return;
|
|
53
|
+
|
|
54
|
+
if (disableAI) {
|
|
55
|
+
setGeneratedAlt("[Testing mode: AI caption generation disabled]");
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!apiEndpoint) {
|
|
60
|
+
console.warn(
|
|
61
|
+
"[SmartImage] Missing 'apiEndpoint' prop. Please provide a backend API URL via props or SmartImageProvider to enable AI caption generation.",
|
|
62
|
+
);
|
|
63
|
+
setGeneratedAlt(fallbackAlt);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const imageUrl = src as string;
|
|
68
|
+
|
|
69
|
+
if (captionCache.has(imageUrl)) {
|
|
70
|
+
console.log("[SmartImage] Cache hit: Reusing existing caption.");
|
|
71
|
+
const cachedCaption = captionCache.get(imageUrl)!;
|
|
72
|
+
setGeneratedAlt(cachedCaption);
|
|
73
|
+
|
|
74
|
+
if (onCaptionGenerated) onCaptionGenerated(cachedCaption);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const generateCaption = async () => {
|
|
79
|
+
setIsGenerating(true);
|
|
80
|
+
try {
|
|
81
|
+
if (pendingRequestCache.has(imageUrl)) {
|
|
82
|
+
console.log("[SmartImage] Pending request detected. Waiting for the existing API call to complete.");
|
|
83
|
+
const caption = await pendingRequestCache.get(imageUrl)!;
|
|
84
|
+
setGeneratedAlt(caption);
|
|
85
|
+
if (onCaptionGenerated) onCaptionGenerated(caption);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const fetchPromise = (async () => {
|
|
90
|
+
const imageResponse = await fetch(imageUrl);
|
|
91
|
+
const imageBlob = await imageResponse.blob();
|
|
92
|
+
|
|
93
|
+
const imageFile = new File([imageBlob], "image.jpg", {
|
|
94
|
+
type: imageBlob.type || "image/jpeg",
|
|
95
|
+
});
|
|
96
|
+
const formData = new FormData();
|
|
97
|
+
|
|
98
|
+
formData.append("file", imageFile);
|
|
99
|
+
|
|
100
|
+
const response = await fetch(apiEndpoint, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
body: formData,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
if (!response.ok) throw new Error("AI API request failed");
|
|
106
|
+
const data = await response.json();
|
|
107
|
+
|
|
108
|
+
if (data.caption) return data.caption;
|
|
109
|
+
throw new Error("No caption returned from the API.");
|
|
110
|
+
})();
|
|
111
|
+
|
|
112
|
+
pendingRequestCache.set(imageUrl, fetchPromise);
|
|
113
|
+
|
|
114
|
+
const newCaption = await fetchPromise;
|
|
115
|
+
|
|
116
|
+
pendingRequestCache.delete(imageUrl);
|
|
117
|
+
captionCache.set(imageUrl, newCaption);
|
|
118
|
+
|
|
119
|
+
setGeneratedAlt(newCaption);
|
|
120
|
+
|
|
121
|
+
if (onCaptionGenerated) onCaptionGenerated(newCaption);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
console.error("[SmartImage] Caption Error:", error);
|
|
124
|
+
pendingRequestCache.delete(imageUrl);
|
|
125
|
+
setGeneratedAlt(fallbackAlt);
|
|
126
|
+
} finally {
|
|
127
|
+
setIsGenerating(false);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
generateCaption();
|
|
132
|
+
}, [src, alt, apiEndpoint, fallbackAlt, onCaptionGenerated, disableAI]);
|
|
133
|
+
|
|
134
|
+
const srOnlyStyle: React.CSSProperties = {
|
|
135
|
+
position: "absolute",
|
|
136
|
+
width: "1px",
|
|
137
|
+
height: "1px",
|
|
138
|
+
padding: 0,
|
|
139
|
+
margin: "-1px",
|
|
140
|
+
overflow: "hidden",
|
|
141
|
+
clip: "rect(0, 0, 0, 0)",
|
|
142
|
+
whiteSpace: "nowrap",
|
|
143
|
+
borderWidth: 0,
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
return (
|
|
147
|
+
<>
|
|
148
|
+
{announceLive && (
|
|
149
|
+
<span style={srOnlyStyle} aria-live="polite" aria-atomic="true">
|
|
150
|
+
{isGenerating
|
|
151
|
+
? "Generating image description. Please wait..."
|
|
152
|
+
: generatedAlt
|
|
153
|
+
? `Image description generated: ${generatedAlt}`
|
|
154
|
+
: ""}
|
|
155
|
+
</span>
|
|
156
|
+
)}
|
|
157
|
+
|
|
158
|
+
<img src={src} alt={generatedAlt} aria-busy={isGenerating} {...props} />
|
|
159
|
+
</>
|
|
160
|
+
);
|
|
161
|
+
};
|
package/src/next.tsx
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import React, { useState, useEffect, useContext } from "react";
|
|
2
|
+
import Image, { ImageProps } from "next/image";
|
|
3
|
+
|
|
4
|
+
import { SmartImageContext } from "./index";
|
|
5
|
+
|
|
6
|
+
function isStaticImport(source: any): source is { src: string } {
|
|
7
|
+
return typeof source === "object" && source !== null && "src" in source;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const captionCache = new Map<string, string>();
|
|
11
|
+
const pendingRequestCache = new Map<string, Promise<string>>();
|
|
12
|
+
|
|
13
|
+
export interface SmartNextImageProps extends Omit<ImageProps, "alt"> {
|
|
14
|
+
alt?: string;
|
|
15
|
+
apiEndpoint?: string;
|
|
16
|
+
fallbackAlt?: string;
|
|
17
|
+
onCaptionGenerated?: (caption: string) => void;
|
|
18
|
+
disableAI?: boolean;
|
|
19
|
+
announceLive?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const SmartNextImage = ({
|
|
23
|
+
src,
|
|
24
|
+
alt,
|
|
25
|
+
apiEndpoint: propsEndpoint,
|
|
26
|
+
fallbackAlt = "Image loading or caption unavailable",
|
|
27
|
+
onCaptionGenerated,
|
|
28
|
+
disableAI: propsDisableAI,
|
|
29
|
+
announceLive = false,
|
|
30
|
+
...props
|
|
31
|
+
}: SmartNextImageProps) => {
|
|
32
|
+
const context = useContext(SmartImageContext);
|
|
33
|
+
const apiEndpoint = propsEndpoint || context?.apiEndpoint;
|
|
34
|
+
const disableAI = propsDisableAI ?? context?.disableAI ?? false;
|
|
35
|
+
|
|
36
|
+
const [generatedAlt, setGeneratedAlt] = useState("");
|
|
37
|
+
const [isGenerating, setIsGenerating] = useState(false);
|
|
38
|
+
|
|
39
|
+
const imageUrl = isStaticImport(src) ? src.src : (src as string);
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (alt) {
|
|
43
|
+
setGeneratedAlt(alt);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!src) return;
|
|
48
|
+
|
|
49
|
+
if (disableAI) {
|
|
50
|
+
setGeneratedAlt("[Testing mode: AI caption generation disabled]");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!apiEndpoint) {
|
|
55
|
+
console.warn(
|
|
56
|
+
"[SmartNextImage] Missing 'apiEndpoint' prop. Please provide a backend API URL via props or SmartImageProvider to enable AI caption generation.",
|
|
57
|
+
);
|
|
58
|
+
setGeneratedAlt(fallbackAlt);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (captionCache.has(imageUrl)) {
|
|
63
|
+
console.log("[SmartNextImage] Cache hit: Reusing existing caption.");
|
|
64
|
+
const cachedCaption = captionCache.get(imageUrl)!;
|
|
65
|
+
setGeneratedAlt(cachedCaption);
|
|
66
|
+
|
|
67
|
+
if (onCaptionGenerated) onCaptionGenerated(cachedCaption);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const generateCaption = async () => {
|
|
72
|
+
setIsGenerating(true);
|
|
73
|
+
try {
|
|
74
|
+
if (pendingRequestCache.has(imageUrl)) {
|
|
75
|
+
console.log("[SmartNextImage] Pending request detected. Waiting for the existing API call to complete.");
|
|
76
|
+
const caption = await pendingRequestCache.get(imageUrl)!;
|
|
77
|
+
setGeneratedAlt(caption);
|
|
78
|
+
if (onCaptionGenerated) onCaptionGenerated(caption);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const fetchPromise = (async () => {
|
|
83
|
+
const imageResponse = await fetch(imageUrl);
|
|
84
|
+
const imageBlob = await imageResponse.blob();
|
|
85
|
+
|
|
86
|
+
const imageFile = new File([imageBlob], "image.jpg", {
|
|
87
|
+
type: imageBlob.type || "image/jpeg",
|
|
88
|
+
});
|
|
89
|
+
const formData = new FormData();
|
|
90
|
+
|
|
91
|
+
formData.append("file", imageFile);
|
|
92
|
+
|
|
93
|
+
const response = await fetch(apiEndpoint, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
body: formData,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
if (!response.ok) throw new Error("AI API request failed");
|
|
99
|
+
const data = await response.json();
|
|
100
|
+
|
|
101
|
+
if (data.caption) return data.caption;
|
|
102
|
+
throw new Error("No caption returned from the API.");
|
|
103
|
+
})();
|
|
104
|
+
|
|
105
|
+
pendingRequestCache.set(imageUrl, fetchPromise);
|
|
106
|
+
|
|
107
|
+
const newCaption = await fetchPromise;
|
|
108
|
+
|
|
109
|
+
pendingRequestCache.delete(imageUrl);
|
|
110
|
+
captionCache.set(imageUrl, newCaption);
|
|
111
|
+
|
|
112
|
+
setGeneratedAlt(newCaption);
|
|
113
|
+
|
|
114
|
+
if (onCaptionGenerated) onCaptionGenerated(newCaption);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error("[SmartNextImage] Caption Error:", error);
|
|
117
|
+
pendingRequestCache.delete(imageUrl);
|
|
118
|
+
setGeneratedAlt(fallbackAlt);
|
|
119
|
+
} finally {
|
|
120
|
+
setIsGenerating(false);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
generateCaption();
|
|
125
|
+
}, [src, alt, apiEndpoint, fallbackAlt, onCaptionGenerated, disableAI, imageUrl]);
|
|
126
|
+
|
|
127
|
+
const srOnlyStyle: React.CSSProperties = {
|
|
128
|
+
position: "absolute",
|
|
129
|
+
width: "1px",
|
|
130
|
+
height: "1px",
|
|
131
|
+
padding: 0,
|
|
132
|
+
margin: "-1px",
|
|
133
|
+
overflow: "hidden",
|
|
134
|
+
clip: "rect(0, 0, 0, 0)",
|
|
135
|
+
whiteSpace: "nowrap",
|
|
136
|
+
borderWidth: 0,
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<>
|
|
141
|
+
{announceLive && (
|
|
142
|
+
<span style={srOnlyStyle} aria-live="polite" aria-atomic="true">
|
|
143
|
+
{isGenerating
|
|
144
|
+
? "Generating image description. Please wait..."
|
|
145
|
+
: generatedAlt
|
|
146
|
+
? `Image description generated: ${generatedAlt}`
|
|
147
|
+
: ""}
|
|
148
|
+
</span>
|
|
149
|
+
)}
|
|
150
|
+
|
|
151
|
+
<Image src={src} alt={generatedAlt || ""} aria-busy={isGenerating} {...props} />
|
|
152
|
+
</>
|
|
153
|
+
);
|
|
154
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"rootDir": "./src",
|
|
4
|
+
"target": "es2020",
|
|
5
|
+
"lib": ["dom", "dom.iterable", "esnext"],
|
|
6
|
+
"allowJs": false,
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"allowSyntheticDefaultImports": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"module": "esnext",
|
|
13
|
+
"moduleResolution": "bundler",
|
|
14
|
+
"resolveJsonModule": true,
|
|
15
|
+
"isolatedModules": true,
|
|
16
|
+
"jsx": "react-jsx"
|
|
17
|
+
},
|
|
18
|
+
"include": ["src"]
|
|
19
|
+
}
|
package/tsup.config.ts
ADDED