import React from 'react' import enhanceWithClickOutside from 'react-click-outside' import { SketchPicker } from 'react-color' import shallowCompare from 'react-addons-shallow-compare' import WindowPointer from './WindowPointer' import ImagePicker from './ImagePicker' import { COLORS, DEFAULT_BG_COLOR } from '../lib/constants' import { validateColor } from '../lib/colors' import { parseRGBA, capitalizeFirstLetter } from '../lib/util' class BackgroundSelect extends React.Component { constructor(props) { super(props) this.state = { isVisible: false, mounted: false } this.toggle = this.toggle.bind(this) this.selectTab = this.selectTab.bind(this) this.handlePickColor = this.handlePickColor.bind(this) } componentDidMount() { this.setState({ mounted: true }) } shouldComponentUpdate(prevProps, prevState) { return ( prevState.isVisible !== this.state.isVisible || (prevState.isVisible && shallowCompare(this, prevProps, prevState)) ) } toggle() { this.setState({ isVisible: !this.state.isVisible }) } selectTab(name) { if (this.props.mode !== name) { this.props.onChange({ backgroundMode: name }) } } handleClickOutside() { this.setState({ isVisible: false }) } handlePickColor(color) { this.props.onChange({ backgroundColor: parseRGBA(color.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 (