|
| 1 | +<script setup> |
| 2 | +import { VTSwitch } from '@vue/theme' |
| 3 | +
|
| 4 | +const storageKey = 'vue-docs-api-preference' |
| 5 | +const className = 'prefer-composition' |
| 6 | +
|
| 7 | +const toggle = typeof localStorage !== 'undefined' ? useAPIToggle() : () => {} |
| 8 | +
|
| 9 | +function useAPIToggle() { |
| 10 | + let preferComposition = JSON.parse(localStorage.getItem(storageKey) || 'false') |
| 11 | + const classList = document.documentElement.classList |
| 12 | + return (value = !preferComposition) => { |
| 13 | + if ((preferComposition = value)) { |
| 14 | + classList.add(className) |
| 15 | + } else { |
| 16 | + classList.remove(className) |
| 17 | + } |
| 18 | + localStorage.setItem(storageKey, preferComposition) |
| 19 | + } |
| 20 | +} |
| 21 | +</script> |
| 22 | + |
| 23 | +<template> |
| 24 | + <p class="api-switch"> |
| 25 | + <span class="api-switch-title"> |
| 26 | + API<span class="api-switch-pref"> Preference</span> |
| 27 | + <a class="api-switch-link" |
| 28 | + title="About API preference" |
| 29 | + href="/guide/introduction.html#api-styles">?</a> |
| 30 | + </span> |
| 31 | + <div style="float:right"> |
| 32 | + <label class="api-switch-label options" @click="toggle(false)" |
| 33 | + >Options</label |
| 34 | + > |
| 35 | + <VTSwitch aria-label="Switch API preference" @click="toggle()" /> |
| 36 | + <label class="api-switch-label composition" @click="toggle(true)" |
| 37 | + >Composition</label |
| 38 | + > |
| 39 | + </div> |
| 40 | + </p> |
| 41 | +</template> |
| 42 | + |
| 43 | +<style> |
| 44 | +h1 + .api-switch { |
| 45 | + margin-top: 64px; |
| 46 | +} |
| 47 | +
|
| 48 | +.api-switch { |
| 49 | + background-color: var(--vt-c-bg-soft); |
| 50 | + border-radius: 8px; |
| 51 | + padding: 16px 30px; |
| 52 | + transition: background-color 0.5s; |
| 53 | +} |
| 54 | +
|
| 55 | +.composition-api { |
| 56 | + display: none; |
| 57 | +} |
| 58 | +
|
| 59 | +.prefer-composition .options-api { |
| 60 | + display: none; |
| 61 | +} |
| 62 | +
|
| 63 | +.prefer-composition .composition-api { |
| 64 | + display: block; |
| 65 | +} |
| 66 | +
|
| 67 | +.prefer-composition .api-switch .vt-switch-check { |
| 68 | + transform: translateX(18px); |
| 69 | +} |
| 70 | +
|
| 71 | +.api-switch-title { |
| 72 | + color: var(--vt-c-text-2); |
| 73 | +} |
| 74 | +
|
| 75 | +.api-switch-link { |
| 76 | + display: inline-block; |
| 77 | + margin-left: 6px; |
| 78 | + font-size: 12px; |
| 79 | + width: 17px; |
| 80 | + height: 17px; |
| 81 | + line-height: 15px; |
| 82 | + text-align: center; |
| 83 | + border: 1px solid var(--vt-c-green); |
| 84 | + border-radius: 50%; |
| 85 | +} |
| 86 | +
|
| 87 | +.api-switch-title, |
| 88 | +.api-switch-label, |
| 89 | +.api-switch .vt-switch { |
| 90 | + display: inline-block; |
| 91 | + vertical-align: middle; |
| 92 | +} |
| 93 | +
|
| 94 | +.api-switch .vt-switch { |
| 95 | + margin: 0 12px; |
| 96 | +} |
| 97 | +
|
| 98 | +.api-switch-title, |
| 99 | +.api-switch-label { |
| 100 | + font-weight: 500; |
| 101 | + font-size: 14px; |
| 102 | + transition: opacity 0.5s; |
| 103 | +} |
| 104 | +
|
| 105 | +.api-switch-label { |
| 106 | + cursor: pointer; |
| 107 | +} |
| 108 | +
|
| 109 | +.prefer-composition .api-switch-label.options, |
| 110 | +.api-switch-label.composition { |
| 111 | + opacity: 0.3; |
| 112 | +} |
| 113 | +
|
| 114 | +.api-switch-label.options, |
| 115 | +.prefer-composition .api-switch-label.composition { |
| 116 | + opacity: 1; |
| 117 | +} |
| 118 | +
|
| 119 | +@media (max-width: 768px) { |
| 120 | + h1 + .api-switch { |
| 121 | + margin-top: 36px; |
| 122 | + } |
| 123 | + .api-switch { |
| 124 | + padding: 12px 20px; |
| 125 | + } |
| 126 | + .api-switch-title, |
| 127 | + .api-switch-label { |
| 128 | + font-size: 13px; |
| 129 | + } |
| 130 | +} |
| 131 | +
|
| 132 | +@media (max-width: 420px) { |
| 133 | + .api-switch-pref { |
| 134 | + display: none; |
| 135 | + } |
| 136 | +} |
| 137 | +</style> |
0 commit comments