feat(FontListItem): refactor component to enhance UX with animations and move away from checkboxes to avoid scroll problems

This commit is contained in:
Ilia Mashkov
2026-01-22 15:41:55 +03:00
parent be13a5c8a0
commit 59b0d9c620

View File

@@ -4,8 +4,8 @@
--> -->
<script lang="ts"> <script lang="ts">
import { Badge } from '$shared/shadcn/ui/badge'; import { Badge } from '$shared/shadcn/ui/badge';
import { Checkbox } from '$shared/shadcn/ui/checkbox'; import { cn } from '$shared/shadcn/utils/shadcn-utils';
import { Label } from '$shared/shadcn/ui/label'; import { Spring } from 'svelte/motion';
import { import {
type UnifiedFont, type UnifiedFont,
selectedFontsStore, selectedFontsStore,
@@ -17,68 +17,118 @@ interface Props {
* Object with information about font * Object with information about font
*/ */
font: UnifiedFont; font: UnifiedFont;
/**
* Is element fully visible
*/
isVisible: boolean;
/**
* From 0 to 1
*/
proximity: number;
} }
const { font }: Props = $props(); const { font, isVisible, proximity }: Props = $props();
const handleChange = (checked: boolean) => { let selected = $state(selectedFontsStore.has(font.id));
if (checked) { let timeoutId = $state<NodeJS.Timeout | null>(null);
selectedFontsStore.addOne(font);
} else {
selectedFontsStore.removeOne(font.id);
}
};
const selected = $derived(selectedFontsStore.has(font.id)); // Create a spring for smooth scale animation
const scale = new Spring(1, {
stiffness: 0.3,
damping: 0.7,
});
// Springs react to the virtualizer's computed state
const bloom = new Spring(0, {
stiffness: 0.15,
damping: 0.6,
});
// Sync spring to proximity for a "Lens" effect
$effect(() => {
bloom.target = isVisible ? 1 : 0;
});
$effect(() => {
selected = selectedFontsStore.has(font.id);
});
$effect(() => {
return () => {
if (timeoutId) {
clearTimeout(timeoutId);
}
};
});
function handleClick() {
animateSelection();
selected ? selectedFontsStore.removeOne(font.id) : selectedFontsStore.addOne(font);
}
function animateSelection() {
scale.target = 0.98;
timeoutId = setTimeout(() => {
scale.target = 1;
}, 150);
}
</script> </script>
<div class="pb-1"> <div
<Label class={cn('pb-1 will-change-transform')}
for={font.id} style:opacity={bloom.current}
class=" style:transform="
w-full hover:bg-accent/50 flex items-start gap-3 rounded-lg border border-transparent p-3 scale({0.92 + (bloom.current * 0.08)})
active:scale-[0.98] active:transition-transform active:duration-75 translateY({(1 - bloom.current) * 10}px)
has-aria-checked:border-blue-600 "
has-aria-checked:bg-blue-50 >
dark:has-aria-checked:border-blue-900 <div style:transform={`scale(${scale.current})`}>
dark:has-aria-checked:bg-blue-950 <div
" class={cn(
> 'w-full hover:bg-accent/50 flex items-start gap-3 rounded-lg border border-transparent p-3',
<div class="w-full"> 'active:transition-transform active:duration-150',
<div class="flex flex-row gap-1 w-full items-center justify-between"> 'border dark:border-slate-800',
<div class="flex flex-col gap-1 transition-all duration-150 ease-out"> 'bg-white/10 border-white/20',
<div class="flex flex-row gap-1"> isVisible && 'bg-white/40 border-white/40',
<Badge variant="outline" class="text-[0.5rem]"> selected && 'ring-2 ring-indigo-600 ring-inset bg-indigo-50/50 hover:bg-indigo-50',
{font.provider} )}
</Badge> role="button"
<Badge variant="outline" class="text-[0.5rem]"> tabindex="0"
{font.category} onmousedown={(e => {
</Badge> // Prevent browser focus-jump
if (e.currentTarget === document.activeElement) return;
e.preventDefault();
handleClick();
})}
onkeydown={(e => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
handleClick();
}
})}
>
<div class="w-full">
<div class="flex flex-row gap-1 w-full items-center justify-between">
<div class="flex flex-col gap-1 transition-all duration-150 ease-out">
<div class="flex flex-row gap-1">
<Badge variant="outline" class="text-[0.5rem]">
{font.provider}
</Badge>
<Badge variant="outline" class="text-[0.5rem]">
{font.category}
</Badge>
</div>
<FontApplicator
id={font.id}
className="text-2xl"
name={font.name}
>
{font.name}
</FontApplicator>
</div> </div>
<FontApplicator
id={font.id}
className="text-2xl"
name={font.name}
>
{font.name}
</FontApplicator>
</div> </div>
<Checkbox
id={font.id}
checked={selected}
onCheckedChange={handleChange}
class="
transition-all duration-150 ease-out
data-[state=checked]:scale-100
data-[state=checked]:border-blue-600
data-[state=checked]:bg-blue-600
data-[state=checked]:text-white
dark:data-[state=checked]:border-blue-700
dark:data-[state=checked]:bg-blue-700
"
/>
</div> </div>
</div> </div>
</Label> </div>
</div> </div>