-
Notifications
You must be signed in to change notification settings - Fork 520
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
PageLayout: Implement responsive hidden
prop
#2174
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6383111
Implement responsive hidden prop
colebemis eca7133
Write tests for useResponsiveValue
colebemis 1ca46d4
Document hidden prop
colebemis 30ba162
Update viewport range variables
colebemis 79c1380
Add hidden prop to header/footer/content
colebemis 1c683eb
Update PageLayout tests
colebemis cbcae8f
Merge remote-tracking branch 'origin/main' into pagelayout-hidden-prop
colebemis 43fc344
Create empty-garlics-clean.md
colebemis a6b1711
Fix lint errors
colebemis 343a5c3
Merge branch 'pagelayout-hidden-prop' of github.com:primer/react into…
colebemis 99d8907
Merge branch 'main' into pagelayout-hidden-prop
colebemis 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copied from https://github.com/streamich/react-use/blob/master/src/useMedia.ts | ||
|
||
import React from 'react' | ||
|
||
const getInitialState = (query: string, defaultState?: boolean) => { | ||
// Prevent a React hydration mismatch when a default value is provided by not defaulting to window.matchMedia(query).matches. | ||
if (defaultState !== undefined) { | ||
return defaultState | ||
} | ||
|
||
if (typeof window !== 'undefined') { | ||
return window.matchMedia(query).matches | ||
} | ||
|
||
// A default value has not been provided, and you are rendering on the server, warn of a possible hydration mismatch when defaulting to false. | ||
if (process.env.NODE_ENV !== 'production') { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'`useMedia` When server side rendering, defaultState should be defined to prevent a hydration mismatches.' | ||
) | ||
} | ||
|
||
return false | ||
} | ||
|
||
export function useMedia(query: string, defaultState?: boolean) { | ||
const [state, setState] = React.useState(getInitialState(query, defaultState)) | ||
|
||
React.useEffect(() => { | ||
let mounted = true | ||
const mql = window.matchMedia(query) | ||
const onChange = () => { | ||
if (!mounted) { | ||
return | ||
} | ||
setState(!!mql.matches) | ||
} | ||
|
||
mql.addListener(onChange) | ||
setState(mql.matches) | ||
|
||
return () => { | ||
mounted = false | ||
mql.removeListener(onChange) | ||
} | ||
}, [query]) | ||
|
||
return state | ||
} |
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,60 @@ | ||
import {useMedia} from './useMedia' | ||
|
||
// TODO: Write tests for this hook | ||
|
||
export type ResponsiveValue<TRegular, TNarrow = TRegular, TWide = TRegular> = { | ||
narrow?: TNarrow // Applies when viewport is < 768px | ||
regular?: TRegular // Applies when viewports is >= 768px | ||
wide?: TWide // Applies when viewports is >= 1400px | ||
} | ||
|
||
/** | ||
* Flattens all possible value types into single union type | ||
* For example, if `T` is `'none' | 'line' | Responsive<'none' | 'line' | 'filled'>`, | ||
* `FlattenResponsiveValue<T>` will be `'none' | 'line' | 'filled'` | ||
*/ | ||
export type FlattenResponsiveValue<T> = | ||
| (T extends ResponsiveValue<infer TRegular, infer TNarrow, infer TWide> ? TRegular | TNarrow | TWide : never) | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| Exclude<T, ResponsiveValue<any>> | ||
|
||
/** | ||
* Checks if the value is a responsive value. | ||
* In other words, is it an object with viewport range keys? | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function isResponsiveValue(value: any): value is ResponsiveValue<any> { | ||
return typeof value === 'object' && Object.keys(value).some(key => ['narrow', 'regular', 'wide'].includes(key)) | ||
} | ||
|
||
/** | ||
* Resolves responsive values based on the current viewport width. | ||
* For example, if the current viewport width is less than 768px, the value of `{regular: 'foo', narrow: 'bar'}` this hook will return `'bar'`. | ||
*/ | ||
export function useResponsiveValue<T, F>(value: T, fallback: F): FlattenResponsiveValue<T> | F { | ||
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. ❤️ this hook |
||
// Check viewport size | ||
// TODO: Get these breakpoint values from primer/primitives | ||
const isNarrowViewport = useMedia('(max-width: 767px)') // < 768px | ||
const isRegularViewport = useMedia('(min-width: 768px)') // >= 768px | ||
colebemis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const isWideViewport = useMedia('(min-width: 1400px)') // >= 1400px | ||
|
||
if (isResponsiveValue(value)) { | ||
// If we've reached this line, we know that value is a responsive value | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const responsiveValue = value as Extract<T, ResponsiveValue<any>> | ||
|
||
if (isNarrowViewport && 'narrow' in responsiveValue) { | ||
return responsiveValue.narrow | ||
} else if (isWideViewport && 'wide' in responsiveValue) { | ||
return responsiveValue.wide | ||
} else if (isRegularViewport && 'regular' in responsiveValue) { | ||
return responsiveValue.regular | ||
} else { | ||
return fallback | ||
} | ||
} else { | ||
// If we've reached this line, we know that value is not a responsive value | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return value as Exclude<T, ResponsiveValue<any>> | ||
} | ||
} |
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.
Just confirming that we want
hidden
to prevent screen readers from accessing the content too.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.
I believe so 👍 cc @hectahertz @alliethu @ichelsea to confirm
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.
If it's part of the page layout that shouldn't be able to be accessed by anyone, then we'd want to hide if from screen reader users too.