-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Showing
8 changed files
with
245 additions
and
77 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
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 |
---|---|---|
@@ -1,73 +1 @@ | ||
import { DependencyList, EffectCallback } from 'react'; | ||
import * as React from 'react'; | ||
|
||
const { useState, useEffect, useLayoutEffect } = React; | ||
|
||
type MediaQueryObject = { [key: string]: string | number | boolean }; | ||
|
||
const camelToHyphen = (str: string) => | ||
str.replace(/[A-Z]/g, m => `-${m.toLowerCase()}`).toLowerCase(); | ||
|
||
const noWindowMatches: MediaQueryList = { | ||
media: '', | ||
addListener: noop, | ||
removeListener: noop, | ||
matches: false, | ||
onchange: noop, | ||
addEventListener: noop, | ||
removeEventListener: noop, | ||
dispatchEvent: (_: Event) => true | ||
}; | ||
|
||
const objectToString = (query: string | MediaQueryObject) => { | ||
if (typeof query === 'string') return query; | ||
return Object.entries(query) | ||
.map(([feature, value]) => { | ||
feature = camelToHyphen(feature); | ||
if (typeof value === 'boolean') { | ||
return value ? feature : `not ${feature}`; | ||
} | ||
if (typeof value === 'number' && /[height|width]$/.test(feature)) { | ||
value = `${value}px`; | ||
} | ||
return `(${feature}: ${value})`; | ||
}) | ||
.join(' and '); | ||
}; | ||
|
||
type Effect = (effect: EffectCallback, deps?: DependencyList) => void; | ||
const createUseMedia = (effect: Effect) => ( | ||
rawQuery: string | MediaQueryObject, | ||
defaultState: boolean = false | ||
) => { | ||
const [state, setState] = useState(defaultState); | ||
const query = objectToString(rawQuery); | ||
effect(() => { | ||
let mounted = true; | ||
const mql = | ||
typeof window === 'undefined' | ||
? noWindowMatches | ||
: window.matchMedia(query); | ||
const onChange = () => { | ||
if (!mounted) return; | ||
setState(!!mql.matches); | ||
}; | ||
|
||
mql.addListener(onChange); | ||
setState(mql.matches); | ||
|
||
return () => { | ||
mounted = false; | ||
mql.removeListener(onChange); | ||
}; | ||
}, [query]); | ||
|
||
return state; | ||
}; | ||
|
||
function noop() {} | ||
|
||
export const useMedia = createUseMedia(useEffect); | ||
export const useMediaLayout = createUseMedia(useLayoutEffect); | ||
|
||
export default useMedia; | ||
export {default, useMedia, useMediaLayout} from './useMedia'; |
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,4 @@ | ||
import {DependencyList, EffectCallback} from 'react'; | ||
|
||
export type Effect = (effect: EffectCallback, deps?: DependencyList) => void; | ||
export type MediaQueryObject = {[key: string]: string | number | boolean}; |
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,53 @@ | ||
import {useState, useEffect, useLayoutEffect} from 'react'; | ||
import {queryObjectToString, noop} from './utilities'; | ||
import {Effect, MediaQueryObject} from './types'; | ||
|
||
export const mockMediaQueryList: MediaQueryList = { | ||
media: '', | ||
matches: false, | ||
onchange: noop, | ||
addListener: noop, | ||
removeListener: noop, | ||
addEventListener: noop, | ||
removeEventListener: noop, | ||
dispatchEvent: (_: Event) => true, | ||
}; | ||
|
||
const createUseMedia = (effect: Effect) => ( | ||
rawQuery: string | MediaQueryObject, | ||
defaultState = false, | ||
) => { | ||
const [state, setState] = useState(defaultState); | ||
const query = queryObjectToString(rawQuery); | ||
|
||
effect(() => { | ||
let mounted = true; | ||
const mediaQueryList: MediaQueryList = | ||
typeof window === 'undefined' | ||
? mockMediaQueryList | ||
: window.matchMedia(query); | ||
|
||
const onChange = () => { | ||
if (!mounted) { | ||
return; | ||
} | ||
|
||
setState(Boolean(mediaQueryList.matches)); | ||
}; | ||
|
||
mediaQueryList.addListener(onChange); | ||
setState(mediaQueryList.matches); | ||
|
||
return () => { | ||
mounted = false; | ||
mediaQueryList.removeListener(onChange); | ||
}; | ||
}, [query]); | ||
|
||
return state; | ||
}; | ||
|
||
export const useMedia = createUseMedia(useEffect); | ||
export const useMediaLayout = createUseMedia(useLayoutEffect); | ||
|
||
export default useMedia; |
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,5 @@ | ||
export default function camelToHyphen(camelString: string) { | ||
return camelString | ||
.replace(/[A-Z]/g, (string) => `-${string.toLowerCase()}`) | ||
.toLowerCase(); | ||
} |
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,3 @@ | ||
export {default as camelToHyphen} from './camelToHyphen'; | ||
export {default as queryObjectToString} from './queryObjectToString'; | ||
export {default as noop} from './noop'; |
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 @@ | ||
export default function noop() {} |
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,30 @@ | ||
import {MediaQueryObject} from '../types'; | ||
import camelToHyphen from './camelToHyphen'; | ||
|
||
const QUERY_COMBINATOR = ' and '; | ||
|
||
export default function queryObjectToString(query: string | MediaQueryObject) { | ||
if (typeof query === 'string') { | ||
return query; | ||
} | ||
|
||
return Object.entries(query) | ||
.map(([feature, value]) => { | ||
const convertedFeature = camelToHyphen(feature); | ||
let convertedValue = value; | ||
|
||
if (typeof convertedValue === 'boolean') { | ||
return convertedValue ? convertedFeature : `not ${convertedFeature}`; | ||
} | ||
|
||
if ( | ||
typeof convertedValue === 'number' && | ||
/[height|width]$/.test(convertedFeature) | ||
) { | ||
convertedValue = `${convertedValue}px`; | ||
} | ||
|
||
return `(${convertedFeature}: ${convertedValue})`; | ||
}) | ||
.join(QUERY_COMBINATOR); | ||
} |