-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patheditor-fields.tsx
More file actions
134 lines (124 loc) · 3.41 KB
/
editor-fields.tsx
File metadata and controls
134 lines (124 loc) · 3.41 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import React from 'react';
import { ColorPicker, Input, InputNumber, Slider, Textarea } from '@tiny-design/react';
import type { SliderFieldConfig } from './types';
export function swatchTextStyle(background: string, foreground: string): React.CSSProperties {
return {
background,
color: foreground,
};
}
function parseSliderValue(value: string, unit?: 'px' | 'em' | 'rem'): number | undefined {
if (unit === 'px') {
const match = /^(-?\d+(?:\.\d+)?)px$/.exec(value.trim());
return match ? Number(match[1]) : undefined;
}
if (unit === 'em') {
const match = /^(-?\d+(?:\.\d+)?)em$/.exec(value.trim());
return match ? Number(match[1]) : undefined;
}
if (unit === 'rem') {
const match = /^(-?\d+(?:\.\d+)?)rem$/.exec(value.trim());
return match ? Number(match[1]) : undefined;
}
const parsed = Number(value);
return Number.isNaN(parsed) ? undefined : parsed;
}
function formatSliderValue(value: number, unit?: 'px' | 'em' | 'rem'): string {
if (!unit) return String(value);
return `${value}${unit}`;
}
export function ColorField({
label,
value,
onChange,
}: {
label: string;
value: string;
onChange: (next: string) => void;
}): React.ReactElement {
return (
<div className="theme-studio__field">
<span className="theme-studio__field-label">{label}</span>
<div className="theme-studio__color-field">
<ColorPicker
value={value}
onChange={onChange}
presets={[
'#000000',
'#ffffff',
'#1e9df1',
'#0f1419',
'#e11d48',
'#22c55e',
'#f59e0b',
'#8b5cf6',
]}
/>
<Input value={value} onChange={(event) => onChange(event.target.value)} />
</div>
</div>
);
}
export function TextField({
label,
value,
onChange,
multiline,
}: {
label: string;
value: string;
onChange: (next: string) => void;
multiline?: boolean;
}): React.ReactElement {
return (
<label className="theme-studio__field">
<span className="theme-studio__field-label">{label}</span>
{multiline ? (
<Textarea rows={3} value={value} onChange={(next) => onChange(next)} />
) : (
<Input value={value} onChange={(event) => onChange(event.target.value)} />
)}
</label>
);
}
export function SliderField({
label,
value,
onChange,
config,
}: {
label: string;
value: string;
onChange: (next: string) => void;
config: SliderFieldConfig;
}): React.ReactElement {
const parsed = parseSliderValue(value, config.unit);
return (
<label className="theme-studio__field">
<div className="theme-studio__field-row">
<span className="theme-studio__field-label">{label}</span>
<span className="theme-studio__field-value">{value}</span>
</div>
<div className="theme-studio__slider-field">
<Slider
min={config.min}
max={config.max}
step={config.step}
value={parsed ?? config.min}
onChange={(next) => {
if (typeof next === 'number') onChange(formatSliderValue(next, config.unit));
}}
/>
<InputNumber
value={parsed}
min={config.min}
max={config.max}
step={config.step}
onChange={(next) => {
if (typeof next === 'number') onChange(formatSliderValue(next, config.unit));
}}
/>
</div>
</label>
);
}