2024-08-12 11:00:25 +02:00
|
|
|
<script lang="ts">
|
2025-02-19 14:28:45 +01:00
|
|
|
import FileInput from '$lib/components/form/file-input.svelte';
|
2024-08-12 11:00:25 +02:00
|
|
|
import { Label } from '$lib/components/ui/label';
|
|
|
|
|
import { cn } from '$lib/utils/style';
|
|
|
|
|
import type { HTMLAttributes } from 'svelte/elements';
|
|
|
|
|
|
|
|
|
|
let {
|
|
|
|
|
id,
|
|
|
|
|
imageClass,
|
|
|
|
|
label,
|
2024-10-02 08:43:44 +02:00
|
|
|
image = $bindable(),
|
2024-08-12 11:00:25 +02:00
|
|
|
imageURL,
|
|
|
|
|
accept = 'image/png, image/jpeg, image/svg+xml',
|
2024-10-03 11:27:31 +02:00
|
|
|
forceColorScheme,
|
2024-08-12 11:00:25 +02:00
|
|
|
...restProps
|
|
|
|
|
}: HTMLAttributes<HTMLDivElement> & {
|
|
|
|
|
id: string;
|
|
|
|
|
imageClass: string;
|
|
|
|
|
label: string;
|
|
|
|
|
image: File | null;
|
|
|
|
|
imageURL: string;
|
2024-10-03 11:27:31 +02:00
|
|
|
forceColorScheme?: 'light' | 'dark';
|
2024-08-12 11:00:25 +02:00
|
|
|
accept?: string;
|
|
|
|
|
} = $props();
|
|
|
|
|
|
|
|
|
|
let imageDataURL = $state(imageURL);
|
|
|
|
|
|
|
|
|
|
function onImageChange(e: Event) {
|
|
|
|
|
const file = (e.target as HTMLInputElement).files?.[0] || null;
|
|
|
|
|
if (!file) return;
|
|
|
|
|
|
|
|
|
|
image = file;
|
|
|
|
|
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onload = (event) => {
|
|
|
|
|
imageDataURL = event.target?.result as string;
|
|
|
|
|
};
|
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
2024-10-03 11:27:31 +02:00
|
|
|
<div class="flex flex-col items-start md:flex-row md:items-center" {...restProps}>
|
|
|
|
|
<Label class="w-52" for={id}>{label}</Label>
|
2024-08-12 11:00:25 +02:00
|
|
|
<FileInput {id} variant="secondary" {accept} onchange={onImageChange}>
|
2024-10-03 11:27:31 +02:00
|
|
|
<div
|
|
|
|
|
class="{forceColorScheme === 'light'
|
|
|
|
|
? 'bg-[#F1F1F5]'
|
|
|
|
|
: forceColorScheme === 'dark'
|
|
|
|
|
? 'bg-[#27272A]'
|
|
|
|
|
: 'bg-muted'} group relative flex items-center rounded"
|
|
|
|
|
>
|
2024-08-12 11:00:25 +02:00
|
|
|
<img
|
|
|
|
|
class={cn(
|
|
|
|
|
'h-full w-full rounded object-cover p-3 transition-opacity duration-200 group-hover:opacity-10',
|
|
|
|
|
imageClass
|
|
|
|
|
)}
|
|
|
|
|
src={imageDataURL}
|
|
|
|
|
alt={label}
|
|
|
|
|
/>
|
|
|
|
|
<span
|
|
|
|
|
class="absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 transform font-medium opacity-0 transition-opacity group-hover:opacity-100"
|
|
|
|
|
>
|
|
|
|
|
Update
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</FileInput>
|
|
|
|
|
</div>
|