Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

caravaggio-react

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

caravaggio-react

Caravaggio react library

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
24
decreased by-31.43%
Maintainers
1
Weekly downloads
 
Created
Source

caravaggio-react

This library provides react components and hooks to integrate Caravaggio in your react projects.

Install

Install with

yarn add caravaggio-react

or

npm install caravaggio-react

This library is entirelly written in typescript.

Caravaggio Provider

Any component or hook in this library need to be descendant of a CaravaggioProvider. This provider will set which Caravaggio instance to use for transformations. Put this provider as on top as you can.

// App.tsx  (or App.js)

import { CaravaggioProvider } from "caravaggio-react";

export default function App(props) {
  return (
    <CaravaggioProvider
      url="https://mycaravaggio.dev"
    >
      {props.children}
    </CaravaggioProvider>
  );
}

The available props for the provider are

propsdescriptionoptional/mandatory
urlTells the provider the url of Caravaggio instance. It can be an absolute of relative url in case Caravaggio is served from the same domain as your appmandatory
baseUrlCaravaggio can only transform images with absolute url. If you want to use relative urls, set this value and all the images will be considered relative to this baseUrloptional, default `null`

Image component

An image component is available to transform your images. It takes the same props as a normal img tag, plus an addotional opt props. You can pass any option available on Caravaggio.

import { Image } from 'caravaggio-react';

<Image
  src="https://pexels.com/cangaroo.png"
  alt="A cangaroo jumping around"
  opt={{
    o: "webp",
    q: 90,
    blur: 10,
    rs: {
      s: "640x480",
    },
  }}
/>

In the above example we transform the image to webp, with a quality of 90, a blur effect and resizing it to 640x480 pixels.

Check Caravaggio documentation to know about all possible options.

Image srcset

You can generate a srcset using the ImageSet component.

import { ImageSet } from "caravaggio-react";

<ImageSet
  src="https://img.com/butterfly.png"
  alt="A butterfly"
  className="myclass"
  // Sets is an array of sources
  sets={[
    {
      // This source produce a webp image
      type: "image/webp",
      // The rules for this source
      rules: {
        // When screen is less wider than 300px
        "300w": {
          // Use this caravaggio options to produce the image
          opt: {
            o: "webp",
            rs: {
              s: "300x",
            },
          },
        },
        // Use this for screen large up to 600px
        "600w": {
          opt: {
            o: "webp",
            rs: {
              s: "600x",
            },
          },
        },
      },
    },
    // This is the second set, we want it for browsers 
    // not capable of handling webp images.
    // The rules are the same except for the output format
    {
      rules: {
        "300w": {
          opt: {
            rs: {
              s: "300x",
            },
          },
        },
        "600w": {
          opt: {
            rs: {
              s: "600x",
            },
          },
        },
      },
    },
  ]}
/>;

The component generates this html:

<figure class="myclass">
  <picture>
    <source
      type="image/webp"
      srcset="
        /api/assets/o:webp/rs,s:300x?image=http://localhost:3000/myimage.png 300w,
        /api/assets/o:webp/rs,s:600x?image=http://localhost:3000/myimage.png 600w
      "
    />
    <source
      srcset="
        /api/assets/rs,s:300x?image=http://localhost:3000/myimage.png 300w,
        /api/assets/rs,s:600x?image=http://localhost:3000/myimage.png 600w
      "
    />
    <img src="/myimage.png" alt="example image" />
  </picture>
</figure>

Hook

A hook, useCaravaggioImage, is provided to get, instead of an image tag, an image url with all the transformation applied. Very useful to insert images in css, or for css-in-js libraries

import { useCaravaggioImage } from "caravaggio-react";

const Component = () => {
  const image = useCaravaggioImage("https://img.com/landscape.png", {
    blur: 0.3,
  });

  return <div style={{ backgroundImage: `url('${image}')` }}>Some content</div>;
};

The first parameter is the image to transform, the second an object with all the transformations.

Available options

To know about all the options, check Caravaggio documentation.

FAQs

Package last updated on 25 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc