-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticleFormHeader.svelte
More file actions
262 lines (247 loc) · 8.11 KB
/
Copy pathArticleFormHeader.svelte
File metadata and controls
262 lines (247 loc) · 8.11 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
<script lang="ts">
import {
ArrowLeft,
Check,
ChevronDown,
ChevronsUpDown,
ExternalLink,
Eye,
EyeOff,
Loader2,
MoreHorizontal,
Search,
Trash2,
UserCircle2,
} from "lucide-svelte";
import { goto } from "$app/navigation";
import { Combobox, DropdownMenu } from "bits-ui";
type Author = {
id: string;
slug: string;
name: string;
imageUrl: string | null;
};
let {
published = $bindable(false),
authorId = $bindable<string | null>(null),
authors = [],
isSubmitting = false,
saveSuccess = $bindable(false),
submitLabel = "Save",
articleId = null,
onPreview = null,
onDelete = null,
}: {
published?: boolean;
authorId?: string | null;
authors?: Author[];
isSubmitting?: boolean;
saveSuccess?: boolean;
submitLabel?: string;
articleId?: string | null;
onPreview?: (() => void) | null;
onDelete?: (() => Promise<void>) | null;
} = $props();
let successTimeout: ReturnType<typeof setTimeout> | null = null;
let authorMenuOpen = $state(false);
let moreMenuOpen = $state(false);
let authorSearch = $state("");
$effect(() => {
if (saveSuccess) {
if (successTimeout) {
clearTimeout(successTimeout);
}
successTimeout = setTimeout(() => {
saveSuccess = false;
successTimeout = null;
}, 3500);
}
return () => {
if (successTimeout) {
clearTimeout(successTimeout);
}
};
});
function openPreviewPage() {
if (articleId && onPreview) {
onPreview();
}
}
const selectedAuthor = $derived(authors.find((a) => a.id === authorId));
// Combobox uses string values; we map "" <-> null for "No author".
const authorComboValue = $derived(authorId ?? "");
function handleAuthorChange(value: string) {
authorId = value === "" ? null : value;
}
const filteredAuthors = $derived(
authorSearch.trim() === ""
? authors
: authors.filter((a) => {
const q = authorSearch.toLowerCase();
return a.name.toLowerCase().includes(q) || a.slug.toLowerCase().includes(q);
}),
);
</script>
<header
class="sticky top-0 z-20 flex flex-col gap-3 border-b border-zinc-200 bg-white px-3 py-2 sm:flex-row sm:flex-wrap sm:items-center sm:justify-between sm:gap-4 sm:px-4 sm:py-3"
>
<div class="flex flex-wrap items-center gap-2 sm:gap-4">
<button
type="button"
onclick={() => goto("/admin/articles")}
class="flex items-center gap-1.5 text-sm text-zinc-500 transition-colors hover:text-zinc-900"
aria-label="Back to Articles"
>
<ArrowLeft class="h-4 w-4" />
<span class="hidden sm:inline">Articles</span>
</button>
<!-- Visibility Toggle (looks like a switch button: bordered, with chevron + clear hover affordance) -->
<button
type="button"
onclick={() => (published = !published)}
aria-pressed={published}
title={published ? "Click to unpublish" : "Click to publish"}
class="inline-flex items-center gap-1.5 rounded-full border px-2.5 py-1 text-xs font-medium shadow-sm transition-all hover:shadow-md active:scale-[0.98] sm:px-3 sm:py-1.5 {published
? 'border-emerald-300 bg-emerald-50 text-emerald-700 hover:border-emerald-400 hover:bg-emerald-100'
: 'border-zinc-300 bg-white text-zinc-700 hover:border-zinc-400 hover:bg-zinc-50'}"
>
{#if published}
<Eye class="h-3.5 w-3.5" />
<span>Public</span>
{:else}
<EyeOff class="h-3.5 w-3.5" />
<span>Draft</span>
{/if}
<ChevronsUpDown class="h-3 w-3 opacity-60" />
</button>
<!-- Author Combobox (searchable) -->
<Combobox.Root
type="single"
value={authorComboValue}
onValueChange={handleAuthorChange}
bind:open={authorMenuOpen}
onOpenChangeComplete={(o) => {
if (!o) authorSearch = "";
}}
>
<Combobox.Trigger
class="inline-flex items-center gap-1.5 rounded-lg border border-zinc-200 bg-white px-2 py-1.5 text-sm text-zinc-700 shadow-sm transition-colors hover:border-primary/30 hover:bg-primary/5 sm:px-3"
aria-label="Select author"
>
<UserCircle2 class="h-3.5 w-3.5 text-zinc-400" />
{#if selectedAuthor}
<span class="max-w-24 truncate sm:max-w-32" title={selectedAuthor.name}>{selectedAuthor.name}</span>
{:else}
<span class="text-zinc-400">No author</span>
{/if}
<ChevronDown class="h-3 w-3 {authorMenuOpen ? 'rotate-180' : ''} transition-transform" />
</Combobox.Trigger>
<Combobox.Portal>
<Combobox.Content
class="z-50 mt-1 w-64 overflow-hidden rounded-lg border border-zinc-200 bg-white shadow-lg"
sideOffset={4}
>
<div class="relative border-b border-zinc-100">
<Search class="pointer-events-none absolute top-1/2 left-2.5 h-3.5 w-3.5 -translate-y-1/2 text-zinc-400" />
<Combobox.Input
oninput={(e) => (authorSearch = e.currentTarget.value)}
class="w-full bg-transparent py-2 pr-3 pl-8 text-sm text-zinc-900 placeholder:text-zinc-400 focus:outline-none"
placeholder="Search authors..."
aria-label="Search authors"
/>
</div>
<Combobox.Viewport class="max-h-60 overflow-auto p-1">
{#if authorSearch.trim() === ""}
<Combobox.Item
value=""
label="No author"
class="data-highlighted:bg-zinc-100 flex cursor-pointer items-center rounded-md px-3 py-2 text-sm text-zinc-500 outline-none {authorId ===
null
? 'bg-primary/5 font-medium'
: ''}"
>
No author
</Combobox.Item>
{/if}
{#each filteredAuthors as author (author.id)}
<Combobox.Item
value={author.id}
label={author.name}
class="data-highlighted:bg-zinc-100 flex cursor-pointer items-center rounded-md px-3 py-2 text-sm text-zinc-900 outline-none {author.id ===
authorId
? 'bg-primary/5 font-medium'
: ''}"
>
<div class="flex flex-col">
<span>{author.name}</span>
<span class="text-xs text-zinc-500">@{author.slug}</span>
</div>
</Combobox.Item>
{:else}
<div class="px-3 py-2 text-sm text-zinc-400">No matches</div>
{/each}
</Combobox.Viewport>
</Combobox.Content>
</Combobox.Portal>
</Combobox.Root>
</div>
<div class="flex w-full items-center gap-1 sm:w-auto sm:gap-2">
<!-- Preview Button -->
{#if articleId}
<button
type="button"
onclick={openPreviewPage}
class="flex items-center gap-1.5 rounded-lg px-2 py-1.5 text-sm text-zinc-600 transition-colors hover:bg-primary/5 hover:text-primary sm:px-3 sm:py-2"
aria-label="Preview article"
>
<ExternalLink class="h-4 w-4" />
<span class="hidden sm:inline">Preview</span>
</button>
{/if}
<!-- More Menu (Delete) -->
{#if onDelete}
<DropdownMenu.Root bind:open={moreMenuOpen}>
<DropdownMenu.Trigger
class="flex items-center rounded-lg p-1.5 text-zinc-600 transition-colors hover:bg-zinc-100 sm:p-2"
aria-label="More options"
>
<MoreHorizontal class="h-4 w-4" />
</DropdownMenu.Trigger>
<DropdownMenu.Content
class="z-50 mt-1 min-w-36 rounded-lg border border-zinc-200 bg-white p-1 shadow-lg"
sideOffset={4}
align="end"
>
<DropdownMenu.Item
class="flex cursor-pointer items-center gap-2 rounded-md px-3 py-2 text-sm text-red-600 transition-colors hover:bg-red-50"
onSelect={() => onDelete?.()}
>
<Trash2 class="h-4 w-4" />
Delete article
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
{/if}
<!-- Save Button -->
<button
type="submit"
disabled={isSubmitting || saveSuccess}
class="inline-flex w-full items-center justify-center gap-1.5 rounded-lg px-3 py-1.5 text-sm font-semibold text-white transition-all active:scale-[0.98] disabled:cursor-not-allowed disabled:opacity-50 sm:w-auto sm:gap-2 sm:px-4 sm:py-2 {published
? 'bg-emerald-600 hover:bg-emerald-700'
: 'bg-zinc-900 hover:bg-zinc-800'}"
>
{#if isSubmitting}
<Loader2 class="h-4 w-4 animate-spin" />
<span class="hidden sm:inline">Saving...</span>
<span class="sm:hidden">...</span>
{:else if saveSuccess}
<Check class="h-4 w-4" />
<span class="hidden sm:inline">Saved</span>
<span class="sm:hidden">✓</span>
{:else}
<span class="hidden sm:inline">{published ? "Publish" : submitLabel}</span>
<span class="sm:hidden">Save</span>
{/if}
</button>
</div>
</header>