A Pen by Florian Bender on CodePen.
Created
October 19, 2024 08:56
-
-
Save fbender/b3f154c33f95c839ffe536c16859bb57 to your computer and use it in GitHub Desktop.
Untitled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="javascript:"> | |
<fieldset class="rating"> | |
<legend>Use number keys or click buttons to rate from 0 to 8, or select "Unclear" (number 9)</legend> | |
<div class="rating-group"> | |
<!-- 0-8 Rating Buttons --> | |
<input type="radio" id="rate-0" name="rating" value="0"> | |
<label for="rate-0" class="grouped">0</label> | |
<input type="radio" id="rate-1" name="rating" value="1"> | |
<label for="rate-1" class="grouped">1</label> | |
<input type="radio" id="rate-2" name="rating" value="2"> | |
<label for="rate-2" class="grouped">2</label> | |
<input type="radio" id="rate-3" name="rating" value="3"> | |
<label for="rate-3" class="grouped">3</label> | |
<input type="radio" id="rate-4" name="rating" value="4"> | |
<label for="rate-4" class="grouped">4</label> | |
<input type="radio" id="rate-5" name="rating" value="5"> | |
<label for="rate-5" class="grouped">5</label> | |
<input type="radio" id="rate-6" name="rating" value="6"> | |
<label for="rate-6" class="grouped">6</label> | |
<input type="radio" id="rate-7" name="rating" value="7"> | |
<label for="rate-7" class="grouped">7</label> | |
<input type="radio" id="rate-8" name="rating" value="8"> | |
<label for="rate-8" class="grouped">8</label> | |
</div> | |
<!-- "Unclear" Button --> | |
<input type="radio" id="rate-9" name="rating" value="9"> | |
<label for="rate-9" class="unclear">Unclear</label> | |
<button type="submit">Submit →</button> | |
</fieldset> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener("keydown", function (event) { | |
if (event.key == "ArrowRight") alert("next"); | |
else if (event.key == "ArrowLeft") alert("prev"); | |
else if (event.key.length() !== 1) return; | |
// Get the pressed key | |
const key = +event.key; // convert to int | |
console.log("pressed key: " + key); | |
// Check if the pressed key is a number (0-9) | |
if (key >= 0 && key <= 9) { | |
// Select the respective radio button by ID | |
const radio = document.getElementById(`rate-${key}`); | |
// If the radio button exists, select it | |
if (radio) { | |
radio.checked = true; | |
radio.focus(); // Optionally focus the radio button | |
// Delay form submission by 200ms | |
setTimeout(function () { | |
if (radio.form) { | |
radio.form.submit(); // Submit the form via the radio input element's form property | |
} | |
}, 200); // Delay by 200ms | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:root { | |
font-family: sans-serif; | |
text-align: center; | |
--form-border-radius: 0.5em; | |
--form-border-size: 1px; | |
--form-border-color: #2e2e2e; | |
--button-fg-color: white; | |
--button-bg-color: #3a3a3a; | |
--button-bg-color-highlight: #007bff; | |
--button-bg-color-variant: #483d8b; | |
--button-bg-color-variant-highlight: #2e8b57; | |
--button-focus-size: 0.1875em; | |
--button-focus-color: #ff00ff; | |
--button-focus-color-variant: #ffa07a; | |
--button-group-spacing: calc(var(--form-border-size) * -1); | |
--button-margin: 0.625em; | |
--button-v-padding: 0.75em; | |
--button-h-padding: calc(var(--button-v-padding) * 1.875); | |
} | |
figure.skyImage { | |
position: relative; | |
} | |
figure.skyImage::before { | |
content: url("static/CloudSectionCross.png"); | |
position: absolute; | |
top: 0; | |
left: 0; | |
right: 0; | |
bottom: 0; | |
/* background: url("static/CloudSectionCross.png") contain no-repeat;/* */ | |
pointer-events: none; | |
} | |
#dayImage { | |
filter: contrast(150%) hue-rotate(315deg) saturate(300%) brightness(100%); | |
} | |
#nightImage { | |
filter: contrast(120%) hue-rotate(315deg) saturate(300%) brightness(160%); | |
} | |
fieldset { | |
margin-left: auto; | |
margin-right: auto; | |
padding: 0.35em 0.625em 0.625em 0.625em; /**/ | |
} | |
.rating { | |
display: flex; | |
flex-direction: row; | |
flex-wrap: wrap; | |
align-items: center; /* Align items vertically centered */ | |
width: auto; /* Automatically fit the content */ | |
min-width: 14.25em; | |
max-width: max-content; | |
border: var(--form-border-size) solid var(--form-border-color); | |
border-radius: var(--form-border-radius); | |
} | |
button, | |
.rating input[type="radio"] + label { | |
margin: var(--button-margin); | |
padding: var(--button-v-padding) var(--button-h-padding); | |
font-size: 1em; | |
cursor: pointer; | |
outline-offset: var(--form-border-size); | |
border-radius: var(--form-border-radius); | |
border: var(--form-border-size) solid transparent; | |
} | |
button[type="submit"] { | |
border-color: transparent; | |
background: white; | |
color: var(--button-bg-color-highlight); | |
} | |
button[type="submit"]:focus, | |
button[type="submit"]:active, | |
button[type="submit"]:hover { | |
/*outline: var(--button-focus-size) solid var(--button-focus-color);*/ | |
box-shadow: 0 0 var(--button-focus-size) var(--button-focus-size) inset; | |
} | |
.rating button[type="submit"] { | |
/*margin-left: calc(var(--button-margin) * 0.5);/* */ | |
/*margin-right: 0;/* */ | |
} | |
.rating input[type="radio"] { | |
position: absolute; | |
opacity: 0; | |
width: 0; | |
height: 0; | |
} | |
.rating input[type="radio"] + label { | |
background-color: var(--button-bg-color); | |
background-image: linear-gradient( | |
to bottom, | |
rgba(255, 255, 255, 0.1), | |
rgba(0, 0, 0, 0.05) | |
); | |
color: white; | |
border-color: var(--form-border-color); | |
transition: background-color 0.3s ease-in, box-shadow 0.3s ease-in; /* outgoing transition */ | |
} | |
.rating input[type="radio"]:checked + label { | |
--button-bg-color: var(--button-bg-color-highlight); | |
} | |
.rating input[type="radio"]:focus + label { | |
outline: var(--button-focus-size) solid var(--button-focus-color); /* Focus outline */ | |
z-index: 9999; | |
} | |
.rating input[type="radio"]:active + label, | |
.rating input[type="radio"]:hover + label { | |
box-shadow: 0 0 var(--button-focus-size) var(--button-focus-size) | |
var(--button-focus-color) inset; | |
/* incoming transition */ | |
transition-timing-function: ease-out; | |
transition-duration: 100ms; | |
z-index: 9998; | |
} | |
.rating-group { | |
display: contents; | |
} | |
/* Grouped buttons (0-8) styling */ | |
.rating-group input[type="radio"] + label:not(:first-of-type) { | |
border-top-left-radius: 0; | |
border-bottom-left-radius: 0; | |
margin-left: var( | |
--button-group-spacing | |
); /* Ensures adjacent buttons appear connected */ | |
} | |
.rating-group input[type="radio"] + label:not(:last-of-type) { | |
border-top-right-radius: 0; | |
border-bottom-right-radius: 0; | |
margin-right: var( | |
--button-group-spacing | |
); /* Ensures adjacent buttons appear connected */ | |
} | |
/* "Unclear" button styling */ | |
.rating input[type="radio"] + .unclear { | |
--button-bg-color: var(--button-bg-color-variant); | |
--button-focus-color: var(--button-focus-color-variant); | |
} | |
.rating input[type="radio"]:checked + .unclear { | |
--button-bg-color: var(--button-bg-color-variant-highlight); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment