Created
May 4, 2020 18:33
-
-
Save gladchinda/3fa1a8828ed0740db05640a651de359d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { useState, useEffect, useRef, useMemo } from 'react'; | |
const COLOR_SCHEMES = ['no-preference', 'dark', 'light']; | |
const DEFAULT_TARGET_COLOR_SCHEME = 'light'; | |
function resolveTargetColorScheme(scheme) { | |
if (!( | |
COLOR_SCHEMES.includes(scheme = String(scheme).toLowerCase()) || | |
scheme === 'no-preference' | |
)) scheme = DEFAULT_TARGET_COLOR_SCHEME; | |
return scheme; | |
} | |
function getCurrentColorScheme() { | |
const QUERIES = {}; | |
return (getCurrentColorScheme = function() { | |
for (let scheme of COLOR_SCHEMES) { | |
const query = QUERIES.hasOwnProperty(scheme) | |
? QUERIES[scheme] | |
: (QUERIES[scheme] = matchMedia(`(prefers-color-scheme: ${scheme})`)); | |
if (query.matches) return { query, scheme }; | |
} | |
})(); | |
} | |
// Define and export the `useColorScheme` hook | |
export default function useColorScheme() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment