11<script setup>
2- import { onMounted , computed , ref , watchEffect } from ' vue'
2+ import { onMounted , onUnmounted , computed , ref , watch } from ' vue'
33import { useData } from ' vitepress'
44import VPSwitch from ' ../VPSwitch.vue'
55import IconNode from ' ./IconNode.vue'
@@ -11,26 +11,33 @@ const checked = ref(false)
1111const toggle = typeof localStorage !== ' undefined' ? useVariant () : () => {}
1212const knownImplVariants = [' node' , ' java' ]
1313
14+ let outlineObserver = null
15+
1416onMounted (() => {
1517 if (! supportsVariants .value ) return
1618
17- let check = currentCheckState ()
18- // Persist value even intially . If query param was used, users expect to get this value from now on, even if not using the query anymore .
19+ const check = currentCheckState ()
20+ // Persist value initially . If query param was used, users expect to get this value from now on.
1921 const variantNew = check ? ' java' : ' node'
2022 localStorage .setItem (' impl-variant' , variantNew)
2123
22- setClass (check)
24+ syncState (check)
25+ observeOutline ()
26+ })
2327
24- // Scroll hash element into view. Needed on first page load if variant is changed by query param.
25- scrollTo (window .location .hash ? .slice (1 ))
28+ onUnmounted (() => {
29+ if (outlineObserver) {
30+ outlineObserver .disconnect ()
31+ outlineObserver = null
32+ }
2633})
2734
28- function scrollTo ( id ) {
29- const elem = document . getElementById (id)
30- if (elem) {
31- setTimeout (() => { elem ? . scrollIntoView ( true ) }, 20 )
35+ watch (supportsVariants, ( supports ) => {
36+ if (supports) {
37+ syncState ( currentCheckState ())
38+ observeOutline ( )
3239 }
33- }
40+ })
3441
3542function currentCheckState () {
3643 const url = new URL (window .location )
@@ -39,76 +46,61 @@ function currentCheckState() {
3946 return localStorage .getItem (' impl-variant' ) === ' java'
4047}
4148
42- function setClass (check ) {
49+ function syncState (check ) {
4350 checked .value = check
4451
45- for (let swtch of document .getElementsByClassName (' SwitchImplVariant' )) {
46- swtch .classList [check ? ' add ' : ' remove ' ] (' checked' )
52+ for (const swtch of document .getElementsByClassName (' SwitchImplVariant' )) {
53+ swtch .classList . toggle (' checked' , check )
4754 }
48- for (let container of document .getElementsByClassName (' SwitchImplVariantContainer' )) {
55+ for (const container of document .getElementsByClassName (' SwitchImplVariantContainer' )) {
4956 container .title = check ? ' Java content. Toggle to see Node.js.' : ' Node.js content. Toggle to see Java.'
5057 }
5158
5259 markOutlineItems ()
53- toggleContent (check ? ' java' : ' node' )
54-
5560}
5661
5762function useVariant () {
5863 function toggle () {
5964 let check = currentCheckState ()
60- setClass ((check = ! check))
65+ check = ! check
66+
6167 const variantNew = check ? ' java' : ' node'
6268 localStorage .setItem (' impl-variant' , variantNew)
6369
70+ toggleContent (variantNew)
71+ syncState (check)
72+
6473 if (supportsVariants .value ) {
6574 const url = new URL (window .location )
6675 url .searchParams .set (' impl-variant' , variantNew)
6776 window .history .replaceState ({}, ' ' , url)
6877 }
69-
7078 }
7179 return toggle
7280}
7381
74- function animationsOff (cb ) {
75- let css
76- css = document .createElement (' style' )
77- css .appendChild (
78- document .createTextNode (
79- ` :not(.VPSwitchAppearance):not(.VPSwitchAppearance *) {
80- -webkit-transition: none !important;
81- -moz-transition: none !important;
82- -o-transition: none !important;
83- -ms-transition: none !important;
84- transition: none !important;
85- }`
86- ))
87- document .head .appendChild (css)
88-
89- cb ()
90-
91- // keep unused declaration, used to force the browser to redraw
92- // eslint-disable-next-line no-unused-vars
93- const _ = window .getComputedStyle (css).opacity
94- document .head .removeChild (css)
95- }
96-
97- watchEffect (() => {
98- if (! supportsVariants .value ) return
99- setTimeout (() => { // otherwise DOM is not ready
100- if (typeof document !== ' undefined' ) {
101- animationsOff (() => setClass (currentCheckState ()) )
102- }
103- }, 20 )
104- })
105-
10682function toggleContent (variant ) {
10783 const htmlClassList = document .documentElement .classList
10884 knownImplVariants .forEach (v => htmlClassList .remove (v))
10985 htmlClassList .add (variant)
11086}
11187
88+ function observeOutline () {
89+ if (outlineObserver) return
90+
91+ const aside = document .querySelector (' .VPDocAside' )
92+ if (! aside) return
93+
94+ markOutlineItems ()
95+ outlineObserver = new MutationObserver (() => {
96+ markOutlineItems ()
97+ })
98+ outlineObserver .observe (aside, {
99+ childList: true ,
100+ subtree: true
101+ })
102+ }
103+
112104// Only mark outline items here, as these are not part of the generated HTML,
113105// but are created on the fly with JS.
114106// All other DOM content is handled at build time on MD level (see md-attrs-propagate.ts)
0 commit comments