react-a11y-auto-caption 1.1.6 → 1.1.7

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.
Files changed (2) hide show
  1. package/README.md +225 -78
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # react-a11y-auto-caption
2
2
 
3
- > AI-powered alt text generation component for React and Next.js images.
3
+ > AI-powered alt text generation component for React and Next.js images.
4
4
  > Generate captions during development, save them once, and reuse them in production for fast, accessible images.
5
5
 
6
6
  [![npm version](https://img.shields.io/npm/v/react-a11y-auto-caption.svg)](https://www.npmjs.com/package/react-a11y-auto-caption)
@@ -8,38 +8,83 @@
8
8
  [![Accessibility](https://img.shields.io/badge/Accessibility-100%25-brightgreen.svg)]()
9
9
 
10
10
  ---
11
+
11
12
  ## Why use react-a11y-auto-caption?
12
13
 
13
14
  - **Generate once, reuse forever** — create captions during development, save them, and skip AI calls in production.
14
15
  - **Built for accessibility** — automatically provide meaningful alt text for screen readers.
15
16
  - **Works with React and Next.js** — includes both `<SmartImage>` and `<SmartNextImage>`.
16
- - **Bring your own backend** — use your own FastAPI, Flask, or Node caption API.
17
- - **Production-friendly** — caching, duplicate-request protection, and error handling are built in.
17
+ - **Local-first by default** — run the official caption server with `npx`.
18
+ - **Production-friendly** — caching, duplicate-request protection, lazy generation, and error handling are built in.
19
+
20
+ ---
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ npm install react-a11y-auto-caption
26
+ ```
27
+
28
+ or:
29
+
30
+ ```bash
31
+ yarn add react-a11y-auto-caption
32
+ ```
33
+
34
+ or:
35
+
36
+ ```bash
37
+ pnpm add react-a11y-auto-caption
38
+ ```
18
39
 
19
40
  ---
20
41
 
21
42
  ## Quick Start
22
43
 
23
- ### React
44
+ ### 1. Start the local caption server
45
+
46
+ ```bash
47
+ npx react-a11y-auto-caption-server
48
+ ```
49
+
50
+ Default endpoint:
51
+
52
+ ```txt
53
+ http://127.0.0.1:8000/api/generate-caption
54
+ ```
55
+
56
+ If port `8000` is unavailable, use another port:
57
+
58
+ ```bash
59
+ npx react-a11y-auto-caption-server --port 5000
60
+ ```
61
+
62
+ Then use:
63
+
64
+ ```txt
65
+ http://127.0.0.1:5000/api/generate-caption
66
+ ```
67
+
68
+ ### 2. Use `SmartImage`
24
69
 
25
70
  ```tsx
26
- import { SmartImage } from 'react-a11y-auto-caption';
71
+ import { SmartImage } from "react-a11y-auto-caption";
27
72
 
28
73
  export default function Demo() {
29
74
  return (
30
75
  <SmartImage
31
- src="https://example.com/image.jpg"
32
- apiEndpoint="http://localhost:8000/api/generate-caption"
76
+ src="/example.jpg"
77
+ apiEndpoint="http://127.0.0.1:8000/api/generate-caption"
33
78
  />
34
79
  );
35
80
  }
36
81
  ```
37
82
 
38
- ### Next.js
83
+ ### 3. Use `SmartNextImage`
39
84
 
40
85
  ```tsx
41
- import { SmartNextImage } from 'react-a11y-auto-caption/next';
42
- import sampleImage from '../public/sample.jpg';
86
+ import { SmartNextImage } from "react-a11y-auto-caption/next";
87
+ import sampleImage from "../public/sample.jpg";
43
88
 
44
89
  export default function Demo() {
45
90
  return (
@@ -47,159 +92,261 @@ export default function Demo() {
47
92
  src={sampleImage}
48
93
  width={500}
49
94
  height={300}
50
- apiEndpoint="http://localhost:8000/api/generate-caption"
95
+ apiEndpoint="http://127.0.0.1:8000/api/generate-caption"
51
96
  />
52
97
  );
53
98
  }
54
99
  ```
55
- > **Optional**: set the API endpoint once with a Provider
56
100
 
57
- ### Wrap your app with the Provider (Optional but Recommended)
101
+ ---
102
+
103
+ ## Provider Setup
58
104
 
59
- Set your backend API endpoint once globally. Otherwise, pass `apiEndpoint` directly as a prop.
105
+ You can set the backend API endpoint once globally instead of passing `apiEndpoint` to every image.
60
106
 
61
107
  ```tsx
62
- // App.tsx or layout.tsx
63
108
  import { SmartImageProvider } from "react-a11y-auto-caption";
64
109
 
65
110
  export default function App({ children }) {
66
111
  return (
67
- <SmartImageProvider value={{ apiEndpoint: "https://your-api.com/api/generate-caption" }}>
112
+ <SmartImageProvider
113
+ value={{
114
+ apiEndpoint: "http://127.0.0.1:8000/api/generate-caption",
115
+ }}
116
+ >
68
117
  {children}
69
118
  </SmartImageProvider>
70
119
  );
71
120
  }
72
121
  ```
73
122
 
123
+ Then use `SmartImage` without repeating the endpoint:
124
+
125
+ ```tsx
126
+ <SmartImage src="/example.jpg" />
127
+ ```
128
+
74
129
  ---
75
130
 
76
- ## Installation
131
+ ## Recommended Workflow
77
132
 
78
- ```bash
79
- # npm
80
- npm install react-a11y-auto-caption
133
+ 1. Run the local caption server with `npx react-a11y-auto-caption-server`
134
+ 2. Generate captions during development
135
+ 3. Save generated captions to your database with `onCaptionGenerated`
136
+ 4. Pass the saved `alt` text in production
137
+ 5. Skip AI requests entirely for faster production pages
81
138
 
82
- # yarn
83
- yarn add react-a11y-auto-caption
139
+ Example:
84
140
 
85
- # pnpm
86
- pnpm add react-a11y-auto-caption
141
+ ```tsx
142
+ <SmartImage
143
+ src="/example.jpg"
144
+ alt={savedAlt || undefined}
145
+ apiEndpoint="http://127.0.0.1:8000/api/generate-caption"
146
+ onCaptionGenerated={(caption) => {
147
+ saveAltTextToDatabase(caption);
148
+ }}
149
+ />
87
150
  ```
88
151
 
152
+ > If `alt` is provided, AI generation is bypassed. This is intentional so saved captions can be reused in production.
153
+
89
154
  ---
90
- ## Recommended workflow
91
155
 
92
- 1. Generate captions during local development with light [python server](https://github.com/kong33/SmartImage)
93
- 2. Save them to your database with `onCaptionGenerated`
94
- 3. Pass the saved `alt` text in production
95
- 4. Skip AI requests entirely for zero extra latency
156
+ ## Public Demo Server
157
+
158
+ You can test the package with the public demo server:
159
+
160
+ ```txt
161
+ https://kong3333-react-a11y-auto-caption-server.hf.space/api/generate-caption
162
+ ```
163
+
164
+ Example:
165
+
166
+ ```tsx
167
+ <SmartImage
168
+ src="/example.jpg"
169
+ apiEndpoint="https://kong3333-react-a11y-auto-caption-server.hf.space/api/generate-caption"
170
+ />
171
+ ```
172
+
173
+ > The public demo server is for testing only. It may be slow, paused, or unavailable depending on free-tier limits.
174
+ > For production, run your own caption server.
175
+
176
+ ---
177
+
178
+ ## Backend Integration
179
+
180
+ This package needs a caption API endpoint that accepts an image file and returns a caption.
181
+
182
+ The official local server is the easiest option:
183
+
184
+ ```bash
185
+ npx react-a11y-auto-caption-server
186
+ ```
187
+
188
+ Default endpoint:
189
+
190
+ ```txt
191
+ http://127.0.0.1:8000/api/generate-caption
192
+ ```
193
+
194
+ Custom port:
195
+
196
+ ```bash
197
+ npx react-a11y-auto-caption-server --port 5000
198
+ ```
199
+
200
+ Custom endpoint:
201
+
202
+ ```txt
203
+ http://127.0.0.1:5000/api/generate-caption
204
+ ```
205
+
206
+ The caption server automatically allows local development origins such as:
207
+
208
+ ```txt
209
+ http://localhost:<any-port>
210
+ http://127.0.0.1:<any-port>
211
+ ```
212
+
213
+ For production or internal company servers, configure `ALLOWED_ORIGINS` on the server side.
214
+
215
+ Example:
216
+
217
+ ```env
218
+ ALLOWED_ORIGINS=https://your-frontend-domain.com,http://localhost:3000
219
+ ```
220
+
221
+ If your frontend runs locally but the caption server runs on another machine, your frontend endpoint should point to that machine:
222
+
223
+ ```tsx
224
+ <SmartImage
225
+ src="/example.jpg"
226
+ apiEndpoint="http://192.168.0.20:8000/api/generate-caption"
227
+ />
228
+ ```
96
229
 
97
230
  ---
98
231
 
99
232
  ## API Reference
100
233
 
101
- Both `<SmartImage>` and `<SmartNextImage>` inherit all standard HTML `<img>` (or `next/image`) attributes, plus the following:
234
+ Both `<SmartImage>` and `<SmartNextImage>` inherit standard HTML `<img>` or `next/image` props, plus the following:
102
235
 
103
236
  | Prop | Type | Default | Description |
104
237
  | :--- | :--- | :--- | :--- |
105
238
  | `apiEndpoint` | `string` | `undefined` | The URL of your AI backend API. Overrides the `SmartImageProvider` endpoint if provided. |
106
239
  | `alt` | `string` | `undefined` | Manual alt text. If provided, AI generation is completely bypassed. |
107
- | `fallbackAlt` | `string` | `"Image loading or caption unavailable"` | Text used when the AI request fails or times out. |
108
- | `lazyGenerate` | `boolean` | `true` | Delays AI API calls until the image enters the viewport using `IntersectionObserver`.|
109
- | `disableAI` | `boolean` | `false` | Disables AI generation and uses a mock caption. Recommended for testing. |
110
- | `announceLive` | `boolean` | `false` | Enables `aria-live` region to announce generation status to screen readers. |
240
+ | `fallbackAlt` | `string` | `"Image loading or caption unavailable"` | Text used when the AI request fails. |
241
+ | `lazyGenerate` | `boolean` | `true` | Delays AI API calls until the image enters the viewport using `IntersectionObserver`. |
242
+ | `disableAI` | `boolean` | `false` | Disables AI generation and uses a mock caption. Useful for tests. |
243
+ | `announceLive` | `boolean` | `false` | Enables an `aria-live` region to announce generation status to screen readers. |
111
244
  | `onCaptionGenerated` | `(caption: string) => void` | `undefined` | Callback fired when a caption is successfully generated. |
112
- | `onCaptionError` | `(error: Error) => void` | `undefined` | Callback fired when caption generation fails. Use for logging or toast notifications. |
245
+ | `onCaptionError` | `(error: Error) => void` | `undefined` | Callback fired when caption generation fails. Useful for logging or toast notifications. |
113
246
 
114
- > **Note:** `<SmartNextImage>` requires standard Next.js image props such as `width` and `height` (unless using `fill`).
247
+ > `<SmartNextImage>` requires standard Next.js image props such as `width` and `height`, unless using `fill`.
115
248
 
116
249
  ---
117
250
 
118
251
  ## Error Handling
119
252
 
120
- You can handle errors in two ways depending on your use case.
121
-
122
- **Via callback** — for external handling like logging or toasts:
253
+ Use `onCaptionError` for logging, toast messages, or debugging.
123
254
 
124
255
  ```tsx
125
256
  <SmartImage
126
- src={imageUrl}
127
- onCaptionError={(err) => toast.error(err.message)}
257
+ src="/example.jpg"
258
+ apiEndpoint="http://127.0.0.1:8000/api/generate-caption"
259
+ onCaptionError={(error) => {
260
+ console.error("Caption generation failed:", error);
261
+ }}
128
262
  />
129
263
  ```
130
264
 
131
- **Via `useAICaptions` hook** for UI branching:
265
+ You can also use the hook directly:
132
266
 
133
267
  ```tsx
134
- const { generatedAlt, isGenerating, error } = useAICaptions({ src });
268
+ const { generatedAlt, isGenerating, error } = useAICaptions({
269
+ src: "/example.jpg",
270
+ apiEndpoint: "http://127.0.0.1:8000/api/generate-caption",
271
+ });
135
272
 
136
273
  if (error) return <p>Failed to generate caption.</p>;
137
274
  ```
138
275
 
139
276
  ---
140
277
 
141
- ## Backend Integration
278
+ ## Troubleshooting
142
279
 
143
- This package needs a caption API endpoint to generate alt text.
280
+ ### API request does not run
144
281
 
145
- We offer a public demo server for testing. Set API endpoint as:
282
+ Check that:
146
283
 
147
- ```bash
148
- https://kong3333-react-a11y-auto-caption-server.hf.space/api/generate-caption
149
- ```
284
+ - `apiEndpoint` points to `/api/generate-caption`
285
+ - the caption server is running
286
+ - the frontend endpoint uses the same port as the server
287
+ - `alt` is not already provided if you expect AI generation
288
+ - `lazyGenerate={false}` is used while debugging viewport-related issues
289
+ - the latest package version is installed
150
290
 
151
- > This public demo server is for testing only and may be slow or unavailable depending on free-tier limits.
152
- > For production, please run your own caption server.
291
+ Debug example:
153
292
 
154
- The easiest way is to run the official local server:
155
-
156
- ```bash
157
- npx react-a11y-auto-caption-server
293
+ ```tsx
294
+ <SmartImage
295
+ src="/example.jpg"
296
+ apiEndpoint="http://127.0.0.1:8000/api/generate-caption"
297
+ lazyGenerate={false}
298
+ onCaptionGenerated={(caption) => console.log("Generated:", caption)}
299
+ onCaptionError={(error) => console.error("Caption error:", error)}
300
+ />
158
301
  ```
159
302
 
160
- By default, the server runs at:
303
+ ### Port 8000 is unavailable
161
304
 
162
- ```txt
163
- http://127.0.0.1:8000/api/generate-caption
305
+ Run the server on another port:
306
+
307
+ ```bash
308
+ npx react-a11y-auto-caption-server --port 8001
164
309
  ```
165
310
 
166
- Use it with `SmartImage` or `SmartNextImage`:
311
+ Then update your frontend:
167
312
 
168
313
  ```tsx
169
314
  <SmartImage
170
315
  src="/example.jpg"
171
- apiEndpoint="http://127.0.0.1:8000/api/generate-caption"
316
+ apiEndpoint="http://127.0.0.1:8001/api/generate-caption"
172
317
  />
173
318
  ```
174
319
 
175
- You can also change the port:
176
-
177
- ```bash
178
- npx react-a11y-auto-caption-server --port 5000
179
- ```
320
+ ### External image URLs
180
321
 
181
- Then update the endpoint:
322
+ If an external image URL fails before reaching the caption server, try a local image first:
182
323
 
183
324
  ```tsx
184
325
  <SmartImage
185
- src="/example.jpg"
186
- apiEndpoint="http://127.0.0.1:5000/api/generate-caption"
326
+ src="/sample.jpg"
327
+ apiEndpoint="http://127.0.0.1:8000/api/generate-caption"
187
328
  />
188
329
  ```
189
330
 
190
- > First run may take a few minutes because the server creates a Python environment and installs AI dependencies.
331
+ Some external image hosts block browser-side `fetch()` access even if the image displays correctly in an `<img>` tag.
332
+
333
+ ---
334
+
335
+ ## What's New
191
336
 
192
- For production, we recommend running your own caption server and applying CORS restrictions, file size limits, and rate limiting.
337
+ - Fixed lazy generation so captions are triggered correctly after images enter the viewport.
338
+ - Added official `npx react-a11y-auto-caption-server` workflow.
339
+ - Added custom backend server port support.
340
+ - Added public demo server option for quick testing.
193
341
 
194
342
  ---
195
343
 
196
- ## What's New in v1.0.4
344
+ ## Related
345
+
346
+ - [`react-a11y-auto-caption-server`](https://www.npmjs.com/package/react-a11y-auto-caption-server)
347
+
348
+ ---
197
349
 
198
- - **LRU Cache:** Replaced unbounded `Map` cache with an LRU implementation to prevent memory leaks in long-running apps.
199
- - **`onCaptionError` callback:** New prop to handle caption generation failures externally (e.g. logging, toast notifications).
200
- - **`error` state:** `useAICaptions` hook now returns an `error` field so you can branch your UI on failure.
201
- - **Unmount safety:** Caption generation is now safely cancelled when a component unmounts, preventing stale state updates.
202
- - **Next.js static import support:** `<SmartNextImage>` now correctly resolves both `StaticImageData` and `StaticRequire` import types.
203
- - **Subpath exports:** Added `react-a11y-auto-caption/next` entry point for cleaner Next.js-specific imports.
350
+ ## License
204
351
 
205
-
352
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-a11y-auto-caption",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "Smart React component that automatically generates alt text for images using AI.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",