Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. The hook fn doesn't set any styles on either of the prop fns (`getRootProps()`/`getInputProps()`).
  2. ### Using inline styles
  3. ```jsx harmony
  4. import React, {useMemo} from 'react';
  5. import {useDropzone} from 'react-dropzone';
  6. const baseStyle = {
  7. flex: 1,
  8. display: 'flex',
  9. flexDirection: 'column',
  10. alignItems: 'center',
  11. padding: '20px',
  12. borderWidth: 2,
  13. borderRadius: 2,
  14. borderColor: '#eeeeee',
  15. borderStyle: 'dashed',
  16. backgroundColor: '#fafafa',
  17. color: '#bdbdbd',
  18. outline: 'none',
  19. transition: 'border .24s ease-in-out'
  20. };
  21. const activeStyle = {
  22. borderColor: '#2196f3'
  23. };
  24. const acceptStyle = {
  25. borderColor: '#00e676'
  26. };
  27. const rejectStyle = {
  28. borderColor: '#ff1744'
  29. };
  30. function StyledDropzone(props) {
  31. const {
  32. getRootProps,
  33. getInputProps,
  34. isDragActive,
  35. isDragAccept,
  36. isDragReject
  37. } = useDropzone({accept: 'image/*'});
  38. const style = useMemo(() => ({
  39. ...baseStyle,
  40. ...(isDragActive ? activeStyle : {}),
  41. ...(isDragAccept ? acceptStyle : {}),
  42. ...(isDragReject ? rejectStyle : {})
  43. }), [
  44. isDragActive,
  45. isDragReject
  46. ]);
  47. return (
  48. <div className="container">
  49. <div {...getRootProps({style})}>
  50. <input {...getInputProps()} />
  51. <p>Drag 'n' drop some files here, or click to select files</p>
  52. </div>
  53. </div>
  54. );
  55. }
  56. <StyledDropzone />
  57. ```
  58. ### Using styled-components
  59. ```jsx harmony
  60. import React from 'react';
  61. import {useDropzone} from 'react-dropzone';
  62. import styled from 'styled-components';
  63. const getColor = (props) => {
  64. if (props.isDragAccept) {
  65. return '#00e676';
  66. }
  67. if (props.isDragReject) {
  68. return '#ff1744';
  69. }
  70. if (props.isDragActive) {
  71. return '#2196f3';
  72. }
  73. return '#eeeeee';
  74. }
  75. const Container = styled.div`
  76. flex: 1;
  77. display: flex;
  78. flex-direction: column;
  79. align-items: center;
  80. padding: 20px;
  81. border-width: 2px;
  82. border-radius: 2px;
  83. border-color: ${props => getColor(props)};
  84. border-style: dashed;
  85. background-color: #fafafa;
  86. color: #bdbdbd;
  87. outline: none;
  88. transition: border .24s ease-in-out;
  89. `;
  90. function StyledDropzone(props) {
  91. const {
  92. getRootProps,
  93. getInputProps,
  94. isDragActive,
  95. isDragAccept,
  96. isDragReject
  97. } = useDropzone({accept: 'image/*'});
  98. return (
  99. <div className="container">
  100. <Container {...getRootProps({isDragActive, isDragAccept, isDragReject})}>
  101. <input {...getInputProps()} />
  102. <p>Drag 'n' drop some files here, or click to select files</p>
  103. </Container>
  104. </div>
  105. );
  106. }
  107. <StyledDropzone />
  108. ```