-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
feat(hooks): build local useBreakpointValue
and useMediaQuery
hooks
#13695
Merged
pettinarip
merged 3 commits into
ethereum:dev
from
TylerAPfledderer:feat/local-use-breakpoint-value
Sep 10, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
a03fc10
feat(hooks): build local `useBreakpointValue` and `useMediaQuery` hooks
TylerAPfledderer 8daf17d
refactor: replace Chakra `useBreakpointValue` with local export
TylerAPfledderer 1d4ef15
Merge remote-tracking branch 'upstream/dev' into feat/local-use-break…
TylerAPfledderer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { screens } from "../../tailwind/screens" | ||
|
||
import { useMediaQuery } from "./useMediaQuery" | ||
|
||
const breakpointMap = { | ||
// Essentially to query from no width if smaller than "sm" | ||
base: "0px", | ||
...screens, | ||
} | ||
|
||
type BreakpointKeys = keyof typeof breakpointMap | ||
|
||
export const useBreakpointValue = <T = unknown>( | ||
values: Partial<Record<BreakpointKeys, T>>, | ||
fallbackBreakpoint?: BreakpointKeys | ||
): T => { | ||
const breakpointKeys = Object.keys(values) as BreakpointKeys[] | ||
|
||
let fallbackPassed = false | ||
|
||
const setBreakpoints = Object.entries(breakpointMap) | ||
.map(([breakpoint, value]) => { | ||
const item = { | ||
breakpoint, | ||
query: `(min-width: ${value})`, | ||
fallback: !fallbackPassed, | ||
} | ||
|
||
if (breakpoint === fallbackBreakpoint) { | ||
fallbackPassed = true | ||
} | ||
|
||
return item | ||
}) | ||
.filter(({ breakpoint }) => | ||
breakpointKeys.includes(breakpoint as BreakpointKeys) | ||
) | ||
|
||
const fallbackQueries = setBreakpoints.map(({ fallback }) => fallback) | ||
|
||
const queryValues = useMediaQuery( | ||
setBreakpoints.map((bp) => bp.query), | ||
fallbackQueries | ||
) | ||
|
||
const index = queryValues.lastIndexOf(true) | ||
|
||
const breakpointPicked = breakpointKeys[index] | ||
|
||
return values[breakpointPicked] as T | ||
} |
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,63 @@ | ||
import { useState } from "react" | ||
|
||
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect" | ||
|
||
const IS_SERVER = typeof window === "undefined" | ||
|
||
/** | ||
* Modified from https://usehooks-ts.com/react-hook/use-media-query | ||
* to account for an array of queries. | ||
* | ||
* Modifications sourced from Chakra-UI. | ||
* https://github.com/chakra-ui/chakra-ui/blob/main/packages/hooks/src/use-media-query.ts | ||
*/ | ||
export function useMediaQuery( | ||
queries: string[], | ||
fallbackQueries?: boolean[] | ||
): boolean[] { | ||
const _fallbackQueries = fallbackQueries?.filter( | ||
(val) => val !== null | ||
) as boolean[] | ||
|
||
const [value, setValue] = useState(() => { | ||
return queries.map((query, idx) => ({ | ||
media: query, | ||
matches: !IS_SERVER | ||
? window.matchMedia(query).matches | ||
: !!_fallbackQueries[idx], | ||
})) | ||
}) | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
const matchMedias = queries.map((query) => window.matchMedia(query)) | ||
|
||
// Handles the change event of the media query. | ||
function handleChange(evt: MediaQueryListEvent) { | ||
setValue((prev) => { | ||
return prev.slice().map((item) => { | ||
if (item.media === evt.media) return { ...item, matches: evt.matches } | ||
return item | ||
}) | ||
}) | ||
return | ||
} | ||
|
||
const cleanups = matchMedias.map((v) => listen(v, handleChange)) | ||
return () => cleanups.forEach((fn) => fn()) | ||
}, [queries]) | ||
|
||
return value.map((item) => item.matches) | ||
} | ||
|
||
type MediaQueryCallback = (event: MediaQueryListEvent) => void | ||
|
||
function listen(query: MediaQueryList, callback: MediaQueryCallback) { | ||
// Use deprecated `addListener` and `removeListener` to support Safari < 14 (#135) | ||
try { | ||
query.addEventListener("change", callback) | ||
return () => query.removeEventListener("change", callback) | ||
} catch (e) { | ||
query.addListener(callback) | ||
return () => query.removeListener(callback) | ||
} | ||
} |
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
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. Nice! |
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,9 @@ | ||
import type { ScreensConfig } from "tailwindcss/types/config" | ||
|
||
export const screens = { | ||
sm: "480px", | ||
md: "768px", | ||
lg: "992px", | ||
xl: "1280px", | ||
"2xl": "1536px", | ||
} satisfies ScreensConfig |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍🏼