4

I want to add a Copy to Clipboard button to my page in a Ruby on Rails 7 project.

config/importmap.rb:

pin "clipboard.js", to: "https://cdn.jsdelivr.net/npm/clipboard@2.0.10/dist/clipboard.min.js", preload: true

app/javascript/application.js:

import { ClipboardJS } from "clipboard.js"

...

document.addEventListener('turbo:load', (event) => {
  new ClipboardJS('.js-clipboard', {
    text: function(trigger) {
      return trigger.getAttribute('data-clipboard-text')
    }
  })
})

But after page loads I get an error:

Uncaught SyntaxError: import not found: ClipboardJS
  • Rails 7.0.2.3
  • Ruby 3.0.2
installero
  • 9,096
  • 3
  • 39
  • 43

1 Answers1

5

Hi try to install clipboard by using the command

bin/importmap pin clipboard

Then instead of importing the module ClipboardJS by using import { ClipboardJS } from "clipboard.js", import just the class this way:

import ClipboardJS from 'clipboard'

Everything else is correct.

installero
  • 9,096
  • 3
  • 39
  • 43
myanch200
  • 76
  • 2