File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
154169To publish the package commit all changes and push them to main. Then run one of the following commands locally:
Original file line number Diff line number Diff line change 66 LocationContext ,
77 LocationContextValue ,
88} from './location-provider/LocationProvider'
9+ import { useMatchMediaQuery } from './useMatchMediaQuery/useMatchMediaQuery'
910
1011export {
1112 useInterval ,
@@ -15,4 +16,5 @@ export {
1516 LocationProvider ,
1617 LocationContext ,
1718 LocationContextValue ,
19+ useMatchMediaQuery ,
1820}
Original file line number Diff line number Diff line change 11import React from 'react'
22import isMatching from 'css-mediaquery'
33import { render , RenderResult } from '@testing-library/react'
4- import useMatchMediaQuery from '../useMatchMediaQuery'
4+ import { useMatchMediaQuery } from '../useMatchMediaQuery'
55
66beforeEach ( ( ) => {
77 // mock window.matchMedia (use 'css-mediaquery')
Original file line number Diff line number Diff line change 11import { 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 }
You can’t perform that action at this time.
0 commit comments