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-advanced-number

v1.0.0

Published

React component formatting and displaying a number with advanced features such as highlighting the differences with the previous value, a privacy mode, etc.

Downloads

8

Readme

react-advanced-number

Codacy Badge Codacy Badge CI

React component formatting and displaying a number with advanced features such as highlighting the differences with the previous value, a privacy mode, etc.

The component has no dependency (except the obvious peer dep of react itself). The formatting relies on the Intl.NumberFormat API. All the options of the API are provided as props of this component.

Table of Contents

Installation

react-advanced-number is available as an npm package

npm install react-advanced-number

Usage

In it's simplest form:

import React from "react";
import { AdvancedNumber } from "react-advanced-number";

function App() {
  return <AdvancedNumber value={1234.56} />;
}

Usage

Formatting

The component uses the native API Intl.NumberFormat. All the options are available as props.

function App() {
  return (
    <AdvancedNumber
      value={1234.56}
      options={{
        style: "currency",
        currency: "USD",
        currencyDisplay: "narrowSymbol",
      }}
    />
  );
}

Usage

Note: the prop significantDecimalDigits, when provided, is a shorthand to overwrite both the props options.minimumFractionDigits and options.maximumFractionDigits.

Note: Check the browser support for the Intl.NumberFormat API.

Features

Privacy Mode

Blur the number when enabled. Perfect for sensitive information to be visually hidden/revealled when relevant.

function App() {
  return (
    <>
      <AdvancedNumber value={1234.56} privacyMode={false} />
      <AdvancedNumber value={1234.56} privacyMode={true} />
    </>
  );
}

Usage

The default shadow color is #7C7C7CD9, you can change it with the optional prop privacyShadowColor

function App() {
  return (
    <AdvancedNumber
      value={1234.56}
      privacyMode={true}
      privacyShadowColor="#03A9F4CC"
    />
  );
}

Usage

Note: currently, it only changes the CSS properties, the number is still in the markup.

Diff Highlighting

Highlighting a difference between the value and a previous value. Interesting when displaying changing prices.

function App() {
  return <AdvancedNumber value={1234.56} previousValue={1235.89} />;
}

Usage

The default color for a positive difference is #4CAF50 and for a negative difference is #F44336. You can change them with the optional props positiveColor and negativeColor.

function App() {
  const positive = "#03A9F4";
  const negative = "#C238DA";

  return (
    <>
      <AdvancedNumber
        value={1234.56}
        previousValue={1233.89}
        positiveColor={positive}
        negativeColor={negative}
      />
      <AdvancedNumber
        value={1234.56}
        previousValue={1234.59}
        positiveColor={positive}
        negativeColor={negative}
      />
    </>
  );
}

Usage

Muted Decimals

Displaying muted decimals between a significant number and the total number of decimals. They are slightly lighter to distinguish them from the significant digits.

function App() {
  return (
    <AdvancedNumber
      value={1234.56}
      showMutedDecimals
      significantDecimalDigits={2}
      maxDecimalDigits={6}
    />
  );
}

Usage

If you wonder why, I think it's a nice way to homogenize the display of values with different significant number of decimals, as it happens often in price values.

function App() {
  return (
    <>
      <AdvancedNumber value={1234.567} />
      <AdvancedNumber
        value={1234.567}
        showMutedDecimals
        significantDecimalDigits={2}
        maxDecimalDigits={6}
      />
      <AdvancedNumber
        value={0.1234}
        showMutedDecimals
        significantDecimalDigits={4}
        maxDecimalDigits={6}
      />
    </>
  );
}

Usage

Note: The feature requires the following to be enabled: showMutedDecimals === true, maxDecimalDigits > significantDecimalDigits and options.notation === 'standard.

Note: significantDecimalDigits is actually optional, a default value is defined by the Intl.NumberFormat API.

Small Decimals

Represent the decimals smaller than the interger part for the number.

function App() {
  return <AdvancedNumber value={1234.56} smallDecimals />;
}

Usage

Styling

The root element is a span receiving the props you will pass it to. Therefore className and style can be use to style the component.

function App() {
  return <AdvancedNumber value={1234.56} style={{ fontFamily: "monospace" }} />;
}

Usage

All Together

function App() {
  return (
    <AdvancedNumber
      value={1234.56}
      previousValue={1233.89}
      options={{
        style: "currency",
        currency: "USD",
        currencyDisplay: "narrowSymbol",
      }}
      style={{ fontFamily: "monospace" }}
      smallDecimals
      showMutedDecimals
      significantDecimalDigits={2}
      maxDecimalDigits={6}
    />
  );
}

Usage

License

This project is licensed under the terms of the MIT license.