File tree Expand file tree Collapse file tree 6 files changed +84
-10
lines changed Expand file tree Collapse file tree 6 files changed +84
-10
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,8 @@ coverage
1313.idea
1414.vscode
1515.vs
16+ * .swp
1617
1718# logs
1819
19- * .log
20+ * .log
Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ module.exports = {
6666 'local-storage' ,
6767 'mouse-position' ,
6868 'network' ,
69+ 'preferred-color-scheme' ,
6970 'script' ,
7071 'scroll-position' ,
7172 'window-size'
Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ These are the currently implemented Web APIs and the planned ones.
3232- [ Local storage API] ( ./local-storage.md ) .
3333- [ Mouse Position] ( ./guide/mouse-position.md ) .
3434- [ Network Information API] ( ./network.md ) .
35+ - [ Preferred Color Scheme] ( ./preferred-color-scheme.md ) .
3536- [ Script] ( ./script.md ) .
3637- [ Window Scroll Position] ( ./scroll-position.md ) .
3738- [ Window Size] ( ./window-size.md ) .
Original file line number Diff line number Diff line change 1+ # Preferred Color Scheme
2+
3+ > The [ matchMedia preferred-color-scheme] ( https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme ) is used to detect the user's preference for a ` light ` or ` dark ` theme. This can be useful when you should adapt your UI depending on the user preference.
4+
5+ ``` js
6+ import { usePreferredColorScheme } from ' vue-use-web' ;
7+
8+ const scheme = usePreferredColorScheme ();
9+ ```
10+
11+ | State | Type | Description |
12+ | --------- | ---------- | ------------------------------------- |
13+ | theme | ` String ` | Current user color scheme preferrence |
14+
15+ ## Example
16+
17+ ``` vue
18+ <template>
19+ <h1>User's preference: {{ theme }}</h1>
20+ </template>
21+
22+ <script>
23+ import { usePreferredColorScheme } from 'vue-use-web';
24+
25+ export default {
26+ setup() {
27+ const theme = usePreferredColorScheme();
28+
29+ return { theme };
30+ }
31+ };
32+ </script>
33+ ```
Original file line number Diff line number Diff line change 1+ import { ref , onMounted , onUnmounted } from '@vue/composition-api' ;
2+
3+ export function usePreferredColorScheme ( ) {
4+ const themesObj = {
5+ light : window . matchMedia ( '(prefers-color-scheme: light)' ) ,
6+ dark : window . matchMedia ( '(prefers-color-scheme: dark)' ) ,
7+ 'no-preference' : window . matchMedia ( '(prefers-color-scheme: no-preference)' )
8+ } ;
9+ const value = ref ( getTheme ( ) ) ;
10+
11+ function getTheme ( ) {
12+ let theme = null ;
13+ for ( const key in themesObj ) {
14+ if ( themesObj [ key ] . matches ) {
15+ theme = key ;
16+ break ;
17+ }
18+ }
19+ return theme ;
20+ }
21+
22+ function handler ( ) {
23+ value . value = getTheme ( ) ;
24+ }
25+
26+ onMounted ( ( ) => {
27+ Object . values ( themesObj ) . forEach ( theme => {
28+ theme . addEventListener ( handler ) ;
29+ } ) ;
30+ } ) ;
31+
32+ onUnmounted ( ( ) => {
33+ Object . values ( themesObj ) . forEach ( themeMedia => {
34+ themeMedia . removeEventListener ( handler ) ;
35+ } ) ;
36+ } ) ;
37+ }
Original file line number Diff line number Diff line change 1- export * from './Network' ;
21export * from './Battery' ;
3- export * from './Geolocation' ;
42export * from './Clipboard' ;
5- export * from './Script' ;
6- export * from './DeviceOrientation' ;
3+ export * from './DeviceLight' ;
74export * from './DeviceMotion' ;
5+ export * from './DeviceOrientation' ;
86export * from './Fetch' ;
9- export * from './DeviceLight' ;
10- export * from './WindowScrollPosition' ;
11- export * from './WindowSize' ;
12- export * from './IntersectionObserver' ;
137export * from './FullScreen' ;
14- export * from './MousePosition' ;
8+ export * from './Geolocation' ;
9+ export * from './IntersectionObserver' ;
1510export * from './LocalStorage' ;
11+ export * from './MousePosition' ;
12+ export * from './Network' ;
13+ export * from './PreferredColorScheme' ;
14+ export * from './Script' ;
15+ export * from './WindowScrollPosition' ;
16+ export * from './WindowSize' ;
You can’t perform that action at this time.
0 commit comments