-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(): add ViewportSizeProvider (#388)
* chore(): add ViewportSizeProvider * chore(): add tests
- Loading branch information
Showing
9 changed files
with
133 additions
and
18 deletions.
There are no files selected for viewing
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
5 changes: 3 additions & 2 deletions
5
ui/design/src/components/Avatar/__test__/__snapshots__/index.test.tsx.snap
Large diffs are not rendered by default.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
ui/design/src/components/Providers/__test__/viewport-dimension.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,44 @@ | ||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
import responsiveBreakpoints from '../../../styles/responsive-breakpoints'; | ||
import { resizeWindow } from '../../../test-utils'; | ||
import ViewportSizeProvider, { useViewportSize } from '../viewport-dimension'; | ||
|
||
const breakpoints = responsiveBreakpoints.global.breakpoints; | ||
|
||
describe('ViewportSize', () => { | ||
let viewportSize: { dimensions: { width: number; height: number }; size: string }; | ||
beforeEach(() => { | ||
const DummyComponent = () => { | ||
viewportSize = useViewportSize(); | ||
return <></>; | ||
}; | ||
const tree = ( | ||
<ViewportSizeProvider> | ||
<DummyComponent /> | ||
</ViewportSizeProvider> | ||
); | ||
render(tree); | ||
}); | ||
it('should react on window resize', () => { | ||
act(() => resizeWindow(250, 200)); | ||
expect(viewportSize).toBeDefined(); | ||
expect(viewportSize.dimensions.width).toBe(250); | ||
expect(viewportSize.dimensions.height).toBe(200); | ||
act(() => resizeWindow(800, 600)); | ||
expect(viewportSize.dimensions.width).toBe(800); | ||
expect(viewportSize.dimensions.height).toBe(600); | ||
}); | ||
it('should return correct size attribute', () => { | ||
act(() => resizeWindow(520, 300)); | ||
expect(viewportSize).toBeDefined(); | ||
expect(breakpoints[viewportSize.size].value).toEqual(breakpoints.small.value); | ||
act(() => resizeWindow(1022, 768)); | ||
expect(breakpoints[viewportSize.size].value).toEqual(breakpoints.medium.value); | ||
act(() => resizeWindow(1223, 800)); | ||
expect(breakpoints[viewportSize.size].value).toEqual(breakpoints.large.value); | ||
act(() => resizeWindow(1920, 1080)); | ||
expect(breakpoints[viewportSize.size].value).toEqual(breakpoints.xlarge.value); | ||
}); | ||
}); |
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,48 @@ | ||
import * as React from 'react'; | ||
import responsiveBreakpoints from '../../styles/responsive-breakpoints'; | ||
|
||
const breakpoints = responsiveBreakpoints.global.breakpoints; | ||
|
||
const ViewportSizeCtx = React.createContext({ | ||
dimensions: { width: 0, height: 0 }, | ||
size: '', | ||
}); | ||
const getSizeByWidth = (width: number) => { | ||
const size = Object.keys(breakpoints).reduce((prev, curr) => { | ||
if (breakpoints[prev].value - width <= 0) { | ||
return curr; | ||
} | ||
return prev; | ||
}); | ||
return size; | ||
}; | ||
|
||
export interface IViewportDimProps { | ||
children: React.ReactElement; | ||
} | ||
const ViewportSizeProvider = ({ children }: IViewportDimProps) => { | ||
const [dimensions, setDimensions] = React.useState({ | ||
width: window.innerWidth, | ||
height: window.innerHeight, | ||
}); | ||
const [size, setSize] = React.useState(getSizeByWidth(dimensions.width)); | ||
|
||
React.useEffect(() => { | ||
const handleResize = () => { | ||
setDimensions({ | ||
width: window.innerWidth, | ||
height: window.innerHeight, | ||
}); | ||
setSize(getSizeByWidth(window.innerWidth)); | ||
}; | ||
window.addEventListener('resize', handleResize); | ||
return () => { | ||
window.removeEventListener('resize', handleResize); | ||
}; | ||
}, []); | ||
return ( | ||
<ViewportSizeCtx.Provider value={{ dimensions, size }}>{children}</ViewportSizeCtx.Provider> | ||
); | ||
}; | ||
export const useViewportSize = () => React.useContext(ViewportSizeCtx); | ||
export default ViewportSizeProvider; |
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
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