Skip to content

Commit

Permalink
fix: change focus when zoom buttons becomes disabled to be able to st…
Browse files Browse the repository at this point in the history
…ill use arrow keys (fix issue frontend-collective#132)
  • Loading branch information
bsander committed Jul 23, 2018
1 parent 357a801 commit 2e76e83
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/__tests__/react-image-lightbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mount } from 'enzyme';
import React from 'react';
import Modal from 'react-modal';
import Lightbox from '../index';
import { MAX_ZOOM_LEVEL, MIN_ZOOM_LEVEL, ZOOM_BUTTON_INCREMENT_SIZE } from '../constant'

// Mock the loadStyles static function to avoid
// issues with a lack of styles._insertCss
Expand Down Expand Up @@ -231,6 +232,30 @@ describe('Key bindings', () => {
simulateKey(37);
expect(mockMovePrevRequest).toHaveBeenCalledTimes(1);
});

it('Focus the ZoomIn Button when ZoomOut is disabled', () => {
wrapper.setState({ zoomLevel: MIN_ZOOM_LEVEL + ZOOM_BUTTON_INCREMENT_SIZE });
const {zoomOutBtn, zoomInBtn} = wrapper.instance();
jest.spyOn(zoomOutBtn, 'focus');
jest.spyOn(zoomInBtn, 'focus');
wrapper.find('.ril-zoom-out').simulate('click')
setTimeout(() => {
expect(zoomOutBtn.focus).toHaveBeenCalledTimes(0);
expect(zoomInBtn.focus).toHaveBeenCalledTimes(1);
}, 250);
});

it('Focus the ZoomOut Button when ZoomIn is disabled', () => {
wrapper.setState({ zoomLevel: MAX_ZOOM_LEVEL - ZOOM_BUTTON_INCREMENT_SIZE });
const {zoomOutBtn, zoomInBtn} = wrapper.instance();
jest.spyOn(zoomOutBtn, 'focus');
jest.spyOn(zoomInBtn, 'focus');
wrapper.find('.ril-zoom-out').simulate('click')
setTimeout(() => {
expect(zoomOutBtn.focus).toHaveBeenCalledTimes(1);
expect(zoomInBtn.focus).toHaveBeenCalledTimes(0);
}, 250);
});
});

describe('Snapshot Testing', () => {
Expand Down
18 changes: 16 additions & 2 deletions src/react-image-lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -1061,11 +1061,19 @@ class ReactImageLightbox extends Component {
}

handleZoomInButtonClick() {
this.changeZoom(this.state.zoomLevel + ZOOM_BUTTON_INCREMENT_SIZE);
const nextZoomLevel = this.state.zoomLevel + ZOOM_BUTTON_INCREMENT_SIZE;
this.changeZoom(nextZoomLevel);
if (nextZoomLevel === MAX_ZOOM_LEVEL) {
this.zoomOutBtn.focus();
}
}

handleZoomOutButtonClick() {
this.changeZoom(this.state.zoomLevel - ZOOM_BUTTON_INCREMENT_SIZE);
const nextZoomLevel = this.state.zoomLevel - ZOOM_BUTTON_INCREMENT_SIZE;
this.changeZoom(nextZoomLevel);
if (nextZoomLevel === MIN_ZOOM_LEVEL) {
this.zoomInBtn.focus();
}
}

handleCaptionMousewheel(event) {
Expand Down Expand Up @@ -1533,6 +1541,9 @@ class ReactImageLightbox extends Component {
? ['ril__builtinButtonDisabled']
: []),
].join(' ')}
ref={el => {
this.zoomInBtn = el
}}
disabled={
this.isAnimating() || zoomLevel === MAX_ZOOM_LEVEL
}
Expand Down Expand Up @@ -1560,6 +1571,9 @@ class ReactImageLightbox extends Component {
? ['ril__builtinButtonDisabled']
: []),
].join(' ')}
ref={el => {
this.zoomOutBtn = el
}}
disabled={
this.isAnimating() || zoomLevel === MIN_ZOOM_LEVEL
}
Expand Down

0 comments on commit 2e76e83

Please sign in to comment.