Skip to content

Commit 97a8468

Browse files
committed
feat: make left & right bar responsive
1 parent 1b29a45 commit 97a8468

File tree

9 files changed

+45
-31
lines changed

9 files changed

+45
-31
lines changed

src/App.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ window.onresize = () => {
2424
left_stat.value = get_leftbar_status();
2525
right_stat.value = get_rightbar_status();
2626
};
27-
28-
const in_view: Ref<number | null> = ref(null);
2927
</script>
3028

3129
<template>
3230
<div id="main" v-if="meta">
3331
<LeftBar :status="left_stat" :current-category="meta.category" />
3432
<Content :meta="meta" />
35-
<RightBar :status="right_stat" :in-view="in_view" :toc="meta.toc" />
33+
<RightBar :status="right_stat" :toc="meta.toc" />
3634
<Logo />
3735
</div>
3836
</template>

src/assets/css/global.styl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ $monospace = "Fira Code", "Fira Mono", Menlo, Consolas, "DejaVu Sans Mono", mono
2525
$theme-color = #60a5fa;
2626
$text-color = #181818;
2727
$text-color-d = #d1d5db;
28+
$background-light = #fefefe;
29+
$background-dark = #1a1b1c;
2830

2931
// SVG
3032
$checkbox-svg = 'data:image/svg+xml;charset=UTF-8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xml:space="preserve" fill="white" viewBox="0 0 9 9"><rect x="0" y="4.3" transform="matrix(-0.707 -0.7072 0.7072 -0.707 0.5891 10.4702)" width="4.3" height="1.6" /><rect x="2.2" y="2.9" transform="matrix(-0.7071 0.7071 -0.7071 -0.7071 12.1877 2.9833)" width="6.1" height="1.7" /></svg>';

src/assets/css/main.styl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@import "https://fonts.cdnfonts.com/css/libertinus-serif";
33
@import "./global.styl";
44

