-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: cleanup hooks to use useBreakpoint for screen width changes
- Loading branch information
1 parent
b53dce6
commit 9b1c5db
Showing
4 changed files
with
78 additions
and
92 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
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 |
---|---|---|
@@ -1,29 +1,35 @@ | ||
import React from 'react' | ||
import {ThemeContext} from 'styled-components' | ||
|
||
function useBreakpoint(breakpoint, minMax = 'min') { | ||
// Handle string values from themes with units at the end | ||
if (typeof breakpoint === 'string') { | ||
breakpoint = parseInt(breakpoint, 10) | ||
} | ||
|
||
const matchMedia = React.useMemo(() => { | ||
if (typeof window === 'undefined') { | ||
const eventTarget = new EventTarget() | ||
eventTarget.matches = false | ||
return eventTarget | ||
} | ||
return window.matchMedia(`(${minMax}-width: ${breakpoint - (minMax === 'min' ? 0 : 1)}px)`) | ||
}, [breakpoint, minMax]) | ||
const getMatches = query => (typeof window !== 'undefined' ? window.matchMedia(query).matches : false) | ||
|
||
const [matches, setMatches] = React.useState(matchMedia.matches) | ||
const handleChange = React.useCallback(() => setMatches(matchMedia.matches), [matchMedia]) | ||
// The MIT License (MIT) | ||
// Copyright (c) 2020 Julien CARON | ||
// https://github.com/juliencrn/usehooks-ts/blob/master/packages/usehooks-ts/src/useMediaQuery/useMediaQuery.ts | ||
export function useMediaQuery(query) { | ||
const [matches, setMatches] = React.useState(getMatches(query)) | ||
const handleChange = React.useCallback(() => setMatches(getMatches(query)), [query]) | ||
|
||
React.useEffect(() => { | ||
handleChange() | ||
const matchMedia = window.matchMedia(query) | ||
matchMedia.addEventListener('change', handleChange) | ||
return () => matchMedia.removeEventListener('change', handleChange) | ||
}, [matchMedia, handleChange]) | ||
}, [query, handleChange]) | ||
|
||
return matches | ||
} | ||
|
||
export function useBreakpoint(breakpoint, minMax = 'min') { | ||
// Handle string values from themes with units at the end | ||
const px = typeof breakpoint === 'string' ? parseInt(breakpoint, 10) : breakpoint | ||
return useMediaQuery(`(${minMax}-width: ${px - (minMax === 'min' ? 0 : 1)}px)`) | ||
} | ||
|
||
// a common breakpoint where things change on mobile | ||
export function useIsMobile() { | ||
const theme = React.useContext(ThemeContext) | ||
return useBreakpoint(theme.breakpoints[2], 'max') | ||
} | ||
|
||
export default useBreakpoint |
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