-
Notifications
You must be signed in to change notification settings - Fork 127
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
1 parent
3d3ae20
commit 3e98f16
Showing
4 changed files
with
64 additions
and
0 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,5 @@ | ||
--- | ||
"@sumup/circuit-ui": minor | ||
--- | ||
|
||
Added a new `useMedia` hook to track the state of a [media query](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries). |
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,22 @@ | ||
import { Meta, Status, Story } from '../../../../.storybook/components'; | ||
import * as Stories from './useMedia.stories'; | ||
|
||
<Meta of={Stories} /> | ||
|
||
# useMedia() | ||
|
||
<Status variant="stable" /> | ||
|
||
Tracks the state of a [media query](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries). | ||
|
||
<Story of={Stories.Example} /> | ||
|
||
```ts | ||
function useMedia(query: string, initialState = false): boolean; | ||
``` | ||
|
||
## Usage | ||
|
||
[Media queries](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries) allow you to apply conditional logic depending on a device's media type (such as print vs. screen) or other features or characteristics such as screen resolution or orientation, aspect ratio, browser viewport width or height, user preferences such as preferring reduced motion, data usage, or transparency. | ||
|
||
Whenever possible, use CSS instead of JavaScript to track the state of a media query. CSS is generally faster to load and parse, while JavaScript might be loaded asynchronously and needs to be executed before it takes effect. The `useMedia` uses the `window.matchMedia` browser API and has to default to a fallback value during server-side rendering, potentially causing a flash of wrong content. |
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,36 @@ | ||
/** | ||
* Copyright 2021, SumUp Ltd. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Card } from '../../components/Card/Card.js'; | ||
|
||
import { useMedia } from './useMedia.js'; | ||
|
||
export default { | ||
title: 'Hooks/useMedia', | ||
}; | ||
|
||
export const Example = () => { | ||
const isMatch = useMedia('(max-width: 800px)'); | ||
|
||
const background = isMatch | ||
? 'var(--cui-bg-success)' | ||
: 'var(--cui-bg-warning)'; | ||
|
||
return ( | ||
<Card style={{ background }}> | ||
{`The viewport is ${isMatch ? 'less' : 'more'} than 800px wide.`} | ||
</Card> | ||
); | ||
}; |
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