-
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
---
// 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, title, ...rest } = Astro.props;
---
<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;
}
.override {
flex: 1;
display: block;
padding: var(--space-px-7);
font-size: 1rem;
line-height: 1.5;
font-family: var(--font-sans);
font-weight: var(--font-regular);
background-color: oklch(var(--color-bg));
color: oklch(var(--color-fg) / var(--alpha-fg-medium));
}
.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,
});
}
});
}
</script>
<div {...rest} class="preview">
{
title && (
<div class="header">
{title}
</div>
)
}
<ThemeOverride
class="override"
color-scheme={colorScheme}
contrast={contrast}
>
<slot />
</ThemeOverride>
<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>