Code Monkey home page Code Monkey logo

react-image-display-control's Introduction

npm version github actions

ย 

Image Display Control React Component

โžก๏ธ See this document rendered at docs.frameright.io/react

An easy way to do Image Display Control in your React web app. Made with โค๏ธ by Frameright. Power to the pictures!

Less than 5kB in your final client-side bundle.

โ€ƒ โœจ Live demo

โ€ƒ ๐Ÿ’ป CodeSandbox

Table of Contents

Overview

This React component extends any <img>-like element/component with the ability to retrieve Image Display Control metadata from its image file in order to automatically and responsively zoom in on the most interesting part of the image.

It integrates nicely with other advanced features you may be using in your existing project, e.g.:

It doesn't change the structure of your DOM: your existing CSS rules still apply.

โ€ƒ โœจ Live demo

โ€ƒ ๐Ÿ’ก GitHub Discussions

NOTE: if you are not using React, you may want to have a look at the Image Display Control Web component instead.

Without this component

When an image is too big for its <img> HTML element, the best option browsers offer nowadays is to use the object-fit: cover; CSS property in order to scale and middle-crop it:

<img
  src="https://react.frameright.io/assets/pics/skater.jpg"
  width="200"
  height="200"
  style="object-fit: cover;"
/>

This is less than optimal, as there might be, in the example above, a better square-ish region in the image that could be displayed instead of the middle-crop.

Basic usage

This React component extends its <img>-like children with the ability to retrieve image regions from their image metadata, and to zoom in on the best one for the current element size:

<ImageDisplayControl>
  <img
    src="https://react.frameright.io/assets/pics/skater.jpg"
    width="200"
    height="200"
  />
</ImageDisplayControl>

The resulting HTML element is responsive and will automatically reassess the best region to zoom in on when it gets resized, e.g. when the user turns their phone from portrait to landscape.

Youtube

โ€ƒ โœจ Live demo

โ€ƒ ๐Ÿ’ป CodeSandbox

โ€ƒ ๐Ÿ’ก GitHub Discussions

Image Display Control metadata

Nowadays an image file (e.g. JPEG, PNG) can contain this type of image regions in their metadata according to the IPTC standard. This React component uses a library to let the back-end or front-end extract the regions from the image file. It then passes them to the <img> tag and turns it into a web component, which automatically and responsively zooms in on the best region.

Photographers, or anyone else, can use the Frameright webapp to define and store image regions in the metadata of their pictures.

Installation

In your Node.js-based project (e.g. using Next.js or Vite) run:

npm install @frameright/react-image-display-control

Less than 5kB in your final client-side bundle.

โ€ƒ ๐Ÿ’พ Importing in your project

Usage

// src/MyComponent.tsx

import { ImageDisplayControl } from "@frameright/react-image-display-control";

export default function MyComponent() {
  return (
    <ImageDisplayControl>
      <img
        src="https://react.frameright.io/assets/pics/skater.jpg"
        width="200"
        height="200"
      />
    </ImageDisplayControl>
  );
}

This doesn't change the structure of the resulting DOM, i.e.:

  • the <img> tag remains an <img> tag, and
  • no new parent elements are added around it, so
  • the CSS rules that used to target the <img> tag directly will still apply, and
  • the <img> tag will still naturally take the same space and position in the layout.

Other <img>-like elements/components are supported as well, e.g. Next.js <Image>s or React-Bootstrap <Image>s.

โ€ƒ โœˆ๏ธ Advanced usage

โ€ƒ ๐ŸŒ Supported environments

โ€ƒ ๐Ÿ”ง Contributing

โ€ƒ ๐Ÿ“ Changelog

โ€ƒ โœจ Local demo

โ€ƒ ๐Ÿ’ก GitHub Discussions

โ€ƒ โœจ Live demo

โ€ƒ ๐Ÿ™ Dependency tree / credits

react-image-display-control's People

Contributors

dependabot[bot] avatar killerkc12 avatar lourot avatar sakibian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-image-display-control's Issues

Listen to children's change in `src=`

<ImageDisplayControl>
  <Image src=... />
</ImageDisplayControl>

In case the child's src= is constructed for example based on its state, updated later asynchronously by a useEffect(), we (the parent <ImageDisplayControl>) won't notice the change, and so we won't fetch the new image regions corresponding to the new image.

How to solve this? With a MutationObserver? With some kind of subscription to the children's React states?

Warn if we are overriding `is=` attribute

When doing

<ImageDisplayControl>
  <img src=... />
</ImageDisplayControl>

an is="image-display-control" attribute will be added to the <img> tag in order to turn it into an Image Display Control web component. This is problematic however if the <img> tag already had another is= attribute, e.g.

<ImageDisplayControl>
  <img is="already-another-component" src=... />
</ImageDisplayControl>

An <img> tag can't be two web components at the same time. We should at least issue a warning if this happens.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.