You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

style-panel.tsx 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import styled from 'styles'
  2. import state, { useSelector } from 'state'
  3. import * as Panel from 'components/panel'
  4. import { useRef } from 'react'
  5. import { IconButton } from 'components/shared'
  6. import { ChevronDown, Trash2, X } from 'react-feather'
  7. import {
  8. deepCompare,
  9. deepCompareArrays,
  10. getPage,
  11. getSelectedIds,
  12. setToArray,
  13. } from 'utils/utils'
  14. import AlignDistribute from './align-distribute'
  15. import { MoveType } from 'types'
  16. import SizePicker from './size-picker'
  17. import {
  18. ArrowDownIcon,
  19. ArrowUpIcon,
  20. AspectRatioIcon,
  21. BoxIcon,
  22. CopyIcon,
  23. EyeClosedIcon,
  24. EyeOpenIcon,
  25. LockClosedIcon,
  26. LockOpen1Icon,
  27. PinBottomIcon,
  28. PinTopIcon,
  29. RotateCounterClockwiseIcon,
  30. } from '@radix-ui/react-icons'
  31. import DashPicker from './dash-picker'
  32. import QuickColorSelect from './quick-color-select'
  33. import ColorPicker from './color-picker'
  34. import IsFilledPicker from './is-filled-picker'
  35. import QuickSizeSelect from './quick-size-select'
  36. import QuickdashSelect from './quick-dash-select'
  37. import Tooltip from 'components/tooltip'
  38. export default function StylePanel() {
  39. const rContainer = useRef<HTMLDivElement>(null)
  40. const isOpen = useSelector((s) => s.data.settings.isStyleOpen)
  41. return (
  42. <StylePanelRoot ref={rContainer} isOpen={isOpen}>
  43. {isOpen ? (
  44. <SelectedShapeStyles />
  45. ) : (
  46. <>
  47. <QuickColorSelect />
  48. <QuickSizeSelect />
  49. <QuickdashSelect />
  50. <IconButton
  51. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  52. title="Style"
  53. size="small"
  54. onClick={() => state.send('TOGGLED_STYLE_PANEL_OPEN')}
  55. >
  56. <Tooltip label="More">
  57. <ChevronDown />
  58. </Tooltip>
  59. </IconButton>
  60. </>
  61. )}
  62. </StylePanelRoot>
  63. )
  64. }
  65. // This panel is going to be hard to keep cool, as we're selecting computed
  66. // information, based on the user's current selection. We might have to keep
  67. // track of this data manually within our state.
  68. function SelectedShapeStyles() {
  69. const selectedIds = useSelector(
  70. (s) => setToArray(getSelectedIds(s.data)),
  71. deepCompareArrays
  72. )
  73. const isAllLocked = useSelector((s) => {
  74. const page = getPage(s.data)
  75. return selectedIds.every((id) => page.shapes[id].isLocked)
  76. })
  77. const isAllAspectLocked = useSelector((s) => {
  78. const page = getPage(s.data)
  79. return selectedIds.every((id) => page.shapes[id].isAspectRatioLocked)
  80. })
  81. const isAllHidden = useSelector((s) => {
  82. const page = getPage(s.data)
  83. return selectedIds.every((id) => page.shapes[id].isHidden)
  84. })
  85. const commonStyle = useSelector((s) => s.values.selectedStyle, deepCompare)
  86. const hasSelection = selectedIds.length > 0
  87. return (
  88. <Panel.Layout>
  89. <Panel.Header side="right">
  90. <h3>Style</h3>
  91. <IconButton
  92. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  93. size="small"
  94. onClick={() => state.send('TOGGLED_STYLE_PANEL_OPEN')}
  95. >
  96. <X />
  97. </IconButton>
  98. </Panel.Header>
  99. <Content>
  100. <ColorPicker
  101. color={commonStyle.color}
  102. onChange={(color) => state.send('CHANGED_STYLE', { color })}
  103. />
  104. <IsFilledPicker
  105. isFilled={commonStyle.isFilled}
  106. onChange={(isFilled) => state.send('CHANGED_STYLE', { isFilled })}
  107. />
  108. <Row>
  109. <label htmlFor="size">Size</label>
  110. <SizePicker size={commonStyle.size} />
  111. </Row>
  112. <Row>
  113. <label htmlFor="dash">Dash</label>
  114. <DashPicker dash={commonStyle.dash} />
  115. </Row>
  116. <ButtonsRow>
  117. <IconButton
  118. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  119. disabled={!hasSelection}
  120. size="small"
  121. onClick={() => state.send('DUPLICATED')}
  122. >
  123. <Tooltip label="Duplicate">
  124. <CopyIcon />
  125. </Tooltip>
  126. </IconButton>
  127. <IconButton
  128. disabled={!hasSelection}
  129. size="small"
  130. onClick={() => state.send('ROTATED_CCW')}
  131. >
  132. <Tooltip label="Rotate">
  133. <RotateCounterClockwiseIcon />
  134. </Tooltip>
  135. </IconButton>
  136. <IconButton
  137. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  138. disabled={!hasSelection}
  139. size="small"
  140. onClick={() => state.send('TOGGLED_SHAPE_HIDE')}
  141. >
  142. <Tooltip label="Toogle Hidden">
  143. {isAllHidden ? <EyeClosedIcon /> : <EyeOpenIcon />}
  144. </Tooltip>
  145. </IconButton>
  146. <IconButton
  147. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  148. disabled={!hasSelection}
  149. size="small"
  150. onClick={() => state.send('TOGGLED_SHAPE_LOCK')}
  151. >
  152. <Tooltip label="Toogle Locked">
  153. {isAllLocked ? <LockClosedIcon /> : <LockOpen1Icon />}
  154. </Tooltip>
  155. </IconButton>
  156. <IconButton
  157. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  158. disabled={!hasSelection}
  159. size="small"
  160. onClick={() => state.send('TOGGLED_SHAPE_ASPECT_LOCK')}
  161. >
  162. <Tooltip label="Toogle Aspect Ratio Lock">
  163. {isAllAspectLocked ? <AspectRatioIcon /> : <BoxIcon />}
  164. </Tooltip>
  165. </IconButton>
  166. </ButtonsRow>
  167. <ButtonsRow>
  168. <IconButton
  169. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  170. disabled={!hasSelection}
  171. size="small"
  172. onClick={() => state.send('MOVED', { type: MoveType.ToBack })}
  173. >
  174. <Tooltip label="Move to Back">
  175. <PinBottomIcon />
  176. </Tooltip>
  177. </IconButton>
  178. <IconButton
  179. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  180. disabled={!hasSelection}
  181. size="small"
  182. onClick={() => state.send('MOVED', { type: MoveType.Backward })}
  183. >
  184. <Tooltip label="Move Backward">
  185. <ArrowDownIcon />
  186. </Tooltip>
  187. </IconButton>
  188. <IconButton
  189. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  190. disabled={!hasSelection}
  191. size="small"
  192. onClick={() => state.send('MOVED', { type: MoveType.Forward })}
  193. >
  194. <Tooltip label="Move Forward">
  195. <ArrowUpIcon />
  196. </Tooltip>
  197. </IconButton>
  198. <IconButton
  199. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  200. disabled={!hasSelection}
  201. size="small"
  202. onClick={() => state.send('MOVED', { type: MoveType.ToFront })}
  203. >
  204. <Tooltip label="More to Front">
  205. <PinTopIcon />
  206. </Tooltip>
  207. </IconButton>
  208. <IconButton
  209. bp={{ '@initial': 'mobile', '@sm': 'small' }}
  210. disabled={!hasSelection}
  211. size="small"
  212. onClick={() => state.send('DELETED')}
  213. >
  214. <Tooltip label="Delete">
  215. <Trash2 size="15" />
  216. </Tooltip>
  217. </IconButton>
  218. </ButtonsRow>
  219. <AlignDistribute
  220. hasTwoOrMore={selectedIds.length > 1}
  221. hasThreeOrMore={selectedIds.length > 2}
  222. />
  223. </Content>
  224. </Panel.Layout>
  225. )
  226. }
  227. const StylePanelRoot = styled(Panel.Root, {
  228. minWidth: 1,
  229. width: 184,
  230. maxWidth: 184,
  231. overflow: 'hidden',
  232. position: 'relative',
  233. border: '1px solid $panel',
  234. boxShadow: '0px 2px 4px rgba(0,0,0,.2)',
  235. display: 'flex',
  236. alignItems: 'center',
  237. pointerEvents: 'all',
  238. variants: {
  239. isOpen: {
  240. true: {},
  241. false: {
  242. padding: 2,
  243. width: 'fit-content',
  244. },
  245. },
  246. },
  247. })
  248. const Content = styled(Panel.Content, {
  249. padding: 8,
  250. })
  251. const Row = styled('div', {
  252. position: 'relative',
  253. display: 'flex',
  254. width: '100%',
  255. background: 'none',
  256. border: 'none',
  257. outline: 'none',
  258. alignItems: 'center',
  259. justifyContent: 'space-between',
  260. padding: '4px 2px 4px 12px',
  261. '& label': {
  262. fontFamily: '$ui',
  263. fontSize: '$1',
  264. fontWeight: '$1',
  265. margin: 0,
  266. padding: 0,
  267. },
  268. '& > svg': {
  269. position: 'relative',
  270. },
  271. })
  272. const ButtonsRow = styled('div', {
  273. position: 'relative',
  274. display: 'flex',
  275. width: '100%',
  276. background: 'none',
  277. border: 'none',
  278. cursor: 'pointer',
  279. outline: 'none',
  280. alignItems: 'center',
  281. justifyContent: 'flex-start',
  282. padding: 4,
  283. })