|
| 1 | +<script type="text/ecmascript-6"> |
| 2 | + export default { |
| 3 | + data () { |
| 4 | + return { |
| 5 | + scheme: 'system' |
| 6 | + } |
| 7 | + }, |
| 8 | +
|
| 9 | + watch: { |
| 10 | + scheme (value) { |
| 11 | + localStorage.setItem('scheme', value); |
| 12 | + } |
| 13 | + }, |
| 14 | +
|
| 15 | + mounted () { |
| 16 | + this.scheme = localStorage.getItem('scheme') ?? 'system'; |
| 17 | +
|
| 18 | + window |
| 19 | + .matchMedia('(prefers-color-scheme: dark)') |
| 20 | + .addEventListener('change', () => this.calculateScheme()) |
| 21 | +
|
| 22 | + this.calculateScheme() |
| 23 | + }, |
| 24 | +
|
| 25 | + methods: { |
| 26 | + toggleScheme () { |
| 27 | + if (this.scheme == 'system') { |
| 28 | + this.scheme = 'dark' |
| 29 | + } else if (this.scheme == 'dark') { |
| 30 | + this.scheme = 'light' |
| 31 | + } else { |
| 32 | + this.scheme = 'system' |
| 33 | + } |
| 34 | +
|
| 35 | + this.calculateScheme() |
| 36 | + }, |
| 37 | +
|
| 38 | + calculateScheme () { |
| 39 | + const light = document.querySelector('link[data-scheme="light"]'); |
| 40 | + const dark = document.querySelector('link[data-scheme="dark"]'); |
| 41 | +
|
| 42 | + if (this.scheme == 'system') { |
| 43 | + const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)'); |
| 44 | +
|
| 45 | + light.disabled = prefersDarkMode.matches; |
| 46 | + dark.disabled = ! prefersDarkMode.matches; |
| 47 | + } else if (this.scheme == 'dark') { |
| 48 | + light.disabled = true; |
| 49 | + dark.disabled = false; |
| 50 | + } else { |
| 51 | + light.disabled = false; |
| 52 | + dark.disabled = true; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +</script> |
| 58 | +
|
| 59 | +<template> |
| 60 | + <button class="btn btn-muted" title="Switch Theme" v-on:click.prevent="toggleScheme"> |
| 61 | + <svg v-if="scheme == 'system'" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" class="icon" fill="currentColor"> |
| 62 | + <path fill-rule="evenodd" d="M2 4.25A2.25 2.25 0 014.25 2h11.5A2.25 2.25 0 0118 4.25v8.5A2.25 2.25 0 0115.75 15h-3.105a3.501 3.501 0 001.1 1.677A.75.75 0 0113.26 18H6.74a.75.75 0 01-.484-1.323A3.501 3.501 0 007.355 15H4.25A2.25 2.25 0 012 12.75v-8.5zm1.5 0a.75.75 0 01.75-.75h11.5a.75.75 0 01.75.75v7.5a.75.75 0 01-.75.75H4.25a.75.75 0 01-.75-.75v-7.5z" clip-rule="evenodd" /> |
| 63 | + </svg> |
| 64 | + <svg v-if="scheme == 'dark'" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" class="icon" fill="currentColor"> |
| 65 | + <path fill-rule="evenodd" d="M7.455 2.004a.75.75 0 01.26.77 7 7 0 009.958 7.967.75.75 0 011.067.853A8.5 8.5 0 116.647 1.921a.75.75 0 01.808.083z" clip-rule="evenodd" /> |
| 66 | + </svg> |
| 67 | + <svg v-if="scheme == 'light'" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" class="icon" fill="currentColor"> |
| 68 | + <path d="M10 2a.75.75 0 01.75.75v1.5a.75.75 0 01-1.5 0v-1.5A.75.75 0 0110 2zM10 15a.75.75 0 01.75.75v1.5a.75.75 0 01-1.5 0v-1.5A.75.75 0 0110 15zM10 7a3 3 0 100 6 3 3 0 000-6zM15.657 5.404a.75.75 0 10-1.06-1.06l-1.061 1.06a.75.75 0 001.06 1.06l1.06-1.06zM6.464 14.596a.75.75 0 10-1.06-1.06l-1.06 1.06a.75.75 0 001.06 1.06l1.06-1.06zM18 10a.75.75 0 01-.75.75h-1.5a.75.75 0 010-1.5h1.5A.75.75 0 0118 10zM5 10a.75.75 0 01-.75.75h-1.5a.75.75 0 010-1.5h1.5A.75.75 0 015 10zM14.596 15.657a.75.75 0 001.06-1.06l-1.06-1.061a.75.75 0 10-1.06 1.06l1.06 1.06zM5.404 6.464a.75.75 0 001.06-1.06l-1.06-1.06a.75.75 0 10-1.061 1.06l1.06 1.06z" /> |
| 69 | + </svg> |
| 70 | + </button> |
| 71 | +</template> |
0 commit comments