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
+34-33Lines changed: 34 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
1
# HTML and Component DSL
2
2
3
-
**Work in progress - ALPHA (docs and code)**
4
-
5
-
A key design goal of the DSL (Domain Specific Language) is to make it work seamlessly with the rest of Ruby and easy to work with HTML elements and Components.
3
+
A key design goal of the DSL (Domain Specific Language) is to make it work seamlessly with the rest of Ruby and easy to work with HTML elements and Components. Additionally, the DSL provides an abstraction layer between your code and the underlying (fast moving) technology stack. Hyperstack always uses the very latest versions of React and React Router yet our DSL does not change often. We believe that a stable DSL abstraction is an advantage.
6
4
7
5
## HTML DSL
8
6
@@ -16,7 +14,7 @@ UL do
16
14
end
17
15
```
18
16
19
-
**Note: All HTML tags are written in CAPS.**
17
+
> **Notice that the HTML elements (BUTTON, DIV, etc.) are in CAPS**. We know this is bending the standard Ruby style rules, but we think it reads better this way.
20
18
21
19
For example, to render a `<div>`:
22
20
@@ -97,28 +95,28 @@ Hyperstack Component DSL is a set of class and instance methods that are used to
97
95
98
96
The DSL has the following major areas:
99
97
100
-
+ The `Hyperstack::Component` class or `Hyperstack::Component::Mixin` mixin
98
+
+ The `Hyperstack::Component` class or `Hyperstack::Component` mixin
+ The four data accessors methods: `params`, `state`, `mutate`, and `children`
104
102
+ Event handlers
105
103
+ Miscellaneous methods
106
104
107
105
## Hyperstack::Component
108
106
109
-
Hyperstack Components classes either include `Hyperstack::Component::Mixin` or are subclasses of `Hyperstack::Component`.
107
+
Hyperstack Components classes either include `Hyperstack::Component` or are subclasses of `Hyperstack::Component`.
110
108
111
109
```ruby
112
110
classComponent < Hyperstack::Component
113
111
end
114
112
115
113
# if subclassing is inappropriate, you can mixin instead
116
114
classAnotherComponent
117
-
includeHyperstack::Component::Mixin
115
+
includeHyperstack::Component
118
116
end
119
117
```
120
118
121
-
At a minimum every component class must define a `render`macro which returns **one single** child element. That child may in turn have an arbitrarily deep structure.
119
+
At a minimum every component class must define a `render`method which returns **one single** child element. That child may in turn have an arbitrarily deep structure.
122
120
123
121
```ruby
124
122
classComponent < Hyperstack::Component
@@ -148,11 +146,11 @@ class FirstComponent < Hyperstack::Component
148
146
end
149
147
```
150
148
151
-
Note that you should never redefine the `new` or `initialize` methods, or call them directly. The equivalent of `initialize` is the `before_mount`callback.
149
+
Note that you should never redefine the `new` or `initialize` methods, or call them directly. The equivalent of `initialize` is the `before_mount`method.
152
150
153
151
### Invoking Components
154
152
155
-
When invoking a custom component **you must have** a (possibly empty) parameter list or (possibly empty) block.
153
+
> Note: when invoking a component **you must have** a (possibly empty) parameter list or (possibly empty) block.
156
154
157
155
```ruby
158
156
MyCustomComponent() # ok
@@ -354,10 +352,10 @@ When designing interfaces, break down the common design elements (buttons, form
354
352
355
353
The `params` method gives *read-only* access to each of the scalar params passed to the Component.
356
354
357
-
Within a React Component the `param`macro is used to define the parameter signature of the component. You can think of params as
355
+
Within a React Component the `param`method is used to define the parameter signature of the component. You can think of params as
358
356
the values that would normally be sent to the instance's `initialize` method, but with the difference that a React Component gets new parameters when it is rerendered.
359
357
360
-
The param macro has the following syntax:
358
+
The param method has the following syntax:
361
359
362
360
```ruby
363
361
param symbol, ...options... # or
@@ -526,7 +524,7 @@ end
526
524
527
525
A common type of React component is one that extends a basic HTML element in a simple way. Often you'll want to copy any HTML attributes passed to your component to the underlying HTML element.
528
526
529
-
To do this use the `collect_other_params_as`macro which will gather all the params you did not declare into a hash. Then you can pass this hash on to the child component
527
+
To do this use the `collect_other_params_as`method which will gather all the params you did not declare into a hash. Then you can pass this hash on to the child component
530
528
531
529
```ruby
532
530
classCheckLink < Hyperstack::Component
@@ -665,11 +663,11 @@ class TickTock < Hyperstack::Component
665
663
end
666
664
```
667
665
668
-
Notice that TickTock effectively has two before_mount callbacks, one that is called to initialize the `@intervals` array and another to initialize `state.seconds`
666
+
Notice that TickTock effectively has two `before_mount` methods, one that is called to initialize the `@intervals` array and another to initialize `state.seconds`
669
667
670
-
## Lifecycle Callbacks
668
+
## Lifecycle Methods
671
669
672
-
A component may define callbacks for each phase of the components lifecycle:
670
+
A component may define lifecycle methods for each phase of the components lifecycle:
673
671
674
672
*`before_mount`
675
673
*`render`
@@ -679,41 +677,44 @@ A component may define callbacks for each phase of the components lifecycle:
679
677
*`after_update`
680
678
*`before_unmount`
681
679
682
-
All the callback macros may take a block or the name of an instance method to be called.
680
+
>Note: A `render` method must be defined and must return just one HTML element.
681
+
682
+
All the Component Lifecycle methods may take a block or the name of an instance method to be called.
683
683
684
684
```ruby
685
685
classMyComponent < Hyperstack::Component
686
686
before_mount do
687
687
# initialize stuff here
688
688
end
689
+
690
+
render do
691
+
# return just one HTML element
692
+
end
693
+
689
694
before_unmount :cleanup# call the cleanup method before unmounting
690
695
...
691
696
end
692
697
```
693
698
694
-
Except for the render callback, multiple callbacks may be defined for each lifecycle phase, and will be executed in the order defined, and from most deeply nested subclass outwards.
695
-
696
-
Details on the component lifecycle is described [here](docs/component-specs.html)
699
+
Except for the render method, multiple lifecycle methods may be defined for each lifecycle phase, and will be executed in the order defined, and from most deeply nested subclass outwards.
697
700
698
701
### Lifecycle Methods
699
702
700
-
A component class may define callbacks for specific points in a component's lifecycle.
703
+
A component class may define lifecycle methods for specific points in a component's lifecycle.
701
704
702
705
### Rendering
703
706
704
-
The lifecycle revolves around rendering the component. As the state or parameters of a component changes, its render method will be called to generate the new HTML. The rest of the callbacks hook into the lifecycle before or after rendering.
705
-
706
-
For reasons described below Hyperstack provides a render callback to simplify defining the render method:
707
+
The lifecycle revolves around rendering the component. As the state or parameters of a component changes, its render method will be called to generate the new HTML.
707
708
708
709
```ruby
709
710
render do ....
710
711
end
711
712
```
712
713
713
-
The render callback will generate the components render method. It may optionally take the container component and params:
714
+
The render method may optionally take the container component and params:
714
715
715
716
```ruby
716
-
render(:DIV, class: 'my-class') do
717
+
render(DIV, class: 'my-class') do
717
718
...
718
719
end
719
720
```
@@ -728,7 +729,7 @@ render do
728
729
end
729
730
```
730
731
731
-
The purpose of the render callback is syntactic. Many components consist of a static outer container with possibly some parameters, and most component's render method by necessity will be longer than the normal *10 line* ruby style guideline. The render call back solves both these problems by allowing the outer container to be specified as part of the callback parameter (which reads very nicely) and because the render code is now specified as a block you avoid the 10 line limitation, while encouraging the rest of your methods to adhere to normal ruby style guides
732
+
The purpose of the render method is syntactic. Many components consist of a static outer container with possibly some parameters, and most component's render method by necessity will be longer than the normal *10 line* ruby style guideline. The render method solves both these problems by allowing the outer container to be specified as part of the method parameter (which reads very nicely) and because the render code is now specified as a block you avoid the 10 line limitation, while encouraging the rest of your methods to adhere to normal ruby style guides
732
733
733
734
### Before Mounting (first render)
734
735
@@ -749,7 +750,7 @@ after_mount do ...
749
750
end
750
751
```
751
752
752
-
Invoked once, only on the client (not on the server), immediately after the initial rendering occurs. At this point in the lifecycle, you can access any refs to your children (e.g., to access the underlying DOM representation). The `after_mount`callbacks of children components are invoked before that of parent components.
753
+
Invoked once, only on the client (not on the server), immediately after the initial rendering occurs. At this point in the lifecycle, you can access any refs to your children (e.g., to access the underlying DOM representation). The `after_mount`methods of children components are invoked before that of parent components.
753
754
754
755
If you want to integrate with other JavaScript frameworks, set timers using the `after` or `every` methods, or send AJAX requests, perform those operations in this method. Attempting to perform such operations in before_mount will cause errors during prerendering because none of these operations are available in the server environment.
755
756
@@ -829,7 +830,7 @@ end
829
830
830
831
Invoked immediately before a component is unmounted from the DOM.
831
832
832
-
Perform any necessary cleanup in this method, such as invalidating timers or cleaning up any DOM elements that were created in the `after_mount`callback.
833
+
Perform any necessary cleanup in this method, such as invalidating timers or cleaning up any DOM elements that were created in the `after_mount`method.
833
834
834
835
### The force_update! method
835
836
@@ -924,7 +925,7 @@ type -> String
924
925
925
926
### Event pooling
926
927
927
-
The underlying React `SyntheticEvent` is pooled. This means that the `SyntheticEvent` object will be reused and all properties will be nullified after the event callback has been invoked. This is for performance reasons. As such, you cannot access the event in an asynchronous way.
928
+
The underlying React `SyntheticEvent` is pooled. This means that the `SyntheticEvent` object will be reused and all properties will be nullified after the event method has been invoked. This is for performance reasons. As such, you cannot access the event in an asynchronous way.
928
929
929
930
### Supported Events
930
931
@@ -1187,7 +1188,7 @@ The `imports` directive takes a string (or a symbol) and will simply evaluate it
1187
1188
1188
1189
### The dom_node method
1189
1190
1190
-
Returns the HTML dom_node that this component instance is mounted to. Typically used in the `after_mount`callback to setup linkages to external libraries.
1191
+
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.
NOTE: in the route you say Hyperstack_prererendering but in the query string its Hyperstack-prerendering (underscore vs. dash). This is because of rails security protection when using defaults.
1411
+
>Note: in the route you say Hyperstack_prererendering but in the query string its Hyperstack-prerendering (underscore vs. dash). This is because of rails security protection when using defaults.
0 commit comments