Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ yarn add use-custom-hooks
- [useGeoLocation](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usegeolocation)
- [useLocalStorage](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-uselocalstorage)
- [useMousePosition](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usemouseposition)
- [useViewport](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usepreview)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [useViewport](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-usepreview)
- [useViewport](https://github.com/aromalanil/useCustomHooks/tree/master/docs#-useviewport)


## 📄 Documentation
For documentaion, examples and other details refer [Documentation](https://github.com/aromalanil/useCustomHooks/tree/master/docs)
Expand Down
44 changes: 44 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [useMousePosition](#-usemouseposition)
- [useGeoLocation](#-usegeolocation)
- [usePrevious](#-useprevious)
- [useViewport](#-useviewport)

</br>

Expand Down Expand Up @@ -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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The viewport is not in the object, I think you meant this.

Suggested change
const { viewport } = useViewport();
const { device } = useViewport();

/*
Using Object destructuring we can get width and device
Copy link
Owner

@aromalanil aromalanil Oct 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Using Object destructuring we can get width and device
Using Object destructuring we can get the width and device type

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

Expand Down
55 changes: 55 additions & 0 deletions hooks/useViewport.js
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';
Copy link
Owner

Choose a reason for hiding this comment

The 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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @returns {*} The constant corresponding the screen size.
* @returns {String} The device type corresponding the screen size.

*/
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.
Copy link
Owner

Choose a reason for hiding this comment

The 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({
Copy link
Owner

Choose a reason for hiding this comment

The 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 };
Copy link
Owner

Choose a reason for hiding this comment

The 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
return { viewport };
return viewport;

}

export default useViewport;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-custom-hooks",
"version": "1.7.0",
"version": "1.8.0",
"description": "A collection of Custom Hooks for React.",
"main": "index.js",
"scripts": {
Expand Down