vuepress-plugin-code-copy

npmopen in new windowdownloadsopen in new windowlicenseopen in new window

This plugin is inspired by vuepress-plugin-code-copyopen in new window, but for Vue 3.x and VuePress 2.x

VuePress is evolving rapidly, and there's no stable version yet. This plugin is only tested with VuePress version 2.0.0-beta.60

Install

This plugin requries VuePress>=2.0.0open in new window

yarn add -D @snippetors/vuepress-plugin-code-copy
// .vuepress/config.js
import codeCopyPlugin from '@snippetors/vuepress-plugin-code-copy'

export default {
  plugins: [
    codeCopyPlugin(options),
  ],
}

Options

OptionDescriptionTypeAccepted ValuesDefault
selectorThis is the CSS selectoropen in new window to which the copy button component will be attached.String-div[class*="language-"]
alignThis option describes the vertical position of the copy button component as well as the successTextStringtop/bottombottom
colorThis sets the color of the copy button and can take any hex codeopen in new window.String-#27b1ff
backgroundColorThis sets the color of the background transition animation and can take any hex codeopen in new window.String-#0075b8
backgroundTransitionEnables the background transition animation of the attached code block when a user presses the copy button.Booleantrue/falsetrue
successTextThis sets the text that displays when a user presses the copy button.String-Copied!
successTextColorThis sets the color of the text that displays when a user presses the copy button and can take any hex codeopen in new window.String-the same value as option color
staticIconCopy icon is only visible when hovering over code block or is always visible.Booleantrue/falsefalse

This plugin listens to event snippetors-vuepress-plugin-code-copy-update-event, and add copy botton to all components selected by selector if there's no button already added. You may find this useful when the page is locally updated and the new components can be selected by selector, in this case, you can dispatch this specific event to let the plugin recheck all components.

To trigger this plugin to recheck all components

window.dispatchEvent(
  new Event("snippetors-vuepress-plugin-code-copy-update-event")
);

this plugin is listening the event like

window.addEventListener(
  "snippetors-vuepress-plugin-code-copy-update-event",
  callbackToUpdateCopyButton
);

Preview

Your code block here.
You can see a copy button at right bottom corner when you hover on this block
Try click it!