Giter Site home page Giter Site logo

react-zeroclipboard's Introduction

react-zeroclipboard

This is a wrapper around ZeroClipboard for use with React. ZeroClipboard has a difficult to work with api which is being abstracted from you. This library...

  • asynchronusly loads zeroclipboard from cdnjs
  • handles mounting/unmounting of components
  • figures out which element was clicked
  • allows you to declare text/html/rtf, or pass a function which returns it dynamically

Warning!

zero-clipboard uses flash which is being phased out across many browsers, and is sometimes buggy in the browsers that do support it. There have been reports of the plugin randomly not working, despite no significant changes to the plugin, and keeping the same zero-clipboard version.

If you're interested in solving this, your help is very much appreciated, and this section will be replaced with a big "thank you" for your work. Until then, using this plugin isn't recommended.

Install

This is only available through npm, it should work with browserify or webpack. It's compatible with react 0.13 and up.

npm install --save react-zeroclipboard

Or for react 0.11 and 0.12

npm install --save [email protected]

Also install react if you haven't already (of course).

Usage

Here's a simple example:

render: function(){
   return (
      <div>
         <p>Click the button to copy some text</p>
            <ReactZeroClipboard text="Hello, world!">
               <button>Copy</button>
            </ReactZeroClipboard>
      </div>
   )
}

The full api offers more flexibility. If you provide e.g. html and text, they'll both be set and the application you're pasting into decides which one to use. Methods have higher priority than the literal strings, if for some reason you pass both.

<ReactZeroClipboard 
   text="text to copy"
   html="<b>html to copy</b>"
   richText="{\\rtf1\\ansi\n{\\b rich text to copy}}"
   getText={(Void -> String)}
   getHtml={(Void -> String)}
   getRichText={(Void -> String)}

   onCopy={(Event -> Void)}
   onAfterCopy={(Event -> Void)}
   onErrorCopy={(Error -> Void)}

   onReady={(Event -> Void)}

   // optional
   swfPath="http://user_defined_cdn_path/ZeroClipboard.swf"
/>

Here's an example where we copy the current url to the clipboard, both in plain text and a html anchor

If the user pastes this in their address bar they get the url, and if they paste it in gmail they get a nice link.

render: function(){
   return (
      <div>
         <p>Copy a link to this page</p>
            <ReactZeroClipboard 
               getText={function(){ return location.href; }}
               getHtml={function(){ return '<a href="' + location.href + '">My Page</a>'; }}>
                  <button>Copy</button>
            </ReactZeroClipboard>
      </div>
   )
}

I'm getting weird ZeroClipboard errors

Usually this is caused by flash weirdness and/or cdnjs where we pull ZeroClipboard from. You can work around this by hosting ZeroClipboard and the swf yourself. Make sure the page and the two assets are either both http, or both https. There's a copy of the swf in the assets directory of this repo.

To access it with webpack, configure a loader for swf files:

{
  test: /\.swf$/g,
  loader: 'file-loader'
}

And then import the swf from this package.

<ReactZeroClipboard swfPath={ require('react-zeroclipboard/assets/ZeroClipboard.swf') }>

react-zeroclipboard's People

Contributors

brigand avatar edgarjs avatar justinbelcher avatar michaelkantor avatar moroshko avatar noor avatar richardh-vccp avatar tmcw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-zeroclipboard's Issues

add ability to use global zeroclipboard script in readme.md

Thanks for your work! Saved me the headache of working with zeroclipboard's not fun api. I just wanted to suggest adding the option that you can globally override the cdnjs script in the readme, which I found mentioned here -> (#4). I was trying to see if this was possible myself when I came upon the closed issue, so it could just help others from searching around. I'll be happy to provide a PR with the addition if you'd like. Thanks again.

semver problem in 2.0.2

the version 2.0.1 works fine with react 0.13, but the 2.0.2 doesn't.

Wouldn't it be better to add a new 2.0.3 version compatible the 2.0.1 (maybe the exact same code) so builds using ^2.0.0 won't break, and then create the version 2.1.0 to follow semver rules?

thanks.

No button hilighting

Thanks for doing this, works really well.

