import * as React from "react"; import { Color4, Color3 } from 'babylonjs/Maths/math.color'; import { ColorPicker } from '../colorPicker/colorPicker'; export interface IColorPickerComponentProps { value: Color4 | Color3; onColorChanged: (newOne: string) => void; } interface IColorPickerComponentState { pickerEnabled: boolean; color: Color3 | Color4; hex: string; } export class ColorPickerLineComponent extends React.Component { private _floatRef: React.RefObject; private _floatHostRef: React.RefObject; constructor(props: IColorPickerComponentProps) { super(props); this.state = {pickerEnabled: false, color: this.props.value, hex: this.props.value.toHexString()}; this._floatRef = React.createRef(); this._floatHostRef = React.createRef(); } syncPositions() { const div = this._floatRef.current as HTMLDivElement; const host = this._floatHostRef.current as HTMLDivElement; if (!div || !host) { return; } let top = host.getBoundingClientRect().top; let height = div.getBoundingClientRect().height; if (top + height + 10 > window.innerHeight) { top = window.innerHeight - height - 10; } div.style.top = top + "px"; div.style.left = host.getBoundingClientRect().left - div.getBoundingClientRect().width + "px"; } shouldComponentUpdate(nextProps: IColorPickerComponentProps, nextState: IColorPickerComponentState) { let diffProps = nextProps.value.toHexString() !== this.props.value.toHexString(); if (diffProps) { nextState.color = nextProps.value; nextState.hex = nextProps.value.toHexString(); } return diffProps || nextState.hex !== this.state.hex || nextState.pickerEnabled !== this.state.pickerEnabled; } componentDidUpdate() { this.syncPositions(); } componentDidMount() { this.syncPositions(); } render() { var color = this.state.color; return (
this.setState({pickerEnabled: true})}>
{ this.state.pickerEnabled && <>
{ if (evt.target !== this._floatRef.current?.ownerDocument?.querySelector(".color-picker-cover")) { return; } this.setState({pickerEnabled: false}); }}>
{ const hex: string = color.toHexString(); this.setState({ hex, color }); this.props.onColorChanged(hex); }} />
}
); } }