0
<script>
import '@interactjs/auto-start'
import '@interactjs/actions/drag'
import '@interactjs/actions/resize'
import '@interactjs/modifiers'
import '@interactjs/dev-tools'
import interact from '@interactjs/interact'

    // Step 1
const slider = interact('.slider')    // target elements with the "slider" class

slider
  // Step 2
  .draggable({                        // make the element fire drag events
    origin: 'self',                   // (0, 0) will be the element's top-left
    inertia: true,                    // start inertial movement if thrown
    modifiers: [
      interact.modifiers.restrict({
        restriction: 'self'           // keep the drag coords within the element
      })
    ],
    // Step 3
    listeners: {
      move (event) {                  // call this listener on every dragmove
        const sliderWidth = interact.getElementRect(event.target).width
        const value = event.pageX / sliderWidth

        event.target.style.paddingLeft = (value * 100) + '%'
        event.target.setAttribute('data-value', value.toFixed(2))
      }
    }
  })
</script>
<style>
.sliders {
  padding: 1.5em
}
/* the slider bar */
.slider {
  position: relative;
  width: 100%;
  height: 1em;
  margin: 1.5em auto;
  background-color: #29e;
  border-radius: 0.5em;
  box-sizing: border-box;

  font-size: 1em;

  -ms-touch-action: none;
     touch-action: none;
}

/* the slider handle */
.slider:before {
  content: "";
  display: block;
  position: relative;
  top: -0.5em;

  width: 2em;
  height: 2em;
  margin-left: -1em;
  border: solid 0.25em #fff;
  border-radius: 1em;
  background-color: inherit;

  box-sizing: border-box;
}

/* display the value */
.slider:after {
  content: attr(data-value);
  position: absolute;
  top: -1.5em;
  width: 2em;
  line-height:1em;
  margin-left: -1em;
  text-align: center;
}</style>
<div class="sliders">
  <div class="slider"></div>
  <div class="slider"></div>
  <div class="slider"></div>
</div>

I'm trying to use interact.js in rails 6, but I cannot get it to work. I am a newbie so this may be a common question, but how do I properly import an external library like interact.js. I've tried everything I've found online so I imagine I'm looking for the wrong thing. Any help will be appreciated, Thanks in advance!

0

1 Answer 1

0

this syntax:

import '@interactjs/auto-start'
import '@interactjs/actions/drag'
import '@interactjs/actions/resize'
import '@interactjs/modifiers'
import '@interactjs/dev-tools'
import interact from '@interactjs/interact'

is used for npm modules import. You can't do that in the browser without preprocessing your code. You can import the library like this, preferably in a separate script tag. But if you plan to import only one library you can set it

<script src="CDN url"></script>

Put it above your other script tag. Find an appropriate CDN host for your library. Example: https://cdnjs.com/libraries/interact.js/1.0.2

You can't use those import statements like this. If you import the library from CDN, you can use it like in the documentation. Look at https://interactjs.io/docs/installation CDN pre-bundled usage. I presume interact is globally exposed and you don't have to import anything.

If you want to go the proper way about this, you would be setting up a separate project/folder for your frontend application. That application has to be then built, and you attach the built distribution files in your Rails HTML. It depends on your purposes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.