Skip to content

Commit 0289982

Browse files
committed
Merge branch 'edge' of github.com:hyperstack-org/hyperstack into edge
2 parents 10375b0 + 30859ee commit 0289982

File tree

5 files changed

+47
-1593
lines changed

5 files changed

+47
-1593
lines changed

.DS_Store

-6 KB
Binary file not shown.

docs/dsl-client/hyper-component.md

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# HTML and Component DSL
22

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.
64

75
## HTML DSL
86

@@ -16,7 +14,7 @@ UL do
1614
end
1715
```
1816

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.
2018
2119
For example, to render a `<div>`:
2220

@@ -97,28 +95,28 @@ Hyperstack Component DSL is a set of class and instance methods that are used to
9795

9896
The DSL has the following major areas:
9997

100-
+ The `Hyperstack::Component` class or `Hyperstack::Component::Mixin` mixin
98+
+ The `Hyperstack::Component` class or `Hyperstack::Component` mixin
10199
+ HTML DSL elements
102-
+ Lifecycle methods (or macros) (`before_mount`, `render`, `after_mount`, `after_update`, `after_error`)
100+
+ Component Lifecycle Methods (`before_mount`, `render`, `after_mount`, `after_update`, `after_error`)
103101
+ The four data accessors methods: `params`, `state`, `mutate`, and `children`
104102
+ Event handlers
105103
+ Miscellaneous methods
106104

107105
## Hyperstack::Component
108106

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`.
110108

111109
```ruby
112110
class Component < Hyperstack::Component
113111
end
114112

115113
# if subclassing is inappropriate, you can mixin instead
116114
class AnotherComponent
117-
include Hyperstack::Component::Mixin
115+
include Hyperstack::Component
118116
end
119117
```
120118

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.
122120

123121
```ruby
124122
class Component < Hyperstack::Component
@@ -148,11 +146,11 @@ class FirstComponent < Hyperstack::Component
148146
end
149147
```
150148

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.
152150

153151
### Invoking Components
154152

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.
156154
157155
```ruby
158156
MyCustomComponent() # ok
@@ -354,10 +352,10 @@ When designing interfaces, break down the common design elements (buttons, form
354352

355353
The `params` method gives *read-only* access to each of the scalar params passed to the Component.
356354

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
358356
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.
359357

360-
The param macro has the following syntax:
358+
The param method has the following syntax:
361359

362360
```ruby
363361
param symbol, ...options... # or
@@ -526,7 +524,7 @@ end
526524

527525
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.
528526

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
530528

531529
```ruby
532530
class CheckLink < Hyperstack::Component
@@ -665,11 +663,11 @@ class TickTock < Hyperstack::Component
665663
end
666664
```
667665

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`
669667

670-
## Lifecycle Callbacks
668+
## Lifecycle Methods
671669

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:
673671

674672
* `before_mount`
675673
* `render`
@@ -679,41 +677,44 @@ A component may define callbacks for each phase of the components lifecycle:
679677
* `after_update`
680678
* `before_unmount`
681679

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.
683683

684684
```ruby
685685
class MyComponent < Hyperstack::Component
686686
before_mount do
687687
# initialize stuff here
688688
end
689+
690+
render do
691+
# return just one HTML element
692+
end
693+
689694
before_unmount :cleanup # call the cleanup method before unmounting
690695
...
691696
end
692697
```
693698

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.
697700

698701
### Lifecycle Methods
699702

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.
701704

702705
### Rendering
703706

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.
707708

708709
```ruby
709710
render do ....
710711
end
711712
```
712713

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:
714715

715716
```ruby
716-
render(:DIV, class: 'my-class') do
717+
render(DIV, class: 'my-class') do
717718
...
718719
end
719720
```
@@ -728,7 +729,7 @@ render do
728729
end
729730
```
730731

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
732733

733734
### Before Mounting (first render)
734735

@@ -749,7 +750,7 @@ after_mount do ...
749750
end
750751
```
751752

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.
753754

754755
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.
755756

@@ -829,7 +830,7 @@ end
829830

830831
Invoked immediately before a component is unmounted from the DOM.
831832

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.
833834

834835
### The force_update! method
835836

@@ -924,7 +925,7 @@ type -> String
924925

925926
### Event pooling
926927

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.
928929

929930
### Supported Events
930931

@@ -1187,7 +1188,7 @@ The `imports` directive takes a string (or a symbol) and will simply evaluate it
11871188

11881189
### The dom_node method
11891190

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.
11911192

11921193
Example:
11931194

@@ -1407,7 +1408,7 @@ http://localhost:3000/my_hyper_app/some_page?Hyperstack-prerendering=off
14071408

14081409
This is useful for development and testing.
14091410

1410-
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.
14111412
14121413
## DSL Gotchas
14131414

0 commit comments

Comments
 (0)