Skip to content

Commit e9aa67a

Browse files
committed
Finishing v5 jails documentation
1 parent b8d79eb commit e9aa67a

22 files changed

+872
-11
lines changed

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1-
# Headline
1+
<h1> <a href="#" style="text-decoration:none">A close to Vanilla <br />Javascript Component Library </a></h1>
22

3-
> An awesome project.
3+
Jails is a project that aims simplicity by keeping as close as possible to vanilla Javascript development, and in the same time automates tasks that help us to improve our **produtivity**.
4+
5+
## The front end problematic actual state
6+
7+
It seems that we missed the simplicity from the time we were building interface applications with vanilla javascript. Many of our tools, language, and browsers has evolved through the years but some ideas might leading us to a place with a lot of unecessary complexity for **the vast majority of our use cases**.
8+
9+
Those ideas that comes in form of frameworks and complex libraries are making us believe that every large application needs complex solutions or a huge paradigm shift.
10+
11+
We are loosing the benefits of reusing libraries already built in Vanilla Javascript because there's no framework that has **interoperability** in mind, that's why you have the same library being rewritten in many forms.
12+
13+
Also, along with complexity the performance delivered in applications using frameworks are often not as good as it should. So, we are paying the technical debt by ignoring important learnings from the past like: Unobtrusive JavaScript, Separation of Concerns and goold Javascript Functional technics.
14+
15+
## The Motivation
16+
17+
Jails is very lightweight and it was designed to give you enough power to write dynamic components with the ability to integrate with any vanilla javascript libraries without leading you to a obscure and dark complex concepts. It's intend to help you to write javascript applications in an elegant fashion using some good concepts of programming that are framework agnostic like:
18+
19+
- Event Delegation
20+
- Event Driven Architecture
21+
- Observer and PubSub patterns for store and component communication
22+
23+
## Best Scenarios to Use
24+
Jails is a library with **separation of concerns** in its core philosophy, so its not a isomorphic library, it doesn't intend to solve all the problems in the web application world. It's fully focus on client side development.
25+
So you can use it on any Server Side Rendered Apps, or Static Sites Generated Apps like :
26+
27+
- Nodejs SSR and SSG apps using template systems ( Nunjucks, Pug, Ejs, Handlebars, Mustache, Liquidjs, etc )
28+
- PHP projects ( Laravel, Ruby on Rails, Wordpress, Drupal, Joomla, etc )
29+
- Static generated apps ( Hugo, Astro, Jekill, etc )
30+
31+
## Not a good scenario
32+
It's not intended to solve all the problems, it is focus only on web applications, not recomended to use on SPA's and it's not a hibrid solution for mobile applications but it's pretty suitable for PWA's and Mobile using Web View's and it can be pretty fast and a good alternative before engage into a native path.

_sidebar.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<div class="logo">
2+
<img class="jails" src="images/logo.svg" alt="" />
3+
<h1>Jails 5</h1>
4+
</div>
5+
6+
- Getting Started
7+
- [Introduction](/)
8+
- [Installing](installing.md)
9+
- [Usage](usage.md)
10+
- [How does Jails Works?](how-it-works.md)
11+
- [Examples](examples.md)
12+
13+
- Components
14+
- [Introduction](components-intro.md)
15+
- [main](main.md)
16+
- [elm](elm.md)
17+
- [on | off | trigger | emit](events.md)
18+
- [publish & subscribe](pubsub.md)
19+
- [state](state.md)
20+
- [unmount](unmount.md)
21+
- [onupdate](onupdate.md)
22+
- [dependencies](dependencies.md)
23+
24+
- Template System
25+
- [Cheatsheet](cheatsheet.md)
26+
- [html-static](html-static.md)
27+
28+
- Packages
29+
- [Introduction](packages.md)

