-
-
Notifications
You must be signed in to change notification settings - Fork 755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Image for srcset and lazy loading #2103
Changes from 43 commits
7d6d66a
46f7527
4f295fb
fa9f0bc
7c36913
2c651dc
eda9c57
fa0df06
a3a8df9
1cfa340
37b1c15
5a183c6
397bee8
86b7ade
542b86f
4da4fb0
2bf73d9
931fd27
d3fe9bb
c168e48
7540b9f
e9a030e
a786b98
fbf2c93
d88e7fd
e458667
5bd771d
a748813
1cb2b46
5f6beb9
e0ee9e5
5cceee9
f4bb5d4
5ff9660
b828e35
103b8f7
1fc051b
8f89c41
071fd69
a210d23
609ecdb
ceda054
86df9d1
4b56ef2
1c2b895
1ac4e2b
4fc573a
1576e3d
53925eb
98e0f46
cf533c1
47df2bd
1932873
4637a37
7b6af2e
3660875
d22b2a3
a5af866
b3c879d
fba8125
af854dc
ed1e9e5
5418d2b
c74c3b3
5a101df
6f4ff20
1fb6a0a
4d764ca
a07bc4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Images | ||
|
||
Images are rendered with the Volto component `Image`. | ||
It renders images using Plone scales from [plone.app.imaging](https://github.com/plone/plone.app.imaging) and handles: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor documentation comment: |
||
|
||
- Lazy load of images | ||
- Loads the most correct one for current image size with `srcset` | ||
|
||
## Usage | ||
|
||
```jsx | ||
import Image from '@plone/volto/components/theme/Image/Image'; | ||
|
||
... | ||
|
||
<Image | ||
className="listing-image" | ||
image={item.image} | ||
aria-hidden="true" | ||
alt="" | ||
useOriginal={false} | ||
maxSize={400} | ||
/> | ||
``` | ||
|
||
## Props | ||
|
||
| prop | type | default | description | | ||
| ----------- | ---------------- | ------- | ------------------------------------------------------------------------------ | | ||
| image | object or string | | Plone image as object or URL | | ||
| alt | string | `''` | Alternative text for the image | | ||
| className | string | | CSS class for the image | | ||
| role | string | `img` | img role attribute | | ||
| critical | boolean | `false` | If true, it will render the actual image on SSR and it will not be lazy loaded | | ||
| maxSize | number | | Maximum size to render, will skip the larger ones | | ||
| useOriginal | boolean | `false` | If true, it will includes the original size in srcset | | ||
|
||
### image prop | ||
|
||
If it is an object, it assumes it is a Plone Image content object and it will render the scales it has inside. | ||
Else if it is a string, it checks if it is an internal URL: if so, it will generate the scales URLs based on the settings in `config.settings.imageScales` which should match the ones in Plone imaging controlpanel (so in Plone registry). | ||
If it is an external URL, it will set it as source for the image. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { flattenToAppURL } from '@plone/volto/helpers'; | ||
import Image from '@plone/volto/components/theme/Image/Image'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not from components?
|
||
import { LinkMore } from '@plone/volto/components'; | ||
|
||
/** | ||
|
@@ -16,14 +16,7 @@ import { LinkMore } from '@plone/volto/components'; | |
const View = ({ data }) => ( | ||
<div className="block hero"> | ||
<div className="block-inner-wrapper"> | ||
{data.url && ( | ||
<img | ||
src={`${flattenToAppURL(data.url)}/@@images/image`} | ||
alt="" | ||
className="hero-image" | ||
loading="lazy" | ||
/> | ||
)} | ||
{data.url && <Image image={data.url} className="hero-image" />} | ||
<div className="hero-body"> | ||
<div className="hero-text"> | ||
{data.title && <h1>{data.title}</h1>} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,23 @@ import { render } from '@testing-library/react'; | |
import { MemoryRouter } from 'react-router-dom'; | ||
import View from './View'; | ||
|
||
import config from '@plone/volto/registry'; | ||
|
||
config.settings.imageScales = { | ||
large: 768, | ||
preview: 400, | ||
mini: 200, | ||
thumb: 128, | ||
tile: 64, | ||
icon: 32, | ||
listing: 16, | ||
}; | ||
|
||
describe('Image View Component', () => { | ||
test('renders a view image component with a local image', () => { | ||
const { getByRole } = render(<View data={{ url: '/image.jpg' }} />); | ||
const img = getByRole('img'); | ||
expect(img).toHaveAttribute('src', '/image.jpg/@@images/image'); | ||
expect(img).toHaveAttribute('src', '/image.jpg/@@images/image/listing'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
expect(img).toHaveAttribute('loading', 'lazy'); | ||
}); | ||
test('renders a view image component with a local image with a link', () => { | ||
|
@@ -19,7 +31,7 @@ describe('Image View Component', () => { | |
); | ||
const img = getByRole('img'); | ||
const a = container.querySelector('a'); | ||
expect(img).toHaveAttribute('src', '/image.jpg/@@images/image'); | ||
expect(img).toHaveAttribute('src', '/image.jpg/@@images/image/listing'); | ||
expect(a).toHaveAttribute('href', '/front-page'); | ||
}); | ||
test('renders a view image component with an external image', () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nzambello Maybe we should use thumb or something else as there are discussions to remove low resolution scales. @tisto ^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I think we already agreed that the listings would use the new
preview
scale.icon would be far too small