-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-back-to-top): rebuild plugin (#55)
- Loading branch information
1 parent
d91cf15
commit 1900257
Showing
22 changed files
with
381 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 79 additions & 23 deletions
102
plugins/plugin-back-to-top/src/client/components/BackToTop.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,93 @@ | ||
import { debounce } from 'ts-debounce' | ||
import { computed, defineComponent, h, onMounted, ref, Transition } from 'vue' | ||
import { getScrollTop, scrollToTop } from '../utils.js' | ||
import { useLocaleConfig } from '@vuepress/helper/client' | ||
import { useElementSize, useWindowScroll, useWindowSize } from '@vueuse/core' | ||
import { | ||
computed, | ||
defineComponent, | ||
h, | ||
onMounted, | ||
shallowRef, | ||
Transition, | ||
} from 'vue' | ||
import { usePageFrontmatter } from 'vuepress/client' | ||
import type { BackToTopPluginLocaleConfig } from '../../shared/index.js' | ||
|
||
import '../styles/vars.css' | ||
import '../styles/back-to-top.css' | ||
|
||
declare const __BACK_TO_TOP_LOCALES__: BackToTopPluginLocaleConfig | ||
declare const __BACK_TO_TOP_PROGRESS__: boolean | ||
declare const __BACK_TO_TOP_THRESHOLD__: number | ||
|
||
export const BackToTop = defineComponent({ | ||
name: 'BackToTop', | ||
|
||
setup() { | ||
const scrollTop = ref(0) | ||
const show = computed(() => scrollTop.value > 300) | ||
const onScroll = debounce(() => { | ||
scrollTop.value = getScrollTop() | ||
}, 100) | ||
setup(props) { | ||
const pageFrontmatter = usePageFrontmatter<{ backToTop?: boolean }>() | ||
const locale = useLocaleConfig(__BACK_TO_TOP_LOCALES__) | ||
const body = shallowRef<HTMLElement>() | ||
const { height: bodyHeight } = useElementSize(body) | ||
const { height: windowHeight } = useWindowSize() | ||
|
||
onMounted(() => { | ||
scrollTop.value = getScrollTop() | ||
/** Scroll distance */ | ||
const { y } = useWindowScroll() | ||
|
||
window.addEventListener('scroll', () => onScroll()) | ||
}) | ||
/** Whether to display button */ | ||
const show = computed( | ||
() => | ||
pageFrontmatter.value.backToTop !== false && | ||
y.value > __BACK_TO_TOP_THRESHOLD__, | ||
) | ||
|
||
const backToTopEl = h('div', { class: 'back-to-top', onClick: scrollToTop }) | ||
const progress = computed( | ||
() => (y.value / (bodyHeight.value - windowHeight.value)) * 100, | ||
) | ||
|
||
onMounted(() => { | ||
body.value = document.body | ||
}) | ||
|
||
return () => | ||
h( | ||
Transition, | ||
{ | ||
name: 'back-to-top', | ||
}, | ||
() => (show.value ? backToTopEl : null), | ||
h(Transition, { name: 'back-to-top' }, () => | ||
show.value | ||
? h( | ||
'button', | ||
{ | ||
'type': 'button', | ||
'class': 'vp-back-to-top-button', | ||
'aria-label': locale.value.backToTop, | ||
'onClick': () => { | ||
window.scrollTo({ top: 0, behavior: 'smooth' }) | ||
}, | ||
}, | ||
[ | ||
__BACK_TO_TOP_PROGRESS__ | ||
? h( | ||
'span', | ||
{ | ||
'class': 'vp-scroll-progress', | ||
'role': 'progressbar', | ||
'aria-labelledby': 'loadinglabel', | ||
'aria-valuenow': progress.value, | ||
}, | ||
h( | ||
'svg', | ||
h('circle', { | ||
cx: '50%', | ||
cy: '50%', | ||
style: { | ||
'stroke-dasharray': `calc(${ | ||
Math.PI * progress.value | ||
}% - ${4 * Math.PI}px) calc(${Math.PI * 100}% - ${ | ||
4 * Math.PI | ||
}px)`, | ||
}, | ||
}), | ||
), | ||
) | ||
: null, | ||
h('div', { class: 'back-to-top-icon' }), | ||
], | ||
) | ||
: null, | ||
) | ||
}, | ||
}) | ||
|
||
export default BackToTop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 68 additions & 14 deletions
82
plugins/plugin-back-to-top/src/client/styles/back-to-top.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.