Examples
Examplesโ
Emojisโ
Show code
- JSX
- CSS
import * as React from 'react';
import StarPicker from 'react-star-picker';
// Function to generate a random color in rgba format
function getRandomColor() {
const r = Math.floor(Math.random() * 256); // Red (0-255)
const g = Math.floor(Math.random() * 256); // Green (0-255)
const b = Math.floor(Math.random() * 256); // Blue (0-255)
const a = (Math.random() * 0.6 + 0.4).toFixed(2); // Opacity between 0.4 and 1
return `rgba(${r}, ${g}, ${b}, ${a})`;
}
// Function to generate a random rotation between -10deg and +10deg
function getRandomRotation() {
const rotation = Math.floor(Math.random() * 21) - 10; // Random value between -10 and +10
return `${rotation}deg`;
}
const faces = ['๐คฌ', '๐', '๐ ', '๐', '๐', '๐คจ', '๐', '๐ฅณ', '๐คฉ', '๐ฆ'];
const faceRenderer = ({ index, selectedIndex }) => {
return (
<span className={index === selectedIndex ? 'lastOfSelection' : ''}>
{faces[index % faces.length]}
</span>
);
};
export const BluePicker = () => {
const [rating, setRating] = React.useState<number | null>(null);
// Recalculate the ripple color and button rotation whenever the rating changes
const rippleColor = React.useMemo(() => {
return getRandomColor();
}, [rating]);
const rotation = React.useMemo(() => {
return getRandomRotation();
}, [rating]);
return (
<div
style={
{
'--ripple-color': rippleColor,
'--rotation': rotation, // Pass the random rotation as a CSS variable
} as React.CSSProperties
}
>
<StarPicker //
className="faceTheme"
value={rating}
onChange={setRating}
starRenderer={faceRenderer}
starCount={10}
/>
</div>
);
};
export default BluePicker;
/* definitions for a "faceTheme" class and targeting button class names */
.StarPicker.faceTheme {
display: flex;
}
.StarPicker.faceTheme .StarPickerButton span {
transition: font-size 0.2s;
filter: grayscale();
font-size: medium;
width: 45px;
height: 40px;
}
.StarPicker.faceTheme .StarPickerButton.isHovered span {
font-size: x-large;
filter: unset;
}
.StarPicker.faceTheme .StarPickerButton.selected span.lastOfSelection {
font-size: xx-large;
filter: unset;
position: relative; /* Relative positioning for the ripple */
/* Apply rotation */
transform: rotate(
var(--rotation)
); /* Use the rotation from custom property */
}
/* Create a ring ripple on click */
.StarPicker.faceTheme .StarPickerButton.selected span.lastOfSelection::before {
content: '';
position: absolute;
width: 30px; /* Initial width of the ripple ring */
height: 30px; /* Initial height of the ripple ring */
left: 50%; /* Center horizontally */
top: 50%; /* Center vertically */
transform: translate(-50%, -50%) scale(0); /* Center and scale from center */
border-radius: 50%; /* Make it a circle */
border: 3px solid var(--ripple-color, rgba(211, 13, 13, 0.6)); /* Use the random color */
animation: ripple-animation 0.6s linear forwards; /* Animation for the ripple */
pointer-events: none; /* Make sure clicks pass through */
}
/* Keyframes to animate the ripple and make it grow in size */
@keyframes ripple-animation {
0% {
transform: translate(-50%, -50%) scale(0); /* Start with no size */
opacity: 1; /* Fully opaque */
}
100% {
transform: translate(-50%, -50%) scale(4); /* Grow to 4 times the size */
opacity: 0; /* Fade out the ripple */
}
}
Default Propsโ
Show code
- JSX
- CSS
import * as React from 'react';
import StarPicker from 'react-star-picker';
export const LargePicker = () => {
const [rating, setRating] = React.useState<number | null>(4);
return (
<StarPicker //
value={rating}
onChange={setRating}
size={50}
/>
);
};
export default LargePicker;
Custom Colorsโ
Show code
- JSX
- CSS
import * as React from 'react';
import StarPicker from 'react-star-picker';
const starRenderer = ({ index, selectedIndex }) => {
const selected = index <= selectedIndex;
if (selected) {
return String.fromCharCode(10022);
}
return String.fromCharCode(10023);
};
const GreenPicker = () => {
const [rating, setRating] = React.useState<number | null>(2);
return (
<StarPicker //
className="greenStars"
value={rating}
onChange={setRating}
starRenderer={starRenderer}
starCount={3}
/>
);
};
export default GreenPicker;
/* definitions for a "greenStars" class */
.StarPicker.greenStars .StarPickerButton {
transition: color 0.2s;
color: #e4e4e4;
font-size: 65px;
background: radial-gradient(
var(--greenPickerColor1) var(--greenPickerColorPercentage1),
var(--greenPickerColor2) var(--greenPickerColorPercentage2)
);
transition: --greenPickerColor1 0.4s, --greenPickerColor2 0.4s,
--greenPickerColorPercentage1 0.4s, --greenPickerColorPercentage2 0.4s;
}
.StarPicker.greenStars .StarPickerButton.selected {
color: #05e80c;
}
.StarPicker.greenStars .StarPickerButton.addSelection {
color: #afff72;
--greenPickerColor1: #f00;
--greenPickerColor2: transparent;
--greenPickerColorPercentage1: 0%;
--greenPickerColorPercentage2: 20%;
}
.StarPicker.greenStars .StarPickerButton.removeSelection {
color: #00a605;
}
.StarPicker.greenStars .StarPickerButton.isHovered {
--greenPickerColorPercentage1: 0%;
--greenPickerColorPercentage2: 40%;
}
@property --greenPickerColor1 {
syntax: '<color>';
initial-value: transparent;
inherits: false;
}
@property --greenPickerColor2 {
syntax: '<color>';
initial-value: transparent;
inherits: false;
}
@property --greenPickerColorPercentage1 {
syntax: '<percentage>';
initial-value: 0%;
inherits: false;
}
@property --greenPickerColorPercentage2 {
syntax: '<percentage>';
initial-value: 20%;
inherits: false;
}
Rotationโ
Show code
- JSX
- CSS
import * as React from 'react';
import StarPicker from 'react-star-picker';
export const RotatePicker = () => {
const [rating, setRating] = React.useState<number | null>(4);
return (
<StarPicker //
className="rotateStar"
value={rating}
onChange={setRating}
size={45}
/>
);
};
export default RotatePicker;
/* definitions for a "rotateStar" class */
.StarPicker.rotateStar .StarPickerButton .DefaultStarComponent {
color: #bbdcb8;
transition: transform 0.2s ease-in-out, color 0.4s;
}
.StarPicker.rotateStar .StarPickerButton.selected .DefaultStarComponent {
color: coral;
transform: rotate(-60deg);
}
.StarPicker.rotateStar .StarPickerButton.removeSelection .DefaultStarComponent {
color: #cb5b44;
}
.StarPicker.rotateStar .StarPickerButton.addSelection .DefaultStarComponent {
color: #f5cb69;
}
Shadowโ
Show code
- JSX
- CSS
import * as React from 'react';
import StarPicker from 'react-star-picker';
export const EnlargePicker = () => {
const [rating, setRating] = React.useState<number | null>(4);
return (
<StarPicker //
className="enlargeStar"
value={rating}
onChange={setRating}
/>
);
};
export default EnlargePicker;
/* definitions for an "enlargeStar" class */
.StarPicker.enlargeStar .StarPickerButton .DefaultStarComponent {
transition: transform 0.2s ease-in-out, color 0.4s;
}
.StarPicker.enlargeStar .StarPickerButton.selected .DefaultStarComponent {
text-shadow: rgb(173, 140, 6) 1px 1px 1px;
transform: scale(1.2);
}
Custom Renderer with Animationโ
Show code
- JSX
- CSS
import * as React from 'react';
import StarPicker from 'react-star-picker';
const DollarSvg = ({ stroke, fill }: { stroke: string; fill: string }) => {
return (
<svg width="1em" height="1em" viewBox="-1 -1 16 24">
<path
d="M 5.7672275,0.10454656 5.7687798,1.8432524 C 3.1530349,2.1696584 0.85511786,3.5105694 0.63651923,5.9335327 0.51636012,10.566614 10.734409,11.654347 10.934687,15.030807 10.864893,17.460972 4.2894804,17.706665 3.2163668,14.995092 L 0.02261549,15.720285 C 0.77014071,18.029563 3.1584552,19.322457 5.7677453,19.684782 L 5.7687798,21.539435 8.6509212,21.539953 8.6504035,19.710663 C 11.644631,19.349577 14.241035,17.773814 14.225752,15.097063 14.229088,9.1564354 4.1341996,9.133676 3.9524306,6.0044467 3.9494677,3.7582878 10.035145,3.326126 10.629806,6.0577627 L 13.68535,5.4536939 C 13.196125,3.4596776 11.059157,2.2534069 8.6509212,1.8856981 L 8.6504035,0.10506428 Z"
stroke={stroke}
fill={fill}
strokeWidth="0.9"
/>
</svg>
);
};
const dollarRenderer = ({ index, hoverIndex, selectedIndex }) => {
const selected = index <= selectedIndex;
const inHoverRange = hoverIndex != null && index <= hoverIndex;
const hoverActive = hoverIndex != null;
const colored = (selected && !hoverActive) || (inHoverRange && hoverActive);
const color = colored ? '#1ca363' : 'grey';
const colorOutline = colored ? 'green' : 'grey';
return (
<div className="dollarContainer">
<DollarSvg //
stroke={colorOutline}
fill={selected ? color : 'none'}
/>
</div>
);
};
export const DollarPicker = () => {
const [rating, setRating] = React.useState<number | null>(3);
return (
<StarPicker //
value={rating}
onChange={setRating}
starRenderer={dollarRenderer}
size={50}
starCount={4}
/>
);
};
export default DollarPicker;
@keyframes rotate {
0% {
transform: rotate(0deg);
}
30% {
transform: rotate(10deg);
}
70% {
transform: rotate(-10deg);
}
}
.dollarContainer {
display: flex;
padding: 0;
margin: 0;
}
.dollarContainer:hover {
animation: rotate 0.3s ease-in-out;
}
Custom Rendererโ
Show code
- JSX
- CSS
import * as React from 'react';
import StarPicker from 'react-star-picker';
const customCharacterRenderer = ({ index, selectedIndex }) => {
const selected = index <= selectedIndex;
return selected ? String.fromCharCode(10029) : String.fromCharCode(9733);
};
export const CustomPicker = () => {
const [rating, setRating] = React.useState<number | null>(4);
return (
<StarPicker //
className="customCharacter"
value={rating}
onChange={setRating}
starRenderer={customCharacterRenderer}
size={50}
/>
);
};
export default CustomPicker;
/* definitions for a "customCharacter" class */
.StarPicker.customCharacter .StarPickerButton {
transition: color 0.2s, transform 0.2s;
color: #e4e4e4;
}
.StarPicker.customCharacter .StarPickerButton.selected {
color: #ff3333;
}
.StarPicker.customCharacter .StarPickerButton.addSelection {
color: #e9c0c0;
}
.StarPicker.customCharacter .StarPickerButton.removeSelection {
color: #ee9d9d;
}
.StarPicker.customCharacter .StarPickerButton.isHovered {
transform: scale(1.2);
}
Custom Renderer with Half-Star Precisionโ
Show code
- JSX
- CSS
import * as React from 'react';
import StarPicker from 'react-star-picker';
const simpleRenderer = ({ index, selectedIndex }) => {
const selected = index <= selectedIndex;
if (selected) {
return String.fromCharCode(9733);
}
return String.fromCharCode(9734);
};
export const BluePicker = () => {
const [rating, setRating] = React.useState<number | null>(3.5);
return (
<StarPicker //
className="blueTheme"
value={rating}
onChange={setRating}
starRenderer={simpleRenderer}
size={60}
halfStars
/>
);
};
export default BluePicker;
/* definitions for a "blueTheme" class and targeting button class names */
.StarPicker.blueTheme .StarPickerButton {
transition: color 0.2s;
color: #e4e4e4;
}
.StarPicker.blueTheme .StarPickerButton.selected {
color: #cc00ff;
}
.StarPicker.blueTheme .StarPickerButton.addSelection {
color: #dfb6e9;
}
.StarPicker.blueTheme .StarPickerButton.removeSelection {
color: #da88ee;
}