0.0.5 • Published 4 years ago

@hopsoft/copy-controller v0.0.5

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Copy Controller

Copies text from input and textarea elements to the clipboard.

Quick Start

yarn add @hopsoft/copy-controller

app/javascript/controllers/index.js

import { Application } from 'stimulus'
import { definitionsFromContext } from 'stimulus/webpack-helpers'
import CopyController from '@hopsoft/copy-controller'

const application = Application.start()
const context = require.context('./controllers', true, /\.js$/)
application.load(definitionsFromContext(context))
application.register('copy', CopyController)

app/views/demos/copy.html.erb

<div data-controller="copy" data-copy-content='Copied...' data-copy-duration='1'>
  <textarea data-target='copy.source'></textarea>
  <button type="button" data-target='copy.target' data-action='click->copy#copy'>Copy</button>
</div>

Usage

The root element is a container that holds holds all other elements and is configured by setting:

data-controller="copy"

Configuration

AttributeDefaultDescription
data-copy-contentoptional"Copied..."Content to show in the button after a copy has been performed
data-copy-durationoptional2000How many milliseconds to show the copied content in the button before reverting to the original content

Copied content can be disabled by setting data-copy-content="" or data-copy-duration="0"

Targets

Targets are child elements identified by setting:

data-target="copy.TARGET_NAME"
NameDescription
sourcerequiredAn input or textarea that holds the content to be copied
triggerrequiredThe button used to perform the copy

Actions

Actions are behaviors that can be triggered and are identified by setting:

data-action="copy#ACTION_NAME"
NameDescription
copyrequiredPerforms the copy action (typically defined on the button target)