Thursday, May 23, 2024
21
rated 0 times [  26] [ 5]  / answers: 1 / hits: 130184  / 5 Years ago, fri, may 31, 2019, 12:00:00

Windows and macOS now have dark mode.


For CSS I can use:


@media (prefers-dark-interface) { 
color: white; background: black
}

But I am using the Stripe Elements API, which puts colors in JavaScript


For example:


const stripeElementStyles = {
base: {
color: COLORS.darkGrey,
fontFamily: `-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"`,
fontSize: '18px',
fontSmoothing: 'antialiased',
'::placeholder': {
color: COLORS.midgrey
},
':-webkit-autofill': {
color: COLORS.icyWhite
}
}
}

How can I detect the OS's preferred color scheme in JavaScript?


More From » macos-darkmode

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

To watch for changes:


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

[#52057] Thursday, May 23, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marinalyssak

Total Points: 637
Total Questions: 101
Total Answers: 94

Location: Morocco
Member since Fri, May 22, 2020
4 Years ago
;