Light Dark System

Rating

<p-rating> | PRating
Since 1.0 stable

Ratings give users a way to quickly view and provide feedback.

<p-rating label="Rating"></p-rating>
import PRating from 'pure-uikit/dist/react/rating';

const App = () => <PRating label="Rating" />;

Examples

Labels

Ratings are commonly identified contextually, so labels aren’t displayed. However, you should always provide one for assistive devices using the label attribute.

<p-rating label="Rate this component"></p-rating>
import PRating from 'pure-uikit/dist/react/rating';

const App = () => <PRating label="Rate this component" />;

Maximum Value

Ratings are 0–5 by default. To change the maximum possible value, use the max attribute.

<p-rating label="Rating" max="3"></p-rating>
import PRating from 'pure-uikit/dist/react/rating';

const App = () => <PRating label="Rating" max={3} />;

Precision

Use the precision attribute to let users select fractional ratings.

<p-rating label="Rating" precision="0.5" value="2.5"></p-rating>
import PRating from 'pure-uikit/dist/react/rating';

const App = () => <PRating label="Rating" precision={0.5} value={2.5} />;

Symbol Sizes

Set the --symbol-size custom property to adjust the size.

<p-rating label="Rating" style="--symbol-size: 2rem;"></p-rating>
import PRating from 'pure-uikit/dist/react/rating';

const App = () => <PRating label="Rating" style={{ '--symbol-size': '2rem' }} />;

Readonly

Use the readonly attribute to display a rating that users can’t change.

<p-rating label="Rating" readonly value="3"></p-rating>
import PRating from 'pure-uikit/dist/react/rating';

const App = () => <PRating label="Rating" readonly value={3} />;

Disabled

Use the disable attribute to disable the rating.

<p-rating label="Rating" disabled value="3"></p-rating>
import PRating from 'pure-uikit/dist/react/rating';

const App = () => <PRating label="Rating" disabled value={3} />;

Detecting Hover

Use the p-hover event to detect when the user hovers over (or touch and drag) the rating. This lets you hook into values as the user interacts with the rating, but before they select a value.

The event has a payload with phase and value properties. The phase property tells when hovering starts, moves to a new value, and ends. The value property tells what the rating’s value would be if the user were to commit to the hovered value.

<div class="detect-hover">
  <p-rating label="Rating"></p-rating>
  <span></span>
</div>

<script>
  const rating = document.querySelector('.detect-hover > p-rating');
  const span = rating.nextElementSibling;
  const terms = ['No rating', 'Terrible', 'Bad', 'OK', 'Good', 'Excellent'];

  rating.addEventListener('p-hover', event => {
    span.textContent = terms[event.detail.value];

    // Clear feedback when hovering stops
    if (event.detail.phase === 'end') {
      span.textContent = '';
    }
  });
</script>

<style>
  .detect-hover span {
    position: relative;
    top: -4px;
    left: 8px;
    border-radius: var(--p-border-radius-small);
    background: var(--p-color-neutral-900);
    color: var(--p-color-neutral-0);
    text-align: center;
    padding: 4px 6px;
  }

  .detect-hover span:empty {
    display: none;
  }
</style>
import { useState } from 'react';
import PRating from 'pure-uikit/dist/react/rating';

const terms = ['No rating', 'Terrible', 'Bad', 'OK', 'Good', 'Excellent'];
const css = `
  .detect-hover span {
    position: relative;
    top: -4px;
    left: 8px;
    border-radius: var(--p-border-radius-small);
    background: var(--p-color-neutral-900);
    color: var(--p-color-neutral-0);
    text-align: center;
    padding: 4px 6px;
  }

  .detect-hover span:empty {
    display: none;
  }
`;

function handleHover(event) {
  rating.addEventListener('p-hover', event => {
    setFeedback(terms[event.detail.value]);

    // Clear feedback when hovering stops
    if (event.detail.phase === 'end') {
      setFeedback('');
    }
  });
}

const App = () => {
  const [feedback, setFeedback] = useState(true);

  return (
    <>
      <div class="detect-hover">
        <PRating label="Rating" onPHover={handleHover} />
        <span>{feedback}</span>
      </div>
      <style>{css}</style>
    </>
  );
};

Custom Icons

You can provide custom icons by passing a function to the getSymbol property.

<p-rating label="Rating" class="rating-hearts" style="--symbol-color-active: #ff4136;"></p-rating>

<script>
  const rating = document.querySelector('.rating-hearts');
  rating.getSymbol = () => '<p-icon name="heart-fill"></p-icon>';
</script>
import PRating from 'pure-uikit/dist/react/rating';

const App = () => (
  <PRating
    label="Rating"
    getSymbol={() => '<p-icon name="heart-fill"></p-icon>'}
    style={{ '--symbol-color-active': '#ff4136' }}
  />
);

Value-based Icons

You can also use the getSymbol property to render different icons based on value.

<p-rating label="Rating" class="rating-emojis"></p-rating>

<script>
  const rating = document.querySelector('.rating-emojis');

  rating.getSymbol = value => {
    const icons = ['emoji-angry', 'emoji-frown', 'emoji-expressionless', 'emoji-smile', 'emoji-laughing'];
    return `<p-icon name="${icons[value - 1]}"></p-icon>`;
  };
</script>
import PRating from 'pure-uikit/dist/react/rating';

function getSymbol(value) {
  const icons = ['emoji-angry', 'emoji-frown', 'emoji-expressionless', 'emoji-smile', 'emoji-laughing'];
  return `<p-icon name="${icons[value - 1]}"></p-icon>`;
}

const App = () => <PRating label="Rating" getSymbol={getSymbol} />;

Importing

If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.

Script Import Bundler React

To import this component from the CDN using a script tag:

<script type="module" src="https://cdn.jsdelivr.net/npm/pure-uikit@1.3.13/cdn/components/rating/rating.js"></script>

To import this component from the CDN using a JavaScript import:

import 'https://cdn.jsdelivr.net/npm/pure-uikit@1.3.13/cdn/components/rating/rating.js';

To import this component using a bundler:

import 'pure-uikit/dist/components/rating/rating.js';

To import this component as a React component:

import PRating from 'pure-uikit/dist/react/rating';

Properties

Name Description Reflects Type Default
label A label that describes the rating to assistive devices. string ""
value The current rating. number 0
max The highest rating to show. number 5
precision The precision at which the rating will increase and decrease. For example, to allow half-star ratings, set this attribute to 0.5. number 1
readonly Makes the rating readonly. boolean false
disabled Disables the rating. boolean false
getSymbol A function that customizes the symbol to be rendered. The first and only argument is the rating’s current value. The function should return a string containing trusted HTML of the symbol to render at the specified value. Works well with <p-icon> elements. (value: number) => string -
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
p-change onPChange Emitted when the rating’s value changes. -
p-hover onPHover Emitted when the user hovers over a value. The phase property indicates when hovering starts, moves to a new value, or ends. The value property tells what the rating’s value would be if the user were to commit to the hovered value. { phase: 'start' | 'move' | 'end', value: number }

Learn more about events.

Methods

Name Description Arguments
focus() Sets focus on the rating. options: FocusOptions
blur() Removes focus from the rating. -

Learn more about methods.

Custom Properties

Name Description Default
--symbol-color The inactive color for symbols.
--symbol-color-active The active color for symbols.
--symbol-size The size of symbols.
--symbol-spacing The spacing to use around symbols.

Learn more about customizing CSS custom properties.

Parts

Name Description
base The component’s base wrapper.

Learn more about customizing CSS parts.

Dependencies

This component automatically imports the following dependencies.

  • <p-icon>