Skip to content

Request: Copy without onClick Event #218

Closed
@mlucool

Description

@mlucool

I would like to suggest the API allow for a programatic copy without a click event. The use case here is better integration with a framework like React where I want to use react's onClick bindings.

Optimally it would work as:

import {copy} from 'clipboard';
copy('my Text'); 

After playing with the code, it seems that this could be accomplished by a singleton Clipboard in the copy function that tracks nothing.

I gave this a quick attempt via adding the following, but the succeeded = document.execCommand(this.action) in clipboard-action.js returns false (does not throw). I am not sure why this would be the case when the normal clipboard via click works on the same page.

    copy(text) {
        if (this.clipboardAction) {
            this.clipboardAction = null;
        }

        this.clipboardAction = new ClipboardAction({
            action  : 'copy',
            target  : undefined,
            text    : text,
            trigger : undefined,
            emitter : this
        });
    }

Tested via:

setTimeout(function() {clipboard.copy('HHOLAAZ')}, 100);

Please let me know if you think this is a good idea and any suggestions on how to make it work. I can make the PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions