Files
outline/app/components/InputColor.tsx
T

41 lines
1.1 KiB
TypeScript

import * as React from "react";
import styled from "styled-components";
import Input, { Props as InputProps } from "./Input";
import Relative from "./Sidebar/components/Relative";
import { SwatchButton } from "./SwatchButton";
/**
* Props for the InputColor component.
*/
type Props = Omit<InputProps, "onChange"> & {
/** The current color value in hex format */
value: string | undefined;
/** Callback function invoked when the color value changes */
onChange: (value: string) => void;
};
/**
* A color input component that combines a text input with a color picker swatch button.
* Automatically formats hex color values with a leading # character.
*/
const InputColor: React.FC<Props> = ({ value, onChange, ...rest }: Props) => (
<Relative>
<Input
value={value}
onChange={(event) => onChange(event.target.value.replace(/^#?/, "#"))}
placeholder="#"
maxLength={7}
{...rest}
/>
<PositionedSwatchButton color={value} onChange={onChange} />
</Relative>
);
const PositionedSwatchButton = styled(SwatchButton)`
position: absolute;
bottom: 20px;
right: 6px;
`;
export default InputColor;