Skip to content
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

feat: add container to render image as background behind children [WIP] #236

Merged
merged 43 commits into from Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
c92dcc0
WIP
frederickfogerty Nov 20, 2018
c3c5db6
Merge branch 'master' into fred/background-mode
frederickfogerty Nov 27, 2018
e80a756
chore: wip
frederickfogerty Dec 4, 2018
31931a5
Merge branch 'master' into fred/background-mode
frederickfogerty Dec 4, 2018
0b32307
test: implement more background tests
frederickfogerty Dec 4, 2018
0545553
feat: add html attribute override before load
frederickfogerty Dec 4, 2018
94b2131
test: update test cases
frederickfogerty Dec 7, 2018
2cc11bc
feat: refactor implementation
frederickfogerty Dec 7, 2018
30a50f7
Sync with Prettier
prettierci-commits Dec 7, 2018
767fe92
test: add test for html overrides after element loaded
frederickfogerty Dec 7, 2018
5db73c3
Sync with Prettier
prettierci-commits Dec 7, 2018
d3e1492
Merge branch 'master' into fred/background-mode
frederickfogerty Dec 7, 2018
bc6f9cc
docs: add information about <Background />
frederickfogerty Dec 7, 2018
0fd9380
Sync with Prettier
prettierci-commits Dec 7, 2018
ac3f00b
Merge branch 'master' into fred/background-mode
frederickfogerty Dec 7, 2018
bd311b6
chore: update Background example to use Background
frederickfogerty Dec 9, 2018
3a3b389
chore: remove extra param from imgix url
frederickfogerty Dec 9, 2018
c3b0d46
chore: refactor findNearestWidth to use a binary search for efficiency
frederickfogerty Dec 9, 2018
911f587
test: ensure dpr is rounded to 2dp
frederickfogerty Dec 9, 2018
e90efcb
feat: ensure dpr is rounded to 2dp
frederickfogerty Dec 9, 2018
e88ce58
Sync with Prettier
prettierci-commits Dec 9, 2018
0374cdf
docs: update wording in README
frederickfogerty Dec 10, 2018
2c0e5ec
Merge branch 'master' into fred/background-mode
frederickfogerty Dec 12, 2018
f5d283a
chore: remove browserstack reporter
frederickfogerty Dec 12, 2018
ef9372c
chore: try downgrade karma
frederickfogerty Dec 12, 2018
b93c6c0
chore: try node 9
frederickfogerty Dec 12, 2018
3843d5a
test: use jsuri in tests instead of URL to support IE 11
frederickfogerty Dec 12, 2018
3772db3
Sync with Prettier
prettierci-commits Dec 12, 2018
6eb8e86
chore: skip dpr override test on IE
frederickfogerty Dec 12, 2018
044a61b
Merge branch 'fred/background-mode' of https://github.com/imgix/react…
frederickfogerty Dec 12, 2018
094f6d7
test: refactor test to see if it fixes IE
frederickfogerty Dec 12, 2018
04aaa97
Sync with Prettier
prettierci-commits Dec 12, 2018
6663e47
chore: fix relative import
adamraider Dec 21, 2018
e923c33
Sync with Prettier
prettierci-commits Dec 21, 2018
2edfc7a
chore: fix toFixed
frederickfogerty Dec 21, 2018
17a74db
chore: support SSR with background mode
frederickfogerty Dec 21, 2018
9ec9660
test: skip dpr test in IE
frederickfogerty Dec 21, 2018
18a4a43
test: fix tests on IE
frederickfogerty Dec 21, 2018
f1577f3
Sync with Prettier
prettierci-commits Dec 21, 2018
8dd046d
test: increase maxTries to try reduce flaky tests
frederickfogerty Dec 21, 2018
c75d7e2
Merge branch 'fred/background-mode' of https://github.com/imgix/react…
frederickfogerty Dec 21, 2018
e56107e
test: fix url() trimming on mobile safari
frederickfogerty Dec 21, 2018
7cc8a38
chore: remove unused file
frederickfogerty Dec 21, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
149 changes: 109 additions & 40 deletions README.md
Expand Up @@ -76,10 +76,14 @@ This will generate HTML similar to the following:

