diff --git a/README.md b/README.md index b099107..6c156a2 100644 --- a/README.md +++ b/README.md @@ -38,26 +38,27 @@ import ReactCompareImage from 'react-compare-image'; ## Props -| Prop (\* required) | type | default | description | -| ------------------------ | ----------------------- | :---------: | --------------------------------------------------------------------------------------------------------------------- | -| aspectRatio | `'taller'` or `'wider'` | `'taller'` | Which to choose if the aspect ratios of the images are different | -| handle | element | null | Custom handle element. Just pass `` if you want to remove handle. | -| handleSize | number (px) | 40 | diameter of slider handle (by pixel) | -| hover | boolean | false | Whether to slide at hover | -| leftImage \* | string | null | left image's url | -| leftImageAlt | string | `''` | alt props for left image | -| leftImageCss | object | {} | Additional css for left image | -| leftImageLabel | string | null | Label for the image (e.g. `before`) | -| onSliderPositionChange | function | null | Callback function called each time the slider changes. The position (0 to 1) of the slider is passed as arg | -| rightImage \* | string | null | right image's url | -| rightImageAlt | string | `''` | alt props for right image | -| rightImageCss | object | {} | Additional css for right image | -| rightImageLabel | string | null | Label for the image (e.g. `after`) | -| skeleton | element | null | Element displayed while image is loading | -| sliderLineColor | string | `'#ffffff'` | line color of slider | -| sliderLineWidth | number (px) | 2 | line width of slider (by pixel) | -| sliderPositionPercentage | number (float) | 0.5 | Default line position (from 0 to 1) | -| vertical | boolean | false | Compare images vertically instead of horizontally. The left image is on the top and the right image is on the bottom. | +| Prop (\* required) | type | default | description | +| ------------------------ | ------------------------------- | :---------: | --------------------------------------------------------------------------------------------------------------------- | +| aspectRatio | `'taller'` or `'wider'` | `'taller'` | Which to choose if the aspect ratios of the images are different | +| handle | element | null | Custom handle element. Just pass `` if you want to remove handle. | +| handleSize | number (px) | 40 | diameter of slider handle (by pixel) | +| hover | boolean | false | Whether to slide at hover | +| leftImage \* | string | null | left image's url | +| leftImageAlt | string | `''` | alt props for left image | +| leftImageCss | object | {} | Additional css for left image | +| leftImageLabel | string | null | Label for the image (e.g. `before`) | +| loadingStrategy | `'eager'`, `'lazy'` or `'auto'` | `'auto'` | Loading strategy for images | +| onSliderPositionChange | function | null | Callback function called each time the slider changes. The position (0 to 1) of the slider is passed as arg | +| rightImage \* | string | null | right image's url | +| rightImageAlt | string | `''` | alt props for right image | +| rightImageCss | object | {} | Additional css for right image | +| rightImageLabel | string | null | Label for the image (e.g. `after`) | +| skeleton | element | null | Element displayed while image is loading | +| sliderLineColor | string | `'#ffffff'` | line color of slider | +| sliderLineWidth | number (px) | 2 | line width of slider (by pixel) | +| sliderPositionPercentage | number (float) | 0.5 | Default line position (from 0 to 1) | +| vertical | boolean | false | Compare images vertically instead of horizontally. The left image is on the top and the right image is on the bottom. | ## Supported browzer diff --git a/src/ReactCompareImage.test.tsx b/src/ReactCompareImage.test.tsx index c126ed7..1f4c3bd 100644 --- a/src/ReactCompareImage.test.tsx +++ b/src/ReactCompareImage.test.tsx @@ -134,4 +134,33 @@ describe('ReactCompareImage', () => { dummyRightImageAlt, ); }); + + describe('loading strategy props', () => { + test('seting loading strategy', () => { + const strategy = 'eager'; + + const { getByTestId } = render( + , + ); + + expect(getByTestId('left-image')).toHaveAttribute('loading', strategy); + expect(getByTestId('right-image')).toHaveAttribute('loading', strategy); + }); + + test('default loading strategy', () => { + const { getByTestId } = render( + , + ); + + expect(getByTestId('left-image')).toHaveAttribute('loading', 'auto'); + expect(getByTestId('right-image')).toHaveAttribute('loading', 'auto'); + }); + }); }); diff --git a/src/ReactCompareImage.tsx b/src/ReactCompareImage.tsx index 899ff37..61c5f49 100644 --- a/src/ReactCompareImage.tsx +++ b/src/ReactCompareImage.tsx @@ -19,6 +19,7 @@ interface IProps { sliderLineWidth?: number; sliderPositionPercentage?: number; vertical?: boolean; + loadingStrategy?: 'lazy' | 'eager' | 'auto'; } const defaultProps = { @@ -38,6 +39,7 @@ const defaultProps = { sliderLineWidth: 2, sliderPositionPercentage: 0.5, vertical: false, + loadingStrategy: 'auto' }; const ReactCompareImage: React.FC = props => { @@ -60,6 +62,7 @@ const ReactCompareImage: React.FC = props => { sliderLineWidth, sliderPositionPercentage, vertical, + loadingStrategy, } = props; const horizontal = !vertical; @@ -393,14 +396,17 @@ const ReactCompareImage: React.FC = props => { > setRightImgLoaded(true)} + loading={loadingStrategy} alt={rightImageAlt} data-testid="right-image" ref={rightImageRef} src={rightImage} + style={styles.rightImage} /> setLeftImgLoaded(true)} + loading={loadingStrategy} alt={leftImageAlt} data-testid="left-image" ref={leftImageRef}