One nitpick - your demo and this example <button>Copy</button> actually do the copy...but the button doesn't depress so it feel broken. Throwing an onClick into the button didn't help..

IE8 support?

ZeroClipboard stopped supporting IE8 sometime in 2.x (re: https://github.com/zeroclipboard/zeroclipboard#support). However, React does continue to support IE8 and react-zeroclipboard may be useful to anyone still supporting IE8.

I'm curious what your thoughts are on using an older version of ZeroClipboard from the CDN so that IE8 can be supported. Also, there is one line of code that needs to change in loadScript.js since document.head isn't supported in IE8: https://github.com/brigand/react-zeroclipboard/blob/master/loadScript.js#L37

Doesn't work at all

This package doesn't appear to work at all. I'm on the latest React.

import ZeroClipboard from 'react-zeroclipboard';

...
        <ZeroClipboard text={link.shareId}>
          <button>Copy</button>
        </ZeroClipboard>

Clicking "Copy" has no effect.

Asynch?

Hi-
Was wondering if there's a way to do copy with a callback... I have text (json blob) generated by the server, is not precomputed. Did not see a way offhand.
Thanks,
Jeff

Clipboard doesn't update if onCopy is defined

Hi, thanks a lot for this tool!

Maybe i doing something wrong but it's looks like the clipboard is only updating if i don't fill the onCopy param.

I tested on a basic example like:

<div>
   <p>Click the button to copy some text</p>
      <ReactZeroClipboard text="Hello, world!" onCopy={function(){}}>
         <button>Copy</button>
      </ReactZeroClipboard>
</div>

with [email protected]

Question: Zeroclipboard Mobile Support

I'm new to zeroclipboard and this component, but thanks for making this. It does an awesome job at making zeroclipboard easy in react.

Before diving deeply into it, I'm wondering if there is already a known issue with zeroclipboard not supporting Android (I'm testing latest chrome & firefox on Android 5.0).

At first, I thought there was something wrong with my implementation, but the demo does not work either. From the little I've looked into it, it looks like the copy event just does not fire (and no console msg or error indicating a problem).

Is this a known issue? Is zeroclipboard not for mobile in general?

Thanks again for this component and taking the time to read this question.

React v0.14 support

React.findDOMNode is deprecated in React 0.14

This warning appears in the console:
Warning: React.findDOMNode is deprecated. Please use ReactDOM.findDOMNode from require('react-dom') instead.

SyntaxError

[Error] SyntaxError: Line 78: Unexpected token ) while parsing      /Users/david/Projects/meetcasts/node_modules/react-zeroclipboard/index.js while parsing file: /Users/david/Projects/meetcasts/node_modules/react-zeroclipboard/index.js
global code (app.js, line 1)

Improve Event Handlers

Working on adding support for event handlers, as suggested in #1. onCopy, onAfterCopy, and onError will be supported.

I'm not sure when the error event would be emitted, though...

Bad build dependencies

Looks like new react-dom dependency should be in 'dependencies', not 'devDependencies'. Our build is breaking with missing package "react-dom". Backing up to 2.0.1 for now

367d1f7

npm repo is out of sync with github

There seems to be no support for swfPath attribute in what's downloaded as 1.1.0 by npm. I looked in index.js and it's different from latest github version. There is no mention of the variable "swfPath"

onlyChild error

I am using this component within an iterator. Due to that, I keep getting this error:

Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of Ayah. See https://fb.me/react-warning-keys for more information.
invariant.js:42Uncaught Error: Invariant Violation: onlyChild must be passed a children with exactly one child.

Using it:

<ReactZeroClipboard text={this.props.ayah.text} className="text-muted" key={this.props.ayah.ayah}>
          <i className="fa fa-clipboard" /> Copy
        </ReactZeroClipboard>

Detail `onCopy`, `onAfterCopy`, `onError` in Readme

Great component - worked first time with zero pain. Nice one!

It's fairly standard to want to change a button to "Copied" from "Copy to clipboard" after click. I noticed this is supported from looking in the source, would be good to put it in the main readme.

Cheers!

OnCopy running even if fail?

Hi!

