Skip to content

Commit

Permalink
Fix some compatibility issues with old browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
ffont committed Nov 3, 2023
1 parent c2c2fbf commit 853c72b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
21 changes: 11 additions & 10 deletions freesound/static/bw-frontend/src/components/uiThemeDetector.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ const isUsingDarkTheme = () => {
return styleLink.getAttribute('href').indexOf('dark') > -1;
}

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
const newColorScheme = event.matches ? "dark" : "light";
if (styleLink.dataset.forcedTheme === undefined){
if (newColorScheme == "dark"){
switchToDarkMode();
} else if (newColorScheme == "dark"){
switchToLightMode();
if (window.matchMedia('(prefers-color-scheme: dark)').addEventListener !== undefined){ // Some old browsers don't support that
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
const newColorScheme = event.matches ? "dark" : "light";
if (styleLink.dataset.forcedTheme === undefined){
if (newColorScheme == "dark"){
switchToDarkMode();
} else if (newColorScheme == "dark"){
switchToLightMode();
}
}
}
});

});
}

if (browserPrefersDarkTheme()){
document.cookie = "systemPrefersDarkTheme=yes;path=/";
Expand Down
1 change: 1 addition & 0 deletions freesound/static/bw-frontend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ import 'normalize.css'
import '../styles/index.scss'

import './components'
import './utils/polyfills'
10 changes: 10 additions & 0 deletions freesound/static/bw-frontend/src/utils/polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function(str, newStr){
// If a regex pattern
if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
return this.replace(str, newStr);
}
// If a string
return this.replace(new RegExp(str, 'g'), newStr);
};
}

0 comments on commit 853c72b

Please sign in to comment.