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 keyboard accessibility to move crop area with arrow keys #570

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ See [#428](https://github.com/ValentinH/react-easy-crop/issues/428), [#409](http
| `setMediaSize` | `(size: MediaSize) => void` | | [Advanced Usage] Used to expose the `mediaSize` value for use with the `getInitialCropFromCroppedAreaPixels` and `getInitialCropFromCroppedAreaPercentages` functions. See [this CodeSandbox instance](https://codesandbox.io/s/react-easy-crop-forked-3v0hi3) for a simple example. |
| `setCropSize` | `(size: Size) => void` | | [Advanced Usage] Used to expose the `cropSize` value for use with the `getInitialCropFromCroppedAreaPixels` and `getInitialCropFromCroppedAreaPercentages` functions. See [this CodeSandbox instance](https://codesandbox.io/s/react-easy-crop-forked-3v0hi3) for a simple example. |
| `nonce` | string | | The nonce to add to the style tag when the styles are auto injected. |
| `keyboardStep` | number | | The keyboardStep prop controls the number of pixels the crop area moves with each press of an arrow key when using keyboard navigation. Defaults to 1. |

<a name="onCropCompleteProp"></a>

Expand Down
37 changes: 37 additions & 0 deletions src/Cropper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type CropperProps = {
setMediaSize?: (size: MediaSize) => void
setCropSize?: (size: Size) => void
nonce?: string
keyboardStep: number
}

type State = {
Expand All @@ -72,6 +73,7 @@ type State = {

const MIN_ZOOM = 1
const MAX_ZOOM = 3
const KEYBOARD_STEP = 1

type GestureEvent = UIEvent & {
rotation: number
Expand All @@ -96,6 +98,7 @@ class Cropper extends React.Component<CropperProps, State> {
zoomSpeed: 1,
restrictPosition: true,
zoomWithScroll: true,
keyboardStep: KEYBOARD_STEP,
}

imageRef: React.RefObject<HTMLImageElement> = React.createRef()
Expand Down Expand Up @@ -727,6 +730,38 @@ class Cropper extends React.Component<CropperProps, State> {
this.emitCropData()
}

onKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
const { crop, onCropChange, keyboardStep, zoom, rotation } = this.props
const step = keyboardStep

if (!this.state.cropSize) return

let newCrop = { ...crop }

switch (event.key) {
case 'ArrowUp':
newCrop.y -= step
break
case 'ArrowDown':
newCrop.y += step
break
case 'ArrowLeft':
newCrop.x -= step
break
case 'ArrowRight':
newCrop.x += step
break
default:
return
}

if (this.props.restrictPosition) {
newCrop = restrictPosition(newCrop, this.mediaSize, this.state.cropSize, zoom, rotation)
}

onCropChange(newCrop)
}

render() {
const {
image,
Expand Down Expand Up @@ -810,6 +845,8 @@ class Cropper extends React.Component<CropperProps, State> {
width: this.state.cropSize.width,
height: this.state.cropSize.height,
}}
tabIndex={0}
onKeyDown={this.onKeyDown}
data-testid="cropper"
className={classNames(
'reactEasyCrop_CropArea',
Expand Down