All Projects → braposo → React Responsive Picture

braposo / React Responsive Picture

Licence: mit
A future-proof responsive image component that supports latest Picture specification

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Responsive Picture

Html To Image
✂️ Generates an image from a DOM node using HTML5 canvas and SVG.
Stars: ✭ 595 (+553.85%)
Mutual labels:  image, picture
Flickrsync
A command line tool to synchronise, upload, download, pictures between the local file system and Flickr. Image hash signature of the picture is used to uniquely identify the image.
Stars: ✭ 14 (-84.62%)
Mutual labels:  image, picture
Tysnapshotscroll
一句代码保存截图,将 UIScrollView UITableView UICollectionView UIWebView WKWebView 网页 保存 为 长图 查看。Save the scroll view page as an image,support UIScrollView,UITableView,UICollectionView,UIWebView,WKWebView.(Support iOS13)
Stars: ✭ 709 (+679.12%)
Mutual labels:  image, picture
Renderhelp
⚡️ 可编程渲染管线实现,帮助初学者学习渲染
Stars: ✭ 494 (+442.86%)
Mutual labels:  image, picture
Imgviewer
jQuery plugin to zoom and pan images, even those with a size that is a percentage of their container
Stars: ✭ 50 (-45.05%)
Mutual labels:  image, responsive
React Viewer
react image viewer, supports rotation, scale, zoom and so on
Stars: ✭ 502 (+451.65%)
Mutual labels:  image, picture
Vue Picture Input
Mobile-friendly picture file input Vue.js component with image preview, drag and drop, EXIF orientation, and more
Stars: ✭ 862 (+847.25%)
Mutual labels:  image, picture
griding
🧱 lean grid & responsive for react
Stars: ✭ 18 (-80.22%)
Mutual labels:  responsive, react-component
Color.js
Extract colors from an image (0.75 KB) 🎨
Stars: ✭ 42 (-53.85%)
Mutual labels:  image, picture
React Responsive Image
🖼️ A React responsive image component.
Stars: ✭ 36 (-60.44%)
Mutual labels:  image, responsive
React Image Magnify
A responsive image zoom component designed for shopping sites.
Stars: ✭ 391 (+329.67%)
Mutual labels:  image, picture
Gopherkon
Go mascot image constructor. Create your cute own gopher.
Stars: ✭ 86 (-5.49%)
Mutual labels:  image, picture
Guillotine
jQuery plugin to crop images within an area (fully responsive), allowing to drag (touch support), zoom and rotate.
Stars: ✭ 318 (+249.45%)
Mutual labels:  image, responsive
React Native Fit Image
Responsive image component to fit perfectly itself.
Stars: ✭ 539 (+492.31%)
Mutual labels:  image, responsive
Pyinstastories
Python script to download Instagram stories from Instagram users.
Stars: ✭ 260 (+185.71%)
Mutual labels:  image, picture
Glightbox
Pure Javascript lightbox with mobile support. It can handle images, videos with autoplay, inline content and iframes
Stars: ✭ 702 (+671.43%)
Mutual labels:  image, responsive
react-notification-alert
React bootstrap 4 notification alert
Stars: ✭ 34 (-62.64%)
Mutual labels:  responsive, react-component
react-super-styled
Responsive JSX layouts with Styled Components
Stars: ✭ 77 (-15.38%)
Mutual labels:  responsive, react-component
React Grid Carousel
React responsive carousel component w/ grid layout
Stars: ✭ 29 (-68.13%)
Mutual labels:  image, react-component
React Bps
🔱 Create breakpoints to your component props
Stars: ✭ 64 (-29.67%)
Mutual labels:  react-component, responsive

react-responsive-picture

A future-proof responsive image component that supports latest <picture> specification. Uses picturefill for backward compatibility from IE9+.

npm version npm downloads gzip size MIT License PRs Welcome


Installation

npm install react-responsive-picture or yarn add react-responsive-picture

Playground

Edit react-responsive-picture

You can also run the examples by cloning the repo and running yarn start.

Usage

import { Picture } from 'react-responsive-picture';

function SomeComponent() {
    return (
        <Picture
            sources = {[
                {
                    srcSet: "path-to-mobile-image.jpg, path-to-mobile-image@2x.jpg 2x",
                    media: "(max-width: 420px)",
                },
                {
                    srcSet: "path-to-desktop-image.jpg 1x, path-to-desktop-image@2x.jpg 2x",
                },
                {
                    srcSet: "path-to-desktop-image.webp",
                    type: "image/webp"
                }
            ]}
        />
    );
}

