Add badge highlighting with animated border transitions, use className in BadgeProps

This commit is contained in:
2024-12-22 15:28:49 -06:00
parent 069471145a
commit e80c8b1c4f
3 changed files with 30 additions and 7 deletions

View File

@@ -18,15 +18,15 @@ const Badge = ({
<span <span
id="badge-dismiss-dark" id="badge-dismiss-dark"
class={cn( class={cn(
className, "inline-flex align-middle items-center px-2 py-1 me-2 text-sm leading-none font-medium text-zinc-800 bg-zinc-100 rounded dark:bg-zinc-700 dark:text-zinc-300",
"inline-flex items-center px-2 py-1 me-2 text-sm font-medium text-zinc-800 bg-zinc-100 rounded dark:bg-zinc-700 dark:text-zinc-300" className
)} )}
> >
{children} {children}
<button <button
type="button" type="button"
onClick={onClick} onClick={onClick}
class="inline-flex items-center p-1 ms-1 text-sm text-zinc-400 bg-transparent rounded-sm hover:bg-zinc-200 hover:text-zinc-900 dark:hover:bg-zinc-600 dark:hover:text-zinc-300" class="inline-flex items-center ms-1 text-sm text-zinc-400 bg-transparent rounded-sm hover:bg-zinc-200 hover:text-zinc-900 dark:hover:bg-zinc-600 dark:hover:text-zinc-300"
data-dismiss-target="#badge-dismiss-dark" data-dismiss-target="#badge-dismiss-dark"
aria-label="Remove" aria-label="Remove"
> >

View File

@@ -6,7 +6,7 @@ type EmboldenedProps = {
copyable?: boolean; copyable?: boolean;
}; };
const Emboldened = ({ children, copyable }: EmboldenedProps) => { const Emboldened = ({ children, copyable, className }: EmboldenedProps) => {
function copyToClipboard() { function copyToClipboard() {
// Copy to clipboard // Copy to clipboard
navigator.clipboard.writeText(children.toString()); navigator.clipboard.writeText(children.toString());
@@ -16,6 +16,7 @@ const Emboldened = ({ children, copyable }: EmboldenedProps) => {
<span <span
onClick={copyable ? copyToClipboard : undefined} onClick={copyable ? copyToClipboard : undefined}
className={cn( className={cn(
className,
"bg-zinc-900/40 rounded border border-zinc-700 py-0.5 px-1 font-mono text-teal-400", "bg-zinc-900/40 rounded border border-zinc-700 py-0.5 px-1 font-mono text-teal-400",
{ {
"cursor-pointer": copyable, "cursor-pointer": copyable,

View File

@@ -1,7 +1,7 @@
import Badge from "@/components/Badge"; import Badge from "@/components/Badge";
import Emboldened from "@/components/Emboldened"; import Emboldened from "@/components/Emboldened";
import { cn, plural, type ClassValue } from "@/util"; import { cn, plural, type ClassValue } from "@/util";
import { useState } from "preact/hooks"; import { useRef, useState } from "preact/hooks";
type StatefulDemoProps = { type StatefulDemoProps = {
class?: ClassValue; class?: ClassValue;
@@ -24,6 +24,22 @@ const StatefulDemo = ({ class: className }: StatefulDemoProps) => {
downloads: Array.from({ length: 7 }).map(() => "0×" + randomBits(16)), downloads: Array.from({ length: 7 }).map(() => "0×" + randomBits(16)),
}); });
const [highlightedIndex, setHighlightedIndex] = useState<number | null>(null);
const highlightedTimeoutRef = useRef<NodeJS.Timeout | null>(null);
function highlight(index: number) {
setHighlightedIndex(index);
if (highlightedTimeoutRef.current != null) {
clearTimeout(highlightedTimeoutRef.current);
}
highlightedTimeoutRef.current = setTimeout(() => {
highlightedTimeoutRef.current = null;
setHighlightedIndex(null);
}, 1000 * 10);
}
return ( return (
<div class={cn(className, "px-5 leading-6")}> <div class={cn(className, "px-5 leading-6")}>
<p class="mt-3 mb-3"> <p class="mt-3 mb-3">
@@ -43,10 +59,16 @@ const StatefulDemo = ({ class: className }: StatefulDemoProps) => {
) : null} ) : null}
</p> </p>
<div class="flex flex-wrap justify-center gap-y-2.5"> <div class="flex flex-wrap justify-center gap-y-2.5">
{session?.downloads.map((download) => ( {session?.downloads.map((download, i) => (
<Badge <Badge
className="grow-0" className={cn(
"transition-colors border hover:border-zinc-500 duration-100 ease-in border-transparent",
{
"!border-zinc-300 dark:bg-zinc-600": i === highlightedIndex,
}
)}
onClick={function onClick() { onClick={function onClick() {
highlight(i);
const audio = new Audio("/notify.wav"); const audio = new Audio("/notify.wav");
audio.volume = 0.3; audio.volume = 0.3;
audio.play(); audio.play();