Comments (1)
I found a solution by just adding a listener to the media event so that the theme would be updated again.
import * as storage from 'actions/storage';
import {
createContext,
useContext,
useEffect,
useState
} from 'react';
type Theme = 'dark' | 'light' | 'system';
type ThemeProviderProps = {
children: React.ReactNode;
defaultTheme ? : Theme;
storageKey ? : string;
};
type ThemeProviderState = {
theme: Theme;
setTheme: (theme: Theme) => void;
};
const ThemeProviderContext = createContext < ThemeProviderState > ({
theme: 'system',
setTheme: () => null
});
export const ThemeProvider = ({
children,
storageKey = '@theme',
defaultTheme = 'system',
...props
}: ThemeProviderProps) => {
const [theme, setTheme] = useState < Theme > (
() => storage.get < Theme > (storageKey) || defaultTheme
);
useEffect(() => {
const root = document.documentElement;
const systemTheme = matchMedia('(prefers-color-scheme: dark)');
const updateTheme = () => {
root.classList.remove('light', 'dark');
if (theme === 'system') {
root.classList.add(systemTheme.matches ? 'dark' : 'light');
return;
}
root.classList.add(theme);
};
updateTheme();
systemTheme.addEventListener('change', updateTheme);
return () => {
systemTheme.removeEventListener('change', updateTheme);
};
}, [theme]);
const value = {
theme,
setTheme: (newTheme: Theme) => {
storage.set(storageKey, newTheme);
setTheme(newTheme);
}
};
return ( <
ThemeProviderContext.Provider {
...props
}
value = {
value
} > {
children
} <
/ThemeProviderContext.Provider>
);
};
export const useTheme = () => {
const context = useContext(ThemeProviderContext);
if (context === null) {
throw new Error('useTheme must be used within a ThemeProvider');
}
return context;
};
from ui.
Related Issues (20)
- [refactor]: Remove unnecessary "use client" directives
- [feat]: More Maintainers HOT 7
- [bug]: Application error: a client-side exception on Shadcn website
- [bug]: client side exception at combobox component HOT 2
- [bug]: cannot use Dropdown menu with dialog
- Change action button position in sonner toast HOT 6
- Main Website Component Bug HOT 1
- [bug]: Shadcn UI drawer: disablePreventScroll={true} does not make the scroll bar appear and it's not scrollable
- [bug]: missing diff highlight in data table sorting docs HOT 1
- [bug]: Shadcn DataPicker HOT 1
- [bug]: redundant empty Interface for InputProps on input component
- [bug]: The โnpx shadcn-ui@latest initโ cannot be executed on the company network. How can I init it offline? HOT 1
- [bug]: Landing page crashes when examples > forms > account > language selection dropdown clicked. HOT 2
- [bug]: Combobox overflow text displaces dropdown icon HOT 1
- [bug]: Application error: a client-side exception has occurred on examples > playground page HOT 2
- [bug]: not able to submit the form, using Next.js 15 and React.js 19 HOT 5
- Dropdown result not stick to input HOT 2
- [bug]: click combobox in form cause error HOT 5
- [bug]: The ToggleGroupItem does not accept dynamic colors when data[state=on] HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ui.