diff options
author | ilyayudovin <46264063+ilyayudovin@users.noreply.github.com> | 2020-11-17 15:33:02 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-17 15:33:02 +0300 |
commit | db731f1b88fdfa95f16255767e44762211f47196 (patch) | |
tree | 84750528e47ae35e136bc8f24c1a575ccc81092f /src/components/ImageCropAreaSelect/ImageCropAreaSelect.tsx | |
parent | cda51156c20c04a20a9fcfe1e0f3aa51f54e9ad2 (diff) | |
parent | 99b4e4aa53d3ade389fc270f9ba9b02904da93f6 (diff) | |
download | which-ui-db731f1b88fdfa95f16255767e44762211f47196.tar.gz |
Merge pull request #108 from which-ecosystem/avatarCrop
feat: Add avatar crop
Diffstat (limited to 'src/components/ImageCropAreaSelect/ImageCropAreaSelect.tsx')
-rw-r--r-- | src/components/ImageCropAreaSelect/ImageCropAreaSelect.tsx | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/components/ImageCropAreaSelect/ImageCropAreaSelect.tsx b/src/components/ImageCropAreaSelect/ImageCropAreaSelect.tsx new file mode 100644 index 0000000..b4540da --- /dev/null +++ b/src/components/ImageCropAreaSelect/ImageCropAreaSelect.tsx @@ -0,0 +1,72 @@ +import React, {useCallback, useState} from 'react'; +import Cropper from 'react-easy-crop'; +import {makeStyles} from '@material-ui/core/styles'; +import {Slider} from "@material-ui/core"; + +interface Area { + x: number; + y: number; + width: number; + height: number; +} + +interface PropTypes { + image: string; + setArea: (area: Area) => void; +} + +const useStyles = makeStyles(theme => ({ + cropperArea: { + position: 'relative', + width: '100%', + height: 'calc(100vh - 130px)', + background: '#333', + [theme.breakpoints.up('sm')]: { + height: 400, + } + }, + sliderContainer: { + padding: 20, + } +})); + +const ImageCropAreaSelect: React.FC<PropTypes> = ({image, setArea}) => { + const classes = useStyles(); + const [crop, setCrop] = useState({x: 0, y: 0}); + const [zoom, setZoom] = useState<any>(1); + + const onCropComplete = useCallback((areaPercentage: Area, areaPixels: Area): void => { + setArea(areaPixels); + }, [setArea]); + + + return ( + <div> + <div className={classes.cropperArea}> + <Cropper + image={image} + crop={crop} + zoom={zoom} + aspect={1} + cropShape="round" + showGrid={false} + onCropChange={setCrop} + onCropComplete={onCropComplete} + onZoomChange={setZoom} + /> + </div> + <div className={classes.sliderContainer}> + <Slider + value={zoom} + min={1} + max={3} + step={0.01} + aria-labelledby="Zoom" + onChange={(e, zoom) => setZoom(zoom)} + /> + </div> + </div> + ) +}; + +export default ImageCropAreaSelect; |