Skip to content

Commit f02cfd4

Browse files
committed
feat: support function form for carousel imageProps
Allow imageProps to be a function that receives the current slide, enabling per-slide image attributes.
1 parent e99f262 commit f02cfd4

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/components/ImageSlide.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ export function ImageSlide({
131131
const sizes =
132132
srcSet && rect && hasWindow() ? `${Math.round(Math.min(estimateActualWidth(), rect.width))}px` : undefined;
133133

134-
const { style: imagePropsStyle, className: imagePropsClassName, ...restImageProps } = imageProps || {};
134+
const {
135+
style: imagePropsStyle,
136+
className: imagePropsClassName,
137+
...restImageProps
138+
} = (typeof imageProps === "function" ? imageProps(image) : imageProps) || {};
135139

136140
return (
137141
<>

src/types.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,16 @@ export interface CarouselSettings {
180180
/** `object-fit` setting for image slides */
181181
imageFit: ImageFit;
182182
/** custom image attributes */
183-
imageProps: Omit<
184-
React.ImgHTMLAttributes<HTMLImageElement>,
185-
"src" | "alt" | "sizes" | "srcSet" | "onLoad" | "onError" | "onClick"
186-
>;
183+
imageProps: ImageProps | ((slide: Slide) => ImageProps);
187184
}
188185

189186
export type LengthOrPercentage = `${number}px` | `${number}%` | number;
190187

188+
export type ImageProps = Omit<
189+
React.ImgHTMLAttributes<HTMLImageElement>,
190+
"src" | "alt" | "sizes" | "srcSet" | "onLoad" | "onError" | "onClick"
191+
>;
192+
191193
/** Animation settings */
192194
export interface AnimationSettings {
193195
/** fade-in / fade-out animation settings */

0 commit comments

Comments
 (0)