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.
<div id="content"></div>
@import url(https://fonts.googleapis.com/css?family=Inconsolata);
$small-screen: "only screen and (max-width: 479px)";
* {
margin: 0px;
padding: 0px;
}
body {
background-image: url(https://www.dropbox.com/s/8l3mwap0o8u44v0/campfire.jpg?raw=1);
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
font-family: "Inconsolata", monospace;
}
#content {
width: 85%;
max-width: 960px;
margin: 0px auto;
padding: 0px 8px;
background-color: rgba(223, 223, 223, 0.8);
@media #{$small-screen} {
width: 95%;
}
h1 {
text-align: center;
margin-bottom: 8px;
padding: 16px 0px;
font-size: 40px;
border-bottom: 2px solid black;
@media #{$small-screen} {
font-size: 22px;
border: none;
}
}
h2 {
font-style: italic;
text-align: center;
padding: 16px 0px;
@media #{$small-screen} {
font-size: 20px;
}
}
h6 {
text-align: center;
padding: 32px 0px;
}
p {
text-align: center;
@media #{$small-screen} {
font-size: 14px;
}
}
a {
color: #004400;
text-decoration: none;
font-weight: bold;
&:hover {
text-decoration: underline;
}
}
table {
width: 100%;
thead tr th {
padding: 16px 0px;
border-bottom: 1px solid black;
@media #{$small-screen} {
font-size: 14px;
}
}
tbody tr td {
text-align: center;
@media #{$small-screen} {
font-size: 14px;
}
img {
margin: 0px auto;
}
}
}
}
// The URL we need to fetch our campers.
const URL = "https://fcctop100.herokuapp.com/api/fccusers/top/recent";
///
/// \class CamperComponent
/// \brief Displays a record of a single camper.
///
/// This class expects four properties:
///
/// name: The username of the camper.
/// img: The camper's profile picture.
/// recent: The amount of Brownie Points (BP) the user has
/// earned over the last 30 days.
/// alltime: The amount of BP the user has earned overall.
///
class CamperComponent extends React.Component {
///
/// \brief The constructor.
///
/// \param props The expected properties.
///
constructor (props) {
super(props);
}
///
/// \fn render
/// \brief Renders the component.
///
/// This camper's record will be rendered in a table row, so
/// render this inside the body of a table.
///
render () {
// Check to see if the camper's name is too long. If so, truncate
// it with the ellipsis.
const renderedName = (
this.props.name.length > 13 ?
this.props.name.slice(0, 10) + "..." :
this.props.name
);
return (
<tr>
<td>
<img src={this.props.img} alt={this.props.name} width="32px" />
</td>
<td>
<a href={"https://freecodecamp.com/" + this.props.name} target="_blank">
{renderedName}
</a>
</td>
<td>
{this.props.recent}
</td>
<td>
{this.props.alltime}
</td>
</tr>
);
}
};
///
/// \class BoardComponent
/// \brief Displays a list of leading Campers.
///
class BoardComponent extends React.Component {
///
/// \fn toggleSorting
/// \brief Toggles the sorting of our leaderboard.
///
/// \param score Which score shall be toggled?
///
toggleSorting (score) {
// Store a temporary copy of the campers array.
let camperArray = this.state.campers.array.slice();
// Find out what score we are sorting, and in what direction,
// if any at all.
const currentDirection = this.state.campers.sorting.direction;
const currentScore = this.state.campers.sorting.score;
// Find out what kind of sorting we'll have to do, and store it
// here.
let newSorting = null;
// Determine the sorting order here.
if (score === currentScore) {
// If we are not sorting the other score, then find out how
// we are sorting it here. If we are sorting in ascending order,
// then sort descending. Otherwise, sort ascending.
if (currentDirection === "ascending") {
newSorting = "descending";
}
else if (currentDirection === "descending") {
newSorting = "ascending";
}
} else {
// If we are sorting out the other score (we were sorting recent, but
// now we want to sort alltime, for instance), then default to descending.
newSorting = "descending";
}
// Sort our array according to the new sorting order.
if (newSorting === "ascending") {
camperArray.sort((a, b) => a[score] - b[score]);
}
else if (newSorting === "descending") {
camperArray.sort((a, b) => b[score] - a[score]);
}
// Update our state.
this.setState({
campers: {
array: camperArray,
sorting: {
score: score,
direction: newSorting
}
}
});
}
///
/// \brief The constructor.
///
/// \param props The expected properties.
///
constructor (props) {
super(props);
this.state = {
campers: {
array: [],
sorting: {
score: "recent",
direction: "none"
}
},
fetchError: null
};
}
///
/// \fn componentDidMount
/// \brief Called when the component is rendered for the first time.
///
componentDidMount () {
// Fetch our campers from the server.
axios.get(URL).then(response => {
// Update the state with our new array.
this.setState({
campers: {
array: response.data,
sorting: {
score: "recent",
direction: "descending"
},
fetchError: null
}
});
}).catch(error => {
// If the request returned a response other than 200...
if (error.response) {
// Adjust our state accordingly.
this.setState({
fetchError: `Could not retrieve the data (Status Code ${error.response.status}).`
});
}
else {
this.setState({
fetchError: `Could not retrieve the data (${error.message}).`
});
}
});
}
///
/// \fn onRecentClicked
/// \brief Handled when the "Last 30 Days" button is clicked.
///
onRecentClicked () {
this.toggleSorting("recent");
}
///
/// \fn onAlltimeClicked
/// \brief Handled when the "All Time" button is clicked.
///
onAlltimeClicked () {
this.toggleSorting("alltime");
}
///
/// \fn renderTable
/// \brief Renders the table inside which our leaderboard will reside.
///
renderTable () {
// Map our campers into an array of renderable elements.
const mapped = this.state.campers.array.map((val, index) => {
return (
<CamperComponent key={index}
name={val.username}
img={val.img}
recent={val.recent}
alltime={val.alltime} />
);
});
// Render a table with our campers in it.
return (
<table>
<thead>
<tr>
<th>Image</th>
<th>Camper</th>
<th>
<a href="#" onClick={this.onRecentClicked.bind(this)}>
{
this.state.campers.sorting.score == "recent" ?
(this.state.campers.sorting.direction === "descending" ?
`Recent (d)` :
`Recent (a)`) :
`Recent`
}
</a>
</th>
<th>
<a href="#" onClick={this.onAlltimeClicked.bind(this)}>
{
this.state.campers.sorting.score == "alltime" ?
(this.state.campers.sorting.direction === "descending" ?
`Overall (d)` :
`Overall (a)`) :
`Overall`
}
</a>
</th>
</tr>
</thead>
<tbody>
{mapped}
</tbody>
</table>
);
}
///
/// \fn render
/// \brief Renders the component.
///
render () {
// Check to see if there was an error fetching the data.
if (this.state.fetchError) {
// Let the user know what went wrong.
return (
<div className="errorMessage">
<h2>There was an error retriving the data.</h2>
<p>{this.state.fetchError}</p>
</div>
);
} else {
// Check to see if we have fetched our campers yet.
if (this.state.campers.array.length === 0) {
// Let us know that we are still fetching.
return (
<h2>Fetching Campers. Please Wait...</h2>
);
} else {
return this.renderTable();
}
}
}
};
///
/// \class MainComponent
/// \brief The main component of our application.
///
class MainComponent extends React.Component {
///
/// \brief The constructor.
///
constructor () {
super();
}
///
/// \fn render
/// \brief Renders the component.
///
render () {
return (
<div>
<h1>Camper Leaderboard</h1>
<BoardComponent />
<h6>
Coded by Dennis Griffin.
Leaderboard powered by freeCodeCamp.
</h6>
</div>
);
}
};
const content = document.getElementById("content");
ReactDOM.render(<MainComponent />, content);
Also see: Tab Triggers