|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Type, AlignJustify } from "lucide-react"; |
| 4 | +import { |
| 5 | + Popover, |
| 6 | + PopoverContent, |
| 7 | + PopoverTrigger, |
| 8 | +} from "@/components/ui/popover"; |
| 9 | +import { Button } from "@/components/ui/button"; |
| 10 | +import { |
| 11 | + useReadingPrefs, |
| 12 | + type FontSize, |
| 13 | + type LineHeight, |
| 14 | +} from "@/lib/hooks/useReadingPrefs"; |
| 15 | +import { cn } from "@/lib/utils"; |
| 16 | + |
| 17 | +const FONT_OPTIONS: Array<{ value: FontSize; label: string; sample: string }> = [ |
| 18 | + { value: "sm", label: "小", sample: "A" }, |
| 19 | + { value: "md", label: "中", sample: "A" }, |
| 20 | + { value: "lg", label: "大", sample: "A" }, |
| 21 | +]; |
| 22 | + |
| 23 | +const LINE_OPTIONS: Array<{ value: LineHeight; label: string }> = [ |
| 24 | + { value: "compact", label: "紧凑" }, |
| 25 | + { value: "normal", label: "标准" }, |
| 26 | + { value: "relaxed", label: "宽松" }, |
| 27 | +]; |
| 28 | + |
| 29 | +const FONT_SAMPLE_CLASS: Record<FontSize, string> = { |
| 30 | + sm: "text-xs", |
| 31 | + md: "text-sm", |
| 32 | + lg: "text-base", |
| 33 | +}; |
| 34 | + |
| 35 | +export function ReadingPrefs() { |
| 36 | + const { fontSize, lineHeight, setFontSize, setLineHeight } = useReadingPrefs(); |
| 37 | + |
| 38 | + return ( |
| 39 | + <Popover> |
| 40 | + <PopoverTrigger asChild> |
| 41 | + <Button |
| 42 | + variant="outline" |
| 43 | + size="icon" |
| 44 | + aria-label="阅读偏好" |
| 45 | + className="fixed bottom-6 right-6 z-30 size-11 rounded-full bg-surface/95 backdrop-blur-lg shadow-lg border-border/60" |
| 46 | + > |
| 47 | + <Type className="size-4" /> |
| 48 | + </Button> |
| 49 | + </PopoverTrigger> |
| 50 | + <PopoverContent |
| 51 | + align="end" |
| 52 | + side="top" |
| 53 | + sideOffset={8} |
| 54 | + className="w-64 space-y-4" |
| 55 | + > |
| 56 | + <div className="space-y-2"> |
| 57 | + <p className="text-xs font-medium text-muted-foreground flex items-center gap-1.5"> |
| 58 | + <Type className="size-3.5" /> |
| 59 | + 字号 |
| 60 | + </p> |
| 61 | + <div className="grid grid-cols-3 gap-1.5"> |
| 62 | + {FONT_OPTIONS.map((opt) => ( |
| 63 | + <button |
| 64 | + key={opt.value} |
| 65 | + type="button" |
| 66 | + onClick={() => setFontSize(opt.value)} |
| 67 | + aria-pressed={fontSize === opt.value} |
| 68 | + aria-label={`字号 ${opt.label}`} |
| 69 | + className={cn( |
| 70 | + "h-12 rounded-lg border flex flex-col items-center justify-center gap-0.5 transition-colors", |
| 71 | + fontSize === opt.value |
| 72 | + ? "border-primary bg-primary/10 text-primary" |
| 73 | + : "border-border bg-surface text-muted-foreground hover:bg-muted" |
| 74 | + )} |
| 75 | + > |
| 76 | + <span className={cn("font-prose", FONT_SAMPLE_CLASS[opt.value])}> |
| 77 | + {opt.sample} |
| 78 | + </span> |
| 79 | + <span className="text-[10px]">{opt.label}</span> |
| 80 | + </button> |
| 81 | + ))} |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + |
| 85 | + <div className="space-y-2"> |
| 86 | + <p className="text-xs font-medium text-muted-foreground flex items-center gap-1.5"> |
| 87 | + <AlignJustify className="size-3.5" /> |
| 88 | + 行距 |
| 89 | + </p> |
| 90 | + <div className="grid grid-cols-3 gap-1.5"> |
| 91 | + {LINE_OPTIONS.map((opt) => ( |
| 92 | + <button |
| 93 | + key={opt.value} |
| 94 | + type="button" |
| 95 | + onClick={() => setLineHeight(opt.value)} |
| 96 | + aria-pressed={lineHeight === opt.value} |
| 97 | + aria-label={`行距 ${opt.label}`} |
| 98 | + className={cn( |
| 99 | + "h-9 rounded-lg border text-xs transition-colors", |
| 100 | + lineHeight === opt.value |
| 101 | + ? "border-primary bg-primary/10 text-primary" |
| 102 | + : "border-border bg-surface text-muted-foreground hover:bg-muted" |
| 103 | + )} |
| 104 | + > |
| 105 | + {opt.label} |
| 106 | + </button> |
| 107 | + ))} |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + </PopoverContent> |
| 111 | + </Popover> |
| 112 | + ); |
| 113 | +} |
0 commit comments