Skip to content

Commit f5b2c32

Browse files
authored
Merge pull request #1 from erhathaway/add-initial-files
Add initial files
2 parents f5d0301 + 9aef6eb commit f5b2c32

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+21058
-1
lines changed

.babelrc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"presets": [
3+
[
4+
"env",
5+
{
6+
"modules": false
7+
}
8+
]
9+
],
10+
"plugins": [
11+
"transform-decorators-legacy",
12+
"transform-class-properties",
13+
"transform-object-rest-spread",
14+
"external-helpers",
15+
"transform-flow-strip-types"
16+
]
17+
}

.eslintrc.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "airbnb-base",
3+
"parser": "babel-eslint",
4+
"rules": {
5+
"no-underscore-dangle": "off",
6+
"consistent-return": "off",
7+
"no-throw-literal": "off",
8+
"max-len": [2, {"code": 150, "ignoreComments": true}],
9+
"no-param-reassign": "off",
10+
"lines-between-class-members": "off"
11+
},
12+
"env": {
13+
"browser": true
14+
}
15+
}

.flowconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[ignore]
2+
3+
[include]
4+
5+
[libs]
6+
7+
[lints]
8+
9+
[options]
10+
11+
[strict]

README.md

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,141 @@
11
# recursive-router
2-
Simple, expressive, mobx-based routing state management. Routers all the way down
2+
3+
Simple cross platform reactive and declarative routing
4+
5+
| | Recursive Router |
6+
| - | ------------ |
7+
| 😎 | View library agnostic - works with Angular, Vue, React, or your favorite hacked together JS lib |
8+
|| Router tree state as a direct function of location (URL) |
9+
| ⏱ | Built in history - Previous router state can be derived from the current state
10+
| 🔀 | One way data flow. Location -> Router State tree -> App |
11+
| 🔗 | Deep linking is trivial - Since state is a function of location, you can use a URL to generate an identical router state tree on any platform |
12+
| 😱 | Opinionated and automatic URL construction - There is no need to think about matching path names or constructing search params. The URL is simply a namespace for the state tree |
13+
| 🍬 | Small size - The only peer dependency is MobX and this will likely be removed in V1 release |
14+
| 🚀 | Reactive - Subscribe to the state of any router in the router state tree |
15+
| 👌 | Simple - Declare the route tree using a small but expressive syntax set |
16+
17+
If you dislike how much ceremony is around configuring a router, then this library may be something that interests you!
18+
19+
## Here's how it works:
20+
1. Recursive router treats the URL as a namespace for the storage of a state tree which represents all routable state.
21+
2. Writing to the URL is handled by the router
22+
3. Any changes to the URL are reduced over the router state tree
23+
4. Various types of routers in the router state tree exist. The differences are used to control how their state and their childrens state will get updated when the URL changes.
24+
5. Once the router state tree has been updated, observers of updated routers are notified.
25+
26+
27+
## How do I use this router?
28+
29+
Simple!
30+
31+
#### 1. Define a router tree representing what the routing logic of your app should look like in terms of `Stack`, `Scene`, `Feature`, and `Data` routers.
32+
```
33+
const tree =
34+
{ name: 'root',
35+
routers: {
36+
scene: [
37+
{ name: 'doc' routers: { feature: [{ name: 'doc-nav' }], stack: [{ name: 'doc-intro' }], },
38+
{ name: 'main', default: { visible: true } }},
39+
],
40+
},
41+
}
42+
```
43+
44+
#### 2. Register the router tree
45+
```
46+
{ registerRouter } from 'recursive-router';
47+
48+
const routers = registerRouter(tree);
49+
```
50+
51+
#### 3. Observe when the routers have changed via the power of mobx
52+
53+
```
54+
<App>
55+
{ routers['doc-nav'] && <DocNav /> }
56+
</App>
57+
```
58+
59+
## Router types
60+
61+
Almost all routeable and dynamic apps can be expressed in terms of the 4 main router types: `Stack`, `Scene`, `Feature`, and `Data`. Router types simply control how information is serialized into the URL and how their children respond when such corresponding information changes.
62+
63+
64+
#### `Stack` router
65+
`Stack` router is how you would control modals or multiple components that you want to exist at the same time but have some cardinality to them. You can use a stack router to control the immediate ordering of multiple child routers.
66+
67+
```
68+
<StackRouter>
69+
<Modal1><Modal2><Modal3>
70+
```
71+
72+
In this case, a `Stack` router would control which modal was showing. If multiple modals were showing it would control the `ordering` of them via a data key. A url with this type of routing, where only `Modal1` and `Modal2` are visible may look like: `http://<something>/stack1?modal1=1&modal2=0`
73+
74+
##### Methods:
75+
76+
```
77+
#show
78+
#hide
79+
#toFront
80+
#toBack
81+
#forward
82+
#backward
83+
```
84+
85+
#### `Scene` router
86+
`Scene` router is how you make sure only one child is showing at a time, if at all. If a child becomes visible, the other children will be hidden.
87+
88+
Ex URL: `http://<something>scene1/stack1/scene2?modal1=1&modal2=0`
89+
90+
##### Methods:
91+
92+
```
93+
#show
94+
#hide
95+
```
96+
97+
#### `Feature` router
98+
`Feature` router is similar to a `Stack` router except there is no cardinality among the children. Either some of the children are showing or they are not. This is desireable if you want to control the presence of a feature in a boolean way. Ulitmately, this type of router can allow for more concise URL construction over what a `Stack` router would be capapble of.
99+
100+
Ex URL: `http://<something>scene1/stack1/scene2?modal1=1&modal2=0&feature1&feature2`
101+
102+
##### Methods:
103+
104+
```
105+
#show
106+
#hide
107+
```
108+
109+
#### `Data` router
110+
`Data` router is how you store arbitrary data in the URL. Arguably, everything could be a `Data` router but you would loose out on all the convenience features that make each router unique and, thus, have to reimplement all the logic that this library is trying to abstract away. A data router primarily handles storing data like redirect URLs or page numbers.
111+
112+
Ex URL: `http://<something>scene1/stack1/scene2/99?modal1=1&modal2=0&feature1=true` For when we are at page 99 of scene2.
113+
114+
##### Methods:
115+
116+
```
117+
#show
118+
#hide
119+
#setData
120+
```
121+
122+
## Things to know:
123+
124+
##### Pathname vs Search params
125+
126+
A given router will store its information in the pathname if all parent routers up to the root are either `Scene` or `Data` routers.
127+
128+
129+
##### Rehydration of state after visibility change
130+
131+
All routers will by default rehydrate their branch back to how it was when they were visible. The exception to this is if a child in the branch had their state updated while said router was hidden. This setting can be overridden on a case-by-case basis during the router tree declaration.
132+
133+
134+
## V1 Roadmap
135+
136+
- Finish playground
137+
- Demos of common apps built with `recursive-router`
138+
- Add `redux` and `react` bindings
139+
- Clean up code and remove dependency on mobx
140+
- Add tests
141+
- Add docs and a better README

0 commit comments

Comments
 (0)