-
-
Notifications
You must be signed in to change notification settings - Fork 8
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 custom hook -> use-preview #39
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,6 +11,7 @@ | |||||
- [useMousePosition](#-usemouseposition) | ||||||
- [useGeoLocation](#-usegeolocation) | ||||||
- [usePrevious](#-useprevious) | ||||||
- [useViewport](#-useviewport) | ||||||
|
||||||
</br> | ||||||
|
||||||
|
@@ -372,6 +373,49 @@ function App() { | |||||
|
||||||
</br> | ||||||
|
||||||
<br> | ||||||
|
||||||
## 🖥 useViewport | ||||||
|
||||||
Returns an object with the current width of the viewport and the type of device. | ||||||
|
||||||
### Usage | ||||||
|
||||||
```jsx | ||||||
import React from "react"; | ||||||
import MobileComponent from ".Example/MobileComponent"; | ||||||
import TabletComponent from ".Example/TabletComponent"; | ||||||
import DesktopComponent from ".Example/DesktopComponent"; | ||||||
import { useViewport, MOBILE, TABLET } from "use-custom-hooks"; | ||||||
|
||||||
const Screen = () => { | ||||||
const { viewport } = useViewport(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
/* | ||||||
Using Object destructuring we can get width and device | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
from viewport object. | ||||||
*/ | ||||||
|
||||||
if (viewport.device === MOBILE) { | ||||||
return <MobileComponent />; | ||||||
} else if (viewport.device === TABLET) { | ||||||
return <TabletComponent />; | ||||||
} else { | ||||||
return <DesktopComponent />; | ||||||
} | ||||||
}; | ||||||
``` | ||||||
### Parameters | ||||||
|
||||||
None : This hooks takes no parameters. | ||||||
|
||||||
### Return value | ||||||
|
||||||
`{width,device}` | ||||||
|
||||||
1. `width` (_number_) : Current width of the viewport. | ||||||
2. `device` (_string_) : Device type. | ||||||
|
||||||
<br> | ||||||
|
||||||
# Contribution Guidelines | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,55 @@ | ||||||
import { useState, useEffect } from 'react'; | ||||||
|
||||||
/** | ||||||
* Helper constants that determines the current screen size. | ||||||
* @constant | ||||||
* @type {string} | ||||||
*/ | ||||||
export const MOBILE = 'MOBILE'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These constants need not be exported |
||||||
export const TABLET = 'TABLET'; | ||||||
export const DESKTOP = 'DESKTOP'; | ||||||
|
||||||
/** | ||||||
* A function which gets the width of the screen. | ||||||
* Returns one of three possible values depending of the screen's current width. | ||||||
* | ||||||
* @param {number} width | ||||||
* @returns {*} The constant corresponding the screen size. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
const getDevice = (width) => { | ||||||
if (width < 768) return MOBILE; | ||||||
if (width < 992) return TABLET; | ||||||
return DESKTOP; | ||||||
}; | ||||||
|
||||||
/** | ||||||
* Custom useState hook which listens to resize events and | ||||||
* manage the viewport of the user's device. | ||||||
* | ||||||
* @returns {Object} viewport An object with the width of the user's viewport and the device type. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should define the type of this object refer this code |
||||||
*/ | ||||||
function useViewport() { | ||||||
const [viewport, setViewport] = useState({ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only save width in the state. Change only the width on resizing. Also, find the device type in the return statement. |
||||||
width: window.innerWidth, | ||||||
device: getDevice(window.innerWidth), | ||||||
}); | ||||||
|
||||||
useEffect(() => { | ||||||
// Event listener to track the window resize. | ||||||
const handleResize = () => { | ||||||
setViewport({ | ||||||
width: window.innerWidth, | ||||||
device: getDevice(window.innerWidth), | ||||||
}); | ||||||
}; | ||||||
window.addEventListener('resize', handleResize); | ||||||
|
||||||
return () => { | ||||||
window.removeEventListener('resize', handleResize); | ||||||
}; | ||||||
}, []); | ||||||
|
||||||
return { viewport }; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are returning a nested object here. Since viewport is an object you should change this
Suggested change
|
||||||
} | ||||||
|
||||||
export default useViewport; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.