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.

controls-panel.tsx 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* eslint-disable @typescript-eslint/ban-ts-comment */
  2. import styled from 'styles'
  3. import React, { useRef } from 'react'
  4. import state, { useSelector } from 'state'
  5. import { X, Code } from 'react-feather'
  6. import { breakpoints, IconButton } from 'components/shared'
  7. import * as Panel from '../panel'
  8. import Control from './control'
  9. import { deepCompareArrays } from 'utils'
  10. function openCodePanel() {
  11. state.send('CLOSED_CODE_PANEL')
  12. }
  13. function closeCodePanel() {
  14. state.send('OPENED_CODE_PANEL')
  15. }
  16. const stopKeyboardPropagation = (e: KeyboardEvent | React.KeyboardEvent) =>
  17. e.stopPropagation()
  18. export default function ControlPanel(): JSX.Element {
  19. const rContainer = useRef<HTMLDivElement>(null)
  20. const isOpen = useSelector((s) => Object.keys(s.data.codeControls).length > 0)
  21. const codeControls = useSelector(
  22. (state) => Object.keys(state.data.codeControls),
  23. deepCompareArrays
  24. )
  25. return (
  26. <Panel.Root
  27. ref={rContainer}
  28. dir="ltr"
  29. data-bp-desktop
  30. variant="controls"
  31. isOpen={isOpen}
  32. onKeyDown={stopKeyboardPropagation}
  33. onKeyUp={stopKeyboardPropagation}
  34. >
  35. {isOpen ? (
  36. <Panel.Layout>
  37. <Panel.Header>
  38. <IconButton bp={breakpoints} size="small" onClick={closeCodePanel}>
  39. <X />
  40. </IconButton>
  41. <h3>Controls</h3>
  42. </Panel.Header>
  43. <ControlsList>
  44. {codeControls.map((id) => (
  45. <Control key={id} id={id} />
  46. ))}
  47. </ControlsList>
  48. </Panel.Layout>
  49. ) : (
  50. <IconButton bp={breakpoints} size="small" onClick={openCodePanel}>
  51. <Code />
  52. </IconButton>
  53. )}
  54. </Panel.Root>
  55. )
  56. }
  57. const ControlsList = styled(Panel.Content, {
  58. padding: 12,
  59. display: 'grid',
  60. gridTemplateColumns: '1fr 4fr',
  61. gridAutoRows: '24px',
  62. alignItems: 'center',
  63. gridColumnGap: '8px',
  64. gridRowGap: '8px',
  65. '& input': {
  66. font: '$ui',
  67. fontSize: '$1',
  68. border: '1px solid $inputBorder',
  69. backgroundColor: '$input',
  70. color: '$text',
  71. height: '100%',
  72. padding: '0px 6px',
  73. },
  74. })