|
| 1 | +import React from 'react'; |
| 2 | +import * as Popover from '@radix-ui/react-popover'; |
| 3 | +import { configStore, useConfigValue } from '@plannotator/ui/config'; |
| 4 | +import { |
| 5 | + DIFF_STYLE_OPTIONS, |
| 6 | + OVERFLOW_OPTIONS, |
| 7 | + INDICATOR_OPTIONS, |
| 8 | + LINE_DIFF_OPTIONS, |
| 9 | +} from '@plannotator/ui/components/Settings'; |
| 10 | + |
| 11 | +function CompactSegmented<T extends string>({ options, value, onChange }: { |
| 12 | + options: { value: T; label: string }[]; |
| 13 | + value: T; |
| 14 | + onChange: (v: T) => void; |
| 15 | +}) { |
| 16 | + return ( |
| 17 | + <div className="flex items-center gap-px bg-muted/60 rounded-md p-px"> |
| 18 | + {options.map((opt) => ( |
| 19 | + <button |
| 20 | + key={opt.value} |
| 21 | + onClick={() => onChange(opt.value)} |
| 22 | + className={`flex-1 px-2 py-1 text-[11px] rounded-[5px] transition-colors ${ |
| 23 | + value === opt.value |
| 24 | + ? 'bg-background text-foreground shadow-sm font-medium' |
| 25 | + : 'text-muted-foreground hover:text-foreground' |
| 26 | + }`} |
| 27 | + > |
| 28 | + {opt.label} |
| 29 | + </button> |
| 30 | + ))} |
| 31 | + </div> |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +function CompactToggle({ checked, onChange, label }: { |
| 36 | + checked: boolean; |
| 37 | + onChange: (v: boolean) => void; |
| 38 | + label: string; |
| 39 | +}) { |
| 40 | + return ( |
| 41 | + <button |
| 42 | + role="switch" |
| 43 | + aria-checked={checked} |
| 44 | + onClick={() => onChange(!checked)} |
| 45 | + className="w-full flex items-center justify-between py-1 group" |
| 46 | + > |
| 47 | + <span className="text-[11px] text-muted-foreground group-hover:text-foreground transition-colors">{label}</span> |
| 48 | + <span className={`relative inline-flex h-4 w-7 items-center rounded-full transition-colors ${ |
| 49 | + checked ? 'bg-primary' : 'bg-muted-foreground/25' |
| 50 | + }`}> |
| 51 | + <span className={`inline-block h-3 w-3 rounded-full bg-white shadow-sm transition-transform ${ |
| 52 | + checked ? 'translate-x-3.5' : 'translate-x-0.5' |
| 53 | + }`} /> |
| 54 | + </span> |
| 55 | + </button> |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +export const DiffOptionsPopover: React.FC = () => { |
| 60 | + const diffStyle = useConfigValue('diffStyle'); |
| 61 | + const diffOverflow = useConfigValue('diffOverflow'); |
| 62 | + const diffIndicators = useConfigValue('diffIndicators'); |
| 63 | + const diffLineDiffType = useConfigValue('diffLineDiffType'); |
| 64 | + const diffShowLineNumbers = useConfigValue('diffShowLineNumbers'); |
| 65 | + const diffShowBackground = useConfigValue('diffShowBackground'); |
| 66 | + const diffHideWhitespace = useConfigValue('diffHideWhitespace'); |
| 67 | + |
| 68 | + return ( |
| 69 | + <Popover.Root> |
| 70 | + <Popover.Trigger asChild> |
| 71 | + <button |
| 72 | + className="text-xs text-muted-foreground hover:text-foreground rounded hover:bg-muted transition-colors flex items-center px-1.5 py-1" |
| 73 | + title="Diff display options" |
| 74 | + > |
| 75 | + <svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}> |
| 76 | + <path strokeLinecap="round" strokeLinejoin="round" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.066 2.573c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.573 1.066c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.066-2.573c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" /> |
| 77 | + <path strokeLinecap="round" strokeLinejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" /> |
| 78 | + </svg> |
| 79 | + </button> |
| 80 | + </Popover.Trigger> |
| 81 | + <Popover.Portal> |
| 82 | + <Popover.Content |
| 83 | + align="end" |
| 84 | + sideOffset={6} |
| 85 | + className="z-50 w-64 bg-popover text-popover-foreground border border-border rounded-lg shadow-lg overflow-hidden origin-[var(--radix-popover-content-transform-origin)] data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0" |
| 86 | + > |
| 87 | + <div className="p-2.5 space-y-2"> |
| 88 | + <div className="space-y-1.5"> |
| 89 | + <div> |
| 90 | + <div className="text-[10px] uppercase tracking-wide text-muted-foreground/70 mb-1">Layout</div> |
| 91 | + <div className="grid grid-cols-2 gap-1.5"> |
| 92 | + <div> |
| 93 | + <CompactSegmented options={DIFF_STYLE_OPTIONS} value={diffStyle} onChange={(v) => configStore.set('diffStyle', v)} /> |
| 94 | + </div> |
| 95 | + <div> |
| 96 | + <CompactSegmented options={OVERFLOW_OPTIONS} value={diffOverflow} onChange={(v) => configStore.set('diffOverflow', v)} /> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + <div> |
| 101 | + <div className="text-[10px] uppercase tracking-wide text-muted-foreground/70 mb-1">Indicators</div> |
| 102 | + <CompactSegmented options={INDICATOR_OPTIONS} value={diffIndicators} onChange={(v) => configStore.set('diffIndicators', v)} /> |
| 103 | + </div> |
| 104 | + <div> |
| 105 | + <div className="text-[10px] uppercase tracking-wide text-muted-foreground/70 mb-1">Inline diff</div> |
| 106 | + <CompactSegmented options={LINE_DIFF_OPTIONS} value={diffLineDiffType} onChange={(v) => configStore.set('diffLineDiffType', v)} /> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + |
| 110 | + <div className="border-t border-border/50" /> |
| 111 | + |
| 112 | + <div> |
| 113 | + <CompactToggle checked={diffShowLineNumbers} onChange={(v) => configStore.set('diffShowLineNumbers', v)} label="Line numbers" /> |
| 114 | + <CompactToggle checked={diffShowBackground} onChange={(v) => configStore.set('diffShowBackground', v)} label="Diff background" /> |
| 115 | + <CompactToggle checked={diffHideWhitespace} onChange={(v) => configStore.set('diffHideWhitespace', v)} label="Hide whitespace" /> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + </Popover.Content> |
| 119 | + </Popover.Portal> |
| 120 | + </Popover.Root> |
| 121 | + ); |
| 122 | +}; |
0 commit comments