npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

caravaggio-react

v1.2.2

Published

Caravaggio react library

Downloads

182

Readme

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

| props | description | optional/mandatory | |---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------| | url | Tells 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 app | mandatory | | baseUrl | Caravaggio 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 baseUrl | optional, 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>

Hooks

useCaravaggio

A hook, useCaravaggio, 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 { useCaravaggio } from "caravaggio-react";

const Component = () => {
  const image = useCaravaggio("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.

useCaravaggioIfAvailable

Sometimes you may not be sure if Caravaggio is available or not. This hook behaves exactly as useCaravaggio but is less sensible to problems: if Caravaggio provider is not available or the image is not defined, it returns a sensible default:

import { useCaravaggioIfAvailable } from "caravaggio-react";

const Component = ({image}: {image?: string}) => {
  const image = useCaravaggioIfAvailable(image, {
    blur: 0.3,
  });

  // If the image is not available returns null.
  // If the provider is not available, returns the original image url

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

useCaravaggioBuilder

This hook return a function that can be invoked later to produce the image url

import { useCaravaggioIfAvailable } from "caravaggio-react";

const Component = ({image}: {image?: string}) => {
  const builder = useCaravaggioBuilder();
  const animals = [
    {
      name: 'tiger',
      image: 'https://images.com/tiger.png'
    },
    {
      name: 'monkey',
      image: 'https://images.com/monkey.png'
    },
    {
      name: 'horse',
      image: 'https://images.com/horse.png'
    }
  ]

  // If the image is not available returns null.
  // If the provider is not available, returns the original image url

  return <div>{
    animals.map(animal => <img src={builder(animal.image, {o: 'jpg'})} key={animal.name} />)
  }</div>;
};

Available options

To know about all the options, check Caravaggio documentation.