Skip to content

Commit ee559e4

Browse files
committed
rename onScaleChange to onRequestScaleChange, wire up in demo
Rename makes it clear the callback is a request, not mandatory. Wire it up in the demo so +/- keyboard zoom works out of the box. Update README props table and GitHub issue comment.
1 parent 41a1c86 commit ee559e4

4 files changed

Lines changed: 12 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ function MyEditor() {
207207
| `onMouseUp` | `() => void` | | Called when the user releases the mouse after dragging. |
208208
| `onMouseMove` | `(event) => void` | | Called on every mouse/touch move while dragging. |
209209
| `onPositionChange` | `(position) => void` | | Called when the crop position changes. Receives `{ x, y }`. |
210+
| `onRequestScaleChange` | `(scale) => void` | | Called when the user presses +/- keys to zoom. Receives the requested new scale value. |
211+
| `keyboardStep` | `number` | `1` | Pixels to move per arrow key press. Shift multiplies by 10. |
210212

211213
## Contributing
212214

packages/demo/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ const App = () => {
140140
disableCanvasRotation={state.disableCanvasRotation}
141141
borderColor={hexToRgba(state.borderColor)}
142142
onPositionChange={(position: Position) => update({ position })}
143+
onRequestScaleChange={(scale: number) => update({ scale })}
143144
/>
144145
<input {...getInputProps()} />
145146
<span className="dropzone-hint">drop image here</span>

packages/lib/src/__tests__/AvatarEditor.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,14 @@ describe('AvatarEditor', () => {
518518
expect(canvas.getAttribute('role')).toBe('application')
519519
})
520520

521-
it('onScaleChange prop is accepted', () => {
522-
const onScaleChange = vi.fn()
521+
it('onRequestScaleChange prop is accepted', () => {
522+
const onRequestScaleChange = vi.fn()
523523
const { container } = render(
524524
<AvatarEditor
525525
width={200}
526526
height={200}
527527
scale={1.5}
528-
onScaleChange={onScaleChange}
528+
onRequestScaleChange={onRequestScaleChange}
529529
/>,
530530
)
531531
expect(container.querySelector('canvas')).toBeInTheDocument()

packages/lib/src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface Props extends AvatarEditorConfig {
3232
onMouseUp?: () => void
3333
onMouseMove?: (e: TouchEvent | MouseEvent) => void
3434
onPositionChange?: (position: Position) => void
35-
onScaleChange?: (scale: number) => void
35+
onRequestScaleChange?: (scale: number) => void
3636
}
3737

3838
export type { Position, ImageState }
@@ -69,7 +69,7 @@ const AvatarEditor = forwardRef<AvatarEditorRef, Props>((props, ref) => {
6969
onMouseUp,
7070
onMouseMove,
7171
onPositionChange,
72-
onScaleChange,
72+
onRequestScaleChange,
7373
borderColor,
7474
style,
7575
keyboardStep = 1,
@@ -114,8 +114,8 @@ const AvatarEditor = forwardRef<AvatarEditorRef, Props>((props, ref) => {
114114
onMouseUpRef.current = onMouseUp
115115
const onMouseMoveRef = useRef(onMouseMove)
116116
onMouseMoveRef.current = onMouseMove
117-
const onScaleChangeRef = useRef(onScaleChange)
118-
onScaleChangeRef.current = onScaleChange
117+
const onRequestScaleChangeRef = useRef(onRequestScaleChange)
118+
onRequestScaleChangeRef.current = onRequestScaleChange
119119
const onPositionChangeRef = useRef(onPositionChange)
120120
onPositionChangeRef.current = onPositionChange
121121

@@ -284,13 +284,13 @@ const AvatarEditor = forwardRef<AvatarEditorRef, Props>((props, ref) => {
284284
case '=': {
285285
e.preventDefault()
286286
const zoomStep = e.shiftKey ? 0.5 : 0.1
287-
onScaleChangeRef.current?.(scale + zoomStep)
287+
onRequestScaleChangeRef.current?.(scale + zoomStep)
288288
break
289289
}
290290
case '-': {
291291
e.preventDefault()
292292
const zoomStep = e.shiftKey ? 0.5 : 0.1
293-
onScaleChangeRef.current?.(Math.max(0.1, scale - zoomStep))
293+
onRequestScaleChangeRef.current?.(Math.max(0.1, scale - zoomStep))
294294
break
295295
}
296296
case 'Escape':

0 commit comments

Comments
 (0)