Skip to content

Commit 37c9cdb

Browse files
document useMatchMediaQuery hook
1 parent a0c2cfd commit 37c9cdb

4 files changed

Lines changed: 21 additions & 2 deletions

File tree

readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This package includes different tools that support you with common tasks.
1212
- [useInterval](#useinterval)
1313
- [Async Data](#async-data)
1414
- [LocationProvider](#locationprovider)
15+
- [useMatchMediaQuery](#usematchmediaquery)
1516
- [Build & Publish](#build--publish)
1617
- [Information](#information)
1718

@@ -149,6 +150,20 @@ const MyComponent = () => {
149150
}
150151
```
151152

153+
### useMatchMediaQuery
154+
155+
This hook is based on the `window.matchQuery` API and can be used to find out if a certain media query matches the current window.
156+
157+
```tsx
158+
import { useMatchMediaQuery } from '@aboutbits/react-toolbox'
159+
160+
const TestComponent = () => {
161+
const matches = useMatchMediaQuery('(min-width : 500px)')
162+
if (matches) return <div>visible</div>
163+
return null
164+
}
165+
```
166+
152167
## Build & Publish
153168

154169
To publish the package commit all changes and push them to main. Then run one of the following commands locally:

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
LocationContext,
77
LocationContextValue,
88
} from './location-provider/LocationProvider'
9+
import { useMatchMediaQuery } from './useMatchMediaQuery/useMatchMediaQuery'
910

1011
export {
1112
useInterval,
@@ -15,4 +16,5 @@ export {
1516
LocationProvider,
1617
LocationContext,
1718
LocationContextValue,
19+
useMatchMediaQuery,
1820
}

src/useMatchMediaQuery/__test__/useMatchMediaQuery.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import isMatching from 'css-mediaquery'
33
import { render, RenderResult } from '@testing-library/react'
4-
import useMatchMediaQuery from '../useMatchMediaQuery'
4+
import { useMatchMediaQuery } from '../useMatchMediaQuery'
55

66
beforeEach(() => {
77
// mock window.matchMedia (use 'css-mediaquery')

src/useMatchMediaQuery/useMatchMediaQuery.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState, useEffect } from 'react'
22

3-
export default function (query: string): boolean | undefined {
3+
function useMatchMediaQuery(query: string): boolean | undefined {
44
if (typeof window !== 'object' || !window.matchMedia) return
55

66
const [matches, setMatches] = useState(window.matchMedia(query).matches)
@@ -22,3 +22,5 @@ export default function (query: string): boolean | undefined {
2222

2323
return matches
2424
}
25+
26+
export { useMatchMediaQuery }

0 commit comments

Comments
 (0)