-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: implement 7 UI fixes for editor interface (#2488) #2489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
da0f24c
f105a5d
b07006e
8c96e6c
1e24a9c
1453c98
2fbbbe7
1989282
d88850b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,24 +15,46 @@ interface InputRangeProps { | |
unit?: string; | ||
onChange?: (value: number) => void; | ||
onUnitChange?: (unit: string) => void; | ||
customIncrements?: number[]; | ||
useTailwindClasses?: boolean; | ||
} | ||
|
||
export const STANDARD_INCREMENTS = [0, 0.25, 0.5, 0.75, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; | ||
|
||
const TAILWIND_RADIUS_MAP: Record<number, string> = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TAILWIND_RADIUS_MAP is defined but not used; implement mapping logic when useTailwindClasses is true. |
||
2: 'rounded-xs', | ||
4: 'rounded-sm', | ||
6: 'rounded-md', | ||
8: 'rounded-lg', | ||
12: 'rounded-xl', | ||
16: 'rounded-2xl', | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. InputRange now supports customIncrements and snapping, which is good. Note that the useTailwindClasses prop is accepted but TAILWIND_RADIUS_MAP isn’t used; if the intent is to map certain values to Tailwind classes, additional logic is needed. |
||
export const InputRange = ({ | ||
value, | ||
icon, | ||
unit = 'px', | ||
onChange, | ||
onUnitChange, | ||
customIncrements, | ||
useTailwindClasses = false, | ||
}: InputRangeProps) => { | ||
const [localValue, setLocalValue] = useState(String(value)); | ||
const rangeRef = useRef<HTMLInputElement>(null); | ||
const [isDragging, setIsDragging] = useState(false); | ||
|
||
// Create debounced onChange handler | ||
// Create debounced onChange handler for CSS updates | ||
const debouncedOnChange = useMemo( | ||
() => debounce((newValue: number) => { | ||
onChange?.(newValue); | ||
}, 500), | ||
}, 300), | ||
[onChange] | ||
); | ||
|
||
const immediateOnChange = useMemo( | ||
() => (newValue: number) => { | ||
onChange?.(newValue); | ||
}, | ||
[onChange] | ||
); | ||
|
||
|
@@ -88,13 +110,23 @@ export const InputRange = ({ | |
} | ||
}; | ||
|
||
const findClosestIncrement = (targetValue: number): number => { | ||
if (!customIncrements) return targetValue; | ||
|
||
return customIncrements.reduce((closest, current) => { | ||
return Math.abs(current - targetValue) < Math.abs(closest - targetValue) ? current : closest; | ||
}); | ||
}; | ||
|
||
const handleMouseMove = (e: MouseEvent) => { | ||
if (isDragging && rangeRef.current) { | ||
const rect = rangeRef.current.getBoundingClientRect(); | ||
const percentage = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width)); | ||
const newValue = Math.round(percentage * 500); | ||
const maxValue = customIncrements ? Math.max(...customIncrements) : 500; | ||
const rawValue = percentage * maxValue; | ||
const newValue = customIncrements ? findClosestIncrement(rawValue) : Math.round(rawValue); | ||
setLocalValue(String(newValue)); | ||
debouncedOnChange(newValue); | ||
immediateOnChange(newValue); | ||
} | ||
}; | ||
|
||
|
@@ -104,19 +136,23 @@ export const InputRange = ({ | |
document.removeEventListener('mouseup', handleMouseUp); | ||
}; | ||
|
||
const maxValue = customIncrements ? Math.max(...customIncrements) : 500; | ||
const currentValue = customIncrements ? findClosestIncrement(Number(localValue)) : Number(localValue); | ||
|
||
return ( | ||
<div className="flex items-center gap-2"> | ||
<div className="flex-1 flex items-center gap-2"> | ||
<input | ||
ref={rangeRef} | ||
type="range" | ||
min="0" | ||
max="500" | ||
value={Number(localValue)} | ||
max={maxValue} | ||
value={currentValue} | ||
onChange={(e) => { | ||
const newValue = Number(e.target.value); | ||
const rawValue = Number(e.target.value); | ||
const newValue = customIncrements ? findClosestIncrement(rawValue) : rawValue; | ||
setLocalValue(String(newValue)); | ||
debouncedOnChange(newValue); | ||
immediateOnChange(newValue); | ||
}} | ||
onMouseDown={handleMouseDown} | ||
className="flex-1 h-3 bg-background-tertiary/50 rounded-full appearance-none cursor-pointer relative | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using setTimeout with a fixed 50ms delay risks race conditions when rapid updates occur. The callback captures a potentially stale 'currentState'. Consider a debounced or cancelable approach to ensure CSS updates use the latest state.