HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<header class="big-header">
<h1>Rating Star Widget <br>with ReactJS</h1>
<p>A simple rating widget using <b>ReactJS</b> and <b>FontAwesome (5!)</b> icons.</p>
<p class="sub">Last I created the same widget using jQuery, it is one of my most popular pen. I thought why not create the same using React. Here it is.</p>
</header>
<div id="widget">
</div>
<footer id="credits">
<p class="note"><b>Note:</b> This widget is not accessible, if you wish to use it in a real life application please do consider taking a look at <a href="https://reactjs.org/docs/accessibility.html" target="_blank">ReactJS Accessibility Guide</a>. Also, check an accessible version of the widget here: <a href="https://codepen.io/depy/details/rJQyNK/" target="_blank">Accessible version</a></p>
<p style="font-size: 14px;">Created with <i class="fas fa-heart" style="color: tomato;"></i> by Deepak Kamat (<a href="https://twitter.com/xxxdepy" target="_blank">@xxxdepy</a>)</p>
</footer>
body {
font-family:"Open Sans", Helvetica, Arial, sans-serif;
color:#555;
max-width:680px;
margin:0 auto;
padding:0 20px;
}
* {
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box;
}
*:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
a {
color: tomato;
text-decoration: none;
}
a:hover {
color: #2196f3;
}
/*----------------------------
BIG HEADER
------------------------------*/
.big-header {
padding: 2em 0;
position: relative;
text-align: center;
}
.big-header:after {
content:"";
display:block;
height:1px;
background:#eee;
position:absolute;
left:30%; right:30%;
bottom: 0;
}
.big-header h1 {
font-size:2.75em;
font-weight:300;
margin-bottom:0.2em;
}
.big-header p {
font-size: 16px;
}
.big-header p.sub {
font-size: 12px;
color: #9999;
}
.note {
margin: 20px 0;
padding: 10px;
color: #888;
background-color: #fefefe;
font-size: 12px;
border-left: 5px solid tomato;
}
/* Widget Container */
#widget {
position: relative;
padding: 20px 0;
margin: 20px 0;
border: 1px solid #eee;
border-width: 0;
}
/*----------------------------
Rating Star Widget Style
-----------------------------*/
.rating-stars {
position: relative;
display: block;
text-align: center;
}
.rating-stars .star {
display: inline-block;
padding: 0 5px;
font-size: 2.5em;
color: #ccc;
transition: 0.1s all ease-in-out;
transform: scale(0.8);
}
.rating-stars .star:hover,
.rating-stars .star.semi-active {
color: #ffd73e;
transform: scale(1);
}
.rating-stars .star.active {
color: #FF9800;
transform: scale(1);
}
/* Rating Message */
.after-rating-message {
display: none;
text-align: center;
margin: 20px auto;
padding: 20px 20px;
background-color: #f7f7f7;
opacity: 0;
transition: 0.2s all ease-in-out;
}
.after-rating-message.show {
display: block;
opacity: 1;
}
function Star( props ){
return (
<div className={`star ${(props.value == 0) ? 'semi-active' : ''} ${(props.position <= props.rated) ? 'active' : ''} `}
onMouseEnter={ props.onMouseEnter }
onMouseLeave={ props.onMouseLeave }
onClick={ props.onClick }
>
<i className="fas fa-star"></i>
</div>
);
}
function Rating( props ){
const messages = {
"1": "Oh. Sorry you had a bad experience :( ",
"2": "We will try to improve.",
"3": "Appreciate it!",
"4": "Thank you!",
"5": "You're Awesome!"
};
let rating = props.rating;
return(
<div className={"after-rating-message " + ((rating > 0) ? 'show': '')} >
<span>You rated this {rating} star{rating > 1 ? 's' : ''}</span>
<br/>
<span>{ messages[rating] }</span>
</div>
);
}
class RatingWidget extends React.Component {
constructor( props ) {
super( props );
this.state = {
stars: Array(5).fill(-1),
rated: 0
};
}
handleMouseOver( i ) {
let currentRating = this.state.rated;
if ( currentRating > 0 ) {
const hoverRatedStars = this.state.stars.slice();
_.fill( hoverRatedStars, 0, currentRating, i );
this.setState({ stars: hoverRatedStars });
}
else {
const hoverStars = Array(5).fill(-1);
_.fill( hoverStars, 0, 0, (i+1) );
this.setState({ stars: hoverStars});
}
}
handleMouseOut() {
let currentRating = this.state.rated;
if ( currentRating > 0) {
const resetRatedStars = this.state.stars.slice();
_.fill( resetRatedStars, -1, currentRating, resetRatedStars.length );
this.setState({stars: resetRatedStars});
}
else {
const resetStars = this.state.stars.slice();
_.fill( resetStars, -1, 0, resetStars.length );
this.setState({stars: resetStars});
}
}
handleClick( i ) {
const clickedStar = this.state.stars.slice();
_.fill( clickedStar, 1, 0, i );
_.fill( clickedStar, 1, i, clickedStar.length );
this.setState({
stars: clickedStar,
rated: i
});
}
handleRating( rating ){
return (<Rating rating={this.state.rated} />)
}
renderStar( i ){
return (
<Star
position={i}
value={this.state.stars[i]}
rated={this.state.rated}
onMouseEnter={ () => this.handleMouseOver(i) }
onMouseLeave={ () => this.handleMouseOut() }
onClick={ () => this.handleClick(i) }
/>
);
}
render(){
return (
<div className='rating-stars-widget-outer'>
<div className='rating-stars'>
{this.renderStar(1)}
{this.renderStar(2)}
{this.renderStar(3)}
{this.renderStar(4)}
{this.renderStar(5)}
</div>
{this.handleRating( this.state.rated )}
</div>
);
}
}
ReactDOM.render(<RatingWidget />, document.getElementById("widget"));
Also see: Tab Triggers