Skip to content

Commit bf80939

Browse files
authored
feat: add min max and step to slider (#2999)
1 parent acb8ed6 commit bf80939

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

apps/web/client/src/app/project/[id]/_components/editor-bar/dropdowns/border.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ export const Border = observer(() => {
117117
onChange={(value) => handleBoxChange('borderWidth', value.toString())}
118118
unit={boxState.borderWidth.unit}
119119
onUnitChange={(unit) => handleUnitChange('borderWidth', unit)}
120+
min={0}
121+
max={16}
122+
step={0.25}
120123
/>
121124
) : (
122125
<SpacingInputs

apps/web/client/src/app/project/[id]/_components/editor-bar/dropdowns/margin.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
'use client';
22

33
import { useEditorEngine } from "@/components/store/editor";
4-
import { Button } from "@onlook/ui/button";
54
import {
65
DropdownMenu,
76
DropdownMenuContent,
87
DropdownMenuTrigger,
98
} from "@onlook/ui/dropdown-menu";
109
import { Icons } from "@onlook/ui/icons";
11-
import { cn } from "@onlook/ui/utils";
1210
import { observer } from "mobx-react-lite";
13-
import { useCallback, useMemo, useState } from "react";
11+
import { useMemo, useState } from "react";
1412
import { useBoxControl } from "../hooks/use-box-control";
1513
import { useDropdownControl } from "../hooks/use-dropdown-manager";
1614
import { HoverOnlyTooltip } from "../hover-tooltip";
@@ -55,25 +53,25 @@ const MARGIN_ICON_MAP: Record<string, typeof Icons.MarginEmpty> = {
5553
export const Margin = observer(() => {
5654
const { boxState, handleBoxChange, handleUnitChange, handleIndividualChange } = useBoxControl('margin');
5755
const editorEngine = useEditorEngine();
58-
56+
5957
const { isOpen, onOpenChange } = useDropdownControl({
6058
id: 'margin-dropdown'
6159
});
62-
63-
60+
61+
6462
const areAllMarginsEqual = useMemo((): boolean => {
6563
const margins = {
6664
top: boxState.marginTop.num ?? 0,
6765
right: boxState.marginRight.num ?? 0,
6866
bottom: boxState.marginBottom.num ?? 0,
6967
left: boxState.marginLeft.num ?? 0,
7068
};
71-
69+
7270
const values = Object.values(margins);
73-
71+
7472
return values.every(val => val === values[0]);
7573
}, [boxState.marginTop.num, boxState.marginRight.num, boxState.marginBottom.num, boxState.marginLeft.num]);
76-
74+
7775
const [activeTab, setActiveTab] = useState<MarginTab>(areAllMarginsEqual ? MarginTab.ALL : MarginTab.INDIVIDUAL);
7876

7977
const getMarginIcon = () => {
@@ -86,13 +84,13 @@ export const Margin = observer(() => {
8684

8785
const values = Object.values(margins);
8886
const nonZeroValues = values.filter(val => val > 0);
89-
87+
9088
if (nonZeroValues.length === 0) {
9189
return Icons.MarginEmpty;
9290
}
9391

94-
const allSame = nonZeroValues.length === 4 &&
95-
nonZeroValues.every(val => val === nonZeroValues[0]);
92+
const allSame = nonZeroValues.length === 4 &&
93+
nonZeroValues.every(val => val === nonZeroValues[0]);
9694
if (allSame) {
9795
return Icons.MarginFull;
9896
}
@@ -192,6 +190,9 @@ export const Margin = observer(() => {
192190
onChange={(value) => handleBoxChange('margin', value.toString())}
193191
unit={boxState.margin.unit}
194192
onUnitChange={(unit) => handleUnitChange('margin', unit)}
193+
min={0}
194+
max={384}
195+
step={16}
195196
/>
196197
) : (
197198
<SpacingInputs

apps/web/client/src/app/project/[id]/_components/editor-bar/dropdowns/padding.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ export const Padding = observer(() => {
164164
onChange={(value) => handleBoxChange('padding', value.toString())}
165165
unit={boxState.padding.unit}
166166
onUnitChange={(unit) => handleUnitChange('padding', unit)}
167+
min={0}
168+
max={384}
169+
step={16}
167170
/>
168171
) : (
169172
<SpacingInputs

apps/web/client/src/app/project/[id]/_components/editor-bar/dropdowns/radius.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ export const Radius = observer(() => {
145145
onChange={(value) => handleBoxChange('borderRadius', value.toString())}
146146
unit={boxState.borderRadius.unit}
147147
onUnitChange={(unit) => handleUnitChange('borderRadius', unit)}
148+
min={0}
149+
max={32}
150+
step={2}
148151
/>
149152
) : (
150153
<SpacingInputs

apps/web/client/src/app/project/[id]/_components/editor-bar/inputs/input-range.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ interface InputRangeProps {
1313
value: number;
1414
icon?: keyof typeof Icons;
1515
unit?: string;
16+
min?: number;
17+
max?: number;
18+
step?: number;
1619
onChange?: (value: number) => void;
1720
onUnitChange?: (unit: string) => void;
1821
}
@@ -21,6 +24,9 @@ export const InputRange = ({
2124
value,
2225
icon,
2326
unit = 'px',
27+
min = 0,
28+
max = 500,
29+
step = 1,
2430
onChange,
2531
onUnitChange,
2632
}: InputRangeProps) => {
@@ -58,6 +64,7 @@ export const InputRange = ({
5864
const handleBlur = () => {
5965
const numValue = Number(localValue);
6066
if (!isNaN(numValue)) {
67+
setLocalValue(String(numValue));
6168
debouncedOnChange(numValue);
6269
} else {
6370
setLocalValue(String(value));
@@ -67,11 +74,11 @@ export const InputRange = ({
6774
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
6875
if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
6976
e.preventDefault();
70-
const step = e.shiftKey ? 10 : 1;
77+
const stepValue = e.shiftKey ? step * 10 : step;
7178
const direction = e.key === 'ArrowUp' ? 1 : -1;
7279
const currentValue = Number(localValue);
7380
if (!isNaN(currentValue)) {
74-
const newValue = currentValue + (step * direction);
81+
const newValue = currentValue + (stepValue * direction);
7582
setLocalValue(String(newValue));
7683
debouncedOnChange(newValue);
7784
}
@@ -92,7 +99,7 @@ export const InputRange = ({
9299
if (isDragging && rangeRef.current) {
93100
const rect = rangeRef.current.getBoundingClientRect();
94101
const percentage = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));
95-
const newValue = Math.round(percentage * 500);
102+
const newValue = Math.round((percentage * (max - min) + min) / step) * step;
96103
setLocalValue(String(newValue));
97104
debouncedOnChange(newValue);
98105
}
@@ -110,8 +117,9 @@ export const InputRange = ({
110117
<input
111118
ref={rangeRef}
112119
type="range"
113-
min="0"
114-
max="500"
120+
min={min}
121+
max={max}
122+
step={step}
115123
value={Number(localValue)}
116124
onChange={(e) => {
117125
const newValue = Number(e.target.value);

0 commit comments

Comments
 (0)