https://github.com/csssr/csssr.images
responsive images + imgproxy, webpack and react utilities
https://github.com/csssr/csssr.images
internal
Last synced: 2 months ago
JSON representation
responsive images + imgproxy, webpack and react utilities
- Host: GitHub
- URL: https://github.com/csssr/csssr.images
- Owner: CSSSR
- License: mit
- Created: 2020-05-24T16:28:06.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-02-01T14:00:09.000Z (over 1 year ago)
- Last Synced: 2024-04-13T16:19:46.274Z (over 1 year ago)
- Topics: internal
- Language: TypeScript
- Homepage: http://master.csssr-images.csssr.cloud/
- Size: 4.18 MB
- Stars: 4
- Watchers: 20
- Forks: 0
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# csssr.images
An opinionated library for handling responsive images with help of [imgproxy](https://imgproxy.net/).
It takes your images, generates optimized 1x, 2x and 3x versions of them and output them as 's srcSet or background css.
## Overview
Having breakpoints like that
```
mobile: (max-width: 767px)
tablet: (min-width: 768px, max-width: 1279px)
desktop: (min-width: 1280px)
```
and images structured like that
```
images/
myImage/ <-| picture name
mobile.png <-| different image variations,
tablet.png <-| names should be the same as breakpoint names above,
desktop.png <-| images should be in 3x resolution
```
this lib helps you to get```html
```
## Install
```console
npm i @csssr/csssr.images
```## Usage
I recommend you to check [the example](./example).
Add loader to your `webpack` config:
**webpack.config.js**
```js
module.exports = {
module: {
rules: [
{
test: /\.(png|jpe?g|webp)$/i,
use: [
{
loader: '@csssr/csssr.images',
options: {
// @csssr/csssr.images loader options, see below
},
},
// file-loader is required
loader: 'file-loader',
options: {
// file-loader options by your preference
},
},
],
},
],
},
}
```Then `import` the target file or `require.context()` target files:
**file.js**
```js
import images from ('./images/foo/boo.png')
```or
```js
const images = require.context('./images/foo')
```The loader will accept your image as the initial and will generate optimized version. Next, it will emit this image on the specified path and return the object, which will contain the path to the all versions of the image and srcSet.
## Options
### `resolution`
Type: `String`
Required: `true`Resolution of original image. Could be `1x`, `2x` or `3x`.
> ℹ️ It is recommended to set the height and width of the initial image multiple of value you specify in this option
### `breakpoints`
Type: `Array`
Required: `true`Array of breakpoints for which you want generate optimized images. Each breakpoint require a `name` and `minWidth` or `maxWidth` fields. Example:
```javascript
[
{
name: 'mobile',
maxWidth: 767,
},
{
name: 'tablet',
minWidth: 768,
maxWidth: 1279,
},
{
name: 'desktop',
minWidth: 1280,
}
]
```Order of breakpoints matter — you should start from the smallest.
> ℹ️ You should name your images according to the breakpoints for which they will be used, with one exception - name `all`. Image with name `all` will be last in source list and will be default, if there is no image for particular breakpoint. See [the example](./example).
### `imgproxy`
Type: `Object`
Required: `true`Object that contains settings for [imgproxy](https://github.com/imgproxy/imgproxy) server.
#### `disable`
Type: `Boolean`
Default: `false`
Required: `false`Disable processing of images by this loader (useful in development). Images data will still be generated but only for the original resolution.
#### `imagesHost`
Type: `string`
Required: `true`URL of your images host.
#### `host`
Type: `string`
Required: `true`URL of your imgproxy host.
## Components
### `Picture`
Returns tag with tags according to your breakpoints.#### `Props`
##### `sources`
Type: [`BreakpointSource`](./src/types.ts)
Required: `true`Accepts an array of objects that contains information about images srsSets for different breakpoints.
##### `alt`
Type: `String`
Required: `false`Alt text for
.
##### `testId`
Type: `String`
Required: `false``data-testid` attribute for
.
##### `loading`
Type: `String`
Required: `false``loading` attribute for
. Can be one of: `eager`, `lazy`.
##### `className`
Type: `String`
Required: `false``className` attribute for .
### `PictureSmart`
Accepts all the same props as [`Picture`](#Picture), except `requireImages`.
#### `Props`
##### `requireImages`
Type: `__WebpackModuleApi.RequireContext`
Required: `true`Accepts webpack `require.context()` object and builds from it [`sources`](#sources) array. See [the example](./example).
## Utils
### getOriginal
Accepts [`BreakpointSource`](./src/types.ts) array and return biggest URL to original image
```js
const pictureData = require('./images/foo/boo.png')
getOriginal(pictureData)
```
#### pictureData
Type: [`BreakpointSource`](./src/types.ts)
An array of objects that contains information about images srsSets for different breakpoints### backgroundCss
Accepts CSS selector and [`BreakpointSource`](./src/types.ts) object and return css string that contains rules for `background-image` for your breakpoints for provided selector.
```js
const pictureData = require('./images/foo/boo.png')
const selector = '.boo'
backgroundCss(selector, pictureData)
```
Will return
```css
.boo {
background-image:url(*generated url to your image*);
}@media *media rules for one of your breakpoints* {
.boo {
background-image: url(*generated url to your image*);
}
}@media *media rules for one of your breakpoints* {
.boo{
background-image:url(*generated url to your image*)
}
}
```
#### selector
Type: `String`
CSS selector for which you want add optimized images#### pictureData
Type: [`BreakpointSource`](./src/types.ts)
An array of objects that contains information about images srsSets for different breakpoints### backgroundCssSmart
Works the same as [`backgroundCss`](#backgroundCss) but instead of [`BreakpointSource`](./src/types.ts) it accepts `__WebpackModuleApi.RequireContext` object.
```js
const pictureData = require.context('./images/foo')
backgroundCss('.boo', pictureData)
```## License
[MIT](./LICENSE)