-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
98 additions
and
1 deletion.
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
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,43 @@ | ||
# `createBreakpoint` | ||
|
||
## Usage | ||
|
||
### use default breakpoint | ||
|
||
laptopL: 1440, laptop: 1024, tablet: 768 | ||
|
||
```jsx | ||
import React from "react"; | ||
import { createBreakpoint } from "react-use"; | ||
|
||
const useBreakpoint = createBreakpoint(); | ||
|
||
const Demo = () => { | ||
const breakpoint = useBreakpoint(); | ||
|
||
if (breakpoint === "laptopL") return <div> This is very big Laptop </div>; | ||
else if (breakpoint == "laptop") return <div> This is Laptop</div>; | ||
else if (breakpoint == "tablet") return <div> This is Tablet</div>; | ||
else return <div> Too small!</div>; | ||
}; | ||
``` | ||
|
||
### use custom breakpoint | ||
|
||
XL: 1280, L: 768, S: 350 | ||
|
||
```jsx | ||
import React from "react"; | ||
import { createBreakpoint } from "react-use"; | ||
|
||
const useBreakpoint = createBreakpoint({ XL: 1280, L: 768, S: 350 }); | ||
|
||
const Demo = () => { | ||
const breakpoint = useBreakpoint(); | ||
|
||
if (breakpoint === "XL") return <div> XL </div>; | ||
else if (breakpoint == "L") return <div> LoL</div>; | ||
else if (breakpoint == "S") return <div> Sexyy</div>; | ||
else return <div> Wth</div>; | ||
}; | ||
``` |
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,29 @@ | ||
import { number, withKnobs } from "@storybook/addon-knobs"; | ||
import { storiesOf } from "@storybook/react"; | ||
import React from "react"; | ||
import { createBreakpoint } from ".."; | ||
import ShowDocs from "./util/ShowDocs"; | ||
|
||
const useBreakpointA = createBreakpoint(); | ||
const useBreakpointB = createBreakpoint({ mobileM: 350, laptop: 1024, tablet: 768 }); | ||
|
||
const Demo = () => { | ||
const breakpointA = useBreakpointA(); | ||
const breakpointB = useBreakpointB(); | ||
return ( | ||
<div> | ||
<p>{"try resize your window"}</p> | ||
<p>{"createBreakpoint() #default : { laptopL: 1440, laptop: 1024, tablet: 768 }"}</p> | ||
<p>{breakpointA}</p> | ||
<p>{"createBreakpoint({ mobileM: 350, laptop: 1024, tablet: 768 })"}</p> | ||
<p>{breakpointB}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf("sensors|createBreakpoint", module) | ||
.addDecorator(withKnobs) | ||
.add("Docs", () => <ShowDocs md={require("../../docs/createBreakpoint.md")} />) | ||
.add("Demo", () => { | ||
return <Demo />; | ||
}); |
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,24 @@ | ||
import { useEffect, useState, useMemo } from 'react' | ||
|
||
const createBreakpoint = (breakpoints: { [name: string]: number } = { laptopL: 1440, laptop: 1024, tablet: 768 }) => () => { | ||
const [screen, setScreen] = useState(0) | ||
|
||
useEffect(() => { | ||
const setSideScreen = (): void => { | ||
setScreen(window.innerWidth) | ||
} | ||
setSideScreen() | ||
window.addEventListener('resize', setSideScreen) | ||
return () => { | ||
window.removeEventListener('resize', setSideScreen) | ||
} | ||
}) | ||
const sortedBreakpoints = useMemo(() => Object.entries(breakpoints).sort((a, b) => a[1] >= b[1] ? 1 : -1), [breakpoints]) | ||
const result = sortedBreakpoints.reduce((acc, [name, width]) => { | ||
if (screen >= width) return name | ||
else return acc | ||
}, sortedBreakpoints[0][0]) | ||
return result | ||
} | ||
|
||
export default createBreakpoint |
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