Skip to content

Commit

Permalink
feat: added recommended colors array
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme Monte authored and gmonte committed Feb 5, 2023
1 parent 5a7ae27 commit be0a19a
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 10 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface InputColorProps {
placement?: string;
onChange?(color: Color): void;
disabled?: boolean;
recommendedColors?: string[];
}

export default function InputColor(props: InputColorProps);
36 changes: 30 additions & 6 deletions src/color-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import {
rgba,
hsv2rgb,
} from '@swiftcarrot/color-fns';
import { rgba2hex } from './utils';
import { rgba2hex, parseColor, onlyUnique } from './utils';
import ColorSquare from './color-square'

const KEY_ENTER = 13;

const ColorPicker = ({ color, onChange, disabled }) => {
const ColorPicker = ({ color, onChange, disabled, recommendedColors }) => {
const { r, g, b, a, h, s, v } = color;

function changeColor(color) {
Expand Down Expand Up @@ -70,6 +71,20 @@ const ColorPicker = ({ color, onChange, disabled }) => {

return (
<div css={styles.picker} onClick={handleClick}>

{!!recommendedColors.length && (
<div css={styles.recommendedColors}>
{recommendedColors.filter(onlyUnique).map((recommendedColor) => (
<ColorSquare
key={recommendedColor}
color={recommendedColor}
onClick={() => !disabled && onChange(parseColor(recommendedColor))}
disabled={disabled}
/>
))}
</div>
)}

<div css={styles.selector} style={{ backgroundColor: hueBackground }}>
<div css={styles.gradientWhite} />
<div css={styles.gradientDark} />
Expand Down Expand Up @@ -153,9 +168,7 @@ const ColorPicker = ({ color, onChange, disabled }) => {
disabled={disabled}
/>
</div>
<div
style={{ backgroundColor: rgbaBackground, width: 30, height: 30 }}
/>
<ColorSquare color={rgbaBackground} />
</div>

<div css={styles.inputs}>
Expand Down Expand Up @@ -219,7 +232,8 @@ const ColorPicker = ({ color, onChange, disabled }) => {

ColorPicker.defaultProps = {
initialValue: '#5e72e4',
disabled: false
disabled: false,
recommendedColors: []
};

const styles = {
Expand Down Expand Up @@ -278,6 +292,16 @@ const styles = {
marginTop: 4,
},
},

recommendedColors: {
width: '100%',
display: 'flex',
flexWrap: 'wrap',
alignItems: 'center',
paddingBottom: 10,
marginBottom: 10,
borderBottom: '1px solid #e9e9e9'
}
};

export default ColorPicker;
38 changes: 38 additions & 0 deletions src/color-square.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/** @jsx jsx */
import { jsx } from '@emotion/core';

const ColorSquare = ({color, onClick, disabled}) => {
return (
<div
onClick={onClick}
css={{...styles.square, ...(onClick && styles.button)}}
style={{ cursor: (onClick && !disabled) ? 'pointer' : 'normal' }}
>
<div css={styles.content} style={{ backgroundColor: color }} />
</div>
);
};

ColorSquare.defaultProps = {
onClick: undefined,
disabled: false,
recommendedColors: []
};

const styles = {
button: {
padding: 3,
border: '1px solid #bebebe',
borderRadius: 4,
},
square: {
display: 'flex',
width: 30,
height: 30,
},
content: {
flex: 1,
}
};

export default ColorSquare;
14 changes: 11 additions & 3 deletions src/input-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Popover from '@xkit/popover';
import ColorPicker from './color-picker';
import { parseColor } from './utils';

const InputColor = ({ initialValue, onChange, placement, disabled, ...props }) => {
const InputColor = ({ initialValue, onChange, placement, disabled, recommendedColors, ...props }) => {
const [color, setColor] = useState(parseColor(initialValue));

useEffect(() => {
Expand All @@ -22,7 +22,14 @@ const InputColor = ({ initialValue, onChange, placement, disabled, ...props }) =
return (
<Popover
placement={placement}
body={<ColorPicker color={color} onChange={changeColor} disabled={ disabled } />}
body={(
<ColorPicker
color={color}
onChange={changeColor}
disabled={disabled}
recommendedColors={recommendedColors}
/>
)}
>
<span
{...props}
Expand Down Expand Up @@ -55,7 +62,8 @@ const InputColor = ({ initialValue, onChange, placement, disabled, ...props }) =

InputColor.defaultProps = {
placement: 'bottom',
disabled: false
disabled: false,
recommendedColors: []
};

export default InputColor;
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ export {
rgba,
hsv2rgb,
} from '@swiftcarrot/color-fns';

export function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
6 changes: 5 additions & 1 deletion stories/InputColor.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export const Demo = () => {
onChange={(e) => setInitial(e.target.value)}
/>
<br />
<InputColor initialValue={initial} onChange={handleChange} />
<InputColor
initialValue={initial}
onChange={handleChange}
recommendedColors={['#000', '#babaca', '#3a1596', '#5e72e4', '#Fa2c4a', '#b24acf']}
/>
</div>
);
};
Expand Down

0 comments on commit be0a19a

Please sign in to comment.