5.1.0 • Published 1 year ago

react-sorcerer v5.1.0

Weekly downloads
11
License
MIT
Repository
github
Last release
1 year ago

React Sorcerer

https://img.shields.io/github/license/brybrophy/react-sorcerer?color=blue https://img.shields.io/npm/v/react-sorcerer https://img.shields.io/bundlephobia/minzip/react-sorcerer https://img.shields.io/librariesio/release/npm/react-sorcerer

React Sorcerer is a react component and companion hook that helps you use source sets for your img tags.

Source sets work by using a set of image sources to allow the browser to make a calculated decision and display the most optimal image for the current screen size. They increase site performance, and save your users bandwidth. They are like the Goldilocks of image optimization.

Demo

react-sorcerer-demo

Install

yarn add react-sorcerer

Use as a Component

import { Sorcerer } from 'react-sorcerer';

<Sorcerer
  alt="an image"
  coverage={100}
  src="https://dummyimage.com/1024x800/f00/fff"
  srcSetData={[
    {
      src: 'https://dummyimage.com/300x200/f00/fff',
      width: 300,
    },
    {
      src: 'https://dummyimage.com/400x300/f00/fff',
      width: 400,
    },
    {
      src: 'https://dummyimage.com/800x600/f00/fff',
      width: 800,
    },
    {
      src: 'https://dummyimage.com/1100x800/f00/fff',
      width: 1100,
    },
  ]}
/>;

Use as a Hook

import { useSorcerer } from 'react-sorcerer';

const imgSrcData = useSorcerer({
  alt: 'an image',
  coverage: 100,
  src: 'https://dummyimage.com/1024x800/f00/fff',
  srcSetData: [
    {
      src: 'https://dummyimage.com/300x200/f00/fff',
      width: 300,
    },
    {
      src: 'https://dummyimage.com/400x300/f00/fff',
      width: 400,
    },
    {
      src: 'https://dummyimage.com/800x600/f00/fff',
      width: 800,
    },
    {
      src: 'https://dummyimage.com/1100x800/f00/fff',
      width: 1100,
    },
  ],
});

<img {...imgSrcData} />;

Note: The coverage prop is used to help the browser determine which image to use at each screen size based on the image's width. A full width image would have a coverage of 100. An image in a 4 column grid might have a coverage of 25.

Props

NameTypeRequiredDescription
altstringtruealt tag for the image
classNamestringfalseclassName for the image
coveragenumbertruean estimate of the display width of the image in percentage
srcstringtruethe src of the main image
srcSetData{ src: string, width: number }[]truean array of image sources and their width in pixels
styleobjectfalsestyle object for the image

License

MIT

Copyright (c) 2017 Bryan Brophy.

5.1.0

1 year ago

5.0.2

2 years ago

5.0.0

3 years ago

4.0.9

4 years ago

4.0.8

4 years ago

4.0.7

4 years ago

4.0.6

4 years ago

4.0.5

4 years ago

4.0.4

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.14

7 years ago

2.0.13

7 years ago

2.0.12

7 years ago

2.0.11

7 years ago

2.0.9

7 years ago

2.0.8

7 years ago

2.0.7

7 years ago

2.0.6

7 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.0

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago