Skip to content

Instantly share code, notes, and snippets.

@easrng
Created September 14, 2021 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save easrng/fe442b487cbbed55e6821c0070ae110e to your computer and use it in GitHub Desktop.
Save easrng/fe442b487cbbed55e6821c0070ae110e to your computer and use it in GitHub Desktop.
Override CSS prefers-color-scheme
function setTheme(theme) {
Array.from(document.styleSheets).map(e => {
try {
return Array.from(e.cssRules)
} catch (e) {
return []
}
}).flat().map(e => {
if (e.constructor != CSSMediaRule) return;
if (e.originalConditionText) e.conditionText = e.originalConditionText;
else e.originalConditionText = e.conditionText
if (theme == "system") return
let match = e.conditionText.match(/prefers-color-scheme:\s*(light|dark)/i)
if (!match) return;
e.conditionText = e.conditionText.replace(match[0], (match[1].toLowerCase() == theme ? 'min' : 'max') + '-width: 0')
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment