-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.tsx
More file actions
289 lines (268 loc) · 10.7 KB
/
index.tsx
File metadata and controls
289 lines (268 loc) · 10.7 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import React, { useEffect, useMemo, useState } from 'react';
import type { ThemeDocument } from '@tiny-design/react';
import { validateThemeDocument } from '@tiny-design/tokens/validate-theme';
import {
Button,
ConfigProvider,
Modal,
Paragraph,
Segmented,
Select,
Text,
Textarea,
useTheme,
} from '@tiny-design/react';
import {
applyPresetToDraft,
buildDraftFromThemeDocument,
buildThemeDocumentFromDraft,
CODE_VIEW_OPTIONS,
type FieldKey,
type ThemeCodeView,
type ThemeEditorDraft,
type ThemeEditorSection,
type ThemeMode,
type ThemePreviewTemplate,
TEMPLATE_OPTIONS,
THEME_EDITOR_PRESETS,
THEME_SECTION_LABELS,
} from './presets';
import { buildPreviewVars, DRAFT_KEY, loadInitialDraft, PRESET_ID_KEY } from './editor-draft';
import { renderPreview } from './preview-components';
import { ThemeStudioSidebarContent } from './sidebar-content';
import {
compareThemeAgainstBase,
generateThemeCssVariables,
generateThemeDocumentJSON,
} from '../../utils/theme-document';
import {
applyThemeDocumentToDOM,
saveThemeDocument,
} from '../../utils/theme-persistence';
import { syncThemeStudioFonts } from './font-loader';
import './theme-studio.scss';
const ThemeStudioPage = (): React.ReactElement => {
const { resolvedTheme } = useTheme();
const [draft, setDraft] = useState<ThemeEditorDraft>(loadInitialDraft);
const [importVisible, setImportVisible] = useState(false);
const [codeVisible, setCodeVisible] = useState(false);
const [importText, setImportText] = useState('');
const [importError, setImportError] = useState<string | null>(null);
const [status, setStatus] = useState<string>('Editing local draft');
const globalMode: ThemeMode = resolvedTheme === 'dark' ? 'dark' : 'light';
useEffect(() => {
localStorage.setItem(DRAFT_KEY, JSON.stringify(draft));
}, [draft]);
const themeDocument = useMemo(() => buildThemeDocumentFromDraft(draft), [draft]);
const themeJson = useMemo(() => generateThemeDocumentJSON(themeDocument), [themeDocument]);
const cssVars = useMemo(() => generateThemeCssVariables(themeDocument), [themeDocument]);
const changedTokens = useMemo(() => compareThemeAgainstBase(themeDocument), [themeDocument]);
const activePreset = useMemo(
() => THEME_EDITOR_PRESETS.find((preset) => preset.id === draft.presetId) ?? THEME_EDITOR_PRESETS[0],
[draft.presetId],
);
useEffect(() => {
setDraft((current) => (
current.mode === globalMode
? current
: applyPresetToDraft(current.presetId, current, globalMode)
));
}, [globalMode]);
useEffect(() => {
saveThemeDocument(themeDocument);
applyThemeDocumentToDOM(themeDocument, { respectThemeMode: true });
localStorage.setItem(PRESET_ID_KEY, draft.presetId);
setStatus('Applied globally');
}, [draft.presetId, themeDocument]);
useEffect(() => {
syncThemeStudioFonts([draft.fields.fontSans, draft.fields.fontMono]);
}, [draft.fields.fontMono, draft.fields.fontSans]);
const updateField = (key: FieldKey, value: string) => {
setDraft((current) => {
const nextFields = {
...current.fields,
[key]: value,
};
// Treat global radius as the baseline shape control for the whole studio.
if (key === 'radius') {
nextFields.buttonRadius = value;
nextFields.inputRadius = value;
nextFields.cardRadius = value;
}
return {
...current,
fields: nextFields,
};
});
};
const resetToPreset = () => {
setDraft((current) => applyPresetToDraft(current.presetId, current));
setStatus('Reset to preset defaults');
};
const handlePresetChange = (presetId: string) => {
setDraft((current) => applyPresetToDraft(presetId, current));
setStatus(`Applied ${THEME_EDITOR_PRESETS.find((preset) => preset.id === presetId)?.name ?? 'preset'}`);
};
const handleImport = () => {
try {
const parsed = JSON.parse(importText) as ThemeDocument;
const validation = validateThemeDocument(parsed);
if (!validation.valid) {
setImportError(validation.errors.join('\n'));
return;
}
setDraft(buildDraftFromThemeDocument(validation.normalizedDocument as ThemeDocument));
setImportVisible(false);
setImportError(null);
setStatus(validation.warnings.length > 0 ? 'Imported theme document with validation warnings' : 'Imported theme document');
} catch {
setImportError('Invalid theme document JSON');
}
};
const handleCopy = async (value: string, label: string) => {
await navigator.clipboard.writeText(value);
setStatus(`${label} copied`);
};
return (
<ConfigProvider theme={themeDocument}>
<div
className={`theme-studio theme-studio_${draft.mode}`}
style={buildPreviewVars(draft.fields)}
>
<div className="theme-studio__topbar">
<div className="theme-studio__topbar-copy">
<Text className="theme-studio__eyebrow">Theme Editor</Text>
<div className="theme-studio__topbar-meta">
<span>{activePreset.name}</span>
<span>{THEME_EDITOR_PRESETS.length} presets</span>
</div>
</div>
<div className="theme-studio__topbar-actions">
<Select
className="theme-studio__select"
value={draft.presetId}
onChange={(value) => handlePresetChange(value)}
>
{THEME_EDITOR_PRESETS.map((preset) => (
<Select.Option key={preset.id} value={preset.id}>{preset.name}</Select.Option>
))}
</Select>
<div className="theme-studio__topbar-utility">
<Button size="sm" btnType="ghost" onClick={resetToPreset}>Reset</Button>
<Button size="sm" btnType="ghost" onClick={() => { setImportText(themeJson); setImportVisible(true); }}>Import</Button>
</div>
<Button size="sm" btnType="outline" onClick={() => setCodeVisible(true)}>Code</Button>
</div>
</div>
<div className="theme-studio__workspace">
<aside className="theme-studio__sidebar">
<div className="theme-studio__sidebar-head">
<Segmented
block
options={Object.entries(THEME_SECTION_LABELS).map(([value, label]) => ({ label, value }))}
value={draft.activeSection}
onChange={(value) => setDraft((current) => ({ ...current, activeSection: value as ThemeEditorSection }))}
/>
</div>
<ThemeStudioSidebarContent
section={draft.activeSection}
draft={draft}
updateField={updateField}
/>
</aside>
<main className="theme-studio__preview-shell">
<div className="theme-studio__preview-toolbar">
<div className="theme-studio__template-tabs">
{TEMPLATE_OPTIONS.map((item) => (
<button
key={item.value}
className={`theme-studio__template-tab${draft.activeTemplate === item.value ? ' theme-studio__template-tab_active' : ''}`}
onClick={() => setDraft((current) => ({ ...current, activeTemplate: item.value as ThemePreviewTemplate }))}
>
{item.label}
</button>
))}
</div>
</div>
{renderPreview(draft.activeTemplate, draft.fields, draft.activeSection)}
</main>
</div>
<Modal
visible={importVisible}
className="theme-studio__modal theme-studio__modal_import"
header="Import Theme Document"
width={760}
bodyStyle={{ maxHeight: 'min(70vh, 720px)', overflow: 'auto' }}
cancelText="Cancel"
confirmText="Apply Theme"
onConfirm={handleImport}
onClose={() => {
setImportVisible(false);
setImportError(null);
}}
>
<div className="theme-studio__modal-copy">
<Paragraph>
Paste a Tiny theme document JSON export to replace the current global theme.
</Paragraph>
<Text type="secondary">Preset selection and all editor controls will sync to the imported values.</Text>
</div>
<Textarea
rows={16}
className="theme-studio__import-textarea"
value={importText}
onChange={(next) => setImportText(next)}
/>
{importError ? <Paragraph className="theme-studio__error">{importError}</Paragraph> : null}
</Modal>
<Modal
visible={codeVisible}
className="theme-studio__modal theme-studio__modal_code"
header="Theme Output"
width={980}
bodyStyle={{ maxHeight: 'min(74vh, 760px)', overflow: 'auto' }}
cancelText="Close"
confirmText={draft.activeCodeView === 'json' ? 'Copy JSON' : draft.activeCodeView === 'css' ? 'Copy CSS' : 'Done'}
confirmButtonProps={draft.activeCodeView === 'tokens' ? { style: { display: 'none' } } : undefined}
onConfirm={draft.activeCodeView === 'json'
? () => handleCopy(themeJson, 'Theme JSON')
: draft.activeCodeView === 'css'
? () => handleCopy(cssVars, 'CSS variables')
: undefined}
onClose={() => setCodeVisible(false)}
>
<div className="theme-studio__code-head">
<div>
<Text strong>Output</Text>
<Text type="secondary">{activePreset.name} · {status}</Text>
</div>
<Segmented
options={CODE_VIEW_OPTIONS}
value={draft.activeCodeView}
onChange={(value) => setDraft((current) => ({ ...current, activeCodeView: value as ThemeCodeView }))}
/>
</div>
{draft.activeCodeView === 'json' ? <pre className="theme-studio__code-block">{themeJson}</pre> : null}
{draft.activeCodeView === 'css' ? <pre className="theme-studio__code-block">{cssVars}</pre> : null}
{draft.activeCodeView === 'tokens' ? (
<div className="theme-studio__tokens-list theme-studio__tokens-list_modal">
{changedTokens.map((token) => (
<div key={token.key} className="theme-studio__token-row">
<div>
<strong>{token.key}</strong>
<small>{token.cssVar}</small>
</div>
<div>
<code>{token.value}</code>
<small>Base: {token.baseValue || 'n/a'}</small>
</div>
</div>
))}
</div>
) : null}
</Modal>
</div>
</ConfigProvider>
);
};
export default ThemeStudioPage;