Repository navigation
Expand file tree
/
Copy pathVoteControl.tsx
More file actions
111 lines (100 loc) · 2.81 KB
/
Copy pathVoteControl.tsx
File metadata and controls
111 lines (100 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"use client";
import { useState } from "react";
type VoteState = "up" | "down" | null;
export interface VoteControlProps {
/** The net/score baseline (excludes the current user's own vote). */
base: number;
/** The user's existing vote, if any. */
initial?: VoteState;
/** Tighter sizing for feed rows + comments/replies. */
compact?: boolean;
/** Logged-out intercept — called instead of voting; voting is skipped. */
onGate?: () => void;
/** Fires the actual mutation with the next vote state. */
onVote?: (next: VoteState) => void;
}
const Chevron = ({
direction,
size,
}: {
direction: "up" | "down";
size: number;
}) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
<path d={direction === "up" ? "M6 14l6-6 6 6" : "M6 10l6 6 6-6"} />
</svg>
);
const VoteControl = ({
base,
initial = null,
compact = false,
onGate,
onVote,
}: VoteControlProps) => {
const [vote, setVote] = useState<VoteState>(initial);
const score = base + (vote === "up" ? 1 : vote === "down" ? -1 : 0);
const handle = (e: React.MouseEvent, direction: "up" | "down") => {
e.stopPropagation();
if (onGate) {
// Logged-out intercept: gate and bail without mutating local state.
onGate();
return;
}
const next: VoteState = vote === direction ? null : direction;
setVote(next);
onVote?.(next);
};
const arrowSize = compact ? 13 : 15;
const pad = "px-1.5 py-0.5";
const upColor =
vote === "up" ? "text-accent-soft" : "text-faint hover:text-accent-soft";
const downColor =
vote === "down" ? "text-danger" : "text-faint hover:text-danger";
const scoreColor =
vote === "up"
? "text-accent-soft"
: vote === "down"
? "text-danger"
: "text-muted";
return (
<div
className={`inline-flex items-center gap-1.5 rounded-full border border-hairline ${pad}`}
onClick={(e) => e.stopPropagation()}
>
<button
type="button"
onClick={(e) => handle(e, "up")}
aria-label="Upvote"
aria-pressed={vote === "up"}
className={`flex items-center transition-colors ${upColor}`}
>
<Chevron direction="up" size={arrowSize} />
</button>
<span
className={`text-center text-xs font-semibold tabular-nums transition-colors ${scoreColor}`}
>
{score}
</span>
<button
type="button"
onClick={(e) => handle(e, "down")}
aria-label="Downvote"
aria-pressed={vote === "down"}
className={`flex items-center transition-colors ${downColor}`}
>
<Chevron direction="down" size={arrowSize} />
</button>
</div>
);
};
export default VoteControl;