Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Created May 4, 2020 18:33
Show Gist options
  • Save gladchinda/3fa1a8828ed0740db05640a651de359d to your computer and use it in GitHub Desktop.
Save gladchinda/3fa1a8828ed0740db05640a651de359d to your computer and use it in GitHub Desktop.
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