cheatsheet.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
2+
# Template System
3+
4+
> An Interface to update DOM with a new state using html directives.
5+
6+
You can either choose to change DOM directly or use Template System in Jails. To use the template system and update the view with a new state you have to call `state.set` function helper passing a **plain javascript object** as a new state.
7+
8+
!> For more information about internal state, jump to `state` section inside **Components**.
9+
10+
11+
### Usage
12+
13+
```js
14+
export default function myComponent ({ main, state }) {
15+
16+
main( _ => [
17+
showInfo
18+
])
19+
20+
const showInfo = () => {
21+
state.set({
22+
name :'Clark Kent',
23+
enemies:[
24+
'Lex Luthor',
25+
'Darkseid',
26+
'General Zod',
27+
'Brainiac'
28+
]
29+
})
30+
}
31+
}
32+
```
33+
34+
```html
35+
<my-component>
36+
<h1>Hi {name}</h1>
37+
<ul>
38+
<li>My Enemies</li>
39+
<li html-for="enemy in enemies">
40+
{enemy}
41+
</li>
42+
</ul>
43+
</my-component>
44+
```
45+
46+
47+
48+
### Cheatsheet
49+
50+
#### plain
51+
52+
```js
53+
state.set({ name : 'Clark Kent' })
54+
```
55+
56+
```html
57+
<div>{name}</div>
58+
```
59+
60+
#### Safe propery chain output
61+
```js
62+
state.set({
63+
name: 'Clark Kent',
64+
info: {
65+
version: '2.0'
66+
}
67+
})
68+
```
69+
70+
```html
71+
<p>{ info.version }</p>
72+
<!-- result => "2.0" -->
73+
74+
<p>{ info.foo.foo1 }</p>
75+
<!-- // result => "" without errors -->
76+
77+
<p>{ info['name'] }</p>
78+
<!-- // result => "Clark Kent" -->
79+
```
80+
81+
#### expression
82+
83+
```html
84+
<p>{ 1 + 2 }</p>
85+
<!-- result => 2 -->
86+
87+
<p>{ true ? 'v' : 'foo' }</p>
88+
<!-- result => "v" -->
89+
90+
<p>{ 1 < 3 && 'v' }</p>
91+
<!-- result => "v" -->
92+
```
93+
94+
#### complex expression
95+
96+
```js
97+
state.set({
98+
list: [
99+
{list: [{'title': '<>aa</h1>'}, {'title': 'bb'}], name: 0, show: 1},
100+
{list: [{'title': 0 }, {'title': 'bb'}], name: 'b'}
101+
]
102+
})
103+
```
104+
105+
```html
106+
<h1>{ list[list[0].show === 1 ? list[0].name : 1].list[0].title }</h1>
107+
```
108+
109+
## Directives
110+
111+
### html-if
112+
113+
``` js
114+
state.set({ name : 'Clark Kent', show: true })
115+
```
116+
117+
```html
118+
<div html-if="show">Hello, {name}</div>
119+
<div html-if="!show">I\'m hidden!</div>
120+
<!-- // result => <div>Hello, Clark Kent</div> -->
121+
```
122+
123+
### html-for, html-foreach
124+
- `html-for` is for arrays
125+
- `html-foreach` is for object
126+
127+
Iterable variables `$index` and `$key` is automatically generated.
128+
You can use foreach for arrays aswell because its iterable, but you will get `$key` variable as your index.
129+
130+
> html-for="item in array"
131+
132+
> html-foreach="item in object"
133+
134+
135+
``` js
136+
state.set({
137+
list: [
138+
{name: "Hello", show: true},
139+
{name: "Clark", show: true},
140+
{name: "Kent"}
141+
]
142+
});
143+
```
144+
145+
```html
146+
<ul>
147+
<li html-for="item in list">
148+
<span html-if="item.show">
149+
{ item.name }
150+
{ $index }
151+
</span>
152+
</li>
153+
</ul>
154+
```
155+
156+
---
157+
158+
## Attributes
159+
160+
#### html-*
161+
You can prepend `html-` for all html attributes, the template system will strip them off. Very usefull for attributes that you need to be quiet on page load, like `src` until js is ready, you don't want to make any requests when before javascript parsing.
162+
163+
#### html-class
164+
> html-class="{ currItem === 'list1' ? 'active' : ''}"
165+
166+
#### html-src
167+
> html-src="hello-{index}.png"
168+
169+
170+
## Boolean HTML Attributes
171+
`selected` | `checked` | `readonly` | `disabled` | `autoplay`
172+
173+
They will evaluate an js expression or a variable and set the attribute if the expression is true, and remove attribute otherwise.
174+
175+
> html-checked="10 > 2"
176+
177+
```html
178+
<input type="radio" html-checked="10 > 2" />
179+
<!-- <input type="radio" checked /> -->
180+
181+
<input type="radio" html-checked="false" />
182+
<!-- <input type="radio" /> -->
183+
184+
```

