File tree Expand file tree Collapse file tree 5 files changed +108
-10
lines changed Expand file tree Collapse file tree 5 files changed +108
-10
lines changed Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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' ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments