## Feature Examples

### Structure
- description: The component consists of a color spectrum canvas, an input area, and a `children` area. Set the initial color using the `value` prop. The `children` area accepts any content, typically used for color swatches.
- example:
```jsx
<ColorPicker value="#3899EC">
  <StorybookComponents.Placeholder>
    Any required content
  </StorybookComponents.Placeholder>
</ColorPicker>;
```

### Removing padding
- description: By default, the component includes padding designed for popover use. Remove the padding to embed the color picker inside other layouts, such as modals, panels, or cards, by setting the `removePadding` prop to true.
- example:
```jsx
<StorybookComponents.Stack>
  <StorybookComponents.Stack>
    <ColorPicker value="#3899EC" />
  </StorybookComponents.Stack>
  <StorybookComponents.Stack height="100%">
    <ColorPicker value="#3899EC" removePadding />
  </StorybookComponents.Stack>
</StorybookComponents.Stack>;
```

### Color converter
- description: Enables to view and edit color codes in HEX/RGB/HSB formats. Converter is visible by default and can be hidden on demand by setting the `showConverter` to false.
- example:
```jsx
<StorybookComponents.Stack>
  <StorybookComponents.Stack>
    <ColorPicker value="#3899EC" />
  </StorybookComponents.Stack>
  <StorybookComponents.Stack height="100%">
    <ColorPicker value="#3899EC" showConverter={false} />
  </StorybookComponents.Stack>
</StorybookComponents.Stack>;
```

### Hide bottom actions
- description: Bottom actions are shown by default. To hide them for specific use cases, set the `showActionButtons` prop to false.
- example:
```jsx
<StorybookComponents.Stack>
  <StorybookComponents.Stack>
    <ColorPicker value="#3899EC" />
  </StorybookComponents.Stack>
  <StorybookComponents.Stack height="100%">
    <ColorPicker value="#3899EC" showActionButtons={false} />
  </StorybookComponents.Stack>
</StorybookComponents.Stack>;
```

### Color dropper
- description: Enable the eye dropper tool using the `showEyeDropper` prop. When used together with the color converter, the eye dropper appears next to it. If `showConverter` is false, the eye dropper displays inline with the input area. Use `eyeDropperLabel` prop to customize the eye dropper button label if needed.
- example:
```jsx
<StorybookComponents.Stack>
  <StorybookComponents.Stack>
    <ColorPicker value="#3899EC" showEyeDropper />
  </StorybookComponents.Stack>
  <StorybookComponents.Stack height="100%">
    <ColorPicker
      value="#3899EC"
      showEyeDropper
      showConverter={false}
      eyeDropperLabel="Pick a color"
    />
  </StorybookComponents.Stack>
</StorybookComponents.Stack>;
```

### Hide color value input
- description: To restrict color selection to visual picking only, hide the color value input by setting `showInput` to false. The `showConverter` prop must also be set to false.
- example:
```jsx
<ColorPicker value="#3899EC" showConverter={false} showInput={false} />;
```

### History bar
- description: Compare the currently active color with a newly picked one before confirming a change. Enable this by using the `showHistory` prop.
- example:
```jsx
<ColorPicker value="#3899EC" showHistory={true} />;
```

### Accept empty value
- description: Allow to submit an empty value with `allowEmpty` prop. Use for non-mandatory color selection. Use the `emptyPlaceholder` prop to define the placeholder message shown in HEX value input.
- example:
```jsx
<ColorPicker
  value=""
  allowEmpty={true}
  emptyPlaceholder={'No color picked'}
/>;
```

### Add color presets
- description: Enable users to build a custom palette by saving frequently used colors. An add button lets users add the current color to the swatches list. Pass the `<Swatches/>` component to children and use the `onAdd` property to handle adding new swatches.
- example:
```jsx
() => {
  const [value, setValue] = React.useState('#3899EC');
  const [presets, setPresets] = React.useState([
    'midnightblue',
    'darkmagenta',
    'lightcoral',
  ]);
  return (
    <ColorPicker
      value={value}
      onAdd={(color) => setPresets([...presets, color])}
      addTooltipContent="Add color"
    >
      <Swatches colors={presets} onClick={setValue} />
    </ColorPicker>
  );
};
```

## Developer Examples

### Triggering the color picker
- description: Call out the picker popover on any component click, for example `<Multiselect/>`.
- example:
```jsx
() => {
  const [colors, setColors] = React.useState([
    { id: '1', color: 'red' },
    { id: '2', color: 'blue' },
    { id: '3', color: 'green' },
  ]);
  const updateColors = ({ id, color }) =>
    setColors(
      colors.map((item) => (item.id === id ? { ...item, color } : item)),
    );
  const TagThumb = ({ color }) => (
    <Box
      backgroundColor={color}
      marginTop="24%"
      marginLeft="24%"
      borderRadius="50%"
      height="50%"
      width="50%"
    />
  );
  const TagPopover = (props) => {
    const [color, setColor] = React.useState(props.color);
    const [shown, setShown] = React.useState(false);
    return (
      <Popover
        showArrow
        shown={shown}
        appendTo="window"
        onClick={() => setShown(!shown)}
        onClickOutside={() => setShown(false)}
      >
        <Popover.Element>
          <Text>{color}</Text>
        </Popover.Element>
        <Popover.Content>
          <ColorPicker
            value={color}
            onCancel={() => setShown(!shown)}
            onConfirm={(value) => {
              setShown(false);
              updateColors({ id: props.id, color: value.hex() });
              setColor(value.hex());
            }}
            onChange={() => {}}
          />
        </Popover.Content>
      </Popover>
    );
  };
  const tags = colors.map(({ color, id }) => ({
    id,
    label: <TagPopover color={color} id={id} />,
    thumb: <TagThumb color={color} />,
  }));
  return (
    <MultiSelect
      tags={tags}
      mode="select"
      placeholder="Select tags from a list"
    />
  );
};
```

### Adding color swatches
- description: Allow users to select a color from a list of presets by adding `<Swatches/>` to the `children` area above the submit actions.
- example:
```jsx
() => {
  const [value, setValue] = React.useState('#3899EC');
  const [colors, setColors] = React.useState([
    'midnightblue',
    'darkmagenta',
    'lightcoral',
  ]);

  return (
    <ColorPicker
      value={value}
      onChange={setValue}
      onCancel={setValue}
      onAdd={(color) => setColors([...colors, color])}
    >
      {({ changeColor }) => (
        <Swatches
          colors={colors}
          onClick={(color) => {
            changeColor(color);
          }}
        />
      )}
    </ColorPicker>
  );
};
```

## Common Use Case Examples

### Custom color selection
- description: Use `colorPickerChildren` props inside `<ColorInput/>` to let users select and input custom color values.
- example:
```jsx
() => {
  const [value, setValue] = React.useState('#3899EC');
  const [presets, setPresets] = React.useState([
    'midnightblue',
    'darkmagenta',
    'lightcoral',
  ]);
  return (
    <Box>
      <StorybookComponents.Stack flexDirection="column">
        <FormField label="Text color">
          <ColorInput
            value={value}
            onChange={setValue}
            colorPickerChildren={({ changeColor }) => (
              <Swatches colors={presets} onClick={changeColor} />
            )}
          />
        </FormField>
      </StorybookComponents.Stack>
    </Box>
  );
};
```

### Adding color to color swatch
- description: Use `<ColorPicker/>` with `<Swatches/>` to let users add new colors to a palette.
- example:
```jsx
() => {
  const [value, setValue] = React.useState(null);
  const [colors, setColors] = React.useState(['tomato', 'thistle', 'tan']);

  return (
    <FormField label="Background color">
      <Box>
        <Swatches
          showAddButton
          addButtonIconSize="small"
          addButtonMessage="Add color"
          onAdd={(color) => setColors([...colors, color])}
          colors={colors}
          onClick={setValue}
          selected={value}
          renderColorPicker={({ cancel, confirm, setColor, currentColor }) => (
            <ColorPicker
              value={currentColor}
              showEyeDropper
              showInput={true}
              showConverter={true}
              onCancel={cancel}
              onConfirm={confirm}
              onChange={setColor}
            />
          )}
        />
      </Box>
    </FormField>
  );
};
```
