colorPickerComponent.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import * as React from "react";
  2. import { Color4, Color3 } from 'babylonjs/Maths/math.color';
  3. import { GlobalState } from '../globalState';
  4. import { ColorPicker } from './colorPicker/colorPicker';
  5. export interface IColorPickerComponentProps {
  6. value: Color4 | Color3;
  7. onColorChanged: (newOne: string) => void;
  8. globalState: GlobalState;
  9. }
  10. interface IColorPickerComponentState {
  11. pickerEnabled: boolean;
  12. color: Color3 | Color4;
  13. hex: string;
  14. }
  15. export class ColorPickerLineComponent extends React.Component<IColorPickerComponentProps, IColorPickerComponentState> {
  16. private _floatRef: React.RefObject<HTMLDivElement>;
  17. private _floatHostRef: React.RefObject<HTMLDivElement>;
  18. constructor(props: IColorPickerComponentProps) {
  19. super(props);
  20. this.state = {pickerEnabled: false, color: this.props.value, hex: this.props.value.toHexString()};
  21. this._floatRef = React.createRef();
  22. this._floatHostRef = React.createRef();
  23. }
  24. syncPositions() {
  25. const div = this._floatRef.current as HTMLDivElement;
  26. const host = this._floatHostRef.current as HTMLDivElement;
  27. if (!div || !host) {
  28. return;
  29. }
  30. let top = host.getBoundingClientRect().top;
  31. let height = div.getBoundingClientRect().height;
  32. if (top + height + 10 > window.innerHeight) {
  33. top = window.innerHeight - height - 10;
  34. }
  35. div.style.top = top + "px";
  36. div.style.left = host.getBoundingClientRect().left - div.getBoundingClientRect().width + "px";
  37. }
  38. shouldComponentUpdate(nextProps: IColorPickerComponentProps, nextState: IColorPickerComponentState) {
  39. let result = nextProps.value.toHexString() !== this.props.value.toHexString()
  40. || nextState.hex !== this.state.hex
  41. || nextState.pickerEnabled !== this.state.pickerEnabled;
  42. if (result) {
  43. nextState.color = nextProps.value;
  44. nextState.hex = nextProps.value.toHexString();
  45. }
  46. return result;
  47. }
  48. componentDidUpdate() {
  49. this.syncPositions();
  50. }
  51. componentDidMount() {
  52. this.syncPositions();
  53. }
  54. setPickerState(enabled: boolean) {
  55. this.setState({ pickerEnabled: enabled });
  56. this.props.globalState.blockKeyboardEvents = enabled;
  57. }
  58. render() {
  59. var color = this.state.color;
  60. this.props.globalState.blockKeyboardEvents = this.state.pickerEnabled;
  61. return (
  62. <div className="color-picker">
  63. <div className="color-rect" ref={this._floatHostRef}
  64. style={{background: this.state.hex}}
  65. onClick={() => this.setPickerState(true)}>
  66. </div>
  67. {
  68. this.state.pickerEnabled &&
  69. <>
  70. <div className="color-picker-cover" onClick={() => this.setPickerState(false)}></div>
  71. <div className="color-picker-float" ref={this._floatRef}>
  72. <ColorPicker color={color}
  73. onColorChanged={(color: Color3 | Color4) => {
  74. const hex = color.toHexString();
  75. this.setState({ hex, color });
  76. this.props.onColorChanged(hex);
  77. }}
  78. />
  79. </div>
  80. </>
  81. }
  82. </div>
  83. );
  84. }
  85. }