|
| 1 | +import React from 'react'; |
| 2 | +import propTypes from 'prop-types'; |
| 3 | + |
| 4 | +import styled, { css } from 'styled-components'; |
| 5 | +import useControlledOrUncontrolled from '../common/hooks/useControlledOrUncontrolled'; |
| 6 | +import Button from '../Button/Button'; |
| 7 | +import Bar from '../Bar/Bar'; |
| 8 | + |
| 9 | +const StyledBar = styled(Bar)` |
| 10 | + height: 23px; |
| 11 | + position: relative; |
| 12 | + top: -1px; |
| 13 | +`; |
| 14 | + |
| 15 | +export const StyledColorInput = styled.input` |
| 16 | + box-sizing: border-box; |
| 17 | + width: 100%; |
| 18 | + height: 100%; |
| 19 | + position: absolute; |
| 20 | + left: 0; |
| 21 | + top: 0; |
| 22 | + opacity: 0; |
| 23 | +`; |
| 24 | + |
| 25 | +// TODO replace with SVG icon |
| 26 | +const ColorPreview = styled.div` |
| 27 | + box-sizing: border-box; |
| 28 | + height: 21px; |
| 29 | + display: inline-block; |
| 30 | + width: 35px; |
| 31 | + margin-right: 0.5rem; |
| 32 | +
|
| 33 | + background: ${({ color }) => color}; |
| 34 | +
|
| 35 | + ${({ isDisabled }) => |
| 36 | + isDisabled |
| 37 | + ? css` |
| 38 | + border: 2px solid ${({ theme }) => theme.textDisabled}; |
| 39 | + filter: drop-shadow( |
| 40 | + 1px 1px 0px ${({ theme }) => theme.textDisabledShadow} |
| 41 | + ); |
| 42 | + ` |
| 43 | + : css` |
| 44 | + border: 2px solid ${({ theme }) => theme.text}; |
| 45 | + `} |
| 46 | +`; |
| 47 | + |
| 48 | +const ChevronIcon = styled.span` |
| 49 | + position: relative; |
| 50 | + width: 0px; |
| 51 | + height: 0px; |
| 52 | + border-left: 6px solid transparent; |
| 53 | + border-right: 6px solid transparent; |
| 54 | + display: inline-block; |
| 55 | + margin-left: 0.5rem; |
| 56 | +
|
| 57 | + ${({ isDisabled }) => |
| 58 | + isDisabled |
| 59 | + ? css` |
| 60 | + border-top: 6px solid ${({ theme }) => theme.textDisabled}; |
| 61 | + filter: drop-shadow( |
| 62 | + 1px 1px 0px ${({ theme }) => theme.textDisabledShadow} |
| 63 | + ); |
| 64 | + ` |
| 65 | + : css` |
| 66 | + border-top: 6px solid ${({ theme }) => theme.text}; |
| 67 | + `} |
| 68 | +`; |
| 69 | + |
| 70 | +// TODO make sure all aria and role attributes are in place |
| 71 | +const ColorInput = React.forwardRef(function ColorInput(props, ref) { |
| 72 | + const { |
| 73 | + value, |
| 74 | + defaultValue, |
| 75 | + onChange, |
| 76 | + disabled, |
| 77 | + variant, |
| 78 | + ...otherProps |
| 79 | + } = props; |
| 80 | + |
| 81 | + const [valueDerived, setValueState] = useControlledOrUncontrolled({ |
| 82 | + value, |
| 83 | + defaultValue |
| 84 | + }); |
| 85 | + |
| 86 | + const handleChange = e => { |
| 87 | + const color = e.target.value; |
| 88 | + setValueState(color); |
| 89 | + if (onChange) { |
| 90 | + onChange(e); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + return ( |
| 95 | + // we only need button styles, so we display it as a div and reset type attribute |
| 96 | + <Button isDisabled={disabled} as='div' type={null} variant={variant}> |
| 97 | + <ColorPreview |
| 98 | + color={valueDerived} |
| 99 | + isDisabled={disabled} |
| 100 | + role='presentation' |
| 101 | + /> |
| 102 | + {variant === 'default' && <StyledBar />} |
| 103 | + <StyledColorInput |
| 104 | + onChange={handleChange} |
| 105 | + readOnly={disabled} |
| 106 | + disabled={disabled} |
| 107 | + value={valueDerived || '#008080'} |
| 108 | + type='color' |
| 109 | + ref={ref} |
| 110 | + {...otherProps} |
| 111 | + /> |
| 112 | + <ChevronIcon isDisabled={disabled} /> |
| 113 | + </Button> |
| 114 | + ); |
| 115 | +}); |
| 116 | + |
| 117 | +ColorInput.defaultProps = { |
| 118 | + value: undefined, |
| 119 | + defaultValue: undefined, |
| 120 | + disabled: false, |
| 121 | + variant: 'default', |
| 122 | + onChange: () => {} |
| 123 | +}; |
| 124 | + |
| 125 | +ColorInput.propTypes = { |
| 126 | + value: propTypes.string, |
| 127 | + defaultValue: propTypes.string, |
| 128 | + onChange: propTypes.func, |
| 129 | + disabled: propTypes.bool, |
| 130 | + variant: propTypes.oneOf(['default', 'flat']) |
| 131 | +}; |
| 132 | +export default ColorInput; |
0 commit comments