colorPickerComponent.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import * as React from "react";
  2. import { Color4, Color3 } from 'babylonjs/Maths/math.color';
  3. import { ColorPicker } from '../colorPicker/colorPicker';
  4. export interface IColorPickerComponentProps {
  5. value: Color4 | Color3;
  6. onColorChanged: (newOne: string) => void;
  7. }
  8. interface IColorPickerComponentState {
  9. pickerEnabled: boolean;
  10. color: Color3 | Color4;
  11. hex: string;
  12. }
  13. export class ColorPickerLineComponent extends React.Component<IColorPickerComponentProps, IColorPickerComponentState> {
  14. private _floatRef: React.RefObject<HTMLDivElement>;
  15. private _floatHostRef: React.RefObject<HTMLDivElement>;
  16. constructor(props: IColorPickerComponentProps) {
  17. super(props);
  18. this.state = {pickerEnabled: false,
  19. color: this.props.value,
  20. 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 diffProps = nextProps.value.toHexString() !== this.props.value.toHexString();
  40. if (diffProps) {
  41. nextState.color = nextProps.value;
  42. nextState.hex = nextProps.value.toHexString();
  43. }
  44. return diffProps
  45. || nextState.hex !== this.state.hex
  46. || nextState.pickerEnabled !== this.state.pickerEnabled;
  47. }
  48. componentDidUpdate() {
  49. this.syncPositions();
  50. }
  51. componentDidMount() {
  52. this.syncPositions();
  53. }
  54. render() {
  55. var color = this.state.color;
  56. return (
  57. <div className="color-picker">
  58. <div className="color-rect" ref={this._floatHostRef}
  59. style={{background: this.state.hex}}
  60. onClick={() => this.setState({pickerEnabled: true})}>
  61. </div>
  62. {
  63. this.state.pickerEnabled &&
  64. <>
  65. <div className="color-picker-cover" onClick={(evt) => {
  66. if (evt.target !== this._floatRef.current?.ownerDocument?.querySelector(".color-picker-cover")) {
  67. return;
  68. }
  69. this.setState({pickerEnabled: false});
  70. }}>
  71. <div className="color-picker-float" ref={this._floatRef}>
  72. <ColorPicker color={color}
  73. onColorChanged={(color: Color3 | Color4) => {
  74. const hex: string = color.toHexString();
  75. this.setState({ hex, color });
  76. this.props.onColorChanged(hex);
  77. }}
  78. />
  79. </div>
  80. </div>
  81. </>
  82. }
  83. </div>
  84. );
  85. }
  86. }