Skip to content

Commit

Permalink
Add styling props (#16)
Browse files Browse the repository at this point in the history
This PR adds styling props to the container, cropping area and image
  • Loading branch information
tafelito authored and ValentinH committed Oct 24, 2018
1 parent 578fb0e commit 41d5e3a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class App extends React.Component {
| `onCropChange` | Function || Called everytime the crop is changed. Use it to update your `crop` state.|
| `onZoomChange` | Function | | Called everytime the zoom is changed. Use it to update your `zoom` state. |
| [`onCropComplete`](#onCropCompleteProp) | Function | | Called when the user stops moving the image or stops zooming. It will be passed the corresponding cropped area on the image in percentages and pixels |
| `style` | `{ containerStyle: object, imageStyle: object, cropAreaStyle: object }` | | Custom styles to be used with the Cropper. Styles passed via the style prop are merged with the defaults. |
| `classes` | `{ containerClassName: string, imageClassName: string, cropAreaClassName: string }` | | Custom class names to be used with the Cropper. Classes passed via the classes prop are merged with the defaults. |

<a name="onCropCompleteProp"></a>
#### onCropComplete(croppedArea, cropperAreaPixels)
Expand Down
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ class Cropper extends React.Component {
zoom,
cropShape,
showGrid,
style: { containerStyle, cropAreaStyle, imageStyle },
classes: { containerClassName, cropAreaClassName, imageClassName },
} = this.props

return (
Expand All @@ -256,6 +258,8 @@ class Cropper extends React.Component {
onWheel={this.onWheel}
innerRef={el => (this.container = el)}
data-testid="container"
containerStyle={containerStyle}
className={containerClassName}
>
<Img
src={this.props.image}
Expand All @@ -265,6 +269,8 @@ class Cropper extends React.Component {
style={{
transform: `translate(${x}px, ${y}px) scale(${zoom})`,
}}
imageStyle={imageStyle}
className={imageClassName}
/>
{this.state.cropSize && (
<CropArea
Expand All @@ -275,6 +281,8 @@ class Cropper extends React.Component {
height: this.state.cropSize.height,
}}
data-testid="cropper"
cropAreaStyle={cropAreaStyle}
className={cropAreaClassName}
/>
)}
</Container>
Expand All @@ -289,6 +297,8 @@ Cropper.defaultProps = {
minZoom: MIN_ZOOM,
cropShape: 'rect',
showGrid: true,
style: {},
classes: {},
}

export default Cropper
57 changes: 32 additions & 25 deletions src/styles.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import styled from 'react-emotion'

export const Container = styled('div')({
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
overflow: 'hidden',
background: '#222',
userSelect: 'none',
touchAction: 'none',
cursor: 'move',
})
export const Container = styled('div')(
{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
overflow: 'hidden',
userSelect: 'none',
touchAction: 'none',
cursor: 'move',
},
({ containerStyle }) => ({ ...containerStyle })
)

export const Img = styled('img')({
maxWidth: '100%',
maxHeight: '100%',
margin: 'auto',
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
willChange: 'transform', // this improves performances and prevent painting issues on iOS Chrome
})
export const Img = styled('img')(
{
maxWidth: '100%',
maxHeight: '100%',
margin: 'auto',
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
willChange: 'transform', // this improves performances and prevent painting issues on iOS Chrome
},
({ imageStyle }) => ({ ...imageStyle })
)

const lineBorder = '1px solid rgba(255, 255, 255, 0.5)'
const cropperLines = {
Expand All @@ -39,7 +44,8 @@ const cropperArea = {
transform: 'translate(-50%, -50%)',
border: lineBorder,
boxSizing: 'border-box',
boxShadow: '0 0 0 9999em rgba(0, 0, 0, 0.5)',
boxShadow: '0 0 0 9999em',
color: 'rgba(0,0,0,0.5)',
overflow: 'hidden',
}
const gridLines = {
Expand All @@ -66,7 +72,7 @@ const roundShape = {
borderRadius: '50%',
}

export const CropArea = styled('div')({}, ({ cropShape, showGrid }) => ({
export const CropArea = styled('div')({}, ({ cropShape, showGrid, cropAreaStyle }) => ({
...(() => {
switch (cropShape) {
case 'round':
Expand All @@ -77,4 +83,5 @@ export const CropArea = styled('div')({}, ({ cropShape, showGrid }) => ({
}
})(),
...(showGrid ? gridLines : {}),
...cropAreaStyle,
}))

0 comments on commit 41d5e3a

Please sign in to comment.