2022-07-26 00:51:55 +02:00
|
|
|
import {
|
|
|
|
|
ColorSwatch,
|
2022-12-06 21:22:37 +01:00
|
|
|
Grid,
|
2022-07-26 00:51:55 +02:00
|
|
|
Group,
|
2022-12-06 21:22:37 +01:00
|
|
|
MantineTheme,
|
2022-07-26 00:51:55 +02:00
|
|
|
Popover,
|
2022-12-06 21:22:37 +01:00
|
|
|
Stack,
|
2022-07-26 00:51:55 +02:00
|
|
|
Text,
|
|
|
|
|
useMantineTheme,
|
|
|
|
|
} from '@mantine/core';
|
2022-12-04 17:36:30 +01:00
|
|
|
import { useDisclosure } from '@mantine/hooks';
|
2022-12-06 21:22:37 +01:00
|
|
|
import { useTranslation } from 'next-i18next';
|
|
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { useConfigContext } from '../../../../config/provider';
|
|
|
|
|
import { useConfigStore } from '../../../../config/store';
|
|
|
|
|
import { useColorTheme } from '../../../../tools/color';
|
2022-06-08 15:36:54 +00:00
|
|
|
|
2022-12-04 17:36:30 +01:00
|
|
|
interface ShadeSelectorProps {
|
|
|
|
|
defaultValue: MantineTheme['primaryShade'] | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ShadeSelector({ defaultValue }: ShadeSelectorProps) {
|
2022-08-24 18:38:28 +02:00
|
|
|
const { t } = useTranslation('settings/customization/shade-selector');
|
2022-12-04 17:36:30 +01:00
|
|
|
const [shade, setShade] = useState(defaultValue);
|
|
|
|
|
const [popoverOpened, popover] = useDisclosure(false);
|
|
|
|
|
const { primaryColor, setPrimaryShade } = useColorTheme();
|
2022-06-08 15:36:54 +00:00
|
|
|
|
2022-12-04 17:36:30 +01:00
|
|
|
const { name: configName } = useConfigContext();
|
|
|
|
|
const updateConfig = useConfigStore((x) => x.updateConfig);
|
2022-06-08 15:36:54 +00:00
|
|
|
|
|
|
|
|
const theme = useMantineTheme();
|
|
|
|
|
const primaryShades = theme.colors[primaryColor].map((s, i) => ({
|
|
|
|
|
swatch: theme.colors[primaryColor][i],
|
|
|
|
|
shade: i as MantineTheme['primaryShade'],
|
|
|
|
|
}));
|
|
|
|
|
|
2022-12-04 17:36:30 +01:00
|
|
|
if (shade === undefined || !configName) return null;
|
|
|
|
|
|
|
|
|
|
const handleSelection = (shade: MantineTheme['primaryShade']) => {
|
2022-06-08 15:36:54 +00:00
|
|
|
setPrimaryShade(shade);
|
2022-12-04 17:36:30 +01:00
|
|
|
setShade(shade);
|
|
|
|
|
updateConfig(configName, (prev) => ({
|
|
|
|
|
...prev,
|
2022-06-08 15:36:54 +00:00
|
|
|
settings: {
|
2022-12-04 17:36:30 +01:00
|
|
|
...prev.settings,
|
|
|
|
|
customization: {
|
|
|
|
|
...prev.settings.customization,
|
|
|
|
|
colors: {
|
|
|
|
|
...prev.settings.customization.colors,
|
|
|
|
|
shade,
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-06-08 15:36:54 +00:00
|
|
|
},
|
2022-12-04 17:36:30 +01:00
|
|
|
}));
|
2022-06-08 15:36:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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"
|
2022-12-04 17:36:30 +01:00
|
|
|
onClick={() => handleSelection(shade)}
|
2022-07-26 00:51:55 +02:00
|
|
|
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-12-04 17:36:30 +01:00
|
|
|
opened={popoverOpened}
|
|
|
|
|
onClose={popover.close}
|
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"
|
2022-12-04 17:36:30 +01:00
|
|
|
color={theme.colors[primaryColor][Number(shade)]}
|
|
|
|
|
onClick={popover.toggle}
|
2022-06-08 15:36:54 +00:00
|
|
|
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}
|
|
|
|
|
</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>
|
|
|
|
|
);
|
|
|
|
|
}
|