-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
---
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import ThemeOverride from "./ThemeOverride.astro";
interface Props {
title?: string;
colorScheme?: "dark" | "light";
contrast?: "more" | "no-preference";
}
const { colorScheme, contrast, size, title, padding, ...rest } = Astro.props;
const style = size ? `--_width:${size[0]}px;--_height:${size[1]}px` : undefined;
const canvasStyle = size ? `aspect-ratio:${size[0]} / ${size[1]}` : undefined;
---
<style>
.preview {
display: flex;
flex-direction: column;
border: 1px solid oklch(var(--color-border) / var(--alpha-border-medium));
border-radius: 4px;
overflow: hidden;
}
.preview.fullscreen {
position: fixed;
inset: 0;
margin: 0;
border: none;
background-color: oklch(var(--color-bg));
border-radius: 0;
z-index: 999;
}
.canvas {
--_grid-color-value: oklch(
var(--color-border)
/
var(--alpha-border-subtle)
);
flex: 1;
display: block;
box-sizing: border-box;
}
.preview.fullscreen > .canvas {
min-height: 0;
height: auto !important;
flex-basis: 100% !important;
background-image:
linear-gradient(
var(--_grid-color-value) 0.5px,
transparent 0.5px,
transparent calc(100% - 0.5px),
var(--_grid-color-value) calc(100% - 0.5px)
),
linear-gradient(
90deg,
var(--_grid-color-value) 0.5px,
transparent 0.5px,
transparent calc(100% - 0.5px),
var(--_grid-color-value) calc(100% - 0.5px)
);
background-size: var(--space-px-12) var(--space-px-12);
}
.preview.fullscreen > .canvas:has(.override.container) {
position: relative;
aspect-ratio: auto !important;
overflow: auto;
}
.override {
display: block;
font-size: 1rem;
line-height: 1.5;
font-family: var(--font-sans);
font-weight: var(--font-regular);
box-sizing: border-box;
background-color: oklch(var(--color-bg));
color: oklch(var(--color-fg) / var(--alpha-fg-medium));
}
.override.padded {
padding: var(--space-px-7);
}
.override.container {
container-type: size;
width: var(--_width);
height: var(--_height);
transform-origin: top left;
}
.preview.fullscreen .override:not(.container) {
width: 100%;
height: 100%;
}
.preview.fullscreen .override.container {
position: absolute;
top: 50%;
left: 50%;
box-shadow: var(--space-px-1) var(--space-px-1) var(--space-px-6)
oklch(0% 0% 0deg / 15%);
transform: translate(-50%, -50%) !important;
}
.header {
display: block;
padding: var(--space-px-5);
font-size: var(--font-sm);
font-weight: bold;
border-bottom: 1px solid
oklch(var(--color-border) / var(--alpha-border-subtle));
background-color: oklch(var(--color-fg) / 2%);
color: oklch(var(--color-fg) / var(--alpha-fg-medium));
}
.footer {
display: flex;
justify-content: space-between;
align-items: center;
gap: var(--space-px-3);
border-top: 1px solid
oklch(var(--color-border) / var(--alpha-border-subtle));
font-family: var(--font-mono);
font-size: var(--font-xs);
padding: var(--space-px-4);
background-color: oklch(var(--color-fg) / 2%);
color: oklch(var(--color-fg) / var(--alpha-fg-subtle));
}
.fullscreen-button {
flex-shrink: 0;
display: flex;
justify-content: center;
align-items: center;
width: var(--space-px-9);
height: var(--space-px-9);
cursor: pointer;
}
.fullscreen-button:focus-visible {
color: oklch(var(--color-focus));
}
.fullscreen-button-icon {
rotate: 45deg;
}
.fullscreen-button-label {
opacity: 0;
position: absolute;
top: 0;
left: 0;
width: 1px;
height: 1px;
overflow: hidden;
white-space: nowrap;
}
.footer > code {
flex-shrink: 1;
}
</style>
<script>
let fullscreenTarget = null;
document.addEventListener("keydown", (event) => {
if (
event.isComposing ||
event.key !== "Escape" ||
!fullscreenTarget ||
!(fullscreenTarget instanceof HTMLButtonElement)
) {
return;
}
event.preventDefault();
event.stopPropagation();
fullscreenTarget.click();
});
function toggleFullscreen({ container, trigger, icon }) {
if (container.classList.contains("fullscreen")) {
container.classList.remove("fullscreen");
icon.textContent = "←→";
trigger.title = "全画面表示";
fullscreenTarget = null;
} else {
container.classList.add("fullscreen");
icon.textContent = "→←";
trigger.title = "全画面表示をやめる";
fullscreenTarget = trigger;
}
}
for (const button of document.getElementsByClassName("fullscreen-button")) {
button.addEventListener("click", (event) => {
event.preventDefault();
event.stopPropagation();
const container = button.parentElement?.parentElement;
if (!container || !container.classList.contains("preview")) {
return;
}
const icon = button.querySelector(".fullscreen-button-icon");
if (typeof document.startViewTransition === "function") {
container.style.viewTransitionName = "preview";
const transition = document.startViewTransition(() => {
toggleFullscreen({
container,
trigger: button,
icon,
});
});
transition.finished.then(() => {
container.style.viewTransitionName = "";
});
} else {
toggleFullscreen({
container,
trigger: button,
icon,
});
}
});
}
function resizeOverride(el) {
if (!el) {
return;
}
const width = parseInt(el.getAttribute("data-width"), 10);
const height = parseInt(el.getAttribute("data-height"), 10);
const rect = el.getBoundingClientRect();
const parent = el.parentElement.getBoundingClientRect();
const scale = parent.width / width;
el.style.transform = `scale(${scale})`;
const canvasHeight = height * scale;
el.parentElement.style.flexBasis = `${canvasHeight}px`;
el.parentElement.style.height = `${canvasHeight}px`;
}
const observer = new ResizeObserver((entries) => {
for (const entry of entries) {
resizeOverride(
entry.target.querySelector("docs-theme-override.override.container"),
);
}
});
for (const container of document.querySelectorAll(
"docs-theme-override.override.container",
)) {
resizeOverride(container);
observer.observe(container.parentElement.parentElement);
}
</script>
<div {...rest} class="preview">
{
title && (
<div class="header">
{title}
</div>
)
}
<div class="canvas" style={canvasStyle}>
<ThemeOverride
class:list={["override", { padded: padding !== "no", container: !!size }]}
color-scheme={colorScheme}
contrast={contrast}
style={style}
data-width={size?.[0]}
data-height={size?.[1]}
>
<slot />
</ThemeOverride>
</div>
<div class="footer">
<button class="fullscreen-button" title="全画面表示">
<span class="fullscreen-button-icon">←→</span>
</button>
{
(colorScheme || contrast) && (
<code>
{colorScheme && <span>(prefers-color-scheme: {colorScheme})</span>}
{colorScheme && contrast ? <span> and </span> : null}
{contrast && <span>(prefers-contrast: {contrast})</span>}
</code>
)
}
</div>
</div>