revite/src/components/ui/ColourSwatches.tsx

131 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-06-18 10:57:08 -04:00
import { useRef } from "preact/hooks";
import { Check } from "@styled-icons/feather";
import styled, { css } from "styled-components";
import { Pencil } from "@styled-icons/bootstrap";
2021-06-18 09:20:57 -04:00
interface Props {
value: string;
onChange: (value: string) => void;
}
const presets = [
[
"#7B68EE",
"#3498DB",
"#1ABC9C",
"#F1C40F",
"#FF7F50",
"#FD6671",
"#E91E63",
2021-06-18 10:57:08 -04:00
"#D468EE",
2021-06-18 09:20:57 -04:00
],
[
"#594CAD",
"#206694",
"#11806A",
"#C27C0E",
"#CD5B45",
"#FF424F",
"#AD1457",
2021-06-18 10:57:08 -04:00
"#954AA8",
],
2021-06-18 09:20:57 -04:00
];
const SwatchesBase = styled.div`
gap: 8px;
display: flex;
input {
opacity: 0;
margin-top: 44px;
position: absolute;
pointer-events: none;
}
`;
2021-06-18 10:57:08 -04:00
const Swatch = styled.div<{ type: "small" | "large"; colour: string }>`
2021-06-18 09:20:57 -04:00
flex-shrink: 0;
cursor: pointer;
border-radius: 4px;
2021-06-18 10:57:08 -04:00
background-color: ${(props) => props.colour};
2021-06-18 09:20:57 -04:00
display: grid;
place-items: center;
&:hover {
border: 3px solid var(--foreground);
2021-06-18 10:57:08 -04:00
transition: border ease-in-out 0.07s;
2021-06-18 09:20:57 -04:00
}
svg {
color: white;
}
2021-06-18 10:57:08 -04:00
${(props) =>
props.type === "small"
? css`
width: 30px;
height: 30px;
2021-06-18 09:20:57 -04:00
2021-06-18 10:57:08 -04:00
svg {
stroke-width: 2;
}
`
: css`
width: 68px;
height: 68px;
`}
2021-06-18 09:20:57 -04:00
`;
const Rows = styled.div`
gap: 8px;
display: flex;
flex-direction: column;
> div {
gap: 8px;
display: flex;
flex-direction: row;
}
`;
2021-06-18 10:18:10 -04:00
export default function ColourSwatches({ value, onChange }: Props) {
2021-06-18 09:20:57 -04:00
const ref = useRef<HTMLInputElement>();
return (
<SwatchesBase>
2021-06-18 10:57:08 -04:00
<Swatch
colour={value}
type="large"
onClick={() => ref.current.click()}
>
2021-06-18 09:20:57 -04:00
<Pencil size={32} />
</Swatch>
<input
type="color"
value={value}
ref={ref}
2021-06-18 10:57:08 -04:00
onChange={(ev) => onChange(ev.currentTarget.value)}
2021-06-18 09:20:57 -04:00
/>
<Rows>
2021-06-18 10:46:30 -04:00
{presets.map((row, i) => (
<div key={i}>
2021-06-18 10:57:08 -04:00
{row.map((swatch, i) => (
<Swatch
colour={swatch}
type="small"
key={i}
onClick={() => onChange(swatch)}
>
{swatch === value && (
<Check size={18} strokeWidth={2} />
)}
2021-06-18 09:20:57 -04:00
</Swatch>
2021-06-18 10:57:08 -04:00
))}
2021-06-18 09:20:57 -04:00
</div>
))}
</Rows>
</SwatchesBase>
2021-06-18 10:57:08 -04:00
);
2021-06-18 09:20:57 -04:00
}