components-intro.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# About Jails Component
2+
> The main and most important part of Jails Library, the **Components**.
3+
4+
We're gonna understand how to build components using this library but in the same time we'll find which parts are gonna help us in tasks that would be more annoying in Vanilla Javascript development.
5+
6+
!> Typescript types are used in examples but they're not mandatory.
7+
8+
---
9+
10+
## Component Anatomy
11+
12+
Your component is a Javscript module that might contain 3 exported variables:
13+
14+
1. `export default Function` - **Required** - That is your component function.
15+
2. `export const model <Object>` - **Optional** - Initial State for your component.
16+
3. `export const view <Function>` - **Optional** - A middleware to filter current state before being passed to the html view.
17+
18+
<br />
19+
20+
*components/my-component.ts*
21+
22+
```ts
23+
import { Component, Model, View } from 'jails-js/types'
24+
25+
export default function myComponent( { main, elm, on, state, ... }: Component ) {
26+
// All javascript goes here...
27+
}
28+
29+
export const model: Model = {
30+
31+
}
32+
33+
export const view: View = (state) => {
34+
return state
35+
}
36+
```
37+
38+
## Component Utility Helpers
39+
40+
Every Component function will get from Jails a set of javascript utilities properties, all of them are encapsulated in the scope of that current `HtmlElement`. Those utilities are not a set of functions like Lodash or Underscore, they are actually functions that help us on tasks we have in front end application. Like managing state, event handlers etc.
41+
42+
In the next Component sections we'll see each of these properties and what they do.
43+
44+
45+
---
46+
47+
## The Benefits
48+
49+
50+
### Encapsulation and Scalability
51+
52+
The Jails role is to run the component function for each html element that match with the registered name using `jails.register` api, giving as parameters a set of utilities functions in order to design your component code and logic.
53+
54+
That means that even if you don't wanna use any of that utilities and just set plain vanilla js code you already will be benefit with the encapsulation running this function for each element in the page, so it scales up for **n** elements with the same logic in the page.
55+
56+
*main.js*
57+
```ts
58+
import jails from 'jails-js'
59+
import * as myComponent from 'components/my-component'
60+
61+
jails.register('my-component', myComponent)
62+
```
63+
64+
*components/my-component.html*
65+
```html
66+
<my-component>
67+
<div class="my-component__content">
68+
<h1>Hello World</h1>
69+
</div>
70+
</my-component>
71+
72+
<my-component>
73+
<div class="my-component__content">
74+
<h1>Hello World 2</h1>
75+
</div>
76+
</my-component>
77+
78+
<my-component>
79+
<div class="my-component__content">
80+
<h1>Hello World 3</h1>
81+
</div>
82+
</my-component>
83+
```

dependencies.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Dependency Injection
2+
3+
> A dependency injection system is a feature that helps you to decouple your components from your project dependencies.
4+
5+
The **Dependency Injection** system can be used to share singleton instances between components, and also useful for testing, so you can stub any dependency.
6+
7+
You can even make your components more generic by making them behave accordandly with dependencies they receive, like a validation component that can expect a set of functions for validations which can vary from one project to another.
8+
9+
*home.js*
10+
11+
```js
12+
import jails from 'jails-js'
13+
import validators from 'helpers/validators'
14+
import * as formValidation from 'components/form-validation'
15+
16+
const dependencies = {
17+
validators // A map of validators to be used : required, email, number, etc.
18+
}
19+
20+
jails.register('form-validation', formValidation, dependencies)
21+
jails.start()
22+
```
23+
24+
*components/formValidation.js*
25+
26+
```js
27+
export default function formValidation ({ main, dependencies }) {
28+
29+
const { validators } = dependencies
30+
31+
main( _ => [
32+
// ... Do UI validations using validators
33+
])
34+
}
35+
```

0 commit comments

Comments
 (0)