Skip to content

Commit 3d7d4a4

Browse files
committed
feat(plugin-back-to-top): add z-index variable
- refactor: extract css vars to a separate file - refactor: extract util functions to separate files - refactor: provide client entry
1 parent 67242e8 commit 3d7d4a4

File tree

17 files changed

+118
-130
lines changed

17 files changed

+118
-130
lines changed

docs/reference/plugin/back-to-top.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,4 @@ This plugin will add a _back to top_ button to your site. The button will be dis
88

99
You can customize the style of the _back to top_ button via CSS variables:
1010

11-
```css
12-
:root {
13-
/* color */
14-
--back-to-top-color: #3eaf7c;
15-
/* color on mouse hover */
16-
--back-to-top-color-hover: #71cda3;
17-
}
18-
```
11+
@[code css](@vuepress/plugin-back-to-top/src/client/styles/vars.css)

docs/zh/reference/plugin/back-to-top.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,4 @@
88

99
你可以通过 CSS 变量来自定义 _返回顶部_ 按钮的样式:
1010

11-
```css
12-
:root {
13-
/* 颜色 */
14-
--back-to-top-color: #3eaf7c;
15-
/* 鼠标悬停时的颜色 */
16-
--back-to-top-color-hover: #71cda3;
17-
}
18-
```
11+
@[code css](@vuepress/plugin-back-to-top/src/client/styles/vars.css)

packages/@vuepress/plugin-back-to-top/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"scripts": {
2727
"build": "tsc -b tsconfig.build.json",
2828
"clean": "rimraf lib *.tsbuildinfo",
29-
"copy": "cpx \"src/**/*.vue\" lib"
29+
"copy": "cpx \"src/**/*.{css,svg}\" lib"
3030
},
3131
"dependencies": {
3232
"@vuepress/core": "2.0.0-beta.15",

packages/@vuepress/plugin-back-to-top/src/client/BackToTop.vue

Lines changed: 0 additions & 103 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { debounce } from 'ts-debounce'
2+
import { computed, defineComponent, h, onMounted, ref, Transition } from 'vue'
3+
import { getScrollTop, scrollToTop } from '../utils'
4+
5+
import '../styles/vars.css'
6+
import '../styles/back-to-top.css'
7+
8+
export const BackToTop = defineComponent({
9+
name: 'BackToTop',
10+
11+
setup() {
12+
const scrollTop = ref(0)
13+
const show = computed(() => scrollTop.value > 300)
14+
15+
onMounted(() => {
16+
scrollTop.value = getScrollTop()
17+
18+
window.addEventListener(
19+
'scroll',
20+
debounce(() => {
21+
scrollTop.value = getScrollTop()
22+
}, 100)
23+
)
24+
})
25+
26+
const backToTopEl = h('div', { class: 'back-to-top', onClick: scrollToTop })
27+
28+
return () =>
29+
h(
30+
Transition,
31+
{
32+
name: 'back-to-top',
33+
},
34+
{
35+
default: () => (show.value ? backToTopEl : null),
36+
}
37+
)
38+
},
39+
})
40+
41+
export default BackToTop
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './components/BackToTop'
2+
export * from './utils'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.back-to-top {
2+
cursor: pointer;
3+
position: fixed;
4+
bottom: 2rem;
5+
right: 2.5rem;
6+
width: 2rem;
7+
height: 1.2rem;
8+
background-color: var(--back-to-top-color);
9+
mask: url('back-to-top.svg') no-repeat;
10+
z-index: var(--back-to-top-z-index);
11+
}
12+
13+
.back-to-top:hover {
14+
background-color: var(--back-to-top-color-hover);
15+
}
16+
17+
@media (max-width: 959px) {
18+
.back-to-top {
19+
display: none;
20+
}
21+
}
22+
23+
.back-to-top-enter-active,
24+
.back-to-top-leave-active {
25+
transition: opacity 0.3s;
26+
}
27+
28+
.back-to-top-enter-from,
29+
.back-to-top-leave-to {
30+
opacity: 0;
31+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
:root {
2+
--back-to-top-z-index: 5;
3+
4+
--back-to-top-color: #3eaf7c;
5+
--back-to-top-color-hover: #71cda3;
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const getScrollTop = (): number =>
2+
window.pageYOffset ||
3+
document.documentElement.scrollTop ||
4+
document.body.scrollTop ||
5+
0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './getScrollTop'
2+
export * from './scrollToTop'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const scrollToTop = (): void =>
2+
window.scrollTo({ top: 0, behavior: 'smooth' })

packages/@vuepress/plugin-back-to-top/src/node/backToTopPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const backToTopPlugin: Plugin<BackToTopPluginOptions> = (_, app) => {
2121

2222
clientAppRootComponentFiles: path.resolve(
2323
__dirname,
24-
'../client/BackToTop.vue'
24+
'../client/components/BackToTop.js'
2525
),
2626
}
2727
}
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
{
22
"extends": "../../../tsconfig.base.json",
3-
"compilerOptions": {
4-
"module": "CommonJS",
5-
"rootDir": "./src",
6-
"outDir": "./lib"
7-
},
8-
"include": ["./src"],
93
"references": [
104
{ "path": "../core/tsconfig.build.json" },
11-
{ "path": "../utils/tsconfig.build.json" }
12-
]
5+
{ "path": "../utils/tsconfig.build.json" },
6+
{ "path": "./tsconfig.esm.json" },
7+
{ "path": "./tsconfig.cjs.json" }
8+
],
9+
"files": []
1310
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"module": "CommonJS",
5+
"rootDir": "./src",
6+
"outDir": "./lib"
7+
},
8+
"include": ["./src/node"]
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"module": "ES2020",
5+
"rootDir": "./src",
6+
"outDir": "./lib"
7+
},
8+
"include": ["./src/client"]
9+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../../../tsconfig.base.json",
3-
"include": ["./src/**/*.ts", "./src/**/*.vue", "./__tests__"]
3+
"include": ["./src/**/*.ts", "./__tests__"]
44
}

0 commit comments

Comments
 (0)