Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions star-rating/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const makeStarRating = function (noOfStars = 5) {
let rating = 0;
let starComponent;

function changeRating(newRating) {
rating = newRating;
}
function getStarComponent() {
if (!starComponent) {
starComponent = document.createElement('ul');
starComponent.className = 'stcomp';
for (let i = 0; i < noOfStars; i++) {
const li = document.createElement('li');
li.setAttribute('data-rating', i + 1);
li.className = 'star';
if (i === 0) li.tabIndex = 0;
starComponent.append(li);
}
starComponent.addEventListener('mouseover', onMouseOver);
starComponent.addEventListener('mouseleave', onMouseLeave);
starComponent.addEventListener('click', onMouseClick);
}
return starComponent;
}
function renderChanges(rating) {
for (let index = 0; index < rating; index++) {
starComponent.children[index].classList.add('star-filled');
}
for (let index = rating; index < noOfStars; index++) {
starComponent.children[index].classList.remove('star-filled');
}
}

function onMouseOver(e) {
let isStar = e.target.classList.contains('star');
if (isStar) {
const { rating } = e.target.dataset;
renderChanges(rating);
}
}

function onMouseLeave(e) {
renderChanges(rating);
}

function onMouseClick(e) {
let star = e.target ?? e;
let isStar = star.classList.contains('star');
if (isStar) {
activate(star);
let { rating } = star.dataset;
if (e.key !== 'Tab' && rating === getRating()) {
rating = 0;
resetTabIndex();
starComponent.firstElementChild.tabIndex = 0;
}
changeRating(rating);
renderChanges(rating);
}
}

function activate(element) {
resetTabIndex();
element.tabIndex = 0;
element.focus();
}

function resetTabIndex() {
starComponent.childNodes.forEach((star) => {
star.tabIndex = -1;
});
}

function getRating() {
return rating;
}

return { getStarComponent };
};

const ratingModule1 = makeStarRating(5);

const starComponent1 = ratingModule1.getStarComponent();
const container = document.querySelector('main');

container.append(starComponent1);
14 changes: 14 additions & 0 deletions star-rating/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Star Rating</title>
</head>
<body>
<main></main>
<script src="./app.js"></script>
</body>
</html>
32 changes: 32 additions & 0 deletions star-rating/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.star {
background: lightgrey;
clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
display: inline-block;
height: 50px;
width: 50px;
margin-left: 2px;
transition: transform 0.1s;
}

.star:hover {
transform: scale(1.2);
}
.star:focus {
transform: scale(1.2);
}
.stcomp {
cursor: pointer;
display: flex;
list-style-type: none;
}

main {
display: flex;
flex-direction: column;
height: 100vh;
justify-content: center;
align-items: center;
}
.star-filled {
background: orange;
}