Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

progress-ring-component

taisuke-j336MIT1.0.36TypeScript support: included

Animated web component showing progress in percentage

progress-ring, progress, ring, donut, web-components, stencil, requestanimationframe, raf, animation, transition, pwa

readme

Progress Ring Component

test npm version Published on webcomponents.org Built With Stencil

This is an animated web component showing progress in percentage. It internally uses easing-animation-frames library to create CPU-friendly easing animations and compiled with Stencil.

  1. Demo 1
  2. Demo 2
  3. Demo 3

How to use

Demo 1

<progress-ring percentage="30"></progress-ring>
<progress-ring percentage="60" round-linecap="true"></progress-ring>
<progress-ring percentage="90" disable-digits="true">
  <p class="completed-count">9/10<br />Complete</p>
</progress-ring>

There is only one mandatory property, percentage, which declares the ending percentage in animation. You can also use radius prop to change the size of the ring, and storkeWidth to change the thickness of the ring. The full list of properties can be found below.

You can also place custom HTML elements within the component's <progress-ring></<progress-ring> tag as HTMLSlotElement, which you have full control on for styling.

This component works reactively in a unidirectional fashion. When the percentage changes, it stops the current animation and resumes it with new percentage. There is pre-defined color scheme (red (< 25%) -> yellow (< 50%) -> blue (< 75%) -> green (>= 75%)).

Demo 2

<progress-ring percentage="10"></progress-ring>

<div class="buttons">
  <button id="buttonOne">30%</button>
  <button id="buttonTwo">60%</button>
  <button id="buttonThree">90%</button>
</div>

<script>
  const ring = document.querySelector("progress-ring");
  const buttonOne = document.querySelector("#buttonOne");
  const buttonTwo = document.querySelector("#buttonTwo");
  const buttonThree = document.querySelector("#buttonThree");

  buttonOne.addEventListener("click", () => {
    ring.setAttribute("percentage", 30);
  });
  buttonTwo.addEventListener("click", () => {
    ring.setAttribute("percentage", 60);
  });
  buttonThree.addEventListener("click", () => {
    ring.setAttribute("percentage", 90);
  });
</script>

Properties

Property Type Default Description
percentage number 0 Percentage value (mandatory)
radius number 80 Radius of the ring
stroke-width number 10 Thickness of the ring
round-linecap boolean false Addes rounded linecap to the ring
duration number 4000 Animation duration in miliseconds
easing-type string "quartInOut" Easing animation function name
int-size number 30 Font size of integer
decimal-size number intSize * 0.7 Font size of decimals
disable-digits boolean false Hides digits
disable-decimals boolean false Hides decimal places
colors string '[[0,"#ff4f40"],[25, "#ffcd40"],[50, "#66a0ff"],[75, "#30bf7a"]]' Color steps with percentage and color code
invert-colors boolean false Inverts the color scheme
event-id string undefined Event Id to be used for animation callbacks

Easing Types

Easing Types refer to Robert Penner's Easing Function names: backInOut, backIn, backOut, bounceInOut, bounceIn, bounceOut, circInOut, circIn, circOut, cubicInOut, cubicIn, cubicOut, elasticInOut, elasticIn, elasticOut, expoInOut, expoIn, expoOut, linear, quadInOut, quadIn, quadOut, quartInOut, quartIn, quartOut, quintInOut, quintIn, quintOut, sineInOut, sineIn and sineOut.

Advanced usage with animation events

By passing even-id as a prop, you can listen to events emitted by with the animation, and register callback functions for them. Please see Demo 3.

Demo 3

Event Name Payload Description
prcProgress { id: string, progress: number, percentage: number } Event to be emitted on every progress change (from 0 to 1)
prcColor { id: string, color: string } Event to be emitted for the color value
prcStart { id: string } Event to be emitted when the animation starts
prcComplete { id: string } Event to be emitted when the animation is completed
prcStop { id: string } Event to be emitted when the animation is stopped
prcResume { id: string } Event to be emitted when the animation is resumed
prcRestart { id: string } Event to be emitted when the animation is restarted

You can either directly import the component in script tag or integrate it into the framework you're using for the project.

How to load in your project

Simply place a script tag <script type="module" src="https://unpkg.com/progress-ring-component/dist/progressring/progressring.esm.js"></script> in the head of your index.html.

<!DOCTYPE html>
<html>
  <head>
    <script
      type="module"
      src="https://unpkg.com/progress-ring-component@1.0.36/dist/progressring/progressring.esm.js"
    ></script>
  </head>
  <body>
    <progress-ring percentage="50"></progress-ring>
  </body>
</html>

If you're using Typescript, you may need additional types. The example below is a React component.

import { JSX as LocalJSX } from "progress-ring-component/loader";
import { HTMLAttributes } from "react";

type StencilToReact<T> = {
  [P in keyof T]?: T[P] &
    Omit<HTMLAttributes<Element>, "className"> & {
      class?: string;
    };
};

declare global {
  export namespace JSX {
    interface IntrinsicElements
      extends StencilToReact<LocalJSX.IntrinsicElements> {}
  }
}

function App() {
  return (
    <div>
      <progress-ring percentage={30}></progress-ring>
    </div>
  );
}

export default App;

changelog

Changelog

0.0.7 (2019-02-28)

  • Fixed vertical alignment issue of the digits
  • Added font style to examples

0.0.8 (2019-02-28)

  • Updated inline demo style

0.0.9 (2019-03-01)

  • Fixed invertColors and disableDigits issues

0.0.10 (2019-03-02)

  • Updated integration guide on readme

0.0.11 (2019-03-02)

  • Fixed typo on readme

0.0.12 (2019-03-03)

  • Added CSS transition effects

0.0.13 (2019-09-05)

  • Updated dependencies
  • Made enhancement to the transitions

1.0.0 (2019-09-05)

  • Fixed a transition issue with restart before completing the initial animation loop

1.0.1 (2019-09-11)

  • Fixed decimals issue for zero percent

1.0.2 (2019-09-26)

  • Added missing implementation of restartProgress in several places

1.0.3 (2020-05-18)

  • Updated dependencies

1.0.4 (2021-06-10)

  • Upgraded Stencil version to 2.6

1.0.5 (2021-06-12)

  • Fixed animations not resuming after getting reattached to DOM

1.0.6 (2021-11-08)

  • Upgraded Stencil version to 2.10
  • Disabling e2e test until stencil supports jest v27
  • Updated README to include framework integration guide

1.0.7 (2021-11-12)

  • Changed percent prop to percentage prop
  • Added roundLinecap prop
  • Removed decimal digits for 0 percent
  • Integrated slotted elements

1.0.8 (2021-11-12)

  • Fixed zero percent issue (digits gets hidden)
  • Added disable-decimals

1.0.9 (2021-11-12)

  • Fixed broken demo

1.0.10 (2021-11-16)

  • Updated easing-animation-frames version to 1.0.2

1.0.11 (2021-11-18)

  • Added callback functions that emit events
  • Updated eslint and prettier settings
  • Updated easing-animation-frames version to 1.0.3

1.0.15 (2021-11-22)

  • Updated progress event to include both progress and percentage values

1.0.17 (2021-11-24)

  • Fixe the issue of start event not emitted
  • Fixed decimal size issue

1.0.18 (2021-11-24)

  • Fixed the issue of complete event not emitted after restart

1.0.19 (2021-12-13)

  • Fixed iOS Safari issue with SVG tspan element positioning

1.0.20 (2021-12-26)

  • Fixed color not being updated on restart

1.0.21 (2022-02-17)

  • Updated libraries and added e2e test to github actions

1.0.22 (2022-03-26)

  • Updated libraries and fixed a security alert

1.0.23 (2022-03-29)

  • Updated README

1.0.24 (2022-06-09)

  • Added colors option as a prop

1.0.25 (2022-06-13)

  • Updated README

1.0.26 (2022-07-30)

  • Updated dependencies

1.0.27 (2022-07-30)

  • Updated dependencies

1.0.28 (2022-08-08)

  • Integrated @stencil/react-output-target for react wrapper

1.0.32 (2022-08-11)

1.0.33 (2022-08-18)

  • Added color change event (prcColor)

1.0.35 (2024-06-08)

  • Updated dependencies

1.0.36 (2024-06-10)

  • Updated README