upvote-rating-basic
stackzero/upvote-rating-basicFreeComponent
stackzero publishes no description for this item.
Install it
npx shadcn@latest add https://ui.stackzero.co/r/upvote-rating-basic.jsonSource
1import { cn } from "@/lib/utils";2import { ArrowBigDown, ArrowBigUp } from "lucide-react";3import * as React from "react";45const UPVOTE_COLOR = "bg-[#ff4500]";6const DOWNVOTE_COLOR = "bg-[#9494ff]";7interface UpvoteRatingBasicProps {8 upvotes: number;9 downvotes: number;10 upvoted: boolean;11 downvoted: boolean;12 upvoteIncrement?: number;13 downvoteIncrement?: number;14 onVoteChange: (newState: {15 upvotes: number;16 downvotes: number;17 upvoted: boolean;18 downvoted: boolean;19 }) => void;20}2122const UpvoteRating_Basic = ({23 downvoted,24 downvoteIncrement = 1,25 downvotes,26 onVoteChange,27 upvoted,28 upvoteIncrement = 1,29 upvotes,30}: UpvoteRatingBasicProps) => {31 const handleUpvote = () => {32 if (upvoted) {33 // Undo upvote34 onVoteChange({35 downvoted: false,36 downvotes,37 upvoted: false,38 upvotes: upvotes - upvoteIncrement,39 });40 } else {41 // Add upvote and remove downvote if exists42 onVoteChange({43 downvoted: false,44 downvotes: downvoted ? downvotes - downvoteIncrement : downvotes,45 upvoted: true,46 upvotes: upvotes + upvoteIncrement,47 });48 }49 };5051 const handleDownvote = () => {52 if (downvoted) {53 // Undo downvote54 onVoteChange({55 downvoted: false,56 downvotes: downvotes - downvoteIncrement,57 upvoted: false,58 upvotes,59 });60 } else {61 // Add downvote and remove upvote if exists62 onVoteChange({63 downvoted: true,64 downvotes: downvotes + downvoteIncrement,65 upvoted: false,66 upvotes: upvoted ? upvotes - upvoteIncrement : upvotes,67 });68 }69 };7071 const totalVotes = upvotes - downvotes;72 const formattedVotes =73 totalVotes >= 100074 ? `${(totalVotes / 1000).toFixed(1)}K`75 : totalVotes.toLocaleString();7677 return (78 <div79 className={cn(80 "flex w-fit flex-row items-center gap-0 rounded-full border",81 upvoted && UPVOTE_COLOR,82 downvoted && DOWNVOTE_COLOR83 )}84 >85 <button86 onClick={handleUpvote}87 className="cursor-pointer rounded-full p-1 hover:bg-zinc-800/30"88 >89 <ArrowBigUp90 size={24}91 className={cn("text-white", upvoted && "fill-white")}92 />93 </button>9495 <span className="min-w-8 p-1 text-center text-white">96 {formattedVotes}97 </span>9899 <button100 onClick={handleDownvote}101// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from stackzero
All 112 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| face-rating-basicface-rating-basic | stackzero | Components | Free | 1 dep | |
| face-rating-gradientface-rating-gradient | stackzero | Components | Free | 2 deps | |
| image-carousel-basicimage-carousel-basic | stackzero | Components | Free | 5 deps | |
| image-viewer-basicimage-viewer-basic | stackzero | Components | Free | 3 deps | |
| image-viewer-basic-ex-01image-viewer-basic-ex-01 | stackzero | Components | Free | 1 dep | |
| image-viewer-basic-ex-02image-viewer-basic-ex-02 | stackzero | Components | Free | 1 dep | |
| image-viewer-motionimage-viewer-motion | stackzero | Components | Free | 4 deps | |
| image-viewer-motion-ex-01image-viewer-motion-ex-01 | stackzero | Components | Free | 1 dep |
