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-preserve-ratio

v1.4.4

Published

> A React component to preserve an element's ratio when scaling.

Downloads

27

Readme

react-preserve-ratio

A React component to preserve an element's ratio when scaling.

For example, how slideshow slides automatically scale to fit a window or screen without distorting. See the interactive examples for a demo!

NPM CI Github Pages

Visualization of react-preserve-ratio

Install

npm install --save react-preserve-ratio

Or for yarn:

yarn add react-preserve-ratio

Usage

import { PreserveRatio } from "react-preserve-ratio";

export const Example = () => {
  <div
    style={{
      border: "1px dotted rgba(0, 0, 0, 0.2)",
      display: "block",
      overflow: "hidden",
      resize: "both",
      width: "640px",
    }}
  >
    <PreserveRatio>
      <div
        style={{
          backgroundColor: "#ffffdd",
          display: "flex",
          width: "320px",
          height: "240px",
          alignItems: "center",
          justifyContent: "center",
        }}
      >
        Hello, world!
      </div>
    </PreserveRatio>
  </div>;
};

Props

Constraints

  • maxScale (optional number): The maximum scale for content relative to their real size.
  • maxHeight (optional number): The maximum height of content, in pixels.
  • maxWidth (optional number): The maximum width of content, in pixels.

minScale, minHeight and minWidth aren't currently supported in favor of having users control the min-height and min-width on the PreserveRatio's container instead. This is largely due to how potentially tricky overflow cases can be and less-clear use cases.

Alignment

  • align (optional string, default: center): Horizontal alignment, center, left or right.
  • valign (optional string, default: center): Vertical alignment, center, top or bottom.
  • cover (optional boolean, default: false): Have content "cover" rather than be "contained".

Tweaks

Dynamic scaling comes with some visual and performance quirks, and these props aim to help work around them.

  • hint (optional bool): Slightly increases the bleed between content and container. This ensures edge-to-edge coverage at the cost of having some overhang. Useful when there's high contrast between content and container that would be noticeable otherwise.
  • safeMode (optional bool): Attempt to reduce non-user-impacting ResizeObserver errors at the cost of latency (See: StackOverflow)

Context

Currently there's only one context attribute:

  • scale (number): The content scale, e.g. 1 when at 100% scale, 2 when at 200% scale, etc.

For convenience components can access it via the useScale hook:

import React from "react";
import { PreserveRatio, useScale } from "react-preserve-ratio";

function DisplayScale() {
  const scale = useScale();
  return <div>{scale}</div>;
}

function Example() {
  return (
    <PreserveRatio>
      <DisplayScale />
    </PreserveRatio>
  );
}

But it's also available via PreserveRatioContext if preferred:

import React from "react";
import { PreserveRatio, PreserveRatioContext } from "react-preserve-ratio";

function DisplayScale() {
  const { scale } = useContext(PreserveRatioContext);
  return <div>{scale}</div>;
}

// ...

Preserving Scale

Occasionally you may want a child component to preserve its scale independent of the PreserveRatio component it lives under -- for such cases there's a PreserveScale component that does exactly that.

Like PreserveRatio it accepts align and valign attributes. Unlike PreserveRatio it's hard to provide good defaults, so you'll probably want to set them explicitly.

Example:

<PreserveRatio>
  <div
    style={{
      alignItems: "center",
      display: "flex",
      height: "240px",
      justifyContent: "center",
      position: "relative",
      overflow: "hidden",
      width: "320px",
    }}
  >
    ... Scaling Content ...
    <div
      style={{
        position: "absolute",
        bottom: 0,
        right: 0,
      }}
    >
      <PreserveScale align="right" valign="bottom">
        Disclaimer: Bottom-Right.
      </PreserveScale>
    </div>
  </div>
</PreserveRatio>

Examples

See the Examples Page.

License

Apache-2.0 © Daniel O'Brien