JSFiddle - React, Tailwind, and code Playground

by filibit

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="main.css" />
    <title>JS Components Example</title>
  </head>
  <body>
    <script src="example.js"></script>
  </body>
</html>

CSS

:root {
    font-family: sans-serif;
    font-size: 2.5vh;
}

.card {
    width: 20%;
    position: absolute;
    left: 50vw;
    top: 50vh;
    transform: translate(-50%, -50%);
    background: #ddd;
    padding: 1rem;
    border-radius: .5rem;
    box-shadow: 0 0 .5rem -.2rem black;
}

.card__content {
    padding: 1rem;
    border-radius: .5rem;
    background: #ccc;
    line-height: 1.3rem;
    box-shadow: 0 0 .5rem -.2rem black;
}

.card__button {
    background: #27f;
    border-radius: .5rem;
    margin-top: 1rem;
    text-align: center;
    user-select: none;
    cursor: pointer;
    padding: 1rem;
    color: #f0f0f0;
    line-height: 1.3rem;
    box-shadow: 0 0 .5rem -.2rem black;
}

.card__button:hover {
    background: rgb(17, 95, 219);
}

.card__button:active {
    box-shadow: none;
}

JavaScript

document.body.appendChild(Card())

function Card() {
    const node = document.createElement('div');
    node.classList.add('card');

    function applyRandomColor() {
        node.style.background = '#' + Math.floor(Math.random()*16777215).toString(16);
    }
    
    // to support older browsers, use appendChild() for every child instead
    node.append(
        CardContent(),
        CardButton({pressHandler: applyRandomColor})
        )
    
    return node
}

function CardContent() {
    const node = document.createElement('div');
    node.classList.add('card__content'); // to support older browsers, use node.setAttribute('class', 'card__content') instead
    node.textContent = 'Text text text text text text text text text text text text';
    
    return node
}

function CardButton({pressHandler}) {
    const node = document.createElement('div');
    node.textContent = 'Press me';
    node.classList.add('card__button');
    
    node.addEventListener('click', pressHandler);

    return node
}