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
// app/javascript/packs/client_and_server.js// to import the whole libraryMui =require('@material-ui/core')// or to import a single componentButton =require('@material-ui/core/Button')
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
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:
bin/webpack # rebuild the webpacks
rm -rf tmp/cache # clear the cached sprockets files
# if you imported the whole library
Mui::Button(variant: :contained, color: :primary) { "Click me" }.on(:click) do
alert 'you clicked the primary button!'
end
# if you just imported the Button component
Button(variant: :contained, color: :secondary) { "Click me" }.on(:click) do
alert 'you clicked the secondary button!'
end
webpackImagesMap = {};
var imagesContext = require.context('../images/', true, /\.(gif|jpg|png|svg)$/i);
function importAll (r) {
r.keys().forEach(key => webpackImagesMap[key] = r(key));
}
importAll(imagesContext);
# app/hyperstack/helpers/images_import.rb
class HyperComponent
def self.img_src(file_path) # for use outside a component
@img_map ||= Native(`webpackImagesMap`)
@img_map["./#{file_path}"]
end
def img_src(file_path) # for use in a component
HyperComponent.img_src(file_path)
end
...
end