memorium/components/Rating.tsx

56 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-08-08 21:50:23 +02:00
import { IconStar, IconStarFilled } from "@components/icons.tsx";
import { useSignal } from "@preact/signals";
import { useState } from "preact/hooks";
export const SmallRating = (
{ max = 5, rating }: { max?: number; rating: number },
) => {
return (
<div
class="flex gap-1 rounded"
style={{ filter: "drop-shadow(0px 3px 3px black)" }}
>
{Array.from({ length: max }).map((_, i) => {
return (
<span>
{(i + 1) <= rating
? <IconStarFilled class="w-3 h-3" />
: <IconStar class="w-3 h-3" />}
</span>
);
})}
</div>
);
};
2023-08-08 21:50:23 +02:00
export const Rating = (
props: { max?: number; rating: number },
) => {
const [rating, setRating] = useState(props.rating);
const [hover, setHover] = useState(0);
const max = useSignal(props.max || 5);
return (
<div
2023-12-08 21:17:34 +01:00
class="flex items-center gap-2 px-5 rounded-2xl bg-gray-200 z-10"
2023-08-08 21:50:23 +02:00
style={{ color: "var(--foreground)", background: "var(--background)" }}
>
{Array.from({ length: max.value }).map((_, i) => {
return (
<span
2023-12-08 21:17:34 +01:00
class={`cursor-pointer opacity-${
2023-08-08 21:50:23 +02:00
(i + 1) <= rating ? 100 : (i + 1) <= hover ? 20 : 100
}`}
onMouseOver={() => setHover(i + 1)}
onClick={() => setRating(i + 1)}
>
{(i + 1) <= rating || (i + 1) <= hover
? <IconStarFilled class="w-4 h-4" />
: <IconStar class="w-4 h-4" />}
</span>
);
})}
</div>
);
};