React Styled Components Star Rating Percent

node v6.12.2
version: 112.0.0
endpointsharetweet
let html const React = require('react'); const { renderToString } = require('react-dom/server'); const _styled = require('styled-components') const styled = _styled.default const { css, keyframes } = _styled const render = component => { const sheet = new _styled.ServerStyleSheet() html = renderToString(sheet.collectStyles(component)) return sheet.getStyleTags() + html }
Here are some other ways of doing it. - https://codepen.io/Bluetidepro/pen/GkpEa - http://jsfiddle.net/james_alley/kR4jn/
const Rating = styled(props => <div {...props}> <Stars count={props.stars} /> <div>{props.ratings}</div> </div> )` display: flex; flex-direction: row; ` const Stars = ({ count }) => <EmptyStars><FilledStars percent={count/5} /></EmptyStars> const EmptyStars = styled.div` height: 16px; width: 90px; background-position: 0 0; /* clear stars */ background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAAgCAYAAAAffCjxAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTExIDc5LjE1ODMyNSwgMjAxNS8wOS8xMC0wMToxMDoyMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb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4PbZBdv1uj5vtWq2Ucw21dPBL13SBeQMTsOpHuBUTkU9+a8wVaD7CsSEDER6fTgaNq1YjGMNalcFi0wOZ24LQch0TTFSPxgEfcx9pTjI/i8fgt+mFGolHoaVoO6x/9dpSn2/pWr9dxcRHz2+gvKLbb7WK5UqmDeeT8sqOl8fqfYKS39crVozzbOD6mfnruhX9/Ayvp0eW70/PpkZSC/6NH0hqsrkchBbKChOdTYEU9MncDVvTVIyLbU4+Qm4Jy9V/h11eAXgPeuvWILgEJd+hRBngnO6E4RsfbVa5gTYYCOF7OSQ3O9Ii1pGALizrpWiiKHrG+1+VUpz7N0Tytw93M65F6BPopPavRYqc+zS3VI/ULDo29d0Ru9M+rR6ddbxCjvkCPOC89EvhZom0TzOuw9Wjsq0dCEERGN4GGPYCfP8xIOdWp76VHbqAcDsIQn+eJCtBswH5fg/sUKac69WluGVCe/jhwYX2gwYweUU51649luR51f4OvHln9OT36K8AAzQ8WEP+HzGwAAAAASUVORK5CYII=) repeat-x; ` const FilledStars = styled.span` height: inherit; display: block; background: inherit; background-position: 0 -16px; /* filled stars */ width: ${props => Math.round(props.percent * 100)}%; ` render( <Rating stars={4.2} ratings='(50)' /> )
Loading…

no comments

    sign in to comment