-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
5752f30
commit efe0dba
Showing
7 changed files
with
152 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 |
---|---|---|
|
@@ -100,6 +100,7 @@ export const menus = [ | |
'useScroll', | ||
'useSize', | ||
'useFocusWithin', | ||
'useMediaQuery', | ||
], | ||
}, | ||
{ | ||
|
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,42 @@ | ||
import { renderHook, act } from '../../utils/tests'; | ||
import useMediaQuery from '..'; | ||
|
||
Object.defineProperty(window, 'matchMedia', { | ||
writable: true, | ||
value: jest.fn().mockImplementation((query) => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), // deprecated | ||
removeListener: jest.fn(), // deprecated | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn(), | ||
})), | ||
}); | ||
|
||
describe('useMediaQuery', () => { | ||
function changeWidth(width: number) { | ||
act(() => { | ||
(global as any).innerWidth = width; | ||
(global as any).dispatchEvent(new Event('resize')); | ||
}); | ||
} | ||
changeWidth(1024); | ||
|
||
it('should be defined', () => { | ||
expect(useMediaQuery).toBeDefined(); | ||
}); | ||
|
||
it('should return the correct value', () => { | ||
const { result } = renderHook(() => useMediaQuery(['(min-width: 1024px)'], [true], false)); | ||
expect(result.current[0]).toBe(false); | ||
}); | ||
|
||
it('should return the correct value when the media query changes', () => { | ||
const { result } = renderHook(() => useMediaQuery(['(min-width: 1024px)'], [true], false)); | ||
expect(result.current[0]).toBe(false); | ||
changeWidth(768); | ||
expect(result.current[0]).toBe(false); | ||
}); | ||
}); |
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,25 @@ | ||
/** | ||
* title: Get responsive info in components | ||
* desc: By calling useMediaQuery in components, you can retrieve the responsive infomation of the browser page and subscribe to it at the same time. | ||
* | ||
* title.zh-CN: 在组件中获取响应式信息 | ||
* desc.zh-CN: 在组件中调用 useMediaQuery 可以获取并订阅浏览器窗口的响应式信息。 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { useMediaQuery } from 'ahooks'; | ||
|
||
export default function () { | ||
const [value, currentQuery] = useMediaQuery( | ||
['(min-width: 1024px)', '(min-width: 768px)', '(min-width: 320px)'], | ||
[3, 2, 1], | ||
0, | ||
); | ||
|
||
return ( | ||
<div> | ||
<div>currentValue: {value.toString()}</div> | ||
<div>currentQuery: {currentQuery}</div> | ||
</div> | ||
); | ||
} |
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,25 @@ | ||
--- | ||
nav: | ||
path: /hooks | ||
--- | ||
|
||
# useMediaQuery | ||
|
||
React Hook for getting responsive info. | ||
|
||
## Examples | ||
|
||
### Get responsive info in components | ||
|
||
<code src="./demo/demo1.tsx" /> | ||
|
||
## API | ||
|
||
```typescript | ||
interface ResponsiveInfo { | ||
value: number; | ||
currentQuery: string; | ||
} | ||
|
||
function useMediaQuery(queries: K[], values: T[], defaultValue: T): ResponsiveInfo; | ||
``` |
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,32 @@ | ||
import { useRef, useState } from 'react'; | ||
|
||
function useMediaQuery<K, T>(queries: K[], values: T[], defaultValue: T) { | ||
const mediaQueryLists = useRef<MediaQueryList[]>([]); | ||
const [value, setValue] = useState<T>(defaultValue); | ||
const [currentQuery, setCurrentQuery] = useState<string>((queries as MediaQueryList[])[0].media); | ||
|
||
const getValue = () => { | ||
const index = mediaQueryLists.current.findIndex((mql) => mql.matches); | ||
setCurrentQuery(mediaQueryLists.current?.[index]?.media || ''); | ||
return values?.[index] || defaultValue; | ||
}; | ||
|
||
const handleQueryListener = () => setValue(getValue()); | ||
|
||
const handleMediaQueryLists = () => { | ||
mediaQueryLists.current.forEach((mql) => | ||
mql.removeEventListener('change', handleQueryListener), | ||
); | ||
mediaQueryLists.current = queries.map((query) => query && window.matchMedia(query as string)); | ||
mediaQueryLists.current.forEach((mql) => mql.addEventListener('change', handleQueryListener)); | ||
setValue(getValue()); | ||
}; | ||
|
||
useState(() => { | ||
handleMediaQueryLists(); | ||
}); | ||
|
||
return [value, currentQuery]; | ||
} | ||
|
||
export default useMediaQuery; |
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,25 @@ | ||
--- | ||
nav: | ||
path: /hooks | ||
--- | ||
|
||
# useMediaQuery | ||
|
||
获取响应式信息。 | ||
|
||
## 代码演示 | ||
|
||
### 在组件中获取响应式信息 | ||
|
||
<code src="./demo/demo1.tsx" /> | ||
|
||
## API | ||
|
||
```typescript | ||
interface ResponsiveInfo { | ||
value: number; | ||
currentQuery: string; | ||
} | ||
|
||
function useMediaQuery(queries: K[], values: T[], defaultValue: T): ResponsiveInfo; | ||
``` |