Skip to content

Commit

Permalink
feat(theme): implement dynamic theme switching based on system prefer…
Browse files Browse the repository at this point in the history
…ences
  • Loading branch information
Valik3201 committed Jan 27, 2024
1 parent 43c52e2 commit 103190c
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/components/Searchbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ class Searchbar extends Component {
];
}

componentDidMount() {
const prefersDarkMode = window.matchMedia(
'(prefers-color-scheme: dark)'
).matches;

this.handleThemeChange(prefersDarkMode ? 'dark' : 'light');

window
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', this.handleSystemThemeChange);
}

componentWillUnmount() {
window
.matchMedia('(prefers-color-scheme: dark)')
.removeEventListener('change', this.handleSystemThemeChange);
}

handleSystemThemeChange = e => {
const prefersDarkMode = e.matches;
this.handleThemeChange(prefersDarkMode ? 'dark' : 'light');
};

handleThemeChange = selectedTheme => {
this.setState({ theme: selectedTheme });

Expand Down

0 comments on commit 103190c

Please sign in to comment.