2022-12-17 17:02:02 -08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-10-25 14:56:25 -07:00
|
|
|
|
2022-12-17 17:02:02 -08:00
|
|
|
import { HStack, Stack, Slider } from 'soapbox/components/ui';
|
|
|
|
import { usePrevious } from 'soapbox/hooks';
|
2022-12-16 17:22:59 -08:00
|
|
|
import { compareId } from 'soapbox/utils/comparators';
|
2022-12-17 17:02:02 -08:00
|
|
|
import { hueShift } from 'soapbox/utils/theme';
|
2022-10-25 15:47:24 -07:00
|
|
|
|
|
|
|
import Color from './color';
|
2022-10-25 14:56:25 -07:00
|
|
|
|
|
|
|
interface ColorGroup {
|
|
|
|
[tint: string]: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IPalette {
|
|
|
|
palette: ColorGroup,
|
2022-10-25 15:47:24 -07:00
|
|
|
onChange: (palette: ColorGroup) => void,
|
2022-12-17 17:39:54 -08:00
|
|
|
resetKey?: string,
|
2022-10-25 14:56:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Editable color palette. */
|
2022-12-17 17:39:54 -08:00
|
|
|
const Palette: React.FC<IPalette> = ({ palette, onChange, resetKey }) => {
|
2022-10-25 14:56:25 -07:00
|
|
|
const tints = Object.keys(palette).sort(compareId);
|
|
|
|
|
2022-12-17 17:02:02 -08:00
|
|
|
const [hue, setHue] = useState(0);
|
|
|
|
const lastHue = usePrevious(hue);
|
|
|
|
|
2022-10-25 15:47:24 -07:00
|
|
|
const handleChange = (tint: string) => {
|
|
|
|
return (color: string) => {
|
|
|
|
onChange({
|
|
|
|
...palette,
|
|
|
|
[tint]: color,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
2022-10-25 14:56:25 -07:00
|
|
|
|
2022-12-17 17:02:02 -08:00
|
|
|
useEffect(() => {
|
|
|
|
const delta = hue - (lastHue || 0);
|
|
|
|
|
|
|
|
const adjusted = Object.entries(palette).reduce<ColorGroup>((result, [tint, hex]) => {
|
|
|
|
result[tint] = hueShift(hex, delta * 360);
|
|
|
|
return result;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
onChange(adjusted);
|
|
|
|
}, [hue]);
|
|
|
|
|
2022-12-17 17:39:54 -08:00
|
|
|
useEffect(() => {
|
|
|
|
setHue(0);
|
|
|
|
}, [resetKey]);
|
|
|
|
|
2022-10-25 14:56:25 -07:00
|
|
|
return (
|
2022-12-17 17:02:02 -08:00
|
|
|
<Stack className='w-full'>
|
|
|
|
<HStack className='h-8 rounded-md overflow-hidden'>
|
|
|
|
{tints.map(tint => (
|
|
|
|
<Color color={palette[tint]} onChange={handleChange(tint)} />
|
|
|
|
))}
|
|
|
|
</HStack>
|
|
|
|
|
|
|
|
<Slider value={hue} onChange={setHue} />
|
|
|
|
</Stack>
|
2022-10-25 14:56:25 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
Palette as default,
|
|
|
|
ColorGroup,
|
|
|
|
};
|