Note.jsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. & h3 {
  15. font-weight: bold;
  16. padding: 0.2em 0;
  17. margin: 0;
  18. }
  19. & textarea {
  20. height: ${height}px;
  21. font-family: "Satisfy", cursive;
  22. width: 100%;
  23. padding: 0.2em 0;
  24. background-color: transparent;
  25. resize: none;
  26. border: 1px solid #00000011;
  27. font-size: ${fontSize}px;
  28. }
  29. `}
  30. `;
  31. const Note = ({
  32. value = "",
  33. color = "#ffc",
  34. label = "",
  35. textColor = "#000",
  36. fontSize = "20",
  37. width = 300,
  38. height = 200,
  39. setState,
  40. }) => {
  41. // To avoid this behaviour https://github.com/facebook/react/issues/955
  42. // We have a local state and a use effect when incoming update occurs
  43. const [currentValue, setCurrentValue] = React.useState(value);
  44. const setValue = (e) => {
  45. const value = e.target.value;
  46. setCurrentValue(value);
  47. setState((prevState) => ({
  48. ...prevState,
  49. value,
  50. }));
  51. };
  52. useEffect(() => {
  53. setCurrentValue(value);
  54. }, [value]);
  55. return (
  56. <NotePane
  57. color={color}
  58. fontSize={fontSize}
  59. textColor={textColor}
  60. width={width}
  61. height={height}
  62. onKeyDown={(e) =>
  63. e.target === document.activeElement && e.stopPropagation()
  64. }
  65. onKeyUp={(e) =>
  66. e.target === document.activeElement && e.stopPropagation()
  67. }
  68. onWheel={(e) =>
  69. e.target === document.activeElement && e.stopPropagation()
  70. }
  71. >
  72. <label style={{ userSelect: "none" }}>
  73. <h3>{label}</h3>
  74. <textarea value={currentValue} onChange={setValue} />
  75. </label>
  76. </NotePane>
  77. );
  78. };
  79. export default memo(Note);