2022-06-08 15:36:54 +00:00
|
|
|
import React, { useState } from 'react';
|
2022-07-26 00:51:55 +02:00
|
|
|
import {
|
|
|
|
|
ColorSwatch,
|
|
|
|
|
Group,
|
|
|
|
|
Popover,
|
|
|
|
|
Text,
|
|
|
|
|
useMantineTheme,
|
|
|
|
|
MantineTheme,
|
|
|
|
|
Stack,
|
|
|
|
|
Grid,
|
|
|
|
|
} from '@mantine/core';
|
2022-08-22 09:50:54 +02:00
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-06-08 15:36:54 +00:00
|
|
|
import { useConfig } from '../../tools/state';
|
|
|
|
|
import { useColorTheme } from '../../tools/color';
|
|
|
|
|
|
|
|
|
|
export function ShadeSelector() {
|
|
|
|
|
const { config, setConfig } = useConfig();
|
|
|
|
|
const [opened, setOpened] = useState(false);
|
2022-08-24 18:38:28 +02:00
|
|
|
const { t } = useTranslation('settings/customization/shade-selector');
|
2022-06-08 15:36:54 +00:00
|
|
|
|
|
|
|
|
const { primaryColor, secondaryColor, primaryShade, setPrimaryShade } = useColorTheme();
|
|
|
|
|
|
|
|
|
|
const theme = useMantineTheme();
|
|
|
|
|
const primaryShades = theme.colors[primaryColor].map((s, i) => ({
|
|
|
|
|
swatch: theme.colors[primaryColor][i],
|
|
|
|
|
shade: i as MantineTheme['primaryShade'],
|
|
|
|
|
}));
|
|
|
|
|
const secondaryShades = theme.colors[secondaryColor].map((s, i) => ({
|
|
|
|
|
swatch: theme.colors[secondaryColor][i],
|
|
|
|
|
shade: i as MantineTheme['primaryShade'],
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const setConfigShade = (shade: MantineTheme['primaryShade']) => {
|
|
|
|
|
setPrimaryShade(shade);
|
|
|
|
|
setConfig({
|
|
|
|
|
...config,
|
|
|
|
|
settings: {
|
|
|
|
|
...config.settings,
|
|
|
|
|
primaryShade: shade,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const primarySwatches = primaryShades.map(({ swatch, shade }) => (
|
2022-08-01 21:11:37 +02:00
|
|
|
<Grid.Col span={1} key={Number(shade)}>
|
2022-07-26 00:51:55 +02:00
|
|
|
<ColorSwatch
|
|
|
|
|
component="button"
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setConfigShade(shade)}
|
|
|
|
|
color={swatch}
|
|
|
|
|
size={22}
|
|
|
|
|
style={{ cursor: 'pointer' }}
|
|
|
|
|
/>
|
|
|
|
|
</Grid.Col>
|
2022-06-08 15:36:54 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
|
|
const secondarySwatches = secondaryShades.map(({ swatch, shade }) => (
|
2022-08-01 21:11:37 +02:00
|
|
|
<Grid.Col span={1} key={Number(shade)}>
|
2022-07-26 00:51:55 +02:00
|
|
|
<ColorSwatch
|
|
|
|
|
component="button"
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setConfigShade(shade)}
|
|
|
|
|
color={swatch}
|
|
|
|
|
size={22}
|
|
|
|
|
style={{ cursor: 'pointer' }}
|
|
|
|
|
/>
|
|
|
|
|
</Grid.Col>
|
2022-06-08 15:36:54 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
|
|
return (
|
2022-07-26 00:51:55 +02:00
|
|
|
<Group>
|
2022-06-08 15:36:54 +00:00
|
|
|
<Popover
|
2022-07-26 00:51:55 +02:00
|
|
|
width={350}
|
|
|
|
|
withinPortal
|
2022-06-08 15:36:54 +00:00
|
|
|
opened={opened}
|
|
|
|
|
onClose={() => setOpened(false)}
|
2022-07-26 00:51:55 +02:00
|
|
|
position="left"
|
|
|
|
|
withArrow
|
|
|
|
|
>
|
|
|
|
|
<Popover.Target>
|
2022-06-08 15:36:54 +00:00
|
|
|
<ColorSwatch
|
|
|
|
|
component="button"
|
|
|
|
|
type="button"
|
|
|
|
|
color={theme.colors[primaryColor][Number(primaryShade)]}
|
|
|
|
|
onClick={() => setOpened((o) => !o)}
|
|
|
|
|
size={22}
|
|
|
|
|
style={{ display: 'block', cursor: 'pointer' }}
|
|
|
|
|
/>
|
2022-07-26 00:51:55 +02:00
|
|
|
</Popover.Target>
|
|
|
|
|
<Popover.Dropdown>
|
|
|
|
|
<Stack spacing="xs">
|
|
|
|
|
<Grid gutter="lg" columns={10}>
|
|
|
|
|
{primarySwatches}
|
|
|
|
|
{secondarySwatches}
|
|
|
|
|
</Grid>
|
|
|
|
|
</Stack>
|
|
|
|
|
</Popover.Dropdown>
|
2022-06-08 15:36:54 +00:00
|
|
|
</Popover>
|
2022-08-22 09:50:54 +02:00
|
|
|
<Text>{t('label')}</Text>
|
2022-06-08 15:36:54 +00:00
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
}
|