0

using ruby 2.7.1p83 and Rails 6.0.3.2 in rails project.

imported huebee inside the rails appliction webpack.

 yarn add huebee

imported its css and js files for the in the application.js and application.css files respectively:

for application.js
import 'huebee/huebee';
for application.css
@import "~huebee/huebee";

Now, if

 <input class="color-input" data-huebee='{ "notation": "hex", "saturations": 2 }'/>

enter image description here

is working fine and showing the drow box for colour selector:

but when I initialise the Huebee object inside js it given error as:

var hueb = new Huebee('.color-sample', {
    notation: 'hex',
    saturations: 2,
    staticOpen: true,
    hues: 6,

});

enter image description here enter image description here

Uncaught ReferenceError: Huebee is not defined

please suggest the right approach to fix this issue thanks in advance.

my user.js file where Huebee is imported as:

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")

import "jquery";
import "jquery-ui";
import "popper.js";
import "bootstrap";
import 'owl.carousel';
import 'jquery.easing';
import "@fortawesome/fontawesome-free/js/all";
import "@fortawesome/fontawesome-free/css/all.css";
import '../stylesheets/user.scss';
import Huebee from 'huebee/huebee';
require.context('../images', true, /\.(png|jpe?g|svg)$/);

user.scss file:

@import '~bootstrap';
@import '~owl.carousel2/dist/assets/owl.theme.default';
@import '~owl.carousel2/dist/assets/owl.carousel';
@import "~huebee/huebee";

Huebee script for initialisation:

(function () {
    const element= document.querySelector('.color-sample');
    var hueb = new Huebee(element, {
        notation: 'hex',
        saturations: 2,
        staticOpen: true,
        hues: 6,
    });
})();

This gives following error: enter image description here

4
  • In which JS file have you initialised the Huebee. If you are using the file other than application js. Then you need to attack Huebee class to global variable or window to access in other js files. Commented Aug 11, 2020 at 19:34
  • I have imported 'huebee/huebee' inside application.js file and initialize Hubee in the same file. Commented Aug 12, 2020 at 16:53
  • Can you edit your question with full code of application.js Commented Aug 12, 2020 at 18:27
  • Updated the answer with user.js and user.scss file becuase application.js and application.scss file are update to some thing else. Commented Aug 17, 2020 at 4:13

2 Answers 2

2

You need to import Huebee class from the package.

In application.js

import Huebee from 'huebee/huebee';

In your js code, pass element as reference instead of directly passing .color-sample. And make sure the following code runs after document load.

const element= document.querySelector('.color-input');
var hueb = new Huebee(element, {
    notation: 'hex',
    saturations: 2,
    staticOpen: true,
    hues: 6,
});
Sign up to request clarification or add additional context in comments.

11 Comments

30:210 Uncaught ReferenceError: Huebee is not defined still same issue.
@vidurpunj I have replicated and it's working fine. Can you edit your question with complete code, so that we can understand the exact issue
Updated the answer with user.js and user.scss file becuase application.js and application.scss file are update to some thing else.
@vidurpunj where is Huebee initialisation? In which js
@vidurpunj is the initialisation in user.js? If not try moving to the user.js or declare it as window or global variable to make it accessible
|
0

user.js file:

import Huebee from 'huebee/huebee';
global.Huebee = Huebee;

The erb file which is using Huebee is as:

  <input class="color-input" data-huebee='{ "notation": "hex", "saturations": 2 }'/>

(function () {
    const element= document.querySelector('.color-input');
    var hueb = new Huebee(element, {
        notation: 'hex',
        saturations: 2,
        staticOpen: true,
        hues: 6,
    });
})();

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.