Skip to content

Commit 7426261

Browse files
kimurazlogaretm
authored andcommitted
Feat/preferred color scheme (#11)
* feat: created PreferredColorScheme * docs: add docs for usePreferredColorScheme
1 parent 4d4ce79 commit 7426261

File tree

6 files changed

+84
-10
lines changed

6 files changed

+84
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ coverage
1313
.idea
1414
.vscode
1515
.vs
16+
*.swp
1617

1718
# logs
1819

19-
*.log
20+
*.log

docs/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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'

docs/guide/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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).
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
```

src/PreferredColorScheme.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

src/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
export * from './Network';
21
export * from './Battery';
3-
export * from './Geolocation';
42
export * from './Clipboard';
5-
export * from './Script';
6-
export * from './DeviceOrientation';
3+
export * from './DeviceLight';
74
export * from './DeviceMotion';
5+
export * from './DeviceOrientation';
86
export * from './Fetch';
9-
export * from './DeviceLight';
10-
export * from './WindowScrollPosition';
11-
export * from './WindowSize';
12-
export * from './IntersectionObserver';
137
export * from './FullScreen';
14-
export * from './MousePosition';
8+
export * from './Geolocation';
9+
export * from './IntersectionObserver';
1510
export * 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';

0 commit comments

Comments
 (0)