JavaScript Components
Last updated
Was this helpful?
Last updated
Was this helpful?
Hyperstack gives you full access to the entire universe of JavaScript libraries and components directly within your Ruby code.
Everything you can do in JavaScript is simple to do in Opal-Ruby; this includes passing parameters between Ruby and JavaScript and even passing Ruby methods as JavaScript callbacks.
Importing and using React libraries from inside Hyperstack is very simple and very powerful. Any JavaScript or React based library can be accessible in your Ruby code.
Using Webpacker there are just a few simple steps:
Add the library source to your project using yarn
or npm
Import the JavaScript objects you require
Use the JavaScript or React component as if it were a Ruby class
Here is an example using the library:
Firstly, you install the library:
Next you import the objects you plan to use:
Theoretically webpacker will detect the change and rebuild everything, but you might have to do the following:
Now you can use Material UI Components in your Ruby code:
Libraries used often with Hyperstack projects:
This can be done using the imports
directive and the Hyperstack::Component::NativeLibrary
superclass.
If you store your images in app/javascript/images
directory and want to display them in components, please add the following code to app/javascript/packs/application.js
The above code creates an images map and stores it in webpackImagesMap variable. It looks something like this
Add the following helpers to your HyperComponent class
After that you will be able to display the images in your components like this
Hyperstack comes with a jQuery wrapper that you can optionally load. First add jQuery using yarn:
then insure jQuery is required in your client_only.js packs file:
finally require it in your hyper_component.rb file:
Note most of the time you will not need to manipulate the dom directly.
Returns the HTML dom_node that this component instance is mounted to. For example you can use dom_node
to set the focus on an input after its mounted.
Google's Material UI as React components
A React wrapper for the Semantic UI stylesheet
Bootstrap 4 React wrapper
Hyperstack will automatically import Javascript components and component libraries as discussed above. Sometimes for complex libraries that you will use a lot it is useful to add some syntactic sugar to the wrapper.
You can access jQuery anywhere in your code using the jQ
method. For details see