generated from mantinedev/next-pages-template
-
-
Notifications
You must be signed in to change notification settings - Fork 258
/
AppearanceCustomization.tsx
227 lines (209 loc) · 6.47 KB
/
AppearanceCustomization.tsx
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
import {
CheckIcon,
ColorSwatch,
Group,
Input,
MantineTheme,
Select,
Slider,
Stack,
TextInput,
createStyles,
rem,
useMantineTheme,
} from '@mantine/core';
import { useTranslation } from 'next-i18next';
import { highlight, languages } from 'prismjs';
import Editor from 'react-simple-code-editor';
import { useColorTheme } from '~/tools/color';
import {
BackgroundImageAttachment,
BackgroundImageRepeat,
BackgroundImageSize,
} from '~/types/settings';
import { useBoardCustomizationFormContext } from '../form';
export const AppearanceCustomization = () => {
const { t } = useTranslation('settings/customization/page-appearance');
const form = useBoardCustomizationFormContext();
return (
<Stack spacing="sm">
<TextInput
label={t('background.label')}
placeholder="/imgs/backgrounds/background.png"
{...form.getInputProps('appearance.backgroundSrc')}
/>
<Select
label={t('backgroundImageAttachment.label')}
data={BackgroundImageAttachment.map((attachment) => ({
value: attachment,
label: t(`backgroundImageAttachment.options.${attachment}`) as string,
}))}
{...form.getInputProps('appearance.backgroundImageAttachment')}
/>
<Select
label={t('backgroundImageSize.label')}
data={BackgroundImageSize.map((size) => ({
value: size,
label: t(`backgroundImageSize.options.${size}`) as string,
}))}
{...form.getInputProps('appearance.backgroundImageSize')}
/>
<Select
label={t('backgroundImageRepeat.label')}
data={BackgroundImageRepeat.map((repeat) => ({
value: repeat,
label: t(`backgroundImageRepeat.options.${repeat}`) as string,
}))}
{...form.getInputProps('appearance.backgroundImageRepeat')}
/>
<ColorSelector type="primaryColor" />
<ColorSelector type="secondaryColor" />
<ShadeSelector />
<OpacitySlider />
<CustomCssInput />
</Stack>
);
};
type ColorSelectorProps = {
type: 'primaryColor' | 'secondaryColor';
};
const ColorSelector = ({ type }: ColorSelectorProps) => {
const { t } = useTranslation('boards/customize');
const theme = useMantineTheme();
const form = useBoardCustomizationFormContext();
const { setPrimaryColor, setSecondaryColor } = useColorTheme();
const colors = Object.keys(theme.colors).map((color) => ({
swatch: theme.colors[color][6],
color,
}));
return (
<Input.Wrapper label={t(`settings.appearance.${type}`)}>
<Group>
{colors.map(({ color, swatch }) => (
<ColorSwatch
key={color}
component="button"
type="button"
onClick={() => {
form.getInputProps(`appearance.${type}`).onChange(color);
if (type === 'primaryColor') {
setPrimaryColor(color);
} else {
setSecondaryColor(color);
}
}}
color={swatch}
style={{ cursor: 'pointer' }}
>
{color === form.values.appearance[type] && <CheckIcon width={rem(10)} />}
</ColorSwatch>
))}
</Group>
</Input.Wrapper>
);
};
const ShadeSelector = () => {
const form = useBoardCustomizationFormContext();
const theme = useMantineTheme();
const { setPrimaryShade } = useColorTheme();
const primaryColor = form.values.appearance.primaryColor;
const primaryShades = theme.colors[primaryColor].map((_, shade) => ({
swatch: theme.colors[primaryColor][shade],
shade,
}));
return (
<Input.Wrapper label="Shade">
<Group>
{primaryShades.map(({ shade, swatch }) => (
<ColorSwatch
key={shade}
component="button"
type="button"
onClick={() => {
form.getInputProps(`appearance.shade`).onChange(shade);
setPrimaryShade(shade as MantineTheme['primaryShade']);
}}
color={swatch}
style={{ cursor: 'pointer' }}
>
{shade === form.values.appearance.shade && <CheckIcon width={rem(10)} />}
</ColorSwatch>
))}
</Group>
</Input.Wrapper>
);
};
const OpacitySlider = () => {
const { t } = useTranslation('settings/customization/opacity-selector');
const form = useBoardCustomizationFormContext();
return (
<Input.Wrapper label={t('label')} mb="sm">
<Slider
step={10}
min={10}
marks={opacityMarks}
styles={{ markLabel: { fontSize: 'xx-small' } }}
{...form.getInputProps('appearance.opacity')}
/>
</Input.Wrapper>
);
};
const opacityMarks = [
{ value: 10, label: '10%' },
{ value: 20, label: '20%' },
{ value: 30, label: '30%' },
{ value: 40, label: '40%' },
{ value: 50, label: '50%' },
{ value: 60, label: '60%' },
{ value: 70, label: '70%' },
{ value: 80, label: '80%' },
{ value: 90, label: '90%' },
{ value: 100, label: '100%' },
];
const CustomCssInput = () => {
const { t } = useTranslation('settings/customization/page-appearance');
const { classes } = useStyles();
const form = useBoardCustomizationFormContext();
return (
<Input.Wrapper
label={t('customCSS.label')}
description={t('customCSS.description')}
inputWrapperOrder={['label', 'description', 'input', 'error']}
>
<div className={classes.codeEditorRoot}>
<Editor
{...form.getInputProps('appearance.customCss')}
onValueChange={(code) => form.getInputProps('appearance.customCss').onChange(code)}
highlight={(code) => highlight(code, languages.extend('css', {}), 'css')}
padding={10}
style={{
fontFamily: '"Fira code", "Fira Mono", monospace',
fontSize: 12,
minHeight: 250,
}}
/>
</div>
</Input.Wrapper>
);
};
const useStyles = createStyles(({ colors, colorScheme, radius }) => ({
codeEditorFooter: {
borderBottomLeftRadius: radius.sm,
borderBottomRightRadius: radius.sm,
backgroundColor: colorScheme === 'dark' ? colors.dark[7] : undefined,
},
codeEditorRoot: {
marginTop: 4,
borderColor: colorScheme === 'dark' ? colors.dark[4] : colors.gray[4],
borderWidth: 1,
borderStyle: 'solid',
borderRadius: radius.sm,
},
codeEditor: {
backgroundColor: colorScheme === 'dark' ? colors.dark[6] : 'white',
fontSize: 12,
'& ::placeholder': {
color: colorScheme === 'dark' ? colors.dark[3] : colors.gray[5],
},
},
}));