Skip to content

Commit babadb2

Browse files
committed
fix broken typescript import types
1 parent b832273 commit babadb2

4 files changed

Lines changed: 138 additions & 4 deletions

File tree

README.md

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,55 @@ For more examples, see [`example/App.jsx`](https://github.com/xiaolin/react-imag
6565

6666
<br />
6767

68+
## 📘 TypeScript
69+
70+
This package includes TypeScript definitions. Import types for props, items, and refs:
71+
72+
```tsx
73+
import ImageGallery from "react-image-gallery";
74+
import type {
75+
GalleryItem,
76+
ImageGalleryProps,
77+
ImageGalleryRef,
78+
} from "react-image-gallery";
79+
80+
const images: GalleryItem[] = [
81+
{
82+
original: "https://picsum.photos/id/1018/1000/600/",
83+
thumbnail: "https://picsum.photos/id/1018/250/150/",
84+
originalAlt: "Mountain landscape",
85+
description: "A beautiful mountain view",
86+
},
87+
{
88+
original: "https://picsum.photos/id/1015/1000/600/",
89+
thumbnail: "https://picsum.photos/id/1015/250/150/",
90+
originalAlt: "Flowing river",
91+
},
92+
{
93+
original: "https://picsum.photos/id/1019/1000/600/",
94+
thumbnail: "https://picsum.photos/id/1019/250/150/",
95+
originalAlt: "Sunset over the ocean",
96+
},
97+
];
98+
99+
function MyGallery() {
100+
const galleryRef = useRef<ImageGalleryRef>(null);
101+
102+
const handleClick = () => {
103+
galleryRef.current?.fullScreen();
104+
};
105+
106+
return (
107+
<>
108+
<ImageGallery ref={galleryRef} items={images} />
109+
<button onClick={handleClick}>Enter Fullscreen</button>
110+
</>
111+
);
112+
}
113+
```
114+
115+
<br />
116+
68117
## ⚙️ Props
69118

70119
- `items`: (required) Array of objects. Available properties:
@@ -153,9 +202,12 @@ For more examples, see [`example/App.jsx`](https://github.com/xiaolin/react-imag
153202

154203
The following functions can be accessed using [refs](https://reactjs.org/docs/refs-and-the-dom.html)
155204

156-
- `play()`: plays the slides
157-
- `pause()`: pauses the slides
158-
- `toggleFullScreen()`: toggles full screen
205+
- `play()`: starts the slideshow
206+
- `pause()`: pauses the slideshow
207+
- `togglePlay()`: toggles between play and pause
208+
- `fullScreen()`: enters fullscreen mode
209+
- `exitFullScreen()`: exits fullscreen mode
210+
- `toggleFullScreen()`: toggles fullscreen mode
159211
- `slideToIndex(index)`: slides to a specific index
160212
- `getCurrentIndex()`: returns the current index
161213

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"exports": {
3333
".": {
34+
"types": "./build/types/types.d.ts",
3435
"import": "./build/image-gallery.es.js",
3536
"require": "./build/image-gallery.cjs"
3637
},
@@ -121,4 +122,4 @@
121122
"peerDependencies": {
122123
"react": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
123124
}
124-
}
125+
}

scripts/test-package.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,82 @@ for (const type of essentialTypes) {
165165
process.exit(1);
166166
}
167167
}
168+
169+
// Check for default export declaration
170+
if (!typesContent.includes('export default')) {
171+
console.error('❌ Missing default export in types');
172+
process.exit(1);
173+
}
174+
console.log('✅ Default export is declared');
168175
EOF
169176

170177
node test-types.mjs
171178

179+
# Test TypeScript compilation with modern module resolution (node16/nodenext/bundler)
180+
echo -e "${YELLOW}🧪 Testing TypeScript compilation with modern moduleResolution...${NC}"
181+
npm install typescript @types/react --silent
182+
183+
cat > test-ts.ts << 'EOF'
184+
import ImageGallery from 'react-image-gallery';
185+
import type { GalleryItem, ImageGalleryProps, ImageGalleryRef } from 'react-image-gallery';
186+
187+
// Test default import is a valid component type
188+
const gallery: typeof ImageGallery = ImageGallery;
189+
190+
// Test named type imports
191+
const item: GalleryItem = { original: 'test.jpg' };
192+
const props: ImageGalleryProps = { items: [item] };
193+
194+
// Test ref type
195+
const ref: ImageGalleryRef | null = null;
196+
197+
console.log('✅ TypeScript types work correctly');
198+
EOF
199+
200+
# Test with moduleResolution: bundler (common modern setup)
201+
cat > tsconfig.json << 'EOF'
202+
{
203+
"compilerOptions": {
204+
"target": "ES2020",
205+
"module": "ESNext",
206+
"moduleResolution": "bundler",
207+
"strict": true,
208+
"skipLibCheck": true,
209+
"noEmit": true
210+
},
211+
"include": ["test-ts.ts"]
212+
}
213+
EOF
214+
215+
if npx tsc --noEmit 2>&1; then
216+
echo -e "${GREEN}✅ TypeScript compilation (bundler) passed${NC}"
217+
else
218+
echo -e "${RED}❌ TypeScript compilation (bundler) failed${NC}"
219+
exit 1
220+
fi
221+
222+
# Test with moduleResolution: node16 (stricter, used by many projects)
223+
cat > tsconfig.json << 'EOF'
224+
{
225+
"compilerOptions": {
226+
"target": "ES2020",
227+
"module": "Node16",
228+
"moduleResolution": "Node16",
229+
"strict": true,
230+
"skipLibCheck": true,
231+
"noEmit": true
232+
},
233+
"include": ["test-ts.ts"]
234+
}
235+
EOF
236+
237+
if npx tsc --noEmit 2>&1; then
238+
echo -e "${GREEN}✅ TypeScript compilation (node16) passed${NC}"
239+
else
240+
echo -e "${RED}❌ TypeScript compilation (node16) failed${NC}"
241+
exit 1
242+
fi
243+
172244
# Cleanup
173245
echo -e "${YELLOW}🧹 Cleaning up...${NC}"
174246
cd /

src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
SyntheticEvent,
77
TouchEvent,
88
} from "react";
9+
import type React from "react";
910

1011
// ============= Gallery Item Types =============
1112

@@ -515,3 +516,11 @@ export type SwipeDirection = "Left" | "Right" | "Up" | "Down";
515516
export interface SVGProps {
516517
strokeWidth?: number;
517518
}
519+
520+
// ============= Default Export Declaration =============
521+
// This declares the ImageGallery component type for consumers
522+
// The actual implementation is in ImageGallery.tsx
523+
declare const ImageGallery: React.ForwardRefExoticComponent<
524+
ImageGalleryProps & React.RefAttributes<ImageGalleryRef>
525+
>;
526+
export default ImageGallery;

0 commit comments

Comments
 (0)