Skip to content

Commit

Permalink
feat: deprecate images photo prop in favor of srcSet
Browse files Browse the repository at this point in the history
  • Loading branch information
igordanchenko committed Apr 26, 2023
1 parent a353e70 commit 7799316
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 58 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ as possible.
React Photo Album automatically generates `sizes` and `srcset` image attributes. In the case of SSR, React Photo Album
includes `sizes` and `srcset` image attributes in the server-rendered markup, allowing browsers to pick images of the
most appropriate resolution depending on their viewport size. To enable images with automatic resolution switching,
simply provide smaller images in the photo `images` attribute.
simply provide smaller images in the photo `srcSet` attribute.

```js
import PhotoAlbum from "react-photo-album";
Expand All @@ -110,7 +110,7 @@ const photos = [
src: "/images/image1_800x600.jpg",
width: 800,
height: 600,
images: [
srcSet: [
{ src: "/images/image1_400x300.jpg", width: 400, height: 300 },
{ src: "/images/image1_200x150.jpg", width: 200, height: 150 }
]
Expand All @@ -119,7 +119,7 @@ const photos = [
src: "/images/image2_1600x900.jpg",
width: 1600,
height: 900,
images: [
srcSet: [
{ src: "/images/image2_800x450.jpg", width: 800, height: 450 },
{ src: "/images/image2_400x225.jpg", width: 400, height: 225 }
]
Expand Down
2 changes: 1 addition & 1 deletion examples/cdn/photos.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var photos = unsplashPhotos.map((photo) => ({
src: unsplashLink(photo.id, photo.width, photo.height),
width: photo.width,
height: photo.height,
images: breakpoints.map((breakpoint) => {
srcSet: breakpoints.map((breakpoint) => {
const height = Math.round((photo.height / photo.width) * breakpoint);
return {
src: unsplashLink(photo.id, breakpoint, height),
Expand Down
2 changes: 1 addition & 1 deletion examples/columns-layout/src/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const photos = unsplashPhotos.map((photo) => ({
src: unsplashLink(photo.id, photo.width, photo.height),
width: photo.width,
height: photo.height,
images: breakpoints.map((breakpoint) => {
srcSet: breakpoints.map((breakpoint) => {
const height = Math.round((photo.height / photo.width) * breakpoint);
return {
src: unsplashLink(photo.id, breakpoint, height),
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-renderers/src/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const photos: Photo[] = unsplashPhotos.map((photo) => ({
src: unsplashLink(photo.id, photo.width, photo.height),
width: photo.width,
height: photo.height,
images: breakpoints.map((breakpoint) => {
srcSet: breakpoints.map((breakpoint) => {
const height = Math.round((photo.height / photo.width) * breakpoint);
return {
src: unsplashLink(photo.id, breakpoint, height),
Expand Down
4 changes: 1 addition & 3 deletions examples/lightbox/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import "yet-another-react-lightbox/plugins/thumbnails.css";

import photos from "./photos";

const slides = photos.map(({ src, width, height, images }) => ({ src, width, height, srcSet: images }));

export default function App() {
const [index, setIndex] = useState(-1);

Expand All @@ -24,7 +22,7 @@ export default function App() {
<PhotoAlbum photos={photos} layout="rows" targetRowHeight={150} onClick={({ index }) => setIndex(index)} />

<Lightbox
slides={slides}
slides={photos}
open={index >= 0}
index={index}
close={() => setIndex(-1)}
Expand Down
2 changes: 1 addition & 1 deletion examples/lightbox/src/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const photos = unsplashPhotos.map((photo) => {
src: unsplashLink(photo.id, width, height),
width,
height,
images: breakpoints.map((breakpoint) => {
srcSet: breakpoints.map((breakpoint) => {
const height = Math.round((photo.height / photo.width) * breakpoint);
return {
src: unsplashLink(photo.id, breakpoint, height),
Expand Down
2 changes: 1 addition & 1 deletion examples/masonry-layout/src/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const photos = unsplashPhotos.map((photo) => ({
src: unsplashLink(photo.id, photo.width, photo.height),
width: photo.width,
height: photo.height,
images: breakpoints.map((breakpoint) => {
srcSet: breakpoints.map((breakpoint) => {
const height = Math.round((photo.height / photo.width) * breakpoint);
return {
src: unsplashLink(photo.id, breakpoint, height),
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/src/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const photos = unsplashPhotos.map((photo) => ({
src: unsplashLink(photo.id, photo.width, photo.height),
width: photo.width,
height: photo.height,
images: breakpoints.map((breakpoint) => {
srcSet: breakpoints.map((breakpoint) => {
const height = Math.round((photo.height / photo.width) * breakpoint);
return {
src: unsplashLink(photo.id, breakpoint, height),
Expand Down
2 changes: 1 addition & 1 deletion examples/playground/src/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const photos = unsplashPhotos.map((photo) => ({
src: unsplashLink(photo.id, photo.width, photo.height),
width: photo.width,
height: photo.height,
images: breakpoints.map((breakpoint) => {
srcSet: breakpoints.map((breakpoint) => {
const height = Math.round((photo.height / photo.width) * breakpoint);
return {
src: unsplashLink(photo.id, breakpoint, height),
Expand Down
2 changes: 1 addition & 1 deletion examples/rows-layout/src/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const photos = unsplashPhotos.map((photo) => ({
src: unsplashLink(photo.id, photo.width, photo.height),
width: photo.width,
height: photo.height,
images: breakpoints.map((breakpoint) => {
srcSet: breakpoints.map((breakpoint) => {
const height = Math.round((photo.height / photo.width) * breakpoint);
return {
src: unsplashLink(photo.id, breakpoint, height),
Expand Down
2 changes: 1 addition & 1 deletion examples/sortable-gallery/src/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const photos = unsplashPhotos.map((photo) => ({
src: unsplashLink(photo.id, photo.width, photo.height),
width: photo.width,
height: photo.height,
images: breakpoints.map((breakpoint) => {
srcSet: breakpoints.map((breakpoint) => {
const height = Math.round((photo.height / photo.width) * breakpoint);
return {
src: unsplashLink(photo.id, breakpoint, height),
Expand Down
68 changes: 34 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@rollup/plugin-typescript": "^11.1.0",
"@types/jest": "^29.5.1",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.0",
"@types/react-dom": "^18.2.1",
"@types/react-test-renderer": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.1",
"@typescript-eslint/parser": "^5.59.1",
Expand All @@ -66,7 +66,7 @@
"husky": "^8.0.3",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"lint-staged": "^13.2.1",
"lint-staged": "^13.2.2",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.8",
"react": "^18.2.0",
Expand Down
7 changes: 4 additions & 3 deletions src/components/renderers/PhotoRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ function srcSetAndSizes<T extends Photo = Photo>(photo: T, layout: PhotoLayout,
let srcSet;
let sizes;

if (photo.images && photo.images.length > 0) {
srcSet = photo.images
const images = photo.srcSet || photo.images;
if (images && images.length > 0) {
srcSet = images
.concat(
!photo.images.find(({ width }) => width === photo.width)
!images.find(({ width }) => width === photo.width)
? [{ src: photo.src, width: photo.width, height: photo.height }]
: []
)
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ export interface Photo extends Image {
alt?: string;
/** Optional image `title` attribute. */
title?: string;
/** Optional array of alternative images to be included in the `srcset` attribute. */
// TODO v3: remove
/** @deprecated - use `srcSet` instead */
images?: Image[];
/** Optional array of alternative images to be included in the `srcset` attribute. */
srcSet?: Image[];
}

export type RenderPhotoProps<T extends Photo = Photo> = {
Expand Down
6 changes: 3 additions & 3 deletions test/PhotoAlbum.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import photos from "./photos";

const photosWithSrcSet = photos.map((photo) => ({
...photo,
images: [{ src: photo.src, width: Math.round(photo.width / 2), height: Math.round(photo.height / 2) }],
srcSet: [{ src: photo.src, width: Math.round(photo.width / 2), height: Math.round(photo.height / 2) }],
}));

const createNodeMock =
Expand Down Expand Up @@ -454,11 +454,11 @@ describe("PhotoAlbum", () => {
whenAskedToRender(
<PhotoAlbum
layout="rows"
photos={photosWithSrcSet.map(({ src, width, height, images }) => ({
photos={photosWithSrcSet.map(({ src, width, height, srcSet }) => ({
src,
width,
height,
images: [{ src, width, height }, ...images],
srcSet: [{ src, width, height }, ...srcSet],
}))}
/>
);
Expand Down

0 comments on commit 7799316

Please sign in to comment.