Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor <ColorPalette /> #154

Merged
merged 1 commit into from
Nov 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 83 additions & 100 deletions packages/styleguide/src/components/ColorPalette/ColorPalette.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,22 @@
import React from 'react';
import React, { useState } from 'react';
import { string, object } from 'prop-types';
import styled from 'styled-components';

import Swatch from './Swatch';

const defaultShade = '500';

export default class ColorPalette extends React.Component {
static displayName = 'ColorPalette';
const ColorPalette = ({ children, name, color, ...other }) => {
const [currentShade, setCurrentShade] = useState(getDefaultShade());

static propTypes = {
/**
* Color object could have these two shapes:
* just one color
* ```js
{
type: 'SassColor',
value: {
hex: '#ff5722'
}
}
* ```
* or multiple shades with shade number as key
* ```js
{
type: 'SassMap',
value: {
'300': {
type: 'SassColor',
value: {
hex: '#ff6b6b'
}
},
'500': {
type: 'SassColor',
value: {
hex: '#fa5252'
}
}
}
}
* ```
*
*/
color: object,
/** Name of the color */
name: string,
};

state = {
currentShade: null,
};

componentWillMount() {
this.setState({
currentShade: this.getDefaultShade(),
});
}

getSwatches() {
const { color } = this.props;
function getSwatches() {
if (color && color.type === 'SassMap') {
return color.value;
}
return [];
}

getDefaultShade() {
const { color } = this.props;

function getDefaultShade() {
if (color && color.type === 'SassMap') {
let shade = defaultShade;
if (!Object.prototype.hasOwnProperty.call(color.value, defaultShade)) {
Expand All @@ -80,59 +28,94 @@ export default class ColorPalette extends React.Component {
return null;
}

getBackroundColor(shade = this.state.currentShade) {
const { color } = this.props;

function getBackroundColor(shade = currentShade) {
if (shade) {
return color.value[shade].value.hex;
}
return color.value.hex;
}

handleColorChange(e, shade) {
this.setState({ currentShade: shade });
function handleColorChange(e, shade) {
setCurrentShade(shade);
}

render() {
const { children, name, color, ...other } = this.props;

const colorSwatches = this.getSwatches();

const swatches = Object.entries(colorSwatches).map(([shade]) => (
<Swatch
key={shade}
shade={shade}
color={this.getBackroundColor(shade)}
isActive={shade === this.state.currentShade}
onClick={e => this.handleColorChange(e, shade)}
/>
));

return (
<StyledColorPalette
{...other}
style={{ backgroundColor: this.getBackroundColor() }}
>
<StyledColorInfo>
{name}{' '}
{swatches.length > 0
? `${this.state.currentShade} ${
color.value[this.state.currentShade].value.hex
}`
: color.value.hex}
</StyledColorInfo>

{swatches.length > 0 && (
<StyledSwatches>
<StyledSwatchSpacer />
{swatches}
<StyledSwatchSpacer />
</StyledSwatches>
)}
</StyledColorPalette>
);
const colorSwatches = getSwatches();

const swatches = Object.entries(colorSwatches).map(([shade]) => (
<Swatch
key={shade}
shade={shade}
color={getBackroundColor(shade)}
isActive={shade === currentShade}
onClick={e => handleColorChange(e, shade)}
/>
));

return (
<StyledColorPalette
{...other}
style={{ backgroundColor: getBackroundColor() }}
>
<StyledColorInfo>
{name}{' '}
{swatches.length > 0
? `${currentShade} ${color.value[currentShade].value.hex}`
: color.value.hex}
</StyledColorInfo>

{swatches.length > 0 && (
<StyledSwatches>
<StyledSwatchSpacer />
{swatches}
<StyledSwatchSpacer />
</StyledSwatches>
)}
</StyledColorPalette>
);
};

ColorPalette.displayName = 'ColorPalette';

ColorPalette.propTypes = {
/**
* Color object could have these two shapes:
* just one color
* ```js
{
type: 'SassColor',
value: {
hex: '#ff5722'
}
}
* ```
* or multiple shades with shade number as key
* ```js
{
type: 'SassMap',
value: {
'300': {
type: 'SassColor',
value: {
hex: '#ff6b6b'
}
},
'500': {
type: 'SassColor',
value: {
hex: '#fa5252'
}
}
}
}
* ```
*
*/
color: object,
/** Name of the color */
name: string,
};

export default ColorPalette;

const StyledColorPalette = styled.div`
position: relative;
Expand Down