Note.jsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, { memo, useEffect } from "react";
  2. import styled, { css } from "styled-components";
  3. const NotePane = styled.div`
  4. ${({ color, fontSize, textColor, width, height }) => css`
  5. background-color: ${color};
  6. width: ${width}px;
  7. padding: 0.5em;
  8. text-align: center;
  9. display: flex;
  10. justify-content: space-between;
  11. flex-direction: column;
  12. box-shadow: 5px 5px 7px rgba(33, 33, 33, 0.7);
  13. color: ${textColor};
  14. .item-library__component & {
  15. background-color: #ccc;
  16. }
  17. & .note__title {
  18. font-weight: bold;
  19. padding: 0.2em 0;
  20. margin: 0;
  21. }
  22. & .note__textarea {
  23. height: ${height}px;
  24. font-family: "Satisfy", cursive;
  25. width: 100%;
  26. padding: 0.2em 0;
  27. background-color: transparent;
  28. resize: none;
  29. border: 1px solid #00000011;
  30. font-size: ${fontSize}px;
  31. .item-library__component & {
  32. height: 35px;
  33. }
  34. }
  35. `}
  36. `;
  37. const Note = ({
  38. value = "",
  39. color = "#ffc",
  40. label = "",
  41. textColor = "#000",
  42. fontSize = "20",
  43. width = 300,
  44. height = 200,
  45. setState,
  46. }) => {
  47. // To avoid this behaviour https://github.com/facebook/react/issues/955
  48. // We have a local state and a use effect when incoming update occurs
  49. const [currentValue, setCurrentValue] = React.useState(value);
  50. const setValue = (e) => {
  51. const value = e.target.value;
  52. setCurrentValue(value);
  53. setState((prevState) => ({
  54. ...prevState,
  55. value,
  56. }));
  57. };
  58. useEffect(() => {
  59. setCurrentValue(value);
  60. }, [value]);
  61. return (
  62. <NotePane
  63. color={color}
  64. fontSize={fontSize}
  65. textColor={textColor}
  66. width={width}
  67. height={height}
  68. onKeyDown={(e) =>
  69. e.target === document.activeElement && e.stopPropagation()
  70. }
  71. onKeyUp={(e) =>
  72. e.target === document.activeElement && e.stopPropagation()
  73. }
  74. onWheel={(e) =>
  75. e.target === document.activeElement && e.stopPropagation()
  76. }
  77. onPointerMove={(e) =>
  78. e.target === document.activeElement && e.stopPropagation()
  79. }
  80. >
  81. <label style={{ userSelect: "none" }}>
  82. <h3 className="note__title">{label}</h3>
  83. <textarea
  84. className="note__textarea"
  85. value={currentValue}
  86. onChange={setValue}
  87. />
  88. </label>
  89. </NotePane>
  90. );
  91. };
  92. export default memo(Note);