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

@moxy/react-contentful-image

v1.0.0

Published

A react image renderer that uses the Contentful Images API.

Downloads

200

Readme

react-contentful-image

NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status

A react image renderer that uses the Contentful Images API.

Installation

$ npm install @moxy/react-contentful-image

This library is written in modern JavaScript and is published in both CommonJS and ES module transpiled variants. If you target older browsers please make sure to transpile accordingly.

Motivation

Contentful provides a very powerful Images API that besides retrieving image files, provides manipulation features such as resizing, cropping and compressing.

This react component returns a <picture> element. If no manipulations are made, the <picture> contains at least one <source> element, otherwise it will have two <source>. It also contains the native <img> as fallback for browsers that do not support the <picture> element.

Usage

import React from 'react';
import ContentfulImage from '@moxy/react-contentful-image';

const src = "//images.ctfassets.net/yadj1kx9rmg0/wtrHxeu3zEoEce2MokCSi/cf6f68efdcf625fdc060607df0f3baef/quwowooybuqbl6ntboz3.jpg";

const MyComponent = (props) => (
    <div {...props}>
        <ContentfulImage
            image={ src }
            format="png"
            resize={ { width: 100, height: 100 } } />
    </div>
);

export default MyComponent;

API

Besides the following supported props by the <ContentfulImage> component, additional props will be spread to the <img> element.

image

Type: string or object | Required: true

The actual image. It can be provided as an URL (string) or as a Contentful asset (object).

A Contentful asset usually has the following structure:

{
    // ...
    fields: {
        // ...
        file: {
            // ...
            url: 'my-image-url',
            // ...
        },
        // ...
    },
    // ...
}

Thus, you can pass it to this component and the image will be properly handled.

Example:

const src = 'my-image-url';

<ContentfulImage image={ src } />

or

const { image } = page.fields;

<ContentfulImage image={ image } />

format

Type: string | Required: false | Default: webp

The new format to convert the image. The possibilities are:

  • webp
  • jpg
  • png
  • progressive jpg
  • 8bit png

Example:

<ContentfulImage
    image={ src }
    format="progressive jpg" />

If no format prop is passed, this component will convert the image to webp by default as it provides small images weight with high visual quality. The example above will override this default value and will convert the image to progressive jpg. If you want to keep your image format with no conversions, please see optimize prop.

ℹ️ Read more about Contentful Images API format conversion here.

resize

Type: object | Required: false

Resizing configuration object. This object has the following shape:

  • width - Desired width
  • height - Desired height
  • behavior - Specifies the resizing behavior. The possible values are:
    • pad
    • fill
    • scale
    • crop
    • thumb
  • focusArea - Specifies the focus area when resizing. The possible values are:
    • top
    • right
    • bottom
    • left
    • center
    • top_right
    • top_left
    • bottom_right
    • bottom_left
    • face
    • faces

Example:

const resizeValues = {
    width: 100,
    height: 100,
    behavior: 'crop',
    focusArea: 'top_right'
};

// ...

<ContentfulImage
    image={ src }
    resize={ resizeValues } />

⚠️ Please, note the following warnings:

  • The maximum value for both width and height properties is 4000 pixels.
  • focusArea property won't have effect on the default or scale behavior.

ℹ️ Read more about resizing images with Contentful Images API here.

cropRadius

Type: string or number | Required: false

Add rounded corners or create a circle/ellipsis. The possible values are:

  • max keyword - Creates a full circle/ellipsis
  • The size of the corner radius in pixels

Example:

<ContentfulImage
    image={ src }
    cropRadius="max" />

// or

<ContentfulImage
    image={ src }
    cropRadius={ 30 } />

ℹ️ Read more about cropping images with Contentful Images API here.

quality

Type: number | Required: false

Sets the quality of the image. The value must be between 1 and 100.

Example:

<ContentfulImage
    image={ src }
    format="jpg"
    quality={ 10 } />

⚠️ This value will be ignored for 8-bit PNGs.

ℹ️ Read more about changing the image quality with Contentful Images API here.

backgroundColor

Type: string | Required: false

Sets the background color when using cropRadius or the pad behavior. Color hex code is expected as the value.

Example:

<ContentfulImage
    image={ src }
    cropRadius="max"
    backgroundColor="#FFFFFF" />

ℹ️ Read more about changing the image background color with Contentful Images API here.

optimize

Type: bool | Required: false | Default: true

If no format is passed, this component will use webp format as default. To convert to any other format, just use format prop to override the default value. If you want to keep your image with no format manipulations set this prop to false.

Example:

<ContentfulImage
    image={ src }
    optimize={ false } />

Tests

$ npm test
$ npm test -- --watch # during development

Demo

A demo Next.js project is available in the /demo folder so you can try out this component.

First, build the react-contentful-image project with:

$ npm run build

To run the demo, do the following inside the demo's folder:

$ npm i
$ npm run dev

Note: Everytime a change is made to the package a rebuild is required to reflect those changes on the demo.

License

Released under the MIT License.