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

use-element-fit

v0.5.0

Published

useElementFit is a React hook that allows you to measure and element's based on certain restraints, similar to object-fit in CSS.

Downloads

7

Readme

Build Status npm version

Description

useElementFit is a React hook that allows you to measure and element's based on certain restraints, similar to object-fit in CSS.

Motivation

Working with canvas or more complex uses of object-fit often makes it useful to calculate the width/height and x/y values of a element or object, this effect helps you do this.

Usage

Demo

See useElementFit in action

Example

import React from 'react';
import { useElementFit, ScaleMode } from 'use-element-fit';

const App = () => {
  const {ref, width, height} = useElementFit(0.5, 1, ScaleMode.COVER); // half the width and the full height of parent element

  return (
    <div ref={ref}>
      <div style={{
        width: `${width}px`,
        height: `${height}px`,
      }}>
        Size: {width}x{height}
      </div>
    </div>
  );
};

API

useElementFit

useElementFit(ratioX, ratioY, scaleMode?, alignmentX?, alignmentY?, maxWidth?, maxHeight?)

Parameters

  • ratioX

  • Type: number

    • Optional: No
    • Description: target horizontal ratio of the result. For example, setting ratioX to 0.5 and ratioY to 1 will result in half the width and the full height of the element
  • ratioY

    • Type: number
    • Optional: No
    • Description: target vertical ratio of the result. For example, setting ratioX to 1 and ratioY to 0.5 will result in half the height and the full width of the element
  • scaleMode

    • Type: ScaleMode or string
    • Optional: Yes
    • Default: ScaleMode.COVER
    • Description: The type of scaling calculation to perform. See ScaleMode for descriptions of different scale modes. This argument can also directly be supplied as a string, such as cover, contain and align-only.
  • alignmentX

    • Type: number
    • Optional: Yes
    • Default: 0.5
    • Description: The horizontal position of the result. For example, setting alignmentX to 1 will position the result at the very right edge, and vice versa for 0
  • alignmentY

    • Type: number
    • Optional: Yes
    • Default: 0.5
    • Description: The horizontal position of the result. For example, setting alignmentY to 1 will position the result at the very bottom edge, and vice versa for 0
  • maxWidth

    • Type: number
    • Optional: Yes
    • Description: The maximum width of the result. Respects alignments, ratios and scale modes.
  • maxHeight

    • Type: number
    • Optional: Yes
    • Description: The maximum height of the result. Respects alignments, ratios and scale modes.

Returns

useElementFit returns and object with the following properties:

  • ref
    • Type: React.RefObject
    • Description: The React ref to attach to the element you want to measure
  • width
    • Type: number
    • Description: The resulting width of the calculation in pixels
  • height
    • Type: number
    • Description: The resulting height of the calculation in pixels
  • x
    • Type: number
    • Description: The resulting horizontal position of the calculation in pixels
  • y
    • Type: number
    • Description: The resulting vertical position of the calculation in pixels

ScaleMode

Description

ScaleMode is a TypeScript enum which determines the type of scaling in useElementFit

  • ScaleMode.COVER
    • Calculates the result to cover the entire measured element. If the proportions of the image differ from the element, the result is cropped either vertically or horizontally where needed, so that no empty space remains.
  • ScaleMode.CONTAIN
    • Scales the result as large as possible without cropping.
  • ScaleMode.ALIGN_ONLY
    • ALIGN_ONLY performs no width/height calculation and only aligns vertically and horizontally the result based on the supplied alignmentX and alignmentY within the container.

Notes

License

MIT