|
| 1 | +--- |
| 2 | +type Props = { |
| 3 | + id: string; |
| 4 | + variant: 'default' | 'turnable'; |
| 5 | + crease?: boolean; |
| 6 | + color?: 'white' | 'beige'; |
| 7 | +}; |
| 8 | +
|
| 9 | +const { id, variant, color = 'white', crease = false }: Props = Astro.props; |
| 10 | +--- |
| 11 | + |
| 12 | +<turnable-tile |
| 13 | + {id} |
| 14 | + class:list={['tile', color]} |
| 15 | + data-crease={crease} |
| 16 | + data-id={id} |
| 17 | + data-variant={variant} |
| 18 | +> |
| 19 | + <div class="tile-front"> |
| 20 | + <slot name="front" /> |
| 21 | + </div> |
| 22 | + {variant === 'turnable' && ( |
| 23 | + <div class="tile-back"> |
| 24 | + <slot name="back" /> |
| 25 | + </div> |
| 26 | + )} |
| 27 | +</turnable-tile> |
| 28 | + |
| 29 | +<style> |
| 30 | + .tile { |
| 31 | + color: inherit; |
| 32 | + font-weight: 600; |
| 33 | + display: grid; |
| 34 | + grid-template-areas: 'stack'; |
| 35 | + position: relative; |
| 36 | + perspective: 1000px; |
| 37 | + transform-style: preserve-3d; |
| 38 | + transition: rotate 500ms linear; |
| 39 | + } |
| 40 | + |
| 41 | + .tile[data-variant='turnable'] { |
| 42 | + cursor: pointer; |
| 43 | + } |
| 44 | + |
| 45 | + .tile.show { |
| 46 | + rotate: y 180deg; |
| 47 | + } |
| 48 | + |
| 49 | + .tile-front, |
| 50 | + .tile-back { |
| 51 | + grid-area: stack; |
| 52 | + padding: var(--size-gutter); |
| 53 | + display: grid; |
| 54 | + place-content: center; |
| 55 | + backface-visibility: hidden; |
| 56 | + text-align: center; |
| 57 | + height: 100%; |
| 58 | + box-sizing: border-box; |
| 59 | + } |
| 60 | + |
| 61 | + .white .tile-front, |
| 62 | + .white .tile-back { |
| 63 | + background-color: var(--color-white); |
| 64 | + } |
| 65 | + |
| 66 | + .beige .tile-front, |
| 67 | + .beige .tile-back { |
| 68 | + background-color: var(--color-background); |
| 69 | + } |
| 70 | + |
| 71 | + .tile-back { |
| 72 | + rotate: y 180deg; |
| 73 | + } |
| 74 | + |
| 75 | + .tile-front { |
| 76 | + grid-area: stack; |
| 77 | + padding: var(--size-gutter-medium); |
| 78 | + display: grid; |
| 79 | + place-content: center; |
| 80 | + backface-visibility: hidden; |
| 81 | + text-align: center; |
| 82 | + height: 100%; |
| 83 | + box-sizing: border-box; |
| 84 | + } |
| 85 | + |
| 86 | + .tile[data-crease='true'] .tile-front { |
| 87 | + clip-path: polygon(0 0, 100% 0, 100% calc(100% - 13px), calc(100% - 13px) 100%, 0 100%); |
| 88 | + } |
| 89 | + |
| 90 | + .tile[data-crease='true'] .tile-front::after { |
| 91 | + content: ''; |
| 92 | + position: absolute; |
| 93 | + bottom: 0; |
| 94 | + right: 0; |
| 95 | + width: 0; |
| 96 | + height: 0; |
| 97 | + |
| 98 | + border-style: solid; |
| 99 | + border-width: 13px 13px 0 0; |
| 100 | + border-color: #d0d0d0 transparent transparent transparent; |
| 101 | + } |
| 102 | +</style> |
| 103 | + |
| 104 | +<script> |
| 105 | + class Tile extends HTMLElement { |
| 106 | + connectedCallback() { |
| 107 | + const id = this.dataset.id; |
| 108 | + if (!id) return; |
| 109 | + |
| 110 | + const tile = document.getElementById(id); |
| 111 | + |
| 112 | + tile?.addEventListener('click', function () { |
| 113 | + if (tile?.dataset.variant === 'turnable') { |
| 114 | + tile?.classList.toggle('show'); |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (!customElements.get('turnable-tile')) { |
| 121 | + customElements.define('turnable-tile', Tile); |
| 122 | + } |
| 123 | +</script> |
0 commit comments