I have a huge react app which has 4 main huge CSSs(darkLTR, lightLTR, darkRTL, lightRTL) which is not ideal i know, but it was a website template that my boss bought for me and asked me to use it(i wanted to use Material ui which is awesome for theme handling but sadly i wasn't allowed). It came with two different templates. Dark mode and light mode. They told me that they want to have both dark and light mode and even RTL support. So i had to translate ltr into rtl and add toggle buttons to switch between different themes and languages. I did all that and now i have 4 CSS files for all the styles. They all have same class names and rules. The only different is directions and colors. I import these CSS files like below:
export default function importStylesheets() {
const theme = localStorage.getItem('theme')
const direction = localStorage.getItem('direction')
if (theme === 'dark' && direction === 'rtl') {
import('./css/styleDarkrtl.css')
} else if (theme === 'dark' && direction === 'ltr') {
import('./css/styleDarkltr.css')
} else if (theme === 'light' && direction === 'rtl') {
import('./css/styleLightrtl.css')
} else if (theme === 'light' && direction === 'ltr') {
import('./css/styleLightltr.css')
}
}
Now my problem begins. Imagine we are in darkltr mode and darkltr CSS is loaded already. Now user switches the theme toggle button. So importStylesheets() function will be called again and this time the value of theme will be light. So this time lightltr will get imported and the whole theme will get updated and everything seems cool. But what if user switch the theme toggle button once again? Nothing happens. Because now that i call importStylesheets() function, it won't re-import darkltr CSS because it is already inside application cache. And the higher priority will be with the last imported file (in this case, lightltr). So nothing will change. I came up with a temporary solution to reload the whole application and load it with localStorage and then call importStylesheets() function. But that's a huge disadvantage because one of the reasons that we use react apps is to have a good and fast user experience. So my question is, can someone help me to clear the imported content? Or whatever. I just need to be abled to switch between different CSSs at anytime or make them top priority. Just help me to achieve this goal please. Much appreciated.
UPDATE: I found this part of code in my bundled file. Thought it might help
function importStylesheets() {
const theme = localStorage.getItem('theme');
const direction = localStorage.getItem('direction');
if (theme === 'dark' && direction === 'rtl') {
Promise.all(/*! import() */[__webpack_require__.e(0), __webpack_require__.e(1)]).then(__webpack_require__.t.bind(null, /*! ./css/styleDarkrtl.css */ "./src/css/styleDarkrtl.css", 7));
} else if (theme === 'dark' && direction === 'ltr') {
Promise.all(/*! import() */[__webpack_require__.e(0), __webpack_require__.e(3)]).then(__webpack_require__.t.bind(null, /*! ./css/styleDarkltr.css */ "./src/css/styleDarkltr.css", 7));
} else if (theme === 'light' && direction === 'rtl') {
Promise.all(/*! import() */[__webpack_require__.e(0), __webpack_require__.e(2)]).then(__webpack_require__.t.bind(null, /*! ./css/styleLightrtl.css */ "./src/css/styleLightrtl.css", 7));
} else if (theme === 'light' && direction === 'ltr') {
Promise.all(/*! import() */[__webpack_require__.e(0), __webpack_require__.e(4)]).then(__webpack_require__.t.bind(null, /*! ./css/styleLightltr.css */ "./src/css/styleLightltr.css", 7));
}
}