-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaces the mouse wheel action in 2.3. There are problems with preventing default on it: see facebook/react#14856 Using a keyboard shortcut seems better anyway since not everyone has a mouse wheel.
- Loading branch information
Chris Klimas
committed
May 1, 2022
1 parent
b7df794
commit 4050a19
Showing
8 changed files
with
228 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
src/routes/story-edit/__tests__/use-zoom-shortcuts.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import {cleanup, fireEvent, render, screen} from '@testing-library/react'; | ||
import {Story, useStoriesContext} from '../../../store/stories'; | ||
import {FakeStateProvider, fakeStory, StoryInspector} from '../../../test-util'; | ||
import {useZoomShortcuts} from '../use-zoom-shortcuts'; | ||
|
||
const TestZoomShortcuts: React.FC = () => { | ||
const {stories} = useStoriesContext(); | ||
|
||
useZoomShortcuts(stories[0]); | ||
return <div>test zoom shortcut</div>; | ||
}; | ||
|
||
describe('useZoomShortcuts()', () => { | ||
function renderComponent(story: Story) { | ||
return render( | ||
<FakeStateProvider stories={[story]}> | ||
<TestZoomShortcuts /> | ||
<StoryInspector /> | ||
</FakeStateProvider> | ||
); | ||
} | ||
|
||
describe('when the + key is pressed', () => { | ||
it('increases the story zoom', async () => { | ||
const story = fakeStory(); | ||
|
||
story.zoom = 0.3; | ||
renderComponent(story); | ||
fireEvent.keyUp(document.body, { | ||
key: '=', | ||
code: '=', | ||
keyCode: 187, | ||
charCode: 187 | ||
}); | ||
expect(screen.getByTestId('story-inspector-default').dataset.zoom).toBe( | ||
'0.6' | ||
); | ||
cleanup(); | ||
story.zoom = 0.6; | ||
renderComponent(story); | ||
fireEvent.keyUp(document.body, { | ||
key: '=', | ||
code: '=', | ||
keyCode: 187, | ||
charCode: 187 | ||
}); | ||
expect(screen.getByTestId('story-inspector-default').dataset.zoom).toBe( | ||
'1' | ||
); | ||
}); | ||
|
||
it('does not increase the story zoom if it is 1', () => { | ||
const story = fakeStory(); | ||
|
||
story.zoom = 1; | ||
renderComponent(story); | ||
fireEvent.keyUp(document.body, { | ||
key: '=', | ||
code: '=', | ||
keyCode: 187, | ||
charCode: 187 | ||
}); | ||
expect(screen.getByTestId('story-inspector-default').dataset.zoom).toBe( | ||
'1' | ||
); | ||
}); | ||
}); | ||
|
||
describe('when the - key is pressed', () => { | ||
it('decreases the story zoom', () => { | ||
const story = fakeStory(); | ||
|
||
story.zoom = 1; | ||
renderComponent(story); | ||
fireEvent.keyUp(document.body, { | ||
key: '-', | ||
code: '-', | ||
keyCode: 189, | ||
charCode: 189 | ||
}); | ||
expect(screen.getByTestId('story-inspector-default').dataset.zoom).toBe( | ||
'0.6' | ||
); | ||
cleanup(); | ||
story.zoom = 0.6; | ||
renderComponent(story); | ||
fireEvent.keyUp(document.body, { | ||
key: '-', | ||
code: '-', | ||
keyCode: 189, | ||
charCode: 189 | ||
}); | ||
expect(screen.getByTestId('story-inspector-default').dataset.zoom).toBe( | ||
'0.3' | ||
); | ||
}); | ||
|
||
it('does not decrease the story zoom if it is 0.3', () => { | ||
const story = fakeStory(); | ||
|
||
story.zoom = 0.3; | ||
renderComponent(story); | ||
fireEvent.keyUp(document.body, { | ||
key: '-', | ||
code: '-', | ||
keyCode: 189, | ||
charCode: 189 | ||
}); | ||
expect(screen.getByTestId('story-inspector-default').dataset.zoom).toBe( | ||
'0.3' | ||
); | ||
}); | ||
}); | ||
|
||
it('does not react to keydown events', () => { | ||
const story = fakeStory(); | ||
|
||
story.zoom = 0.6; | ||
renderComponent(story); | ||
fireEvent.keyDown(document.body, { | ||
key: '-', | ||
code: '-', | ||
keyCode: 189, | ||
charCode: 189 | ||
}); | ||
expect(screen.getByTestId('story-inspector-default').dataset.zoom).toBe( | ||
'0.6' | ||
); | ||
fireEvent.keyDown(document.body, { | ||
key: '=', | ||
code: '=', | ||
keyCode: 187, | ||
charCode: 187 | ||
}); | ||
expect(screen.getByTestId('story-inspector-default').dataset.zoom).toBe( | ||
'0.6' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {useHotkeys} from 'react-hotkeys-hook'; | ||
import {Story, updateStory, useStoriesContext} from '../../store/stories'; | ||
|
||
export function useZoomShortcuts(story: Story) { | ||
const {dispatch, stories} = useStoriesContext(); | ||
|
||
useHotkeys( | ||
'-', | ||
() => { | ||
switch (story.zoom) { | ||
case 1: | ||
dispatch(updateStory(stories, story, {zoom: 0.6})); | ||
break; | ||
case 0.6: | ||
dispatch(updateStory(stories, story, {zoom: 0.3})); | ||
break; | ||
// Do nothing if zoom is 0.3 | ||
} | ||
}, | ||
{keydown: false, keyup: true}, | ||
[dispatch, stories, story] | ||
); | ||
useHotkeys( | ||
'=', | ||
() => { | ||
switch (story.zoom) { | ||
case 0.3: | ||
dispatch(updateStory(stories, story, {zoom: 0.6})); | ||
break; | ||
case 0.6: | ||
dispatch(updateStory(stories, story, {zoom: 1})); | ||
break; | ||
// Do nothing if zoom is 1 | ||
} | ||
}, | ||
{keydown: false, keyup: true}, | ||
[dispatch, stories, story] | ||
); | ||
} |