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

react-image-file

v0.3.0

Published

An image component that accepts blobs (as returned, from example, from couchdb) and file objects (such as the ones uploaded in the browser)

Downloads

559

Readme

React-Image-File

An image component that accepts:

  • files objects (such as the ones returned from an upload field in the browser)
  • blobs (as returned, from example, from couchdb)
  • strings (normal src string used in regular images)

It has several mechanisms to act on the image at different stages of loading.

This is a suitable component to build an upload field that previews images, or load images asynchronously and fade them in once they are

The module also contains a few bonuses, such as an image upload field and a propType

Entirely written in Typescript and decently well typed, but lacking tests for the time being unfortunately :(

Usage

npm install react-image-file
import ImageLoader from 'react-image-file';

<ImageLoader file={file} alt='some text'/>

file can be either a Blob, a File, or a src string.

You might also need to include the following css somehow:

input[type=file], input[type=file]::-webkit-file-upload-button { cursor: pointer }

Other useful props:

  • template [string|React] decides which template is rendered. This must be a valid React component or string. Two templates are provided by default, a <div> template that renders to a background-image and an <img> template
  • type: one of div or img. Decides which template is to be used, of the two default ones. Defaults to div.
  • alt [string]: used if the image renders to an <img> tag
  • crop [CropAttribute]: one of contain, cover, or null. Defaults to contain. Used if the image renders to a <div> template
  • loadingURL [string]: an image URL to display while loading an image
  • errorURL [string]: an image URL to display in case of error
  • emptyURL [string]: an image URL to display when nothing is loading yet
  • width [number]: specify the width of the image. If not provided, the width of the actual loaded bitmap will be used
  • height [number]: specify the height of the image. If not provided, the height of the actual loaded bitmap will be used

Additionally, the following are passed to the template, should you want to use your own:

  • src: a string (either the string passed in file, or a base-64 representation of the Blob/File)
  • imgWidth: the width of the loaded image
  • imgHeight: the height of the loaded image
  • status: one of EMPTY, DONE, LOADING, or ERROR
  • ... and all other props passed to Image.

You also get, for free, an UploadField and an ImageUploadField components, but they're raw and will stay this way. It is my opinion that providing hooks for all the possible customizations that one would want from an upload field is counter productive, so feel free to study the structure and build your own implementations.

If you want to use them:

import React from 'react'
import ImageUploadField from 'react-image-file/ImageUploadField'

export class App extends React.Component{ 
  constructor( props, context ){ 
    super( props, context )
    this.state = { files: [] }
  }
  onFilesChange = ( files: File[] ) =>{ 
    this.setState( { files } )
  }
  onSubmit = ( evt: FormEvent<HTMLFormElement> ) => { 
    evt.preventDefault()
    const { files, file:[file], text } = this.state
    const body = new FormData();
    body.append('text',text)
    body.append('file',file)
    files.map( ( f, i ) => body.append(`files[${i}]`,f) )
    fetch('http://localhost:8000', { method:'POST', body })
      .then( response => response.json() )
      .then( response => console.log( response ) )
    console.log(body)
    console.log( { text, file ,files } )
  }
  render(){
      return(
        <ImageUploadField
          label='upload images'
          imageWidth={50} 
          imageHeight={50} 
          multiple 
          thumbnailClassName="upload-thumbnail"
          onChange={(files)=>this.setState({files:files})} 
          files={this.state.files}
      />
    );
  }

You can check a full example, including a server to handle files on the other end, in ./src/demo. I have kind of a disgusting style, so if you have problems, open an issue, I'll revert to something more classical.

From version 0.3.0 on, this package will follow semver. However, internal packages such as image file upload, provided as convenience, will not.

Devving

I welcome contributions of course. Scripts of interest:

Start a demo with an image file uploader to check if everything works

npm start

Builds the demo in './dist' and uploads it to gh-pages

npm run deploy

Compiles typescript and readies the module for publishing on npm

npm run compile

Removes the './build' directory

npm clean:build

Removes the './dist' directory

npm clean:dist

Log

  • 0.3.0
    • total re-writing
    • got rid of typings
    • broken down functionality in bite-sized files
    • breaking change: Image is now called ImageLoader, if someone was requiring it by name
    • breaking change: the file's entry point only exports Image now. Other components must be loaded individually
  • 0.2.0
    • Converted to Typescript
  • 0.1.0
    • First iteration

License

MIT