zenorocha / clipboard.js

:scissors: Modern copy to clipboard. No Flash. Just 3kb gzipped :clipboard:
https://clipboardjs.com
MIT License
33.98k stars 3.98k forks source link

Need event.stopImmediatePropagation() to prevent the event from triggering a redirect #850

Closed artknight closed 1 year ago

artknight commented 1 year ago

🐛 Bug Report

When the copy-to-clipboard button is inside an element that that has an onclick event, the onclick event is getting triggered.

Desired behavior: the text gets copied to the clipboard without triggering the underlying event.

Corrective action

e.preventDefault(); // <=== ADDED BY ME
e.stopImmediatePropagation();  // < === ADDED BY ME

Affected method ( lines 301 - 331 )

}, {
key: "onClick",
value: function onClick(e) {
    e.preventDefault(); // <=== ADDED BY ME
    e.stopImmediatePropagation();  // < === ADDED BY ME

        var trigger = e.delegateTarget || e.currentTarget;
    var action = this.action(trigger) || 'copy';
    var text = actions_default({
        action: action,
        container: this.container,
        target: this.target(trigger),
        text: this.text(trigger)
    }); // Fires an event based on the copy operation result.

    this.emit(text ? 'success' : 'error', {
        action: action,
        text: text,
        trigger: trigger,
        clearSelection: function clearSelection() {
            if (trigger) {
                trigger.focus();
            }

            window.getSelection().removeAllRanges();
        }
    });
}
/**
    * Default `action` lookup function.
    * @param {Element} trigger
    */

}, {
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.