```html
<img
src="https://assets.imgix.net/examples/pione.jpg?auto=format&amp;crop=faces&amp;fit=crop&amp;ixlib=react-7.2.0"
sizes="100vw"
srcset="https://assets.imgix.net/examples/pione.jpg?auto=format&amp;crop=faces&amp;fit=crop&amp;ixlib=react-7.2.0&amp;w=100 100w, https://assets.imgix.net/examples/pione.jpg?auto=format&amp;crop=faces&amp;fit=crop&amp;ixlib=react-7.2.0&amp;w=200 200w,..."
>
src="https://assets.imgix.net/examples/pione.jpg?auto=format&amp;crop=faces&amp;fit=crop&amp;ixlib=react-7.2.0"
sizes="100vw"
srcset="
https://assets.imgix.net/examples/pione.jpg?auto=format&amp;crop=faces&amp;fit=crop&amp;ixlib=react-7.2.0&amp;w=100 100w,
https://assets.imgix.net/examples/pione.jpg?auto=format&amp;crop=faces&amp;fit=crop&amp;ixlib=react-7.2.0&amp;w=200 200w,
...
"
/>
```

Since imgix can generate as many derivative resolutions as needed, react-imgix calculates them programmatically, using the dimensions you specify. All of this information has been placed into the srcset and sizes attributes.
Expand Down Expand Up @@ -194,14 +198,14 @@ If you'd like to lazy load images, we recommend using [lazysizes](https://github

```jsx
<Imgix
className="lazyload"
src="..."
sizes="..."
attributeConfig={{
src: 'data-src',
srcSet: 'data-srcset'
sizes: 'data-sizes'
}}
className="lazyload"
src="..."
sizes="..."
attributeConfig={{
src: 'data-src',
srcSet: 'data-srcset'
sizes: 'data-sizes'
}}
/>
```

Expand All @@ -215,17 +219,17 @@ If you'd like to use LQIP images, like before, we recommend using [lazysizes](ht

```jsx
<Imgix
className="lazyload"
src="..."
sizes="..."
attributeConfig={{
src: 'data-src',
srcSet: 'data-srcset'
sizes: 'data-sizes'
}}
htmlAttributes={{
src: '...' // low quality image here
}}
className="lazyload"
src="..."
sizes="..."
attributeConfig={{
src: 'data-src',
srcSet: 'data-srcset'
sizes: 'data-sizes'
}}
htmlAttributes={{
src: '...' // low quality image here
}}
/>
```

Expand Down Expand Up @@ -259,16 +263,16 @@ In order to reduce the duplication in props, JSX supports object spread for prop
import Imgix, { Picture, Source } from 'react-imgix'

const commonProps = {
src: 'https://...',
imgixParams: {
fit: 'crop',
crop: 'faces'
}
src: 'https://...',
imgixParams: {
fit: 'crop',
crop: 'faces'
}
}

<Picture>
<Source
{...commonProps}
<Source
{...commonProps}
width={400}
htmlAttributes={{ media: "(min-width: 768px)" }}
/>
Expand All @@ -295,7 +299,44 @@ This works for Source and Picture elements as well.

#### Background mode

