-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PageLayout: Implement responsive
hidden
prop (#2174)
* Implement responsive hidden prop * Write tests for useResponsiveValue * Document hidden prop * Update viewport range variables * Add hidden prop to header/footer/content * Update PageLayout tests * Create empty-garlics-clean.md * Fix lint errors
- Loading branch information
Showing
9 changed files
with
390 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
"@primer/react": minor | ||
--- | ||
|
||
Add a responsive `hidden` prop to `PageLayout.Header`, `PageLayout.Pane`, `PageLayout.Content`, and `PageLayout.Footer` that allows you to hide layout regions based on the viewport width. Example usage: | ||
|
||
```jsx | ||
// Hide pane on narrow viewports | ||
<PageLayout.Pane hidden={{narrow: true}}>...</PageLayout.Pane> | ||
``` |
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
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,99 @@ | ||
import {act, render} from '@testing-library/react' | ||
import MatchMediaMock from 'jest-matchmedia-mock' | ||
import {ResponsiveValue, useResponsiveValue, viewportRanges} from '../../hooks/useResponsiveValue' | ||
import React from 'react' | ||
|
||
let matchMedia: MatchMediaMock | ||
|
||
beforeAll(() => { | ||
matchMedia = new MatchMediaMock() | ||
}) | ||
|
||
afterEach(() => { | ||
matchMedia.clear() | ||
}) | ||
|
||
it('accepts non-responsive values', () => { | ||
const Component = () => { | ||
const value = useResponsiveValue('test', 'fallback') | ||
return <div>{value}</div> | ||
} | ||
|
||
const {getByText} = render(<Component />) | ||
|
||
expect(getByText('test')).toBeInTheDocument() | ||
}) | ||
|
||
it('returns narrow value when viewport is narrow', () => { | ||
const Component = () => { | ||
const value = useResponsiveValue({narrow: false, regular: true} as ResponsiveValue<boolean>, true) | ||
return <div>{JSON.stringify(value)}</div> | ||
} | ||
|
||
// Set narrow viewport | ||
act(() => { | ||
matchMedia.useMediaQuery(viewportRanges.narrow) | ||
}) | ||
|
||
const {getByText} = render(<Component />) | ||
|
||
expect(getByText('false')).toBeInTheDocument() | ||
}) | ||
|
||
it('returns wide value when viewport is wide', () => { | ||
const Component = () => { | ||
const value = useResponsiveValue( | ||
{narrow: 'narrowValue', regular: 'regularValue', wide: 'wideValue'} as ResponsiveValue<string>, | ||
'fallbackValue' | ||
) | ||
return <div>{value}</div> | ||
} | ||
|
||
// Set wide viewport | ||
act(() => { | ||
matchMedia.useMediaQuery(viewportRanges.wide) | ||
}) | ||
|
||
const {getByText} = render(<Component />) | ||
|
||
expect(getByText('wideValue')).toBeInTheDocument() | ||
}) | ||
|
||
it('returns regular value when viewport is regular', () => { | ||
const Component = () => { | ||
const value = useResponsiveValue( | ||
{narrow: 'narrowValue', regular: 'regularValue', wide: 'wideValue'} as ResponsiveValue<string>, | ||
'fallbackValue' | ||
) | ||
return <div>{value}</div> | ||
} | ||
|
||
// Set regular viewport | ||
act(() => { | ||
matchMedia.useMediaQuery(viewportRanges.regular) | ||
}) | ||
|
||
const {getByText} = render(<Component />) | ||
|
||
expect(getByText('regularValue')).toBeInTheDocument() | ||
}) | ||
|
||
it('returns fallback when no value is defined for current viewport', () => { | ||
const Component = () => { | ||
const value = useResponsiveValue( | ||
// Missing value for `regular` viewports | ||
{narrow: 'narrowValue', wide: 'wideValue'} as ResponsiveValue<string>, | ||
'fallbackValue' | ||
) | ||
return <div>{value}</div> | ||
} | ||
|
||
// Set regular viewport | ||
act(() => { | ||
matchMedia.useMediaQuery(viewportRanges.regular) | ||
}) | ||
|
||
const {getByText} = render(<Component />) | ||
|
||
expect(getByText('fallbackValue')).toBeInTheDocument() | ||
}) |
Oops, something went wrong.