Skip to content

Commit b0a3257

Browse files
kendallgassnerkendallgassner
andauthored
Add hooks to detect lessContrast + highContrast preferences (#681)
Co-authored-by: kendallgassner <[email protected]>
1 parent 27169ab commit b0a3257

File tree

5 files changed

+108
-10
lines changed

5 files changed

+108
-10
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import { usePrefersHighContrast } from '../usePrefersHighContrast';
3+
4+
test('it detect if prefers contrast more is set', () => {
5+
// @ts-ignore
6+
jest.spyOn(window, 'matchMedia').mockImplementation(() => ({
7+
matches: true,
8+
addListener: jest.fn(),
9+
removeListener: jest.fn(),
10+
}));
11+
12+
const { result } = renderHook(() => usePrefersHighContrast());
13+
expect(result.current).toBe(true);
14+
});
15+
16+
test('it detect if prefers contrast less is set', () => {
17+
const addListener = jest.fn();
18+
const removeListener = jest.fn();
19+
// @ts-ignore
20+
jest.spyOn(window, 'matchMedia').mockImplementation(() => ({
21+
matches: false,
22+
addListener,
23+
removeListener,
24+
}));
25+
26+
const { result, unmount } = renderHook(() => usePrefersHighContrast());
27+
28+
expect(result.current).toBe(false);
29+
unmount();
30+
expect(removeListener).toHaveBeenCalled();
31+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import { usePrefersLessContrast } from '../usePrefersLessContrast';
3+
4+
test('it detect if prefers contrast more is set', () => {
5+
// @ts-ignore
6+
jest.spyOn(window, 'matchMedia').mockImplementation(() => ({
7+
matches: false,
8+
addListener: jest.fn(),
9+
removeListener: jest.fn(),
10+
}));
11+
12+
const { result } = renderHook(() => usePrefersLessContrast());
13+
expect(result.current).toBe(false);
14+
});
15+
16+
test('it detect if prefers contrast less is set', () => {
17+
const addListener = jest.fn();
18+
const removeListener = jest.fn();
19+
// @ts-ignore
20+
jest.spyOn(window, 'matchMedia').mockImplementation(() => ({
21+
matches: true,
22+
addListener,
23+
removeListener,
24+
}));
25+
26+
const { result, unmount } = renderHook(() => usePrefersLessContrast());
27+
28+
expect(result.current).toBe(true);
29+
unmount();
30+
expect(removeListener).toHaveBeenCalled();
31+
});

packages/hooks/src/index.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
export * from "./useClickOutside";
2-
export * from "./useConditionalAnimation";
3-
export * from "./useDarkMode";
4-
export * from "./useKeyboardNavigation";
5-
export * from "./useLocale";
6-
export * from "./useMatchMedia";
7-
export * from "./useOverflowing";
8-
export * from "./usePopper";
9-
export * from "./useReducedMotion";
10-
export * from "./useStickyState";
1+
export * from './useClickOutside';
2+
export * from './useConditionalAnimation';
3+
export * from './useDarkMode';
4+
export * from './useKeyboardNavigation';
5+
export * from './useLocale';
6+
export * from './useMatchMedia';
7+
export * from './useOverflowing';
8+
export * from './usePopper';
9+
export * from './useReducedMotion';
10+
export * from './usePrefersHighContrast';
11+
export * from './usePrefersLessContrast';
12+
export * from './useStickyState';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { useMatchMedia } from './useMatchMedia';
2+
3+
/**
4+
* Determine if the user has "prefers-contrast: more" enabled in their browser.
5+
*
6+
* @example
7+
* const Example = () => {
8+
* const isPrefersHighContrast = usePrefersHighContrast();
9+
*
10+
* return (
11+
* <div className={makeClass(!isPrefersHighContrast && styles.highContrast)}>Content</div>
12+
* );
13+
* };
14+
*/
15+
export const usePrefersHighContrast = () => {
16+
return useMatchMedia('(prefers-contrast: more)');
17+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { useMatchMedia } from './useMatchMedia';
2+
3+
/**
4+
* Determine if the user has "prefers-contrast: less" enabled in their browser.
5+
*
6+
* @example
7+
* const Example = () => {
8+
* const isPrefersLessContrast = usePrefersLessContrast();
9+
*
10+
* return (
11+
* <div className={makeClass(!isPrefersLessContrast && styles.highContrast)}>Content</div>
12+
* );
13+
* };
14+
*/
15+
export const usePrefersLessContrast = () => {
16+
return useMatchMedia('(prefers-contrast: less)');
17+
};

0 commit comments

Comments
 (0)