-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchBox.astro
More file actions
378 lines (326 loc) · 10 KB
/
SearchBox.astro
File metadata and controls
378 lines (326 loc) · 10 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
---
/**
* Pagefind検索ボックスコンポーネント
* キーボードショートカット: / または Ctrl+K
* 日英両言語対応
* 注意: 開発環境では動作しません。npm run preview で確認してください。
*/
interface Props {
lang?: 'en' | 'ja';
}
const { lang = 'en' } = Astro.props;
const isDev = import.meta.env.DEV;
---
{isDev ? (
<div class="search-container search-dev-notice">
<div class="dev-notice">
{lang === 'ja' ? '検索: 本番ビルドでのみ利用可' : 'Search: Production only'}
</div>
</div>
) : (
<div class="search-container">
<div id="search-wrapper">
<div id="search"></div>
</div>
</div>
)}
<!-- Pagefind UI CSS (本番環境のみ) -->
{!isDev && <link href="/_pagefind/pagefind-ui.css" rel="stylesheet">}
<style>
.search-container {
position: relative;
display: flex;
align-items: center;
width: 100%;
max-width: 280px;
}
#search-wrapper {
width: 100%;
}
/* 開発環境の通知スタイル */
.search-dev-notice {
display: flex;
align-items: center;
justify-content: center;
max-width: 280px;
}
.dev-notice {
padding: 0.4rem 0.8rem;
font-size: 0.75rem;
color: #6b7280;
background-color: #f3f4f6;
border-radius: 6px;
border: 1px solid #e5e7eb;
white-space: nowrap;
}
@media (prefers-color-scheme: dark) {
.dev-notice {
color: #9ca3af;
background-color: #374151;
border-color: #4b5563;
}
}
:root[data-theme="dark"] .dev-notice {
color: #9ca3af;
background-color: #374151;
border-color: #4b5563;
}
/* Pagefind UIのカスタマイズ - ライトモード */
:global(.pagefind-ui) {
--pagefind-ui-scale: 0.9;
--pagefind-ui-primary: #2563eb;
--pagefind-ui-text: #374151;
--pagefind-ui-background: #ffffff;
--pagefind-ui-border: #e5e7eb;
--pagefind-ui-tag: #f3f4f6;
--pagefind-ui-border-width: 1px;
--pagefind-ui-border-radius: 8px;
--pagefind-ui-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
/* ライトモード明示設定 */
:root[data-theme="light"] :global(.pagefind-ui) {
--pagefind-ui-background: #ffffff;
--pagefind-ui-text: #374151;
--pagefind-ui-border: #e5e7eb;
}
/* ダークモード対応 */
@media (prefers-color-scheme: dark) {
:global(.pagefind-ui) {
--pagefind-ui-primary: #60a5fa;
--pagefind-ui-text: #e5e7eb;
--pagefind-ui-background: #1f2937;
--pagefind-ui-border: #374151;
--pagefind-ui-tag: #374151;
}
}
:root[data-theme="dark"] :global(.pagefind-ui) {
--pagefind-ui-primary: #60a5fa;
--pagefind-ui-text: #e5e7eb;
--pagefind-ui-background: #1f2937;
--pagefind-ui-border: #374151;
--pagefind-ui-tag: #374151;
}
/* 検索ボックスのスタイル調整 */
:global(.pagefind-ui__search-input) {
width: 100%;
padding: 0.5rem 1rem;
font-size: 0.95rem;
border: 1px solid var(--pagefind-ui-border);
border-radius: var(--pagefind-ui-border-radius);
background: var(--pagefind-ui-background);
color: var(--pagefind-ui-text);
transition: border-color 0.2s;
}
:global(.pagefind-ui__search-input:focus) {
outline: none;
border-color: var(--pagefind-ui-primary);
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}
/* 検索結果のスタイル */
:global(.pagefind-ui__results) {
position: absolute;
top: calc(100% + 0.5rem);
left: 0;
right: 0;
max-height: 400px;
overflow-y: auto;
background: var(--pagefind-ui-background);
border: 1px solid var(--pagefind-ui-border);
border-radius: var(--pagefind-ui-border-radius);
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
z-index: 9999;
}
:global(.pagefind-ui__result) {
padding: 0.75rem 1rem;
border-bottom: 1px solid var(--pagefind-ui-border);
cursor: pointer;
transition: background-color 0.2s;
}
:global(.pagefind-ui__result:hover) {
background-color: var(--pagefind-ui-tag);
}
:global(.pagefind-ui__result:last-child) {
border-bottom: none;
}
:global(.pagefind-ui__result-title) {
font-weight: 600;
color: var(--pagefind-ui-primary);
margin-bottom: 0.25rem;
}
:global(.pagefind-ui__result-excerpt) {
font-size: 0.875rem;
color: var(--pagefind-ui-text);
line-height: 1.5;
}
/* 検索結果のクリック可能性を確保 */
:global(.pagefind-ui__results) {
position: relative;
z-index: 1;
}
:global(.pagefind-ui__result) {
position: relative;
z-index: 1;
}
:global(.pagefind-ui__result) * {
pointer-events: none !important;
}
:global(.pagefind-ui__result-link),
:global(.pagefind-ui__result-title),
:global(.pagefind-ui__result a) {
pointer-events: auto !important;
position: relative;
z-index: 2;
}
/* ホバー時のすべての疑似要素とスタイルを無効化 */
:global(.pagefind-ui) *::before,
:global(.pagefind-ui) *::after {
display: none !important;
content: none !important;
}
:global(.pagefind-ui__result:hover),
:global(.pagefind-ui__result:hover) * {
background: transparent !important;
}
:global(.pagefind-ui__result-excerpt) {
position: relative !important;
}
:global(.pagefind-ui__result-excerpt)::before,
:global(.pagefind-ui__result-excerpt)::after {
display: none !important;
}
/* ハイライト(mark要素)のスタイル */
:global(.pagefind-ui__result mark) {
background-color: rgba(255, 237, 0, 0.4) !important;
color: inherit !important;
padding: 0 !important;
font-weight: 600 !important;
pointer-events: none !important;
position: relative;
z-index: -1;
}
:global(.pagefind-ui__result mark)::before,
:global(.pagefind-ui__result mark)::after {
display: none !important;
}
:root[data-theme="dark"] :global(.pagefind-ui__result mark) {
background-color: rgba(250, 204, 21, 0.3) !important;
}
@media (prefers-color-scheme: dark) {
:global(.pagefind-ui__result mark) {
background-color: rgba(250, 204, 21, 0.3) !important;
}
}
/* モバイル対応 */
@media (max-width: 768px) {
.search-container {
max-width: 100%;
}
:global(.pagefind-ui__results) {
left: -1rem;
right: -1rem;
max-height: 300px;
}
}
/* クリアボタンのスタイル調整 */
:global(.pagefind-ui__search-clear) {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0.25rem;
min-width: 24px;
height: 24px;
}
</style>
{!isDev && (
<script>
// Pagefind UI の初期化
const initPagefind = async () => {
try {
// Pagefind UIをグローバルスコープに読み込む
await import('/_pagefind/pagefind-ui.js');
// グローバルオブジェクトからPagefindUIを取得
// @ts-ignore
const PagefindUI = window.PagefindUI;
if (!PagefindUI) {
throw new Error('PagefindUI not loaded on window object');
}
const searchElement = document.querySelector('#search');
if (searchElement) {
const lang = document.documentElement.lang || 'en';
new PagefindUI({
element: searchElement,
bundlePath: '/_pagefind/',
showImages: false,
showSubResults: true,
excerptLength: 15,
translations: lang === 'ja' ? {
placeholder: 'ドキュメントを検索...',
clear_search: '✕',
load_more: 'さらに読み込む',
search_label: '検索',
filters_label: 'フィルター',
zero_results: '結果が見つかりませんでした。',
many_results: '[SEARCH_TERM] の検索結果: [COUNT] 件',
one_result: '[SEARCH_TERM] の検索結果: 1 件',
alt_search: '[SEARCH_TERM] の検索結果はありません。代わりに [DIFFERENT_TERM] を検索中',
search_suggestion: '[SEARCH_TERM] の検索結果はありません。以下のいずれかを試してください:',
searching: '検索中...',
} : {
clear_search: '✕',
},
});
}
} catch (error) {
console.warn('Pagefind not available:', error);
}
};
// 編集可能な要素かどうかを判定
const isEditableTarget = (target: EventTarget | null): boolean => {
const el = target as HTMLElement | null;
if (!el) return false;
// contenteditable属性やisContentEditableがtrueの場合
if (el.isContentEditable) return true;
const tagName = el.tagName;
if (['INPUT', 'TEXTAREA', 'SELECT'].includes(tagName)) {
return true;
}
const role = el.getAttribute('role');
if (role === 'textbox' || role === 'searchbox') {
return true;
}
return false;
};
// キーボードショートカット: / または Ctrl+K
// 重複登録を防ぐため、初期化済みフラグをチェック
const initKeyboardShortcuts = () => {
if ((window as any).__pagefindKeyboardInitialized) {
return;
}
(window as any).__pagefindKeyboardInitialized = true;
document.addEventListener('keydown', (e) => {
const searchInput = document.querySelector('.pagefind-ui__search-input') as HTMLInputElement;
// / キーで検索ボックスにフォーカス(入力フィールド内でなければ)
if (e.key === '/' && !isEditableTarget(e.target)) {
e.preventDefault();
searchInput?.focus();
}
// Ctrl+K または Cmd+K で検索ボックスにフォーカス
if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
e.preventDefault();
searchInput?.focus();
}
// Escape キーで検索ボックスからフォーカスを外す
if (e.key === 'Escape' && document.activeElement === searchInput) {
searchInput?.blur();
}
});
};
initKeyboardShortcuts();
// ページロード時に初期化
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initPagefind);
} else {
initPagefind();
}
</script>
)}