Skip to content

Commit

Permalink
Add new useMedia hook (#2511)
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer authored May 15, 2024
1 parent 3d3ae20 commit 3e98f16
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-files-smell.md
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).
22 changes: 22 additions & 0 deletions packages/circuit-ui/hooks/useMedia/useMedia.mdx
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.
36 changes: 36 additions & 0 deletions packages/circuit-ui/hooks/useMedia/useMedia.stories.tsx
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>
);
};
1 change: 1 addition & 0 deletions packages/circuit-ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,4 @@ export { useEscapeKey } from './hooks/useEscapeKey/index.js';
export { useFocusList } from './hooks/useFocusList/index.js';
export { useCollapsible } from './hooks/useCollapsible/index.js';
export { useSwipe } from './hooks/useSwipe/index.js';
export { useMedia } from './hooks/useMedia/index.js';

0 comments on commit 3e98f16

Please sign in to comment.