You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/dsl-client/hyper-component.md
+83-63Lines changed: 83 additions & 63 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1212,8 +1212,6 @@ Event names:
1212
1212
1213
1213
## Javascript Components
1214
1214
1215
-
**TODO - improve this section**
1216
-
1217
1215
Hyperstack gives you full access to the entire universe of JavaScript libraries and components directly within your Ruby code.
1218
1216
1219
1217
Everything you can do in JavaScript is simple to do in Ruby; this includes passing parameters between Ruby and JavaScript and even passing Ruby methods as JavaScript callbacks. See the JavaScript section for more information.
@@ -1222,9 +1220,9 @@ While it is quite possible to develop large applications purely in Hyperstack Co
1222
1220
1223
1221
Either way you are going to need to import Javascript components into the Hyperstack namespace. Hyperstack provides both manual and automatic mechanisms to do this depending on the level of control you need.
1224
1222
1225
-
### Importing Components
1223
+
### Importing React Components
1226
1224
1227
-
Lets say you have an existing React Component written in javascript that you would like to access from Hyperstack.
1225
+
Let's say you have an existing React Component written in Javascript that you would like to access from Hyperstack.
1228
1226
1229
1227
Here is a simple hello world component:
1230
1228
@@ -1254,75 +1252,70 @@ end
1254
1252
1255
1253
The `imports` directive takes a string (or a symbol) and will simply evaluate it and check to make sure that the value looks like a React component, and then set the underlying native component to point to the imported component.
1256
1254
1257
-
### The dom_node method
1255
+
### Importing Javascript or React Libraries
1258
1256
1259
-
Returns the HTML dom_node that this component instance is mounted to. Typically used in the `after_mount` method to setup linkages to external libraries.
1257
+
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.
1260
1258
1261
-
Example:
1259
+
Using Webpacker (or Webpack) there are just a few simple steps:
1262
1260
1263
-
TODO
1261
+
+ Add the library source to your project using `yarn` or `npm`
1262
+
+ Import the JavaScript objects you require
1263
+
+ Package your bundle with `webpack`
1264
+
+ Use the JavaScript or React component as if it were a Ruby class
1264
1265
1265
-
### Importing Libraries
1266
+
Here is an example of setting up [Material UI](https://material-ui.com/):
1266
1267
1267
-
Many React components come in libraries. The `ReactBootstrap` library is one example. You can import the whole library at once using the `React::NativeLibrary` class. Assuming that you have initialized `ReactBootstrap` elsewhere, this is how you would bring it into Hyperstack.
1268
+
Firstly, you install the library:
1268
1269
1269
-
```ruby
1270
-
classRBS < React::NativeLibrary
1271
-
imports 'ReactBootstrap'
1272
-
end
1270
+
```
1271
+
// with yarn
1272
+
yarn add @material-ui/core
1273
+
1274
+
// or with npm
1275
+
npm install @material-ui/core
1273
1276
```
1274
1277
1275
-
We can now access our bootstrap components as components defined within the RBS scope:
1278
+
Next you import the objects you plan to us (or you can import the whole library)
1276
1279
1277
1280
```ruby
1278
-
classShow < HyperComponent
1281
+
# app/javascript/packs/hyperstack.js
1279
1282
1280
-
defsay_hello(i)
1281
-
alert "Hello from number #{i}"
1282
-
end
1283
+
# to import the whole library
1284
+
import * as Muifrom '@material-ui/core';
1285
+
global.Mui=Mui;
1283
1286
1284
-
render RBS::Navbar, bsStyle::inversedo
1285
-
RBS::Nav() do
1286
-
RBS::NavbarBrand() do
1287
-
A(href:'#') { 'Hyperstack Showcase' }
1288
-
end
1289
-
RBS::NavDropdown(eventKey:1, title:'Things', id::drop_down) do
1290
-
(1..5).each do |n|
1291
-
RBS::MenuItem(href:'#', key: n, eventKey:"1.#{n}") do
1292
-
"Number #{n}"
1293
-
end.on(:click) { say_hello(n) }
1294
-
end
1295
-
end
1296
-
end
1297
-
end
1298
-
end
1287
+
# or if you just want one component from the library
1288
+
import Button from '@material-ui/core/Button';
1289
+
global.Button=Button;
1299
1290
```
1300
1291
1301
-
Besides the `imports` directive, `React::NativeLibrary` also provides a rename directive that takes pairs in the form `oldname => newname`. For example:
`React::NativeLibrary` will import components that may be deeply nested in the library. For example consider a component was defined as `MyLibrary.MySubLibrary.MyComponent`:
1298
+
Now you can use Material UI Components in your Ruby code:
1308
1299
1309
1300
```ruby
1310
-
classMyLib < React::NativeLibrary
1311
-
imports 'MyLibrary'
1301
+
# if you imported the whole library
1302
+
Mui.Button(variant::contained, color::primary) { "Click me" }.on(:click) do
1303
+
alert 'you clicked the button!'
1312
1304
end
1313
1305
1314
-
classApp < React::NativeLibrary
1315
-
render do
1316
-
...
1317
-
MyLib::MySubLibrary::MyComponent ...
1318
-
...
1319
-
end
1306
+
# if you just imported the Button component
1307
+
Button(variant::contained, color::primary) { "Click me" }.on(:click) do
1308
+
alert 'you clicked the button!'
1320
1309
end
1321
1310
```
1322
1311
1323
-
Note that the `rename` directive can be used to rename both components and sublibraries, giving you full control over the ruby names of the components and libraries.
1312
+
Libraries used often with Hyperstack projects:
1324
1313
1325
-
### Importing Webpack Images
1314
+
+[Material UI](https://material-ui.com/) Google's Material UI as React components
1315
+
+[Semantic UI](https://react.semantic-ui.com/) A React wrapper for the Semantic UI stylesheet
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
Returns the HTML dom_node that this component instance is mounted to. Typically used in the `after_mount` method to setup linkages to external libraries.
1376
+
1377
+
Example:
1378
+
1379
+
TODO - write example
1380
1380
1381
-
### Auto Import
1381
+
### The `as_node` and `to_n` methods
1382
1382
1383
-
If you use a lot of libraries and are using a Javascript tool chain with Webpack, having to import the libraries in both Hyperstack and Webpack is redundant and just hard work.
1383
+
Sometimes you need to create a Component without rendering it so you can pass it as a parameter of a method. This model is used often in the React world.
1384
1384
1385
-
Instead you can opt-in for *auto importing* Javascript components into Hyperstack as you need them. Simply `require hyper-react/auto-import` immediately after you `require hyper-react`.
1385
+
The example below is taken from Semantic UI, building a [Tab Component](https://react.semantic-ui.com/modules/tab/#types-basic) with multiple tabs:
1386
1386
1387
-
Now you do not have to use component `imports` directive or `React::NativeLibrary` unless you need to rename a component.
1387
+
Here is the Javascript example:
1388
1388
1389
-
In Ruby all module and class names normally begin with an uppercase letter. However in Javascript this is not always the case, so the auto import will first try the Javascript name that exactly matches the Ruby name, and if that fails it will try the same name with the first character downcased. For example
1389
+
```javascript
1390
+
importReactfrom'react'
1391
+
import { Tab } from'semantic-ui-react'
1390
1392
1391
-
`MyComponent` will first try `MyComponent` in the Javascript name space, then `myComponent`.
Likewise MyLib::MyComponent would match any of the following in the Javascript namespace: `MyLib.MyComponent`, `myLib.MyComponent`, `MyLib.myComponent`, `myLib.myComponent`
1398
+
constTabExampleBasic= () =><Tab panes={panes} />
1399
+
1400
+
exportdefaultTabExampleBasic
1401
+
```
1394
1402
1395
-
*How it works: The first time Ruby hits a native library or component name, the constant value will not be defined. This will trigger a lookup in the javascript name space for the matching component or library name. This will generate either a new subclass of HyperComponent or React::NativeLibrary that imports the javascript object, and no further lookups will be needed.*
1403
+
And here is the same example converted to Ruby:
1404
+
1405
+
```ruby
1406
+
# notice we use .as_node to create the Component without rendering it
1407
+
tab_1 =Sem.TabPanedo
1408
+
P { 'Tab 1 Content' }
1409
+
end.as_node
1410
+
1411
+
tab_2 =Sem.TabPanedo
1412
+
P { 'Tab 2 Content' }
1413
+
end.as_node
1414
+
1415
+
# notice how we use .to_n to convert the Ruby component to a native JS object
Just a word on Webpack: If you a Ruby developer who is new to using Javascript libraries then we recommend using Webpack to manage javascript component dependencies. Webpack is essentially bundler for Javascript. Please see our Tutorials section for more information.
1410
-
1411
-
There are also good tutorials on integrating Webpack with existing rails apps a google search away.
0 commit comments