ConstraintSelectionBox.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { css } from '@emotion/css';
  2. import React from 'react';
  3. import { GrafanaTheme2 } from '@grafana/data';
  4. import { useStyles2 } from '@grafana/ui';
  5. import { Constraint, HorizontalConstraint, VerticalConstraint } from 'app/features/canvas';
  6. interface Props {
  7. onVerticalConstraintChange: (v: VerticalConstraint) => void;
  8. onHorizontalConstraintChange: (h: HorizontalConstraint) => void;
  9. currentConstraints: Constraint;
  10. }
  11. export const ConstraintSelectionBox = ({
  12. onVerticalConstraintChange,
  13. onHorizontalConstraintChange,
  14. currentConstraints,
  15. }: Props) => {
  16. const styles = useStyles2(getStyles(currentConstraints));
  17. const onClickTopConstraint = () => {
  18. onVerticalConstraintChange(VerticalConstraint.Top);
  19. };
  20. const onClickBottomConstraint = () => {
  21. onVerticalConstraintChange(VerticalConstraint.Bottom);
  22. };
  23. const onClickVerticalCenterConstraint = () => {
  24. onVerticalConstraintChange(VerticalConstraint.Center);
  25. };
  26. const onClickLeftConstraint = () => {
  27. onHorizontalConstraintChange(HorizontalConstraint.Left);
  28. };
  29. const onClickRightConstraint = () => {
  30. onHorizontalConstraintChange(HorizontalConstraint.Right);
  31. };
  32. const onClickHorizontalCenterConstraint = () => {
  33. onHorizontalConstraintChange(HorizontalConstraint.Center);
  34. };
  35. return (
  36. <svg
  37. version="1.0"
  38. xmlns="http://www.w3.org/2000/svg"
  39. width="75.000000pt"
  40. height="75.000000pt"
  41. viewBox="0 0 228.000000 228.000000"
  42. preserveAspectRatio="xMidYMid meet"
  43. style={{ marginBottom: '4.8px' }}
  44. >
  45. <g transform="translate(0.000000,228.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
  46. <path
  47. fill="#e5e5e5"
  48. d="M198 2028 l-28 -32 0 -912 0 -912 31 -31 31 -31 915 0 915 0 29 29
  49. 29 29 0 917 0 917 -27 29 -28 29 -920 0 -920 0 -27 -32z m1876 -17 c15 -16 16
  50. -98 16 -927 0 -860 -1 -909 -18 -926 -17 -17 -66 -18 -927 -18 -862 0 -910 1
  51. -927 18 -17 17 -18 65 -18 926 0 832 1 911 16 927 16 18 45 19 468 21 248 2
  52. 659 2 912 0 431 -2 462 -4 478 -21z"
  53. />
  54. <rect className={styles.topConstraint} height="228" width="46" y="1735" x="1123" />
  55. <rect
  56. className={styles.constraintHover}
  57. onClick={onClickTopConstraint}
  58. height="350"
  59. width="300"
  60. y="1680"
  61. x="995"
  62. fill="transparent"
  63. />
  64. <rect className={styles.bottomConstraint} height="228" width="46" y="210" x="1123" />
  65. <rect
  66. className={styles.constraintHover}
  67. onClick={onClickBottomConstraint}
  68. height="350"
  69. width="300"
  70. y="135"
  71. x="995"
  72. fill="transparent"
  73. />
  74. <rect className={styles.leftConstraint} height="46" width="228" y="1060" x="265" />
  75. <rect
  76. className={styles.constraintHover}
  77. onClick={onClickLeftConstraint}
  78. height="300"
  79. width="350"
  80. y="925"
  81. x="200"
  82. fill="transparent"
  83. />
  84. <rect className={styles.rightConstraint} height="46" width="228" y="1060" x="1795" />
  85. <rect
  86. className={styles.constraintHover}
  87. onClick={onClickRightConstraint}
  88. height="300"
  89. width="350"
  90. y="925"
  91. x="1730"
  92. fill="transparent"
  93. />
  94. <path
  95. className={styles.box}
  96. d="M568 1669 c-17 -9 -18 -48 -18 -584 0 -558 1 -575 19 -585 27 -14
  97. 1125 -14 1152 0 18 10 19 27 19 580 0 504 -2 570 -16 584 -14 14 -80 16 -577
  98. 16 -363 -1 -568 -4 -579 -11z m1119 -42 c4 -5 4 -1079 0 -1084 -5 -4 -1079 -4
  99. -1084 0 -5 6 -4 1077 1 1085 4 7 1076 6 1083 -1z"
  100. />
  101. <rect className={styles.verticalCenterConstraint} height="456" width="46" y="855" x="1123" />
  102. <rect
  103. className={styles.constraintHover}
  104. onClick={onClickVerticalCenterConstraint}
  105. height="660"
  106. width="300"
  107. y="750"
  108. x="995"
  109. fill="transparent"
  110. />
  111. <rect className={styles.horizontalCenterConstraint} height="46" width="456" y="1060" x="918" />
  112. <rect
  113. className={styles.constraintHover}
  114. onClick={onClickHorizontalCenterConstraint}
  115. height="300"
  116. width="660"
  117. y="925"
  118. x="815"
  119. fill="transparent"
  120. />
  121. </g>
  122. </svg>
  123. );
  124. };
  125. const getStyles = (currentConstraints: Constraint) => (theme: GrafanaTheme2) => {
  126. const HOVER_COLOR = '#daebf7';
  127. const HOVER_OPACITY = '0.6';
  128. const SELECTED_COLOR = '#0d99ff';
  129. const selectionBoxColor = theme.isDark ? '#ffffff' : '#000000';
  130. return {
  131. constraintHover: css`
  132. &:hover {
  133. fill: ${HOVER_COLOR};
  134. fill-opacity: ${HOVER_OPACITY};
  135. }
  136. `,
  137. topConstraint: css`
  138. ${currentConstraints.vertical === VerticalConstraint.Top ||
  139. currentConstraints.vertical === VerticalConstraint.TopBottom
  140. ? `width: 92pt; x: 1085; fill: ${SELECTED_COLOR};`
  141. : `fill: ${selectionBoxColor};`}
  142. `,
  143. bottomConstraint: css`
  144. ${currentConstraints.vertical === VerticalConstraint.Bottom ||
  145. currentConstraints.vertical === VerticalConstraint.TopBottom
  146. ? `width: 92pt; x: 1085; fill: ${SELECTED_COLOR};`
  147. : `fill: ${selectionBoxColor};`}
  148. `,
  149. leftConstraint: css`
  150. ${currentConstraints.horizontal === HorizontalConstraint.Left ||
  151. currentConstraints.horizontal === HorizontalConstraint.LeftRight
  152. ? `height: 92pt; y: 1014; fill: ${SELECTED_COLOR};`
  153. : `fill: ${selectionBoxColor};`}
  154. `,
  155. rightConstraint: css`
  156. ${currentConstraints.horizontal === HorizontalConstraint.Right ||
  157. currentConstraints.horizontal === HorizontalConstraint.LeftRight
  158. ? `height: 92pt; y: 1014; fill: ${SELECTED_COLOR};`
  159. : `fill: ${selectionBoxColor};`}
  160. `,
  161. horizontalCenterConstraint: css`
  162. ${currentConstraints.horizontal === HorizontalConstraint.Center
  163. ? `height: 92pt; y: 1014; fill: ${SELECTED_COLOR};`
  164. : `fill: ${selectionBoxColor};`}
  165. `,
  166. verticalCenterConstraint: css`
  167. ${currentConstraints.vertical === VerticalConstraint.Center
  168. ? `width: 92pt; x: 1085; fill: ${SELECTED_COLOR};`
  169. : `fill: ${selectionBoxColor};`}
  170. `,
  171. box: css`
  172. fill: ${selectionBoxColor};
  173. `,
  174. };
  175. };