Coder Social home page Coder Social logo

tcarterjr / ember-cli-clipboard Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jkusa/ember-cli-clipboard

0.0 0.0 0.0 8.86 MB

A simple ember wrapper around clipboard.js

Home Page: https://jkusa.github.io/ember-cli-clipboard

License: MIT License

JavaScript 67.69% HTML 4.74% Handlebars 18.52% SCSS 9.05%

ember-cli-clipboard's Introduction

ember-cli-clipboard

Downloads Build Status Ember Observer Score

A simple ember wrapper around clipboard.js (no flash)

Demo Page

http://jkusa.github.io/ember-cli-clipboard

Usage

CopyButton Component

<!-- Set text directly -->
<CopyButton
  @text='text to be copied'
  @onSuccess={{this.onSuccess}}
  @onError={{this.onError}}
>
  Click To Copy
</CopyButton>

<!-- Get text from action that returns a string -->
<CopyButton
  @text={{this.getText}}
  @onSuccess={{this.onSuccess}}
  @onError={{this.onError}}
>
  Click To Copy
</CopyButton>

<!-- Get text from target element -->
<input
  id='url'
  type='text'
  value='https://github.com/jkusa/ember-cli-clipboard'
/>
<CopyButton
  @target='#url'
  @onSuccess={{this.onSuccess}}
  @onError={{this.onError}}
>
  Click To Copy
</CopyButton>

Arguments

  • text - string value or action that returns a string to be copied
  • target - selector string of element or action that returns an element from which to copy text
  • action - string value of operation: copy or cut (default is copy)
  • container - selector string or element object of containing element. "For use in Bootstrap Modals or with any other library that changes the focus you'll want to set the focused element as the container value".
  • delegateClickEvent - clipboard.js defaults event listeners to the body in order to reduce memory footprint if there are hundreds of event listeners on a page. If you want to scope the event listener to the copy button, set this property to false
  • buttonType - string value of the button's type attribute (for Ember < 3.25.x, see below)

Any HTML button attribute passed to the component will be "splatted" on the button element. The one exception to this is the type attribute due to this issue < Ember 3.25.x.

Actions

The following clipboard.js custom events are sent as actions

  • onSuccess sent on successful copy
  • onError sent on failed copy

More information about the clipboard.js events can be found here

Clipboard Element Modifier

Under the hood the <CopyButton> component is powered by a {{clipboard}} element modifier. This can be used directly as an alternative to the <CopyButton> component. It has the same argument contract as the <CopyButton> component except for the exclusion of the buttonType argument.

<button
  class='button is-outline'
  type='button'
  {{clipboard text='text to be copied' onSuccess=this.onSuccess}}
>
  Click To Copy
</button>

Template Helper

The helper is-clipboard-supported can be used to check if clipboard.js is supported or not.

{{#if (is-clipboard-supported)}}
  <CopyButton @target='#url'>
    Click To Copy
  </CopyButton>
{{/if}}

Test Helpers

Some browsers do not allow simulated clicks to fire execCommand('copy'). This makes testing difficult. To assist with integration testing, the following test helpers are available to test the wiring of the success and error action handlers.

Acceptance Test Helpers

  • triggerCopySuccess(selector='.copy-btn')
  • triggerCopyError(selector='.copy-btn')
// tests/acceptance/my-test.js

import {
  triggerCopyError,
  triggerCopySuccess,
} from 'ember-cli-clipboard/test-support';

Example:

// tests/acceptance/my-test.js

test('copy button message', async function (assert) {
  assert.expect(3);

  await visit('/');
  assert.dom('.alert').doesNotExist('no alert message is initially present');

  triggerCopySuccess();

  assert
    .dom('.alert.alert-success')
    .exists('a success message is displayed when a copy is successful');

  triggerCopyError();

  assert
    .dom('.alert.alert-info')
    .exists('an error message is displayed when a copy is unsuccessful');
});

Integration Test Helpers

  • triggerCopySuccess(selector='.copy-btn')
  • triggerCopyError(selector='.copy-btn')

Example:

// tests/integration/components/my-test.js

// if using NEW ember testing api
import {
  triggerCopyError,
  triggerCopySuccess,
} from 'ember-cli-clipboard/test-support';

test('copy-button integration', async function (assert) {
  assert.expect(2);

  this.set('onSuccess', () => {
    assert.ok(true, '`success` action handler correctly fired');
  });

  this.set('onError', () => {
    assert.ok(true, '`error` action handler correctly fired');
  });

  await render(hbs`
    <CopyButton
      class="my-copy-btn"
      @text="text to be copied"
      @onSuccess={{this.onSuccess}}
      @onError={{this.onError}}
    >
      Click To Copy
    </CopyButton>
  `);

  //If using NEW ember testing api
  triggerCopyError('.my-copy-btn');
  triggerCopySuccess('.my-copy-btn');
});

Browser Support

For browser support information, checkout the clipboard.js documentation:

https://github.com/zenorocha/clipboard.js/#browser-support

Contributing

Contributions are welcomed. Please read the contributing guidelines.

License

This project is licensed under the MIT License.

ember-cli-clipboard's People

Contributors

jkusa avatar dependabot-preview[bot] avatar dependabot[bot] avatar hibariya avatar balajimanoharan avatar snewcomer avatar yugandharb avatar osxi avatar dependabot-support avatar ember-tomster avatar sivakumar-kailasam avatar ryanponce avatar ryanpatrickcook avatar rwjblue avatar rlivsey avatar puwelous avatar oscarni avatar miguelcobain avatar martinmalinda avatar jrjohnson avatar dcyriller avatar camskene avatar enna avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.