Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add styling props #16

Merged
merged 8 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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={cropAreaClassName}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be imageClassName here 😄

tafelito marked this conversation as resolved.
Show resolved Hide resolved
/>
{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={imageClassName}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opposite here: should be cropAreaClassName

tafelito marked this conversation as resolved.
Show resolved Hide resolved
/>
)}
</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,
}))