Thanks for making an awesome plugin!!! I have been having an absolute BLAST with React and your plugin seems like the best solution for my code generating react application!

However it seems intermittent, it works sometimes, othertimes it does not work. I'm not sure how to troubleshoot.. can you help me?

Here is my code:

       <ReactZeroClipboard
            getText={function(){ return clipdata }}
            getHtml={function(){ return clipdata }}
            onCopy={function(e) {
                AppActions.createMessage('Table HTML has been saved in your Clipboard!', 'info');
                _gaq.push(['_trackEvent', 'Saved', 'generator']);
              }}>
            <a title="Copy to Clipboard" className="btn btn-success"><i className="fa fa-clipboard"/> Copy HTML</a>
          </ReactZeroClipboard>

This is what it looks like:
Imgur

Then when you click on the item the message pops up like so:
Imgur

But clipboard does not update..

Any ideas????

Funny thing is it was working just fine yesterday!
You can test the site here: http://www.tablesnap.net

Uncaught exception using basic example

I'm getting the following error using a simple instance following the example:

<ReactZeroClipboard text={ token }><button>COPY</button></ReactZeroClipboard>

I'm using webpack and React 15.4, but otherwise not sure what information might help.

Any ideas?

Uncaught Error: Error: An invalid exception was thrown. at HTMLObjectElement.swf.(anonymous function) (<anonymous>:19:32) at _setHandCursor (http://cdnjs.cloudflare.com/ajax/libs/zeroclipboard/2.2.0/ZeroClipboard.js:1678:28) at Function._focus (http://cdnjs.cloudflare.com/ajax/libs/zeroclipboard/2.2.0/ZeroClipboard.js:707:5) at Function.ZeroClipboard.focus.ZeroClipboard.activate (http://cdnjs.cloudflare.com/ajax/libs/zeroclipboard/2.2.0/ZeroClipboard.js:1984:19) at HTMLButtonElement._elementMouseOver (http://cdnjs.cloudflare.com/ajax/libs/zeroclipboard/2.2.0/ZeroClipboard.js:2380:21)

Support to custom key combination(like ctrl+alt+c) or adding data to clipboard with custom menu link

Hi,

I have question that does this module support custom key combination for copying to clipboard(like Ctrl+Alt+C)? Also if there is any support to copy to clipboard on click of any custom menu implementation like in any react function I want to copy some custom data to clipboard dynamically and later paste it on some other menu item (like paste to clipboard) click.

Is there any example for it? I couldn't find anything of this sought

ZeroClipboard as a dependency instead of from a CDN?

I'm interested in using this library on an application that will ideally only use a limited set of allowed CNAMEs for all resources. Would you accept a pull request that adds the ability to use a local copy of ZeroClipboard$ instead of loading it from a CDN?

Fallback to global ZeroClipboard?

We've gotten bitten a few times by cdnjs being down (or a particular network blocking for one reason or another). It'd be nice to have a synchronous version that just uses an already global ZeroClipboard (although I understand the convenience of loading on-demand async).

Not working, possibly issue with latest cdnjs version

Update - It seems to work fine on Google Canary - but I'm seeing this issue with latest Chrome, and Firefox.

You can see right on your demo page @brigand.

When you hover over the copy button:
Uncaught Error: Error: An invalid exception was thrown.

I haven't had a chance to dig in to it, but it looks like the issue may have something to do with loading the latest cdnjs version 2.2.0, as I have another app (using react-zerodclipboard v1.1.0) running in production that is loading 2.1.5 and it's working fine

Looks like there were some zeroclipboard API changes that maybe are causing the issue?.

How to automatic copy the text?

Hi, thanks a lot for this tool!

I used it in my project, code as follows:

<ReactZeroClipboard  text={"sssssss"}  onAfterCopy={this.handleCopy.bind(this)}
                                                   onErrorCopy={this.handleErrorCopy.bind(this)}>
        <span className="copy">Copy Link</span>
 </ReactZeroClipboard>

If I don't click the span, I can't copy the text. So how to automatic copy the text when the ReactZeroClipboard component is rendering(don't need to click the span)?

Thanks your res.

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.