just a fun challenge, interesting right? and yes this was my first thought, and turns out a single element can do quite a lot
Press enter or click to view image in full size
screenshot of rating component with single element
less elements, better performance, alough most projects won’t encounter performance problem, still someone may need it
single-element(bottom left) rating vs multi-element(top right), both repeated 10,000 times (single-element react much faster and cost less resource)
How To Code It Out?
1. remove default styles of input[range]
a input of type range is pretty suitable for the rating job, for rating 1–5 stars, just like setting the range, but first we need to remove the default styles of range element
Press enter or click to view image in full size
we need browser support appearance
Press enter or click to view image in full size
then remove dot
Press enter or click to view image in full size
we need browser support -moz-range-thumb or -webkit-scrollbar-thumb
Press enter or click to view image in full size
Press enter or click to view image in full size
2. create svg backgrounds
svg draw star
svg as background using data uri
Press enter or click to view image in full size
more like a rating component now, next we join the pieces and make it a whole
3. build a react component
generate svg backgrounds, adjust styles, apply value and callback when change, for the full codes please refer this repo