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="root"></div>
// Normalize.css
// General
:root {
--fonts: "Heebo", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
--white: #ffffff;
--black: #010101;
--gray: #f6f6f6;
--blue: #bdbef2;
--gradient-left: #b8adf1;
--gradient-center: #a191f1;
--gradient-right: #bf85e0;
--glass: #75757520;
--gap-step: 4px;
--padding: 1.2rem;
}
h1,
p {
font-family: var(--fonts);
}
body {
color: var(--black);
background: #e3e7ed;
font-family: var(--fonts);
overflow-x: hidden;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
min-height: 100vh;
}
.d-flex {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.d-inline-flex {
display: -webkit-inline-box;
display: -ms-inline-flexbox;
display: inline-flex;
}
.flex-column {
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
}
.align-center {
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.justify-content-center {
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
.justify-content-space-between {
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
.gap1 {
gap: var(--gap-step);
}
.gap2 {
gap: calc(2 * var(--gap-step));
}
.gap3 {
gap: calc(3 * var(--gap-step));
}
.gap4 {
gap: calc(4 * var(--gap-step));
}
.gap5 {
gap: calc(5 * var(--gap-step));
}
.relative {
position: relative;
}
.absolute {
position: absolute;
}
.pe-none {
pointer-events: none;
}
.user-select-none {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.text-center {
text-align: center;
}
.text-right {
text-align: right;
}
.overflow-auto {
overflow: auto;
&::-webkit-scrollbar {
width: 0px;
height: 0px;
}
}
.streamApp {
max-width: 100vw;
width: 100%;
height: 100%;
margin: auto;
padding: var(--padding) 0 75px;
background: var(--gray);
overflow-x: hidden;
z-index: 1;
&::-webkit-scrollbar {
width: 0px;
height: 0px;
}
}
.sectionTitle {
margin: 0 var(--padding);
font-weight: 900;
font-size: 24px;
}
@media (min-width: 480px) {
.streamApp {
max-width: 400px;
width: 100%;
max-height: 720px;
height: 100%;
padding: 0;
-webkit-box-shadow: 0 0 0 4px #939393, 0 10px 32px #0005;
box-shadow: 0 0 0 4px #939393, 0 10px 32px #0005;
border: solid 10px #010101;
border-radius: 40px;
.stub {
position: sticky;
top: 0;
width: 100%;
height: 24px;
z-index: 10;
padding-bottom: 10px;
&::before {
content: "";
position: absolute;
top: 0;
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
background: #010101;
height: 24px;
width: 50%;
border-radius: 0 0 12px 12px;
}
&::after {
content: "";
position: absolute;
top: 6px;
left: 30%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
background: -webkit-gradient(
linear,
left top,
right top,
from(#164865),
to(#123579)
);
background: -o-linear-gradient(left, #164865, #123579);
background: linear-gradient(to right, #164865, #123579);
height: 6px;
width: 6px;
border-radius: 50%;
}
}
}
}
// Carousel - Wrapper
.carousel {
margin: var(--padding) 0 calc(var(--padding) * 1.4);
padding: 0 var(--padding);
}
// Search
.searchWrapper {
padding: 0 var(--padding);
margin-bottom: calc(var(--padding) * 1.2);
.searchBar {
background: var(--white);
border-radius: 20px;
padding: 8px calc(var(--padding) - 6px);
width: 100%;
input {
background: none;
border: none;
padding: 0;
outline: none;
width: 100%;
color: #aaa;
&::-webkit-input-placeholder {
font-weight: 200;
color: #aaa;
opacity: 1;
}
&::placeholder {
font-weight: 200;
color: #aaa;
opacity: 1;
}
}
}
}
// Game Cards
.cardWrapper {
font-size: 90%;
font-weight: 900;
color: var(--white);
min-width: 200px;
border-radius: 12px;
overflow: hidden;
cursor: pointer;
position: relative;
z-index: 1;
.thumb {
width: 100%;
height: auto;
-o-object-fit: cover;
object-fit: cover;
border-radius: 12px;
-webkit-transition: all 0.8s ease;
-o-transition: all 0.8s ease;
transition: all 0.8s ease;
}
.glass {
padding: 14px 12px;
background: var(--glass);
-webkit-backdrop-filter: blur(10px);
backdrop-filter: blur(10px);
position: absolute;
width: calc(100% - 40px);
border-radius: 10px;
bottom: 8px;
left: 50%;
-webkit-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%);
z-index: 2;
.title {
-o-text-overflow: ellipsis;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
font-size: 120%;
}
}
&:hover .thumb {
-webkit-transform: scale(1.04);
-ms-transform: scale(1.04);
transform: scale(1.04);
}
}
// Live Cards
.liveCard {
color: var(--white);
min-width: 268px;
overflow: hidden;
.preview {
height: 140px;
border-radius: 12px;
overflow: hidden;
position: relative;
z-index: 1;
img {
width: 100%;
overflow: hidden;
-webkit-transition: all 0.8s ease;
-o-transition: all 0.8s ease;
transition: all 0.8s ease;
}
&:hover img {
-webkit-transform: scale(1.1);
-ms-transform: scale(1.1);
transform: scale(1.1);
}
}
.status {
position: absolute;
top: 10px;
right: 10px;
z-index: 1;
color: var(--white);
background: #ff0000;
border-radius: 10px;
padding: 5px 8px 4px;
font-size: 80%;
}
.vitals {
border-radius: 12px;
height: 50px;
overflow: hidden;
position: relative;
z-index: 1;
.logo {
border-radius: 50%;
-webkit-box-shadow: 0 0 4px #0005;
box-shadow: 0 0 4px #0005;
width: 26px;
height: 26px;
-o-object-fit: cover;
object-fit: cover;
}
.name {
font-weight: 600;
font-size: 90%;
}
.playing {
font-weight: 300;
font-size: 80%;
}
.viewers {
font-weight: 300;
font-size: 80%;
}
}
.blurred {
position: absolute;
left: 0;
top: 0;
border-radius: 12px;
overflow: hidden;
width: 100%;
height: 100%;
-o-object-fit: cover;
object-fit: cover;
}
.glass {
padding: 0 calc(var(--padding) / 2);
-webkit-backdrop-filter: blur(12px);
backdrop-filter: blur(12px);
z-index: 1;
border-radius: 12px;
overflow: hidden;
position: relative;
z-index: 1;
width: 100%;
height: 100%;
}
}
.gray {
&.blurred,
img {
-webkit-filter: grayscale(1);
filter: grayscale(1);
}
.status {
background: #555;
}
}
.example {
display: -ms-grid;
display: grid;
-webkit-transition: all 0.5s;
-o-transition: all 0.5s;
transition: all 0.5s;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background: -webkit-gradient(
linear,
left top,
left bottom,
from(white),
to(black)
);
background: -o-linear-gradient(top, white, black);
background: linear-gradient(to bottom, white, black);
}
// Streamer Cards
.streamerCard {
color: var(--black);
min-width: 170px;
border-radius: 12px;
overflow: hidden;
cursor: pointer;
.details {
background: var(--white);
padding: 0 var(--padding) var(--padding);
.logo {
height: 32px;
border-radius: 50%;
margin-top: -16px;
margin-bottom: calc(3 * var(--gap-step));
}
.name {
font-size: 110%;
}
.status {
font-weight: 300;
border-bottom: solid 1px var(--gray);
padding-bottom: calc(3 * var(--gap-step));
margin-top: var(--gap-step);
margin-left: auto;
margin-right: auto;
margin-bottom: calc(4 * var(--gap-step));
&::before {
content: "";
position: absolute;
left: calc(-7px - var(--gap-step));
top: 5px;
width: 7px;
height: 7px;
border-radius: 50%;
}
&.offline::before {
background: red;
-webkit-animation: offline 2.4s infinite;
animation: offline 2.4s infinite;
}
&.online::before {
background: green;
-webkit-animation: online 2.4s infinite;
animation: online 2.4s infinite;
}
}
.value {
font-weight: 500;
font-size: 110%;
}
.label {
font-weight: 300;
font-size: 80%;
}
}
}
@-webkit-keyframes offline {
0% {
-webkit-box-shadow: 0 0 0 0 rgba(255, 0, 0, 0.4);
}
70% {
-webkit-box-shadow: 0 0 0 6px rgba(255, 0, 0, 0);
}
100% {
-webkit-box-shadow: 0 0 0 0 rgba(255, 0, 0, 0);
}
}
@keyframes offline {
0% {
-webkit-box-shadow: 0 0 0 0 rgba(255, 0, 0, 0.4);
box-shadow: 0 0 0 0 rgba(255, 0, 0, 0.4);
}
70% {
-webkit-box-shadow: 0 0 0 6px rgba(255, 0, 0, 0);
box-shadow: 0 0 0 6px rgba(255, 0, 0, 0);
}
100% {
-webkit-box-shadow: 0 0 0 0 rgba(255, 0, 0, 0);
box-shadow: 0 0 0 0 rgba(255, 0, 0, 0);
}
}
@-webkit-keyframes online {
0% {
-webkit-box-shadow: 0 0 0 0 rgba(0, 128, 0, 0.4);
}
70% {
-webkit-box-shadow: 0 0 0 6px rgba(0, 128, 0, 0);
}
100% {
-webkit-box-shadow: 0 0 0 0 rgba(0, 128, 0, 0);
}
}
@keyframes online {
0% {
-webkit-box-shadow: 0 0 0 0 rgba(0, 128, 0, 0.4);
box-shadow: 0 0 0 0 rgba(0, 128, 0, 0.4);
}
70% {
-webkit-box-shadow: 0 0 0 6px rgba(0, 128, 0, 0);
box-shadow: 0 0 0 6px rgba(0, 128, 0, 0);
}
100% {
-webkit-box-shadow: 0 0 0 0 rgba(0, 128, 0, 0);
box-shadow: 0 0 0 0 rgba(0, 128, 0, 0);
}
}
// Navigation
.navbar {
background: var(--white);
position: fixed;
bottom: 0;
left: 0;
width: 100%;
z-index: 10;
-webkit-box-shadow: 0 -4px 12px #0001;
box-shadow: 0 -4px 12px #0001;
.btn {
text-decoration: none;
-ms-flex-preferred-size: 20%;
flex-basis: 20%;
padding: var(--padding);
padding-bottom: calc(var(--padding) + 8px);
.add {
background: -webkit-gradient(
linear,
left top,
right top,
from(var(--gradient-left)),
color-stop(var(--gradient-center)),
to(var(--gradient-right))
);
background: -o-linear-gradient(
left,
var(--gradient-left),
var(--gradient-center),
var(--gradient-right)
);
background: linear-gradient(
to right,
var(--gradient-left),
var(--gradient-center),
var(--gradient-right)
);
width: 34px;
height: 34px;
margin-top: -6px;
border-radius: 50%;
color: var(--white);
font-weight: 300;
font-size: 150%;
}
&:active {
background: var(--gray);
}
}
}
@media (min-width: 480px) {
.navbar {
position: sticky;
}
}
// Icons
const icons = {
search:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/icons/search.svg",
logo:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/icons/react.svg",
home:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/icons/home.svg",
badge:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/icons/badge.svg",
joystick:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/icons/joystick.svg",
message:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/icons/message.svg",
play:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/icons/play.svg"
};
// JSON Dummy Data
const dummy =
"https://raw.githubusercontent.com/gestok/streaming-app/data/dummy.json";
// Random Data
const streamers = [
{
id: "ESL_SC2",
logo:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/streamers/ESL_SC2.png",
banner:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/banners/esl_sc2.jpg",
viewers: Math.floor(Math.random() * 500) + 10,
followers: Math.floor(Math.random() * 10000) + 100,
videos: Math.floor(Math.random() * 200) + 20
},
{
id: "OgamingSC2",
logo:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/streamers/OG_SC2.png",
banner:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/banners/og-sc2.jpg",
viewers: Math.floor(Math.random() * 500) + 10,
followers: Math.floor(Math.random() * 10000) + 100,
videos: Math.floor(Math.random() * 200) + 20
},
{
id: "cretetion",
logo:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/streamers/cretetion.png",
banner:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/banners/cretetion.jpg",
viewers: Math.floor(Math.random() * 500) + 10,
followers: Math.floor(Math.random() * 10000) + 100,
videos: Math.floor(Math.random() * 200) + 20
},
{
id: "freecodecamp",
logo:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/streamers/freecodecamp.png",
banner:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/banners/freecodecamp.jpg",
viewers: Math.floor(Math.random() * 500) + 10,
followers: Math.floor(Math.random() * 10000) + 100,
videos: Math.floor(Math.random() * 200) + 20
},
{
id: "storbeck",
banner:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/banners/league.jpg",
viewers: Math.floor(Math.random() * 500) + 10,
followers: Math.floor(Math.random() * 10000) + 100,
videos: Math.floor(Math.random() * 200) + 20
},
{
id: "habathcx",
banner:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/banners/wow.jpg",
viewers: Math.floor(Math.random() * 500) + 10,
followers: Math.floor(Math.random() * 10000) + 100,
videos: Math.floor(Math.random() * 200) + 20
},
{
id: "RobotCaleb",
logo:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/streamers/RobotCaleb.png",
banner:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/banners/code.jpg",
viewers: Math.floor(Math.random() * 500) + 10,
followers: Math.floor(Math.random() * 10000) + 100,
videos: Math.floor(Math.random() * 200) + 20
},
{
id: "noobs2ninjas",
logo:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/streamers/noobs2ninjas.png",
banner:
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/banners/codespiral.jpg",
viewers: Math.floor(Math.random() * 500) + 10,
followers: Math.floor(Math.random() * 10000) + 100,
videos: Math.floor(Math.random() * 200) + 20
}
];
const Carousel = ({ children }) => {
let pos = { left: 0, x: 0 };
const carousel = React.useRef(null);
const handleMouseDown = (e) => {
e.preventDefault();
pos = {
left: carousel.current.scrollLeft,
x: e.clientX
};
carousel.current.addEventListener("mousemove", handleMouseMove);
carousel.current.addEventListener("mouseup", handleMouseUp);
};
const handleMouseMove = (e) => {
const dx = e.clientX - pos.x;
carousel.current.scrollLeft = pos.left - dx;
};
const handleMouseUp = () => {
carousel.current.removeEventListener("mousemove", handleMouseMove);
carousel.current.removeEventListener("mouseup", handleMouseUp);
};
return (
<div
ref={carousel}
className="carousel d-flex gap4 overflow-auto"
onMouseDown={handleMouseDown}
>
{children}
</div>
);
};
const Search = () => {
return (
<div className="searchWrapper d-flex gap3">
<div className="searchBar d-flex gap1 relative">
<img src={icons.search} className="user-select-none pe-none" />
<input type="text" placeholder="Search streams" />
</div>
<img src={icons.logo} className="user-select-none pe-none" />
</div>
);
};
const Gamecard = ({ thumb, title, viewers }) => {
const formatViewers = (viewers) => {
return (viewers / 1000).toFixed(1);
};
return (
<div className="cardWrapper d-inline-flex flex-column relative">
<img className="thumb pe-none user-select-none" src={thumb} alt={title} />
<div className="d-flex align-center justify-content-space-between glass gap2 user-select-none pe-none">
<span className="title">{title}</span>
<span className="d-flex align-center gap2">
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
viewBox="0 0 48 48"
fill="var(--white)"
>
<path d="M24 31.5q3.55 0 6.025-2.475Q32.5 26.55 32.5 23q0-3.55-2.475-6.025Q27.55 14.5 24 14.5q-3.55 0-6.025 2.475Q15.5 19.45 15.5 23q0 3.55 2.475 6.025Q20.45 31.5 24 31.5Zm0-2.9q-2.35 0-3.975-1.625T18.4 23q0-2.35 1.625-3.975T24 17.4q2.35 0 3.975 1.625T29.6 23q0 2.35-1.625 3.975T24 28.6Zm0 9.4q-7.3 0-13.2-4.15Q4.9 29.7 2 23q2.9-6.7 8.8-10.85Q16.7 8 24 8q7.3 0 13.2 4.15Q43.1 16.3 46 23q-2.9 6.7-8.8 10.85Q31.3 38 24 38Zm0-15Zm0 12q6.05 0 11.125-3.275T42.85 23q-2.65-5.45-7.725-8.725Q30.05 11 24 11t-11.125 3.275Q7.8 17.55 5.1 23q2.7 5.45 7.775 8.725Q17.95 35 24 35Z" />
</svg>
{`${formatViewers(viewers)}K`}
</span>
</div>
</div>
);
};
const Livecard = ({ thumb, status, game, url, logo, name, viewers }) => {
return (
<div className="liveCard d-flex flex-column gap2">
<a
target="_blank"
href={url ? url : "#"}
className={`${!url && "gray"} preview d-flex relative`}
>
<img
className="d-flex pe-none user-select-none"
src={thumb}
alt={`${name} Live`}
/>
<span className="status pe-none user-select-none">
{status === "offline" ? "OFF" : "LIVE"}
</span>
</a>
<div className="vitals d-flex gap2 justify-content-space-between align-center relative">
<img src={thumb} className={`${!url && "gray"} blurred`} />
<div className="d-flex gap1 justify-content-space-between align-center glass user-select-none">
<div className="d-flex align-center gap1">
{logo && <img src={logo} className="logo d-flex" />}
<div className="d-flex flex-column gap1">
{name && <span className="name">{name}</span>}
{url && <span className="playing">{game}</span>}
</div>
</div>
{url && <div className="viewers">{viewers} VIEWERS</div>}
</div>
</div>
</div>
);
};
const Streamer = ({ banner, logo, name, status, followers, videos }) => {
const formatFollowers = (num) =>
num < 1000 ? num : `${(num / 1000).toFixed(1)}K`;
return (
<div className="streamerCard d-flex flex-column relative">
<img className="banner" src={banner} />
<div className="details d-flex flex-column">
<div className="d-flex justify-content-center">
<img className="logo" src={logo} />
</div>
<div className="d-flex gap1 align-center justify-content-center">
<span className="name">{name}</span>
<img src={icons.badge} alt="verified" />
</div>
<div
className={`status ${status} d-inline-flex justify-content-center relative`}
>
{status}
</div>
<div className="d-flex justify-content-space-between gap2">
<div className="d-flex flex-column gap1">
<span className="value">{formatFollowers(followers)}</span>
<span className="label">FOLLOWERS</span>
</div>
<div className="d-flex flex-column text-right gap1">
<span className="value">{videos}</span>
<span className="label">VIDEOS</span>
</div>
</div>
</div>
</div>
);
};
const Navigation = () => {
return (
<div className="navbar d-flex">
<a
className="btn d-flex align-center justify-content-center user-select-none"
href="#"
title="Home"
>
<img src={icons.home} alt="" className="d-flex" />
</a>
<a
className="btn d-flex align-center justify-content-center user-select-none"
href="#"
title="Clips"
>
<img src={icons.play} alt="" className="d-flex" />
</a>
<a
className="btn d-flex align-center justify-content-center user-select-none"
href="#"
title="Add"
>
<span className="add d-flex justify-content-center align-center">
+
</span>
</a>
<a
className="btn d-flex align-center justify-content-center user-select-none"
href="#"
title="Games"
>
<img src={icons.joystick} alt="" className="d-flex" />
</a>
<a
className="btn d-flex align-center justify-content-center user-select-none"
href="#"
title="Messages"
>
<img src={icons.message} alt="" className="d-flex" />
</a>
</div>
);
};
const useFetchStreamers = () => {
const offline =
"https://raw.githubusercontent.com/gestok/streaming-app/main/src/assets/thumbs/streamers/offline.jpg";
const url = "https://twitch-proxy.freecodecamp.rocks/twitch-api/streams";
const [streamerData, setStreamerData] = React.useState([]);
const [dummyData, setDummyData] = React.useState(null);
const [completed, setCompleted] = React.useState(false);
const fetchDummy = async () => {
let res = await fetch(dummy);
let data = await res.json();
await setDummyData(data);
};
const fetchStreamers = async () => {
await Promise.all(
streamers.map(async (streamer) => {
const res = await fetch(`${url}/${streamer.id}`);
const data = await res.json();
let info = {
name: streamer.id,
logo: streamer.logo || offline,
banner: streamer.banner,
viewers: 0,
followers: streamer.followers,
videos: streamer.videos
};
if (data["stream"] === null) {
info.game = "Offline";
info.status = "offline";
} else if (data["stream"] === undefined) {
info.game = "Account Closed";
info.status = "offline";
} else {
info.game = data.stream.game;
info.viewers = data.stream.viewers;
info.url = data.stream.channel.url;
info.status = "online";
}
setStreamerData((streamerData) => [...streamerData, info]);
})
);
await fetchDummy();
setCompleted(true);
};
React.useEffect(() => {
fetchStreamers();
return () => fetchStreamers();
}, []);
return [completed, streamerData, dummyData];
};
const App = () => {
const [completed, streamers, dummy] = useFetchStreamers();
const shuffleArray = (array) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
};
return (
<div className="streamApp relative">
<div className="stub"></div>
<Search />
<span className="sectionTitle user-select-none">Recommended games</span>
<Carousel>
{completed &&
shuffleArray(dummy.games).map((game, i) => {
return (
<Gamecard
key={game.slug + i}
thumb={game.thumb}
title={game.name}
viewers={game.viewers}
/>
);
})}
</Carousel>
<span className="sectionTitle user-select-none">Popular Lives</span>
<Carousel>
{completed &&
streamers.map((streamer, i) => {
return (
<Livecard
key={`${streamer.name + i}_live`}
thumb={dummy.lives[i].thumb}
status={streamer.status}
game={streamer.game}
url={streamer.url}
logo={streamer.logo}
name={streamer.name}
viewers={streamer.viewers}
/>
);
})}
</Carousel>
<span className="sectionTitle user-select-none">Favourite Streamers</span>
<Carousel>
{completed &&
streamers.map((streamer, i) => {
return (
<Streamer
key={streamer.name + i}
banner={streamer.banner}
logo={streamer.logo}
name={streamer.name}
status={streamer.status}
followers={streamer.followers}
viewers={streamer.viewers}
videos={streamer.videos}
/>
);
})}
</Carousel>
<Navigation />
</div>
);
};
// Render
const rootElement = document.getElementById("root");
const root = ReactDOM.createRoot(rootElement);
root.render(<App />);
Also see: Tab Triggers