JavaScript Components
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.
For more information on writing Javascript within your Ruby code...
Importing Javascript or React Libraries
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
ornpm
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 Material UI 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:
Material UI Google's Material UI as React components
Semantic UI A React wrapper for the Semantic UI stylesheet
ReactStrap Bootstrap 4 React wrapper
Making Custom Wrappers - WORK IN PROGRESS ...
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.
This can be done using the imports
directive and the Hyperstack::Component::NativeLibrary
superclass.
Importing Image Assets via Webpack
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
jQuery
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:
You can access jQuery anywhere in your code using the jQ
method. For details see https://github.com/opal/opal-jquery
Note most of the time you will not need to manipulate the dom directly.
The dom_node method
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.
Last updated