JSFiddle - React, Tailwind, and code Playground

by Alexandru Gatea

HTML

<link rel="stylesheet" href="https://alexandrugatea.bitbucket.io/templates/scss-framework/css/reset-frame.css">
<div class="relative ratio-half background-primary">
                                <img src="https://scontent-dfw5-1.cdninstagram.com/vp/4e6b15bac90f623ccf6e94de99407372/5C23CEAF/t51.2885-15/e35/35252658_483292288771161_2050478779372929024_n.jpg" class="absolute absolute-full image-fit">
                                <div class="absolute pos-center-center text-light padding-all-20">
                                   <h1>
                                   Checkbox.js
                                   </h1>
                                </div>
   
  </div>
  <br>

    <strong>Briefing</strong>
    <br>
    
    <p>
    Please create a js functionality for <code>[type="checkbox"]</code>; <br><br>
    <strong>Have in mind that:</strong>
    </p>
    
    <ul>
        <li>input + label must have a common parent, <code>pretty-checkbox</code></li>
        <li><code>inputs</code> must have <code>ID</code> attribute</li>
        <li><code>labels</code> must have <code>FOR</code> attribute, matching input's ID</li>
        
        
        <li>Match label's <code>FOR</code> attribute with matching <code>ID</code></li>
        <li><hr></li>
        <li> <strong>If's</strong></li>
        
        <li>If the wrapper doesn't exist, wrap input and label in <code>pretty-checkbox</code></li>
        <li>If inputs do not have ID, add incremental ID on each type="checkbox" in the page"</li>
        <li>If labels do not have FOR attr, add it, matched with corresponsing input</li>
        <li>If input is placed before label, and both have matching ID and FOR, no js is needed</li>
        <li>If input is placed before label, but do not have  matching ID and FOR, just add ID and FOR</li>
        <li>If input is placed inside or after the label, check with <code>JS</code> wheter the input is checked or not and <code>add / remove</code>...

SCSS

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background: #fafafa;
    padding: 50px;
}

p {
    margin-bottom: 20px;
}

ul {
    padding-left: 40px;
}

$primary: #1a976f;
.checkbox {
    margin-top: 200px;
    width: 90%;
    margin: 50px auto;
    position: relative;
    top: 0;
    input {
        position: absolute;
        opacity: 0;
        top: 0;
        left: 0;
        width: 20px;
        height: 20px;
        display: block;
        padding: 0;
        z-index: 10;
        pointer-events: none;
    }
    label {
        display: block;
        padding-left: 40px;
        line-height: 20px;
        user-select: none;
        cursor: pointer;
        &:before {
            content: "✓";
            display: flex;
            width: 20px;
            height: 20px;
            border: 1px solid $primary;
            justify-content: center;
            align-items: center;
            border-radius: 4px;
            position: absolute;
            left: -1px;
            top: -1px;
            background: $primary;
            box-shadow: inset 0px 0px 0px 20px #fff;
            transition: all 0.3s ease;
            color: #fff;
            font-size: 12px;
        }
        &:after {
            content: "";
            display: flex;
            width: 20px;
            height: 20px;
            border: 1px solid $primary;
            justify-content: center;
            align-items: center;
            border-radius: 4px;
            position: absolute;
            left: -1px;
            top: -1px;
            background: $primary;
            opacity: 0;
        }
        &:before,
        &:after {
            cursor: pointer;
        }
        a {
            text-decoration: none;
            font-weight: bold;
            color: inherit;
            &:hover {
                text-decoration: underline;
            }
        }
    }
    input:checked+label:before, label.checked:before {
        box-shadow: inset...