|
10 | 10 | import { slide } from 'svelte/transition'; |
11 | 11 | import { afterNavigate } from '$app/navigation'; |
12 | 12 | import { page } from '$app/stores'; |
13 | | - import { displays } from '$lib/shared.svelte.js'; |
| 13 | + import { rightDisplay } from '$lib/shared.svelte.js'; |
14 | 14 | import { leftDisplay } from '$lib/shared.svelte.js'; |
15 | 15 | import ZSibar from '$lib/ZSibar/ZSibar.svelte'; |
16 | 16 | import ZSidebar from '$lib/ZSibar/ZSidebar.svelte'; |
|
58 | 58 | let moblieSidebar = $state(); |
59 | 59 |
|
60 | 60 | /** |
61 | | - * @type {object} - Snapshot of `displays`. |
| 61 | + * @type {object} - Snapshot of `rightDisplay`. |
62 | 62 | */ |
63 | | - let { display, displayToc } = $state(displays); |
| 63 | + let { displayToc } = $state(rightDisplay); |
64 | 64 |
|
65 | 65 | // Scroll header while scrolling. |
66 | 66 | let top = $state(0); |
|
78 | 78 | onMount(() => { |
79 | 79 | lastY = Y; |
80 | 80 |
|
81 | | - // Show sidebar if not in home page. |
82 | | - if ($page.url.pathname !== '/') { |
83 | | - displays.display = displays.displayToc = true; |
84 | | - } else { |
85 | | - display = displayToc = true; |
86 | | - } |
| 81 | + // Show right sidebar if not in home page. |
| 82 | + $page.url.pathname !== '/' |
| 83 | + ? (rightDisplay.displayToc = true) |
| 84 | + : (rightDisplay.displayToc = false); |
87 | 85 |
|
| 86 | + // Hide left sidebar if in home page. Always hide mobile sidebar on start. |
88 | 87 | leftDisplay.displayMobile = false; |
89 | | - if ($page.url.pathname === '/') { |
90 | | - leftDisplay.displayPC = false; |
91 | | - } else { |
92 | | - leftDisplay.displayPC = true; |
93 | | - } |
| 88 | + $page.url.pathname === '/' ? (leftDisplay.displayPC = false) : (leftDisplay.displayPC = true); |
94 | 89 | }); |
95 | 90 |
|
96 | 91 | afterNavigate(() => { |
97 | 92 | // get head elements passed to <ZTocList>. |
98 | 93 | headings = document.querySelectorAll(titles); |
99 | 94 |
|
100 | | - // Reset `displays` when navigating between home and other pages. |
| 95 | + // Reset sidebars when navigating between home and other pages. |
101 | 96 | if ($navigating && windowWidth >= 768) { |
102 | 97 | const { from, to } = $navigating; |
103 | 98 | if (from.url.pathname !== '/' && to.url.pathname === '/') { |
104 | | - displayToc = displays.displayToc; |
105 | | - displays.display = displays.displayToc = false; |
| 99 | + leftDisplay.displayPC = false; |
| 100 | +
|
| 101 | + // Save the current value of `displayToc` and set it to false. |
| 102 | + displayToc = rightDisplay.displayToc; |
| 103 | + rightDisplay.displayToc = false; |
106 | 104 | } else if (from.url.pathname === '/' && to.url.pathname !== '/') { |
107 | | - displays.displayToc = displayToc; |
| 105 | + leftDisplay.displayPC = true; |
| 106 | +
|
| 107 | + rightDisplay.displayToc = displayToc; |
108 | 108 | } |
109 | 109 | } |
110 | 110 | }); |
111 | 111 |
|
112 | 112 | $effect(() => { |
113 | 113 | if (moblieSidebar) { |
| 114 | + removeEventListener('click', clickOutside); |
114 | 115 | function clickOutside(e) { |
115 | | - if (e.clientY > moblieSidebar.getBoundingClientRect().bottom) { |
| 116 | + if (e.clientY > moblieSidebar?.getBoundingClientRect().bottom) { |
116 | 117 | leftDisplay.displayMobile = false; |
117 | 118 | removeEventListener('click', clickOutside); |
118 | 119 | } |
|
172 | 173 | <div class="content"> |
173 | 174 | {@render children?.()} |
174 | 175 | </div> |
175 | | - {#if displays.displayToc} |
| 176 | + {#if rightDisplay.displayToc} |
176 | 177 | <div class="toc"> |
177 | 178 | {#if headings && headings?.length} |
178 | 179 | <div class="tocContainer" transition:slide|global={{ duration: 300, axis: 'x' }}> |
|
0 commit comments