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-rater-plus

v0.2.0

Published

A star rater in react.js with support for CSS modules and theming

Downloads

19

Readme

React-rater-plus

Improved ReactJS star rater based on NdYAG's react-rater

See the Live demo

Improvements included:

  • state management now compatible with Flux architecture (uses props rather than events)
  • uses CSS modules and supports theming via react-css-themr, no global CSS namespace
  • supports custom icons
  • better performance due to removal of .bind() calls in render()

install

npm install react-rater-plus
import Rater from 'react-rater-plus'

// ...
render() {
  return (<Rater total={5} rating={2} />)
}

API

<Rater /> accepts custom properties (all optional):

  • total: default 5
  • rating: default 0
  • onRate: function(). Callback which is invoked when the user clicks on a new rating
  • item: a string or React object that is rendered for each 'star' (by default, a star ★)

Read-only mode

If onRate prop is omitted the rater will be read-only and displays the rating value provided in the rating property. Just like react-rater fractional values (e.g 3.6 stars) are supported too and are displayed as half stars.

Interactive mode

When the onRate property is passed and contains a valid function the rater will be interactive, using this function as a callback to set new values.

react-rater-plus is redesigned with Flux architecture in mind so the value of the rating is supposed to be externalized in an application reducer or parent state component, and passed to the Rater as a property.

So, unlike react-rater, the onRate function is only called when the user clicks on a new rating value and its argument is the new rating value. The containing component is responsible for managing the state and update the value of the rating property. Note that this component will always display the value of the rating property regardless of any selection.

When using flux/redux this function call would trigger an action callback to update the application state which would then map back into the rating property. In a 'traditional' React architecture the parent component would update its state's rating and pass it down back to the rater by updating it's rating prop.

Theming & Styling

Styling via themes

react-rater-plus relies on Webpack and CSS modules so it uses react-css-themr to manage themes. See react-css-themr for usage of the theming component.

The Rater component imported by default from react-rater-plus is a themed component and is bundled with its default theme.

This default theme can be further customized by creating another CSS module which would be imported. Because react-css-themr will merge the themes it's possible to only override specific settings, for example:

mytheme.css

:root {
    --react-rater-hover: #600;
    --react-rater-active: #000;
}

.rater a {
        &.willBeActive {
            color: var(--react-rater-hover);
        }
        &.active, &.halfActive span {
            color: var(--react-rater-active);
        }
}

mytheme.js

import theme from './mytheme.css'
import Rater from 'react-rater-plus'

class Example extends React.Component {
  render() {
    return (<Rater theme={theme} />)
  }
}

Raw theming/styling

In extreme cases it may be simpler to import the so-called raw Rater component which does not contain ANY default theming or CSS from the {Rater} export. Because the raw component lacks any styling, the entire theme has to be provided to the component via a theme property as a CSS module object:

import theme from './entire-rating-styling.css'
import {Rater} from 'react-rater-plus'

class Example extends React.Component {
  render() {
    return (<Rater theme={theme} />)
  }
}

The raw component also defaults to using 'static' classnames in the absence of a theme property. So using the raw component without a theme makes it possible to do CSS customization the 'old-way' using CSS global styles, where a stylesheet has to be bundled with the application providing the global styles for all states (see bundled rater.css for example). It's also possible to @include the rater.css when using SASS.

Customizing the 'star' items

If you want to change the the rater to use items other than stars, you can either specify them as the item property or use children which accept properties.

Using the item property is simpler in case you want to only change the appearance of the item. Using children enables total control.

The mechanism is the same as react-rater. You can define your own 'star' component and pass it as a child to the <Rater />. If you pass more children they will be repeated.

<Rater total={5}>
  <Heart />
</Rater>

Each child item receives custom properties from the Rater which then it can use to style itself. Because react-rater-plus support CSS modules they can have their own CSS module or even theme provided via react-css-themr

{
  active: PropTypes.bool,
  halfActive: PropTypes.bool,
  willBeActive: PropTypes.bool,
  disabled: PropTypes.bool
}

License

BSD.