Back-to-top button plugin for VuePress documentation sites with smooth scrolling and configurable visibility
npx @tessl/cli install tessl/npm-vuepress--plugin-back-to-top@1.9.0VuePress Back-to-Top Plugin provides a floating back-to-top button component for VuePress documentation sites. It features smooth scrolling animation, configurable visibility threshold, responsive design, and debounced scroll event handling for optimal performance.
npm install @vuepress/plugin-back-to-topThis is a VuePress plugin, so it's primarily configured in VuePress config files rather than imported directly:
// .vuepress/config.js
module.exports = {
plugins: ['@vuepress/plugin-back-to-top']
}For advanced configurations:
// .vuepress/config.js
module.exports = {
plugins: [
['@vuepress/plugin-back-to-top', {
// Plugin currently accepts no configuration options
}]
]
}Simply add the plugin to your VuePress configuration. The plugin automatically:
BackToTop Vue component// .vuepress/config.js
module.exports = {
title: 'My VuePress Site',
description: 'My documentation site',
plugins: [
'@vuepress/plugin-back-to-top'
]
}The plugin follows VuePress's plugin architecture with these key components:
BackToTop.vue component providing the UI and behaviorThe main plugin entry point that integrates with VuePress.
/**
* VuePress plugin object for back-to-top functionality
* @type {import('@vuepress/types').Plugin}
*/
module.exports = {
enhanceAppFiles: string[];
globalUIComponents: string;
};
/**
* Plugin options interface (currently empty)
*/
interface PluginConfig4BackToTop {}The plugin object contains:
enhanceAppFiles: Array pointing to the app enhancement fileglobalUIComponents: String specifying 'BackToTop' as a global UI componentThe main UI component that renders the back-to-top button.
/**
* BackToTop Vue component
*/
interface BackToTopComponent {
name: 'BackToTop';
props: {
threshold: {
type: Number;
default: 300;
};
};
data(): {
scrollTop: number | null;
};
computed: {
show(): boolean;
};
methods: {
getScrollTop(): number;
scrollToTop(): void;
};
mounted(): void;
}
/**
* Template interface - SVG-based back-to-top button
*/
interface BackToTopTemplate {
/** Click handler for scrolling to top */
'@click': 'scrollToTop';
/** Conditional visibility based on show computed property */
'v-if': 'show';
/** CSS class applied to the SVG element */
class: 'go-to-top';
/** Fade transition wrapper */
transition: 'fade';
}Props:
threshold: Number - Scroll position in pixels after which the button becomes visible (default: 300)Data Properties:
scrollTop: Current scroll position in pixelsComputed Properties:
show: Boolean indicating whether the button should be visible (based on scroll position vs threshold)Methods:
getScrollTop(): Returns current scroll position from window, document, or bodyscrollToTop(): Scrolls to top of page with smooth behavior and resets scrollTop to 0Lifecycle Hooks:
mounted(): Sets up initial scroll position and adds debounced scroll event listener to windowRegisters the BackToTop component globally in the Vue application.
/**
* VuePress app enhancement function
* @param {Object} context - VuePress app context
* @param {Vue} context.Vue - Vue constructor
*/
function enhanceApp({ Vue }: { Vue: any }): void;This function is automatically called by VuePress and registers the BackToTop component globally, making it available as <BackToTop/> in all Vue templates.
window.scrollTo({ top: 0, behavior: 'smooth' })$accentColor variableCSS Classes:
.go-to-top: Main button styling (fixed position, cursor, color, dimensions).go-to-top:hover: Hover state with lighter accent color.fade-enter-active, .fade-leave-active: Transition animation classes (0.3s opacity).fade-enter, .fade-leave-to: Fade animation states (opacity: 0)@vuepress/types: TypeScript definitions for VuePress plugin APIlodash.debounce: Scroll event throttling@vuepress/shared-utils: Path utilities for file resolutionThe plugin is designed to be fault-tolerant:
// VuePress Plugin Types
interface Plugin<T = any, U = any> {
enhanceAppFiles?: string | string[];
globalUIComponents?: string | string[];
}
interface PluginConfig4BackToTop {}
// Component Props Interface
interface BackToTopProps {
threshold?: number;
}
// Vue Component Data Interface
interface BackToTopData {
scrollTop: number | null;
}
// App Enhancement Context
interface EnhanceAppContext {
Vue: any;
}