Props

Prop Type Default Definition
sources array The array of source objects. Check Sources section for more information.
src string (transparent pixel) Source for standalone/fallback image. To prevent issues in some browsers, by default src is set to a 1x1 transparent pixel data image.
sizes string Sizes attribute to be used with src for determing best image for user's viewport.
alt string Alternative text for image
className string Any additional CSS classes you might want to use to style the image

Examples

Simple image

Normal <img> like behaviour. The same image is displayed on every device/viewport.

<Picture src="path-to-image.jpg" />

will render:

<img srcset="path-to-image.jpg" />

Image with different resolutions

Different images for specific devices (usually retina).

<Picture src="[email protected] 2x, path-to-image.png 1x" />

will render:

<img srcset="[email protected] 2x, path-to-image.png 1x" />

Image with sizes

When you want to let the browser determine the best image for user's current viewport. More information about size attribute on this great blog post.

<Picture
    src="large.jpg 1024w, medium.jpg 640w, small.jpg 320w"
    sizes="(min-width: 36em) 33.3vw, 100vw"
/>

will render:

<img srcset="large.jpg 1024w, medium.jpg 640w, small.jpg 320w" sizes="(min-width: 36em) 33.3vw, 100vw" />

Image with art direction

When you want to explicitly control which image is displayed at specific viewport sizes.

<Picture
    sources = {[
        {
            srcSet: "path-to-mobile-image.jpg, [email protected] 2x",
            media: "(max-width: 420px)",
        },
        {
            srcSet: "path-to-desktop-image.jpg 1x, [email protected] 2x",
        },
        {
            srcSet: "path-to-desktop-image.webp",
            type: "image/webp"
        }
    ]}
/>

will render:

<picture>
    <source srcset="path-to-mobile-image.jpg, [email protected] 2x" media="(max-width: 420px)">
    <source srcset="path-to-desktop-image.jpg 1x, [email protected] 2x">
    <source srcset="path-to-desktop-image.webp" type="image/webp">
    <img srcset="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" />
</picture>

The sources prop is where you can determine the behaviour of the <Picture> component and which images will show for the specific screens.

For each source you can send an object containing srcSet, media and type although the last two are optional.

Styling

You can use your favourite styling library and style the Picture component using the className prop.

import { css } from "emotion";

<Picture 
    className={css`
      opacity: 0.7;
    `}
    src="[email protected] 2x, path-to-image.png 1x" 
/>

Fullsize images

There's also a <FullsizePicture> component that you can use to display full-size images using the same benefits of <Picture> for art direction.

<div style={{ height: 200 }}>
    <FullsizePicture
        sources = {[
            {
                srcSet: "path-to-mobile-image.jpg, path-to-mobile-image@2x.jpg 2x",
                media: "(max-width: 420px)",
            },
            {
                srcSet: "path-to-desktop-image.jpg 1x, path-to-desktop-image@2x.jpg 2x",
            },
        ]}
    />
</div>

It will automatically fill the parent element maintaining the original image ratio. Please note that the parent element needs to have a defined height as you would expect for any background image as well.

Props

FullsizePicture accepts the same props as Picture plus a few more for styling and positioning.

Prop Type Default Definition
sources array The array of source objects. Check Sources section for more information.
src string (transparent pixel) Source for standalone/fallback image. To prevent issues in some browsers, by default src is set to a 1x1 transparent pixel data image.
sizes string Sizes attribute to be used with src for determing best image for user's viewport.
alt string Alternative text for image
className string Any additional CSS classes you might want to use to style the image
wrapperClassName string Any additional CSS classes you might want to use to style the wrapper of the Picture component
cover "both" | "width" | "height" "both" Decides the fullsize behaviour of the Picture component. By default it covers the entire parent, but can be changed to cover just the height or width instead.
center boolean true Helper prop to horizontally and vertically center the image.

Use as background image

If you want to use FullsizePicture as a background image for other components, you can pass them as children too.

<section style={{ height: 200 }}>
    <FullsizePicture
        sources = {[
            {
                srcSet: "path-to-mobile-image.jpg, path-to-mobile-image@2x.jpg 2x",
                media: "(max-width: 420px)",
            },
            {
                srcSet: "path-to-desktop-image.jpg 1x, path-to-desktop-image@2x.jpg 2x",
            },
        ]}
    >
      <Heading1>This is the section title</Heading1>
    </FullsizePicture>
</section>

Contributing

Please follow our contributing guidelines.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].