This feature has been removed from react-imgix when `sizes` and `srcset` was implemented. It was decided that it was too hard to implement this feature consistently. If you would still like to use this feature, please give this issue a thumbs up: [https://github.com/imgix/react-imgix/issues/160](https://github.com/imgix/react-imgix/issues/160) If we get enough requests for this, we will re-implement it.
Images can be rendered as the background of a container around some children by using `<Background />`. The component will measure the natural size of the container as determined by the CSS on the page, and will render an optimal image for those dimensions.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "around some children" part of this sentence is very confusing to me. Is there a way to convey this better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it could use "behind" instead? Here are some options:

  • Images can be rendered as a background behind children by using <Background />.
  • <Background /> allows an image to be the background behind its children.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the first one best.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is done.


Example:

```jsx
// In CSS
.blog-title {
width: 100vw;
height: calc(100vw - 100px);
}

// In Component (React)
import { Background } from 'react-imgix'

<Background src="https://.../image.png" className="blog-title">
<h2>Blog Title</h2>
</Background>
```

This component shares a lot of props that are used in the main component, such as `imgixParams`, and `htmlAttributes`.

As the component has to measure the element in the DOM, it will mount it first and then re-render with an image as the background image. Thus, this technique doesn't work very well with server rendering. If you'd like for this to work well with server rendering, you'll have to set a width and height manually.

**Set width and height:**

Setting the width and/or height explicitly is recommended if you already know these beforehand. This will save the component from having to do two render passes, and it will render a background image immediately.

This is accomplished by passing `w` and `h` as props to imgixParams.

```jsx
<Background
src="https://.../image.png"
imgixParams={{ w: 1920, h: 500 }}
className="blog-title"
>
<h2>Blog Title</h2>
</Background>
```

#### Custom URLS

Expand Down Expand Up @@ -368,11 +409,11 @@ Called on `componentDidMount` with the mounted DOM node as an argument.
Allows the src, srcset, and sizes attributes to be remapped to different HTML attributes. For example:

```js
attributeConfig={{
src: 'data-src',
srcSet: 'data-srcset'
sizes: 'data-sizes'
}}
attributeConfig={{
src: 'data-src',
srcSet: 'data-srcset'
sizes: 'data-sizes'
}}
```

This re-maps src to `data-src`, srcSet to `data-srcset`, etc.
Expand All @@ -391,6 +432,34 @@ Called on `componentDidMount` with the mounted DOM node as an argument.

Any other attributes to add to the html node (example: `alt`, `data-*`, `className`).

#### Background Props

##### src :: string, required

Usually in the form: `https://[your_domain].imgix.net/[image]`. Don't include any parameters.

##### imgixParams :: object

Imgix params to add to the image `src`. This is also how width and height can be explicitly set. For more information about this, see the "Background" section above.

_For example_:

```js
<Imgix imgixParams={{ mask: "ellipse" }} />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this example use <Background instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

```

##### className :: string

`className` applied to top level component. To set `className` on the image itself see `htmlAttributes`.

##### disableLibraryParam :: bool

By default this component adds a parameter to the generated url to help imgix with analytics and support for this library. This can be disabled by setting this prop to `true`.

##### htmlAttributes :: object

Any other attributes to add to the html node (example: `alt`, `data-*`, `className`).

### Global Configuration

#### Warnings
Expand Down Expand Up @@ -433,14 +502,14 @@ To upgrade to version 8, the following changes should be made.

// this...
<Imgix type='picture'>
<Imgix type='source' src={src}>
<Imgix type='source' src={src}>
<Imgix type='source' src={src}>
<Imgix type='source' src={src}>
</Imgix>

// becomes...
<Picture>
<Source src={src}>
<Source src={src}>
<Source src={src}>
<Source src={src}>
</Picture>

See [Picture support](#picture-support) for more information.
Expand Down
24 changes: 21 additions & 3 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -53,6 +53,7 @@
"dependencies": {
"js-base64": "^2.3.2",
"jsuri": "^1.3.1",
"react-measure": "^2.1.3",
"shallowequal": "^1.1.0",
"warning": "^4.0.1"
},
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Expand Up @@ -6,3 +6,5 @@ export { buildURL };

export default ReactImgix;
export { Picture, Source, PublicConfigAPI };

export { Background } from "./react-imgix-bg";