import { createElement, Fragment, Component } from '@wordpress/element' import classnames from 'classnames' import ResponsiveControls, { maybePromoteScalarValueIntoResponsive, isOptionEnabledFor } from '../customizer/components/responsive-controls' import deepEqual from 'deep-equal' const CORE_OPTIONS_CONTEXT = require.context('./options/', true, /\.js$/) const hasCoreOptionModifier = type => { let index = CORE_OPTIONS_CONTEXT.keys() .map(module => module.replace(/^\.\//, '').replace(/\.js$/, '')) .indexOf(type) return index > -1 && CORE_OPTIONS_CONTEXT.keys()[index] } const capitalizeFirstLetter = str => { str = str == null ? '' : String(str) return str.charAt(0).toUpperCase() + str.slice(1) } export const getOptionFor = option => { if (hasCoreOptionModifier(option.type)) { return CORE_OPTIONS_CONTEXT(hasCoreOptionModifier(option.type)).default } return null } class GenericOptionType extends Component { state = { device: wp.customize && wp.customize.previewedDevice ? wp.customize.previewedDevice() : 'desktop' } listener = () => this.setState({ device: wp.customize && wp.customize.previewedDevice ? wp.customize.previewedDevice() : 'desktop' }) componentDidMount() { if (!this.props.option.responsive) return if (!wp.customize) return setTimeout(() => wp.customize.previewedDevice.bind(this.listener), 1000) } componentWillUnmount() { if (!this.props.option.responsive) return if (!wp.customize) return wp.customize.previewedDevice.unbind(this.listener) } setDevice(device) { this.setState({ device }) wp.customize && wp.customize.previewedDevice.set(device) } render() { let { value, values, onChange, option, id, purpose } = this.props let OptionComponent = getOptionFor(option) const valueWithResponsive = option.responsive ? maybePromoteScalarValueIntoResponsive(value)[this.state.device] : value const onChangeWithMobileBridge = value => { if ( option.switchDeviceOnChange && wp.customize && wp.customize.previewedDevice() !== option.switchDeviceOnChange ) { wp.customize.previewedDevice.set(option.switchDeviceOnChange) } onChange(value) } const onChangeWithResponsiveBridge = scalarValue => { onChangeWithMobileBridge( option.responsive ? { ...maybePromoteScalarValueIntoResponsive(value), [this.state.device]: scalarValue } : scalarValue ) } /** * Handle transparent components */ if (!OptionComponent) { return