|
| 1 | +<script> |
| 2 | + import { onMount, onDestroy } from "svelte"; |
| 3 | + export let title = ""; |
| 4 | + export let image = ""; // imported image path |
| 5 | + export let description = ""; |
| 6 | + export let href = "#"; |
| 7 | +
|
| 8 | + let isTouch = false; |
| 9 | + let revealed = false; |
| 10 | + let el; // root anchor element |
| 11 | +
|
| 12 | + onMount(() => { |
| 13 | + // Detect primary touch devices (no hover, coarse pointer) |
| 14 | + if (typeof window !== "undefined" && typeof window.matchMedia === "function") { |
| 15 | + const mq = window.matchMedia("(hover: none) and (pointer: coarse)"); |
| 16 | + isTouch = mq.matches; |
| 17 | + // keep in sync if device orientation/profile changes |
| 18 | + const handler = (e) => (isTouch = e.matches); |
| 19 | + mq.addEventListener?.("change", handler); |
| 20 | +
|
| 21 | + // Close overlay when tapping outside on touch devices |
| 22 | + const handleDocPointerDown = (e) => { |
| 23 | + if (!isTouch) return; |
| 24 | + if (!revealed) return; |
| 25 | + if (!el) return; |
| 26 | + if (!el.contains(e.target)) { |
| 27 | + revealed = false; |
| 28 | + } |
| 29 | + }; |
| 30 | + document.addEventListener("pointerdown", handleDocPointerDown, true); |
| 31 | +
|
| 32 | + onDestroy(() => { |
| 33 | + mq.removeEventListener?.("change", handler); |
| 34 | + document.removeEventListener("pointerdown", handleDocPointerDown, true); |
| 35 | + }); |
| 36 | + } |
| 37 | + }); |
| 38 | +
|
| 39 | + function handleClick(event) { |
| 40 | + // On touch devices: first tap reveals overlay, second tap follows link |
| 41 | + if (isTouch && !revealed) { |
| 42 | + revealed = true; |
| 43 | + event.preventDefault(); |
| 44 | + } |
| 45 | + } |
| 46 | +</script> |
| 47 | +
|
| 48 | +<a bind:this={el} class="card" class:revealed={revealed} href={href} target="_blank" rel="noopener noreferrer" aria-label={`Open project ${title} in a new tab`} on:click={handleClick}> |
| 49 | + <div class="media"> |
| 50 | + <img src={image} alt={title} loading="lazy" /> |
| 51 | + <div class="overlay"> |
| 52 | + <h3>{title}</h3> |
| 53 | + <p>{description}</p> |
| 54 | + </div> |
| 55 | + </div> |
| 56 | +</a> |
| 57 | +
|
| 58 | +<style> |
| 59 | + .card { |
| 60 | + display: block; |
| 61 | + color: inherit; |
| 62 | + text-decoration: none; |
| 63 | + border-radius: 12px; |
| 64 | + overflow: hidden; |
| 65 | + background: #fff; |
| 66 | + box-shadow: 0 2px 8px rgba(0,0,0,0.06); |
| 67 | + transition: transform 0.15s ease, box-shadow 0.15s ease; |
| 68 | + } |
| 69 | + .card:focus-visible { |
| 70 | + outline: 3px solid #111; |
| 71 | + outline-offset: 2px; |
| 72 | + } |
| 73 | + .card:hover { |
| 74 | + transform: translateY(-2px); |
| 75 | + box-shadow: 0 6px 20px rgba(0,0,0,0.12); |
| 76 | + } |
| 77 | +
|
| 78 | + .media { |
| 79 | + position: relative; |
| 80 | + aspect-ratio: 16 / 9; |
| 81 | + background: #f0f0f0; |
| 82 | + } |
| 83 | + .media img { |
| 84 | + width: 100%; |
| 85 | + height: 100%; |
| 86 | + object-fit: cover; |
| 87 | + display: block; |
| 88 | + transition: filter 0.2s ease; |
| 89 | + } |
| 90 | +
|
| 91 | + .overlay { |
| 92 | + position: absolute; |
| 93 | + inset: 0; |
| 94 | + display: flex; |
| 95 | + flex-direction: column; |
| 96 | + justify-content: flex-end; |
| 97 | + gap: 0.25rem; |
| 98 | + padding: 1rem; |
| 99 | + background: linear-gradient(to top, rgba(0,0,0,0.7), rgba(0,0,0,0.25) 55%, rgba(0,0,0,0)); |
| 100 | + color: #fff; |
| 101 | + opacity: 0; |
| 102 | + transition: opacity 0.2s ease; |
| 103 | + } |
| 104 | + .card:hover .overlay { |
| 105 | + opacity: 1; |
| 106 | + } |
| 107 | + .card.revealed .overlay { |
| 108 | + opacity: 1; |
| 109 | + } |
| 110 | + .card:hover .media img, |
| 111 | + .card.revealed .media img { |
| 112 | + filter: brightness(0.7) saturate(0.95); |
| 113 | + } |
| 114 | + .overlay h3 { |
| 115 | + margin: 0; |
| 116 | + font-size: 1.1rem; |
| 117 | + line-height: 1.2; |
| 118 | + } |
| 119 | + .overlay p { |
| 120 | + margin: 0; |
| 121 | + font-size: 0.9rem; |
| 122 | + color: #eaeaea; |
| 123 | + } |
| 124 | +</style> |
0 commit comments