1.0.7 • Published 3 years ago

react-multisource-image v1.0.7

Weekly downloads
25
License
MIT
Repository
github
Last release
3 years ago

react-multisource-image

Picture with source tags https://developer.mozilla.org/ru/docs/Web/HTML/Element/picture and media queries to automatically adjust for different screens

NPM JavaScript Style Guide

Install

npm install --save react-multisource-image

or

yarn add react-multisource-image

Usage

import React, { Component } from 'react'
import MultiSourceImage from 'react-multisource-image'

class Example extends Component {
  render() {
    return <MultiSourceImage
      // ignore all jpeg formats
      ignore={["image/jpeg"]}
      sources={{
        defaultSource: {
          url: "/uploads/hello_origin.jpg",
          mime: "image/jpeg"
        },
        formats: {
          large: [
            {
              url: "/uploads/hello.jpg",
              mime: "image/jpeg"
            },
            {
              url: "/uploads/hello.webp",
              mime: "image/webp"
            },
          ],
          medium: [
            {
              url: "/uploads/hello_medium.jpg",
              mime: "image/jpeg"
            },
            {
              url: "/uploads/hello_medium.webp",
              mime: "image/webp"
            },
          ],
          small: [
            {
              url: "/uploads/hello_small.jpg",
              mime: "image/jpeg"
            },
            {
              url: "/uploads/hello_small.webp",
              mime: "image/webp"
            },
            {
              url: "/uploads/hello_small.png",
              mime: "image/png"
            },
          ],
          thumbnail: [
            {
              url: "/uploads/hello_thumbnail.jpg",
              mime: "image/jpeg"
            },
            {
              url: "/uploads/hello_thumbnail.webp",
              mime: "image/webp"
            },
          ]
        }
      }}
      mediaMapping={{
        large: '(min-width: 1440px)',
        medium: '(min-width: 960px)'
      }}
      alt="Some alternative text"
      backend="https://hello.backend.com"
    />
  }
}

Possible Arguments

ArgumentTypeDescription
sourcesrequiredContains a structure as in this example
mediaMappingoptionalContains a structure as in this example
altrequiredAlternative text for img
backendoptionalBackend host e.g http://backend.com is added to the url of the picture
styleoptionalReact style
ignoreoptionalArray for skip any mime types
classNameoptionalClass Name

Sources structure

{
  // default image source
  "defaultSource": {
    "url": "/some.jpg",
    "mime": "image/jpeg"
  },
  "formats": {
    "large": [
      {
        "url": "/some_large.jpg",
        "mime": "image/jpeg"
      },
      {
        "url": "/some_large.webp",
        "mime": "image/webp"
      }
      //...other formats
    ],
    // medium, small, thumbnail etc.
  }
}

Media Mapping structure

Queries will be applied by the key to all nested formats

{
  large: '(min-width: 1440px)',
  medium: '(min-width: 960px)',
  //... optional small, thumbnail etc.
}

License

MIT © stovv

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago