JSFiddle - React, Tailwind, and code Playground

by Slava

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Popover Image Transition</title>
</head>
<body>
		<button popovertarget="lightbox">
    	<img src="/api/placeholder/100/100" class="thumbnail" popovertarget="lightbox">
		</button>
    
    <div id="lightbox" class="lightbox" popover>
        <img src="https://picsum.photos/840/300" class="fullscreen-image">
        <button class="close-button" popovertarget="lightbox" popovertargetaction="hide">×</button>
    </div>

</body>
</html>

CSS

body {
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-color: #f0f0f0;
        }

        .thumbnail {
            width: 100px;
            height: 100px;
            object-fit: cover;
            cursor: pointer;
        }

        .lightbox {
            background: rgba(0, 0, 0, 0.8);
            transition: all 0.3s ease;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .lightbox:not(:open) {
            opacity: 0;
            transform: scale(0.1);
            pointer-events: none;
        }

        .lightbox[open] {
            opacity: 1;
            transform: scale(1);
        }

        .fullscreen-image {
            max-width: 100%;
            max-height: 100vh;
            object-fit: contain;
            transition: all 0.3s ease;
        }

        .lightbox:not(:open) .fullscreen-image {
            transform: scale(0.1);
        }

        .lightbox[open] .fullscreen-image {
            transform: scale(1);
        }

        .close-button {
            position: absolute;
            top: 20px;
            right: 20px;
            color: white;
            font-size: 24px;
            cursor: pointer;
            background: none;
            border: none;
            padding: 10px;
        }

JavaScript

const lightbox = document.getElementById("lightbox")
lightbox.addEventListener("toggle", (event) => {
  if (event.newState === "open") {
    lightbox.style.position = "fixed"
    lightbox.style.top = "0"
    lightbox.style.left = "0"
    lightbox.style.width = "100vw"
    lightbox.style.height = "100vh"
  }
})