Git Product home page Git Product logo

chrisuser / react-usage-bar Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 2.0 4.32 MB

React component to display the usage of space or the sectors for which a container is divided.

Home Page: https://chrisuser.github.io/react-usage-bar/

License: MIT License

TypeScript 73.80% JavaScript 6.54% CSS 17.93% HTML 1.73%
react usage-data design bar usage typescript storybook component react-component disk-usage

react-usage-bar's Introduction

React Usage Bar Graphic Component

React Usage Bar first example React Usage Bar compact design

Latest version Build passing Contributions welcome License

Display disc sectors, stats and more with this lightweight user-friendly React component.
Now compatible with Tailwind.

๐Ÿ”Œ Installation

Install via npm

npm install react-usage-bar --save

or yarn

yarn add react-usage-bar

Compatibility table

Package v Node
<= 1.1.18 <= 16.14.x
> 1.1.19 18.x.x

๐Ÿ”ง Usage

Live demo at: https://chrisuser.github.io/react-usage-bar/

The usage bar needs to receive an array of items. In order to display all the values correctly every item should follow this interface:

Item type

Attribute Type Required
value number Yes
name string Yes
color string No
className string No tailwind compatible
dotClassName string No tailwind compatible

The value attribute indicates the amount of space taken up by the sector with a specific name. The color property can be utilized to define the background color of that particular portion in the bar.

To further customize the sector element, you can apply your own CSS classes or Tailwind classes using the className attribute.

In case you are using the compact layout and have not specified a color value, you can customize the dotClassName similarly to the className attribute.

The defined color property value will have a priority on the background color defined in the className and/or dotClassName ones.

It is recommended to use exclusively color or one of the two other properties per item.

Example

import * from 'react'
import UsageBar from 'react-usage-bar'
import "react-usage-bar/dist/index.css"

const App = () => {

  const itemsToDisplay = [
  {
    name: "UI",
    value: 10,
    color: "#000000",
  },
  {
    name: "Photos",
    value: 30,
  },
  {
    name: "Videos",
    value: 15,
  },
  {
    name: "System Data",
    value: 33,
  },
  {
    name: "Other",
    value: 8,
  }]

  return <UsageBar items={itemsToDisplay} total={100} />
})

export default App

The total prop is also required.


Props (Options)

showPercentage | boolean | default: false

When true, shows the percentage value of all the elements.

showLabels | boolean | default: true

When false, hides all the tooltips or lables of the items.

darkMode | boolean | default: false

When true, enables the component to work in dark-mode.

compactLayout | boolean | default: false

When true, enables the new compact design. Try it. ๐Ÿ˜Ž

showFallbackColors | boolean | default: false

When true, this feature dynamically assigns a random color to the background of any items where a value has not been specified

errorMessage | string | default: Error: Total elements values exceed 100%.

Customize the error string that appears when the total values of the provided items exceeds 100%.



Custom classes props: add custom or Tailwind classes to elements of the component.

usageBarContainerClassName | string

Can customize the main div of the component.

usageBarClassName | string

Can customize the actual bar element of the component.

tooltipClassName | string

Can customize the tooltip div of the item in which are written all the textual info.

errorMessageClassName | string

Can customize the style of the error message.


CSS Styles

You must import the style directly from the package directory, like this:

import "react-usage-bar/build/index.css"

The project variables are:

  • --text-color
  • --background-tooltip-color
  • --background-bar-color

The main css classes are the following:

.u-UsageBar__error

The error message.

.u-UsageBar-light

The class that contains all the colors for the light mode.

.u-UsageBar-dark

The class that contains all the colors for the dark mode.

.c-UsageBar

The main div of the component.

.o-UsageBar__bar

The actual bar of the component.

.o-UsageBar__bar__element

The single item represented in the bar. This class is vastly used.

.o-UsageBar__bar__tooltip

The tooltip of the item in which are written all the textual info.

  • .o-UsageBar__bar__tooltip__percentage - Used to control the style of the percentage lables.
  • ::after - Is used to make the triangular shape on the bottom (or top) of the tooltips.

.o-UsageBar__bar__elements__labels__container

Used in the compact layout to list all the labels for the elements.

.o-UsageBar__bar__elements__label

The labels for the elements of the bar.

.o-UsageBar__bar__elements__label--dot

The colored dot before the label of the elements.


๐Ÿ“– Docs

You can run the project in a local enviroment using Storybook:

$ yarn storybook

Contribution

If you have a suggestion that would make this component better feel free to fork the project and open a pull request or create an issue for any idea or bug you find.
Remeber to follow the Contributing Guidelines.


Licence

React Usage Bar is MIT licensed.

react-usage-bar's People

Contributors

chrisuser avatar dependabot[bot] avatar vgribdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-usage-bar's Issues

[v1.1.8] Color fallback property is not always correctly defined

Affected versions

v1.1.7 - v1.1.8

Bug description

Most of the times one or more slots, without a previous defined background color, render as a transparent block.

Possible solution

  • This bug could be caused by the concurrence of the two useEffect hooks: one sets the array of the colors used (later) for the items without one; the other formats the items array with the correct color property.

The effect is that the items are, sometimes, formatted before the full usedColors array is populated.

  • Also the callback that renders the usageBar isn't triggered by the formatted items array changes, it runs only once.

[New Feature]: Support classes for colors

Describe your proposal

In my project I use tailwind which supports theme-based color classes like this

<div class="bg-red-500 dark:bg-red-100">

This applies a medium red background color when in light mode, and a light red color when in dark mode. I would love the ability to do set the item colors using these classes.

<UsageBar
  items={[
    { name: "macOS", value: 1, class: "bg-blue-500 dark:bg-blue:100" },
    { name: "Debian", value: 1, class: "bg-red-500 dark:bg-red:100" },
  ]}/>

Technical details

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

[Bug]: Remove padding from UsageBar

What happened?

Is it possible to remove the padding from the UsageBar? My personal opinion is that child components shouldn't enforce on the parent how the child is allowed to lay out on the page.
image

image

Version

1.1.22

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.