Skip to content

Instantly share code, notes, and snippets.

@DonKoko
Forked from adammark/Meter.js
Created May 18, 2021 11:26
Show Gist options
  • Save DonKoko/bce09863ce65f5ff25eedec6fbe9b2e3 to your computer and use it in GitHub Desktop.
Save DonKoko/bce09863ce65f5ff25eedec6fbe9b2e3 to your computer and use it in GitHub Desktop.
var Meter = function (props) {
var {
percent = 0, // a number between 0 and 1, inclusive
width = 100, // the overall width
height = 10, // the overall height
rounded = true, // if true, use rounded corners
color = "#0078bc", // the fill color
animate = false, // if true, animate when the percent changes
label = null // a label to describe the contents (for accessibility)
} = props;
var r = rounded ? Math.ceil(height / 2) : 0;
var w = percent ? Math.max(height, width * Math.min(percent, 1)) : 0;
var style = animate ? { "transition": "width 500ms, fill 250ms" } : null;
return (
<svg width={width} height={height} aria-label={label} data-tooltip={label}>
<rect width={width} height={height} fill="#ccc" rx={r} ry={r}/>
<rect width={w} height={height} fill={color} rx={r} ry={r} style={style}/>
</svg>
);
};
module.exports = Meter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment