import React from 'react' import shallowCompare from 'react-addons-shallow-compare' import ImagePicker from './ImagePicker' import ColorPicker from './ColorPicker' import Popout from './Popout' import { COLORS, DEFAULT_BG_COLOR } from '../lib/constants' import { validateColor } from '../lib/colors' import { toggle, capitalize, stringifyRGBA } from '../lib/util' class BackgroundSelect extends React.Component { state = { isVisible: false, mounted: false } componentDidMount() { this.setState({ mounted: true }) } shouldComponentUpdate(prevProps, prevState) { return [ prevState.isVisible !== this.state.isVisible, prevProps.color !== this.props.color, prevState.isVisible && shallowCompare(this, prevProps, prevState) ].some(Boolean) } toggle = e => { e.stopPropagation() this.setState(toggle('isVisible')) } selectTab = name => { if (this.props.mode !== name) { this.props.onChange({ backgroundMode: name }) } } handlePickColor = ({ rgb }) => this.props.onChange({ backgroundColor: stringifyRGBA(rgb) }) render() { const { color, mode, image, onChange, aspectRatio } = this.props const { isVisible, mounted } = this.state let background = typeof color === 'string' ? color .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, ''') .replace(/\//g, '/') : color if (!validateColor(background)) { background = DEFAULT_BG_COLOR } return (