5-
schemer(--body-background-color, #fefefe, #1a1b1c);
5+
schemer(--body-background-color, $background-light, $background-dark);
66
schemer(--body-text-color, $text-color, $text-color-d);
77

88
// Overlay Scrollbar Theme

src/assets/ts/leftbar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import type { JSX } from "vue/jsx-runtime";
1616
*/
1717
export const get_leftbar_status = (): LEFTBAR_STATUS => {
1818
if (isMobile()) {
19+
return LEFTBAR_STATUS.SHOW_SEARCH_AND_CATEGORY;
20+
} else {
1921
return isWidthLessThan(SMALL_SCREEN_WIDTH)
2022
? LEFTBAR_STATUS.SHOW_SEARCH_AND_CATEGORY
21-
: LEFTBAR_STATUS.ONLY_SEARCH_BUTTON;
22-
} else {
23-
return isWidthLessThan(LEFTBAR_THRESHOLD)
23+
: isWidthLessThan(LEFTBAR_THRESHOLD)
2424
? LEFTBAR_STATUS.ONLY_SEARCH_BUTTON
2525
: LEFTBAR_STATUS.HOVER_TO_SHOW;
2626
}

src/assets/ts/rightbar.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@ export type SerialHeader = MarkdownHeaderJsx & {
1313
};
1414
export type CascadeHeader = SerialHeader & { children: SerialHeader[] };
1515

16-
const width_preset: string[] = ["50px", "40px", "30px", "20px", "13px"];
17-
const indent_preset: string[] = [
18-
"0rem",
19-
"1.3rem",
20-
"1.7rem",
21-
"2.3rem",
22-
"2.8rem",
23-
];
24-
const opacity_preset: string[] = ["1", "0.8", "0.7", "0.7", "0.7"];
16+
const width_preset = ["50px", "40px", "30px", "20px", "13px"];
17+
const indent_preset = ["0rem", "1.3rem", "1.7rem", "2.3rem", "2.8rem"];
18+
const opacity_preset = ["1", "0.8", "0.7", "0.7", "0.7"];
2519

2620
/**
2721
* Determine rightbar status (i.e. whether to display or not).
@@ -80,14 +74,14 @@ export const cascade_toc = (s_toc: SerialHeader[]): CascadeHeader[] => {
8074
*/
8175
export class ScrollListener {
8276
targets: Element[];
83-
store: Ref<number | null>;
77+
store: Ref<number>;
8478

8579
/**
8680
* Constructor.
8781
*
8882
* @param in_view - Ref to store the index of the element in view
8983
*/
90-
constructor(in_view: Ref<number | null>) {
84+
constructor(in_view: Ref<number>) {
9185
this.targets = [];
9286
this.store = in_view;
9387

@@ -115,7 +109,7 @@ export class ScrollListener {
115109
*/
116110
reset(): void {
117111
this.targets = [];
118-
this.store.value = null;
112+
this.store.value = -1;
119113
}
120114

121115
/**

src/assets/ts/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const shuffle = <T>(array: T[]): T[] => {
9191
export const SMALL_SCREEN_WIDTH: number = 768;
9292

9393
/** The minimum screen width required for rightbar to display. */
94-
export const RIGHTBAR_THRESHOLD: number = 1280;
94+
export const RIGHTBAR_THRESHOLD: number = 880;
9595

9696
/** The threshold screen width required for leftbar to switch style. */
97-
export const LEFTBAR_THRESHOLD: number = 1260;
97+
export const LEFTBAR_THRESHOLD: number = 880;

src/components/LeftBar.vue

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,22 @@ watchEffect(() => {
154154
</script>
155155

156156
<template>
157-
<div id="left" @mouseenter="mouse.enter" @mouseleave="mouse.leave">
157+
<div
158+
id="left"
159+
@mouseenter="mouse.enter"
160+
@mouseleave="mouse.leave"
161+
:style="{
162+
'--z-index': do_show_detail
163+
? 1001
164+
: status == LEFTBAR_STATUS.HOVER_TO_SHOW
165+
? 0
166+
: 1001,
167+
'--height':
168+
status == LEFTBAR_STATUS.HOVER_TO_SHOW
169+
? 'calc(100vh - var(--offset-top) * 2)'
170+
: 'unset',
171+
}"
172+
>
158173
<ul class="nav">
159174
<li class="btn" id="search" @click="search.reveal">
160175
<font-awesome-icon :icon="['fas', 'magnifying-glass']" />
@@ -234,10 +249,9 @@ $width = $toc-offset-left + $toc-width;
234249
scheme(--toc-color, lighten($text-color, 30%), darken($text-color-d, 5%));
235250
236251
// Global
252+
scheme(--background-color, alpha($background-light, 90%), alpha($background-dark, 90%));
237253
dual(--offset-top, 28px, 32px);
238254
dual(--offset-left, 35px, 27px);
239-
dual(--height, calc(100vh - var(--offset-top) * 2), unset);
240-
dual(--z-index, 100, 1001);
241255
242256
position: fixed;
243257
left: var(--offset-left);
@@ -246,9 +260,13 @@ $width = $toc-offset-left + $toc-width;
246260
height: var(--height);
247261
z-index: var(--z-index);
248262
263+
.nav, .category, .content
264+
background-color: var(--background-color);
265+
249266
.nav
250267
display: flex;
251268
flex-direction: row;
269+
width: fit-content;
252270
gap: var(--nav-gap);
253271
254272
.btn

src/components/RightBar.vue

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const mouse = {
1818
leave: () => (show_text.value = false),
1919
};
2020
21-
const in_view: Ref<number | null> = ref(null);
22-
const sl: ScrollListener = new ScrollListener(in_view);
21+
const in_view: Ref<number> = ref(-1);
22+
const sl = new ScrollListener(in_view);
2323
2424
const registerScrollListener = () => {
2525
sl.reset();
@@ -42,6 +42,7 @@ onMounted(registerScrollListener);
4242
:key="$route.path"
4343
@mouseenter="mouse.enter"
4444
@mouseleave="mouse.leave"
45+
:style="{ '--z-index': show_text ? 1001 : 0 }"
4546
>
4647
<!-- https://cn.vuejs.org/guide/built-ins/transition.html#javascript-hooks -->
4748
<Transition name="bars">
@@ -62,7 +63,7 @@ onMounted(registerScrollListener);
6263
<RightBarDetail
6364
class="root"
6465
:item="item"
65-
:in_view="in_view!"
66+
:in_view="in_view"
6667
:class="{
6768
expand:
6869
item.children.length &&
@@ -79,7 +80,7 @@ onMounted(registerScrollListener);
7980
v-for="child in item.children"
8081
class="sub"
8182
:item="child"
82-
:in_view="in_view!"
83+
:in_view="in_view"
8384
:key="child.index"
8485
/>
8586
</div>
@@ -113,6 +114,7 @@ $bar-padding = 4px;
113114
$indicator-margin = 4px;
114115
115116
#right
117+
scheme(--background-color, alpha($background-light, 90%), alpha($background-dark, 90%));
116118
scheme(--detail-color, lighten($text-color, 10%), $text-color-d);
117119
scheme(--detail-color-passed, lighten($text-color, 55%), darken($text-color-d, 50%));
118120
scheme(--bar-background-color, lighten(black, 88%), lighten(black, 28%));
@@ -122,7 +124,7 @@ $indicator-margin = 4px;
122124
width: $width;
123125
height: $height;
124126
top: $offset-top;
125-
z-index: 100;
127+
z-index: var(--z-index);
126128
127129
// To avoid scrollbar flickering
128130
left: "calc(100vw - %s)" % ($offset-right + $width);
@@ -137,6 +139,8 @@ $indicator-margin = 4px;
137139
right: 0;
138140
padding: $toc-padding;
139141
margin: $toc-margin;
142+
background-color: var(--background-color);
143+
border-radius: 4px;
140144
141145
// To avoid flickering on hovering edges
142146
margin-right: $toc-translate-offset;

todo.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
## 近期
22

33
- LeftBar 添加折叠
4-
5-
- Left Right Bar 的 Responsive Design
64
- 深色模式切换按钮(切换的时候给所有元素设置临时的 transition !important?)(放 rightbar?或者不要也可以)
75
- 每次修改 md 后,因为 routes.tsx 变动,导致页面需要全部刷新,可以优化一下
86
- 渲染 plugin,遇到 fatal error 不要直接挂掉

0 commit comments

Comments
 (0)