Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react check if browser is in dark mode

if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
    // dark mode
}

//To watch for changes:

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
    const newColorScheme = e.matches ? "dark" : "light";
});

//Or in React Hooks:

const [mode, setMode] = useState<"light" | "dark" | undefined>(
    window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? "dark" :"light"
);

useEffect(() => {
  const modeMe = (e: any) => {
    setMode(e.matches ? "dark" : "light");
  }  
  window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', modeMe);
  return window.matchMedia('(prefers-color-scheme: dark)').removeListener(modeMe);
}, []);
Comment

PREVIOUS NEXT
Code Example
Javascript :: clear cache javascript 
Javascript :: SELECT * FROM USERSs 
Javascript :: push to an array javascript 
Javascript :: node js post multipart/form-data 
Javascript :: Set CSS styles with javascript 
Javascript :: convert a string to array 
Javascript :: what is diffrence between redux and context 
Javascript :: how to loop over dom objects javascript 
Javascript :: get the last item in an array 
Javascript :: javascript first class functions 
Javascript :: javascript sort object by value descending 
Javascript :: javascript set header text 
Javascript :: javascript how to select a array 
Javascript :: check if property has value in array javascript 
Javascript :: js a function that takes in multiple arguments. 
Javascript :: javascript canvas load image 
Javascript :: create file node 
Javascript :: js get smallest value of array 
Javascript :: maximum number of an array 
Javascript :: React_Weather_APp 
Javascript :: how to add a function in javascript 
Javascript :: Bracket Notation Example 
Javascript :: datapack structure 
Javascript :: how to read if a person has send a message on discord.js 
Javascript :: react js how to do array range 
Javascript :: favicon express js 
Javascript :: join string js with and at the last item 
Javascript :: how to loop elements in javascript for of loop 
Javascript :: node js write read string to file 
Python :: matplotlib change thickness of line 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =