Skip to content

bicycle-codes/copy-button

Repository files navigation

copy button

tests module semantic versioning license

A button to copy some text to the system clipboard, made with webcomponents.

install

npm i -S @bicycle-codes/copy-button

demonstration

See bicycle-codes.github.io/copy-button for an example with the default CSS.

globals

This looks at a css variable --success-color, which determines the color the checkmark that is displayed after you copy something.

use

There are several options.

Bundler

Import this file. It depends on tonic and clipboard-copy, so you will need to provide them somehow, either through a bundler or importmap.

example

You must use an ID attribute, because the CopyButton element contains state.

import Tonic from '@bicycle-codes/tonic'
import { CopyButton, SuccessSvg, CopySvg } from '@bicycle-codes/copy-button'
import '@bicycle-codes/copy-button/style.css'

function ButtonExample () {
    // this needs an ID attribute
    return this.html`
        <copy-button class="copy example" id="example"></copy-button>
    `
}

Tonic.add(ButtonExample)
Tonic.add(CopyButton)
Tonic.add(SuccessSvg)
Tonic.add(CopySvg)

pre-bundled

First copy the bundled file to a location that is accessible to your web server:

cp ./node_modules/@bicycle-codes/copy-button/dist/index.bundle.js ./public/copy-button.js

Then link to the bundled file, and you can use as an html element.

<body>
    <copy-button></copy-button>
</div>
<script type="module" src="./copy-button.js"></script>

pre-bundled + minifed

Copy the bundled and minified file to a location that is accessible to your web server:

cp ./node_modules/@bicycle-codes/copy-button/dist/index.bundle.min.js ./public/copy-button.min.js

Link to the minified file in HTML:

<body>
    <copy-button></copy-button>
</div>
<script type="module" src="./copy-button.min.js"></script>

CSS

Override the variable --success-color to customize the color.

.copy-button {
    --success-color: green;
}

Create a button like this

screenshot of the button, pre-click


screenshot of the button, post-click