aberdeen 1.4.3 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/aberdeen.d.ts +72 -7
- package/dist/aberdeen.js +52 -7
- package/dist/aberdeen.js.map +3 -3
- package/dist/route.js +4 -4
- package/dist/route.js.map +3 -3
- package/dist-min/aberdeen.js +7 -5
- package/dist-min/aberdeen.js.map +3 -3
- package/dist-min/route.js.map +2 -2
- package/package.json +5 -2
- package/skill/SKILL.md +579 -199
- package/skill/aberdeen.md +2322 -0
- package/skill/dispatcher.md +126 -0
- package/skill/prediction.md +73 -0
- package/skill/route.md +249 -0
- package/skill/transitions.md +59 -0
- package/src/aberdeen.ts +129 -15
- package/src/route.ts +3 -3
- package/skill/references/prediction.md +0 -45
- package/skill/references/routing.md +0 -81
- package/skill/references/transitions.md +0 -52
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
[**Aberdeen v1.6.0**](README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[Aberdeen](README.md) / dispatcher
|
|
6
|
+
|
|
7
|
+
# dispatcher
|
|
8
|
+
|
|
9
|
+
## Classes
|
|
10
|
+
|
|
11
|
+
### Dispatcher
|
|
12
|
+
|
|
13
|
+
Defined in: [dispatcher.ts:58](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/dispatcher.ts#L58)
|
|
14
|
+
|
|
15
|
+
Simple route matcher and dispatcher.
|
|
16
|
+
|
|
17
|
+
Example usage:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
const dispatcher = new Dispatcher();
|
|
21
|
+
|
|
22
|
+
dispatcher.addRoute("user", Number, "stream", String, (id, stream) => {
|
|
23
|
+
console.log(`User ${id}, stream ${stream}`);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
dispatcher.dispatch(["user", "42", "stream", "music"]);
|
|
27
|
+
// Logs: User 42, stream music
|
|
28
|
+
|
|
29
|
+
dispatcher.addRoute("search", matchRest, (terms: string[]) => {
|
|
30
|
+
console.log("Search terms:", terms);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
dispatcher.dispatch(["search", "classical", "piano"]);
|
|
34
|
+
// Logs: Search terms: [ 'classical', 'piano' ]
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
#### Constructors
|
|
38
|
+
|
|
39
|
+
##### Constructor
|
|
40
|
+
|
|
41
|
+
> **new Dispatcher**(): [`Dispatcher`](#dispatcher)
|
|
42
|
+
|
|
43
|
+
###### Returns
|
|
44
|
+
|
|
45
|
+
[`Dispatcher`](#dispatcher)
|
|
46
|
+
|
|
47
|
+
#### Methods
|
|
48
|
+
|
|
49
|
+
##### addRoute()
|
|
50
|
+
|
|
51
|
+
> **addRoute**\<`T`, `H`\>(...`args`): `void`
|
|
52
|
+
|
|
53
|
+
Defined in: [dispatcher.ts:70](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/dispatcher.ts#L70)
|
|
54
|
+
|
|
55
|
+
Add a route with matchers and a handler function.
|
|
56
|
+
|
|
57
|
+
###### Type Parameters
|
|
58
|
+
|
|
59
|
+
###### T
|
|
60
|
+
|
|
61
|
+
`T` *extends* `Matcher`[]
|
|
62
|
+
|
|
63
|
+
Array of matcher types.
|
|
64
|
+
|
|
65
|
+
###### H
|
|
66
|
+
|
|
67
|
+
`H` *extends* (...`args`) => `void`
|
|
68
|
+
|
|
69
|
+
Handler function type, inferred from the matchers.
|
|
70
|
+
|
|
71
|
+
###### Parameters
|
|
72
|
+
|
|
73
|
+
###### args
|
|
74
|
+
|
|
75
|
+
...\[`...T[]`, `H`\]
|
|
76
|
+
|
|
77
|
+
An array of matchers followed by a handler function. Each matcher can be:
|
|
78
|
+
- A string: matches exactly that string.
|
|
79
|
+
- A function: takes a string segment and returns a value (of any type) if it matches, or [matchFailed](#matchfailed) if it doesn't match. The return value (if not `matchFailed` and not `NaN`) is passed as a parameter to the handler function. The built-in functions `Number` and `String` can be used to match numeric and string segments respectively.
|
|
80
|
+
- The special [matchRest](#matchrest) symbol: matches the rest of the segments as an array of strings. Only one `matchRest` is allowed, and it must be the last matcher.
|
|
81
|
+
|
|
82
|
+
###### Returns
|
|
83
|
+
|
|
84
|
+
`void`
|
|
85
|
+
|
|
86
|
+
##### dispatch()
|
|
87
|
+
|
|
88
|
+
> **dispatch**(`segments`): `boolean`
|
|
89
|
+
|
|
90
|
+
Defined in: [dispatcher.ts:91](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/dispatcher.ts#L91)
|
|
91
|
+
|
|
92
|
+
Dispatches the given segments to the first route handler that matches.
|
|
93
|
+
|
|
94
|
+
###### Parameters
|
|
95
|
+
|
|
96
|
+
###### segments
|
|
97
|
+
|
|
98
|
+
`string`[]
|
|
99
|
+
|
|
100
|
+
Array of string segments to match against the added routes. When using this class with the Aberdeen `route` module, one would typically pass `route.current.p`.
|
|
101
|
+
|
|
102
|
+
###### Returns
|
|
103
|
+
|
|
104
|
+
`boolean`
|
|
105
|
+
|
|
106
|
+
True if a matching route was found and handled, false otherwise.
|
|
107
|
+
|
|
108
|
+
## Variables
|
|
109
|
+
|
|
110
|
+
### matchFailed
|
|
111
|
+
|
|
112
|
+
> `const` **matchFailed**: unique `symbol`
|
|
113
|
+
|
|
114
|
+
Defined in: [dispatcher.ts:4](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/dispatcher.ts#L4)
|
|
115
|
+
|
|
116
|
+
Symbol to return when a custom [Dispatcher.addRoute](#addroute) matcher cannot match a segment.
|
|
117
|
+
|
|
118
|
+
***
|
|
119
|
+
|
|
120
|
+
### matchRest
|
|
121
|
+
|
|
122
|
+
> `const` **matchRest**: unique `symbol`
|
|
123
|
+
|
|
124
|
+
Defined in: [dispatcher.ts:9](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/dispatcher.ts#L9)
|
|
125
|
+
|
|
126
|
+
Special [Dispatcher.addRoute](#addroute) matcher that matches the rest of the segments as an array of strings.
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
[**Aberdeen v1.6.0**](README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[Aberdeen](README.md) / prediction
|
|
6
|
+
|
|
7
|
+
# prediction
|
|
8
|
+
|
|
9
|
+
## Functions
|
|
10
|
+
|
|
11
|
+
### applyCanon()
|
|
12
|
+
|
|
13
|
+
> **applyCanon**(`canonFunc?`, `dropPredictions?`): `void`
|
|
14
|
+
|
|
15
|
+
Defined in: [prediction.ts:115](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/prediction.ts#L115)
|
|
16
|
+
|
|
17
|
+
Temporarily revert all outstanding predictions, optionally run the provided function
|
|
18
|
+
(which will generally make authoritative changes to the data based on a server response),
|
|
19
|
+
and then attempt to reapply the predictions on top of the new canonical state, dropping
|
|
20
|
+
any predictions that can no longer be applied cleanly (the data has been modified) or
|
|
21
|
+
that were specified in `dropPredictions`.
|
|
22
|
+
|
|
23
|
+
All of this is done such that redraws are only triggered if the overall effect is an
|
|
24
|
+
actual change to an `Observable`.
|
|
25
|
+
|
|
26
|
+
#### Parameters
|
|
27
|
+
|
|
28
|
+
##### canonFunc?
|
|
29
|
+
|
|
30
|
+
() => `void`
|
|
31
|
+
|
|
32
|
+
The function to run without any predictions applied. This will typically
|
|
33
|
+
make authoritative changes to the data, based on a server response.
|
|
34
|
+
|
|
35
|
+
##### dropPredictions?
|
|
36
|
+
|
|
37
|
+
`Patch`[] = `[]`
|
|
38
|
+
|
|
39
|
+
An optional list of predictions (as returned by `applyPrediction`)
|
|
40
|
+
to undo. Typically, when a server response for a certain request is being handled,
|
|
41
|
+
you'd want to drop the prediction that was done for that request.
|
|
42
|
+
|
|
43
|
+
#### Returns
|
|
44
|
+
|
|
45
|
+
`void`
|
|
46
|
+
|
|
47
|
+
***
|
|
48
|
+
|
|
49
|
+
### applyPrediction()
|
|
50
|
+
|
|
51
|
+
> **applyPrediction**(`predictFunc`): `Patch`
|
|
52
|
+
|
|
53
|
+
Defined in: [prediction.ts:93](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/prediction.ts#L93)
|
|
54
|
+
|
|
55
|
+
Run the provided function, while treating all changes to Observables as predictions,
|
|
56
|
+
meaning they will be reverted when changes come back from the server (or some other
|
|
57
|
+
async source).
|
|
58
|
+
|
|
59
|
+
#### Parameters
|
|
60
|
+
|
|
61
|
+
##### predictFunc
|
|
62
|
+
|
|
63
|
+
() => `void`
|
|
64
|
+
|
|
65
|
+
The function to run. It will generally modify some Observables
|
|
66
|
+
to immediately reflect state (as closely as possible) that we expect the server
|
|
67
|
+
to communicate back to us later on.
|
|
68
|
+
|
|
69
|
+
#### Returns
|
|
70
|
+
|
|
71
|
+
`Patch`
|
|
72
|
+
|
|
73
|
+
A `Patch` object. Don't modify it. This is only meant to be passed to `applyCanon`.
|
package/skill/route.md
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
[**Aberdeen v1.6.0**](README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[Aberdeen](README.md) / route
|
|
6
|
+
|
|
7
|
+
# route
|
|
8
|
+
|
|
9
|
+
## Interfaces
|
|
10
|
+
|
|
11
|
+
### Route
|
|
12
|
+
|
|
13
|
+
Defined in: [route.ts:8](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L8)
|
|
14
|
+
|
|
15
|
+
The class for the global `route` object.
|
|
16
|
+
|
|
17
|
+
#### Properties
|
|
18
|
+
|
|
19
|
+
##### depth
|
|
20
|
+
|
|
21
|
+
> **depth**: `number`
|
|
22
|
+
|
|
23
|
+
Defined in: [route.ts:20](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L20)
|
|
24
|
+
|
|
25
|
+
The navigation depth of the current session. Starts at 1. Writing to this property has no effect.
|
|
26
|
+
|
|
27
|
+
##### hash
|
|
28
|
+
|
|
29
|
+
> **hash**: `string`
|
|
30
|
+
|
|
31
|
+
Defined in: [route.ts:14](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L14)
|
|
32
|
+
|
|
33
|
+
The hash fragment including the leading `#`, or an empty string. For instance `"#my_section"` or `""`.
|
|
34
|
+
|
|
35
|
+
##### nav
|
|
36
|
+
|
|
37
|
+
> **nav**: `NavType`
|
|
38
|
+
|
|
39
|
+
Defined in: [route.ts:28](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L28)
|
|
40
|
+
|
|
41
|
+
The navigation action that got us to this page. Writing to this property has no effect.
|
|
42
|
+
- `"load"`: An initial page load.
|
|
43
|
+
- `"back"` or `"forward"`: When we navigated backwards or forwards in the stack.
|
|
44
|
+
- `"go"`: When we added a new page on top of the stack.
|
|
45
|
+
- `"push"`: When we added a new page on top of the stack, merging with the current page.
|
|
46
|
+
Mostly useful for page transition animations. Writing to this property has no effect.
|
|
47
|
+
|
|
48
|
+
##### p
|
|
49
|
+
|
|
50
|
+
> **p**: `string`[]
|
|
51
|
+
|
|
52
|
+
Defined in: [route.ts:12](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L12)
|
|
53
|
+
|
|
54
|
+
An convenience array containing path segments, mapping to `path`. For instance `[]` (for `"/"`) or `['users', '123', 'feed']` (for `"/users/123/feed"`).
|
|
55
|
+
|
|
56
|
+
##### path
|
|
57
|
+
|
|
58
|
+
> **path**: `string`
|
|
59
|
+
|
|
60
|
+
Defined in: [route.ts:10](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L10)
|
|
61
|
+
|
|
62
|
+
The current path of the URL as a string. For instance `"/"` or `"/users/123/feed"`. Paths are normalized to always start with a `/` and never end with a `/` (unless it's the root path).
|
|
63
|
+
|
|
64
|
+
##### search
|
|
65
|
+
|
|
66
|
+
> **search**: `Record`\<`string`, `string`\>
|
|
67
|
+
|
|
68
|
+
Defined in: [route.ts:16](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L16)
|
|
69
|
+
|
|
70
|
+
The query string interpreted as search parameters. So `"a=x&b=y"` becomes `{a: "x", b: "y"}`.
|
|
71
|
+
|
|
72
|
+
##### state
|
|
73
|
+
|
|
74
|
+
> **state**: `Record`\<`string`, `any`\>
|
|
75
|
+
|
|
76
|
+
Defined in: [route.ts:18](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L18)
|
|
77
|
+
|
|
78
|
+
An object to be used for any additional data you want to associate with the current page. Data should be JSON-compatible.
|
|
79
|
+
|
|
80
|
+
## Variables
|
|
81
|
+
|
|
82
|
+
### current
|
|
83
|
+
|
|
84
|
+
> `const` **current**: [`Route`](#route)
|
|
85
|
+
|
|
86
|
+
Defined in: [route.ts:261](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L261)
|
|
87
|
+
|
|
88
|
+
The global [Route](#route) object reflecting the current URL and browser history state. Changes you make to this affect the current browser history item (modifying the URL if needed).
|
|
89
|
+
|
|
90
|
+
## Functions
|
|
91
|
+
|
|
92
|
+
### back()
|
|
93
|
+
|
|
94
|
+
> **back**(`target`): `void`
|
|
95
|
+
|
|
96
|
+
Defined in: [route.ts:185](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L185)
|
|
97
|
+
|
|
98
|
+
Try to go back in history to the first entry that matches the given target. If none is found, the given state will replace the current page. This is useful for "cancel" or "close" actions that should return to the previous page if possible, but create a new page if not (for instance when arriving at the current page through a direct link).
|
|
99
|
+
|
|
100
|
+
Consider using [up](#up) to go up in the path hierarchy.
|
|
101
|
+
|
|
102
|
+
#### Parameters
|
|
103
|
+
|
|
104
|
+
##### target
|
|
105
|
+
|
|
106
|
+
`RouteTarget` = `{}`
|
|
107
|
+
|
|
108
|
+
The target route to go back to. May be a subset of [Route](#route), or a string (for `path`), or an array of strings (for `p`).
|
|
109
|
+
|
|
110
|
+
#### Returns
|
|
111
|
+
|
|
112
|
+
`void`
|
|
113
|
+
|
|
114
|
+
***
|
|
115
|
+
|
|
116
|
+
### go()
|
|
117
|
+
|
|
118
|
+
> **go**(`target`, `nav`): `void`
|
|
119
|
+
|
|
120
|
+
Defined in: [route.ts:151](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L151)
|
|
121
|
+
|
|
122
|
+
Navigate to a new URL by pushing a new history entry.
|
|
123
|
+
|
|
124
|
+
Note that this happens synchronously, immediately updating `route` and processing any reactive updates based on that.
|
|
125
|
+
|
|
126
|
+
#### Parameters
|
|
127
|
+
|
|
128
|
+
##### target
|
|
129
|
+
|
|
130
|
+
`RouteTarget`
|
|
131
|
+
|
|
132
|
+
A subset of the [Route](#route) properties to navigate to. If neither `p` nor `path` is given, the current path is used. For other properties, an empty/default value is assumed if not given. For convenience:
|
|
133
|
+
- You may pass a string instead of an object, which is interpreted as the `path`.
|
|
134
|
+
- You may pass an array instead of an object, which is interpreted as the `p` array.
|
|
135
|
+
- If you pass `p`, it may contain numbers, which will be converted to strings.
|
|
136
|
+
- If you pass `search`, its values may be numbers, which will be converted to strings.
|
|
137
|
+
|
|
138
|
+
Examples:
|
|
139
|
+
```js
|
|
140
|
+
// Navigate to /users/123
|
|
141
|
+
route.go("/users/123");
|
|
142
|
+
|
|
143
|
+
// Navigate to /users/123?tab=feed#top
|
|
144
|
+
route.go({p: ["users", 123], search: {tab: "feed"}, hash: "top"});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
##### nav
|
|
148
|
+
|
|
149
|
+
`NavType` = `"go"`
|
|
150
|
+
|
|
151
|
+
#### Returns
|
|
152
|
+
|
|
153
|
+
`void`
|
|
154
|
+
|
|
155
|
+
***
|
|
156
|
+
|
|
157
|
+
### persistScroll()
|
|
158
|
+
|
|
159
|
+
> **persistScroll**(`name`): `void`
|
|
160
|
+
|
|
161
|
+
Defined in: [route.ts:237](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L237)
|
|
162
|
+
|
|
163
|
+
Restore and store the vertical and horizontal scroll position for
|
|
164
|
+
the parent element to the page state.
|
|
165
|
+
|
|
166
|
+
#### Parameters
|
|
167
|
+
|
|
168
|
+
##### name
|
|
169
|
+
|
|
170
|
+
`string` = `"main"`
|
|
171
|
+
|
|
172
|
+
A unique (within this page) name for this
|
|
173
|
+
scrollable element. Defaults to 'main'.
|
|
174
|
+
|
|
175
|
+
The scroll position will be persisted in `route.aux.scroll.<name>`.
|
|
176
|
+
|
|
177
|
+
#### Returns
|
|
178
|
+
|
|
179
|
+
`void`
|
|
180
|
+
|
|
181
|
+
***
|
|
182
|
+
|
|
183
|
+
### push()
|
|
184
|
+
|
|
185
|
+
> **push**(`target`): `void`
|
|
186
|
+
|
|
187
|
+
Defined in: [route.ts:172](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L172)
|
|
188
|
+
|
|
189
|
+
Modify the current route by merging `target` into it (using [merge](aberdeen.md#merge)), pushing a new history entry.
|
|
190
|
+
|
|
191
|
+
This is useful for things like opening modals or side panels, where you want a browser back action to return to the previous state.
|
|
192
|
+
|
|
193
|
+
#### Parameters
|
|
194
|
+
|
|
195
|
+
##### target
|
|
196
|
+
|
|
197
|
+
`RouteTarget`
|
|
198
|
+
|
|
199
|
+
Same as for [go](#go), but merged into the current route instead deleting all state.
|
|
200
|
+
|
|
201
|
+
#### Returns
|
|
202
|
+
|
|
203
|
+
`void`
|
|
204
|
+
|
|
205
|
+
***
|
|
206
|
+
|
|
207
|
+
### setLog()
|
|
208
|
+
|
|
209
|
+
> **setLog**(`value`): `void`
|
|
210
|
+
|
|
211
|
+
Defined in: [route.ts:37](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L37)
|
|
212
|
+
|
|
213
|
+
Configure logging on route changes.
|
|
214
|
+
|
|
215
|
+
#### Parameters
|
|
216
|
+
|
|
217
|
+
##### value
|
|
218
|
+
|
|
219
|
+
`true` to enable logging to console, `false` to disable logging, or a custom logging function. Defaults to `false`.
|
|
220
|
+
|
|
221
|
+
`boolean` | (...`args`) => `void`
|
|
222
|
+
|
|
223
|
+
#### Returns
|
|
224
|
+
|
|
225
|
+
`void`
|
|
226
|
+
|
|
227
|
+
***
|
|
228
|
+
|
|
229
|
+
### up()
|
|
230
|
+
|
|
231
|
+
> **up**(`stripCount`): `void`
|
|
232
|
+
|
|
233
|
+
Defined in: [route.ts:210](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/route.ts#L210)
|
|
234
|
+
|
|
235
|
+
Navigate up in the path hierarchy, by going back to the first history entry
|
|
236
|
+
that has a shorter path than the current one. If there's none, we just shorten
|
|
237
|
+
the current path.
|
|
238
|
+
|
|
239
|
+
Note that going back in browser history happens asynchronously, so `route` will not be updated immediately.
|
|
240
|
+
|
|
241
|
+
#### Parameters
|
|
242
|
+
|
|
243
|
+
##### stripCount
|
|
244
|
+
|
|
245
|
+
`number` = `1`
|
|
246
|
+
|
|
247
|
+
#### Returns
|
|
248
|
+
|
|
249
|
+
`void`
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
[**Aberdeen v1.6.0**](README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[Aberdeen](README.md) / transitions
|
|
6
|
+
|
|
7
|
+
# transitions
|
|
8
|
+
|
|
9
|
+
## Functions
|
|
10
|
+
|
|
11
|
+
### grow()
|
|
12
|
+
|
|
13
|
+
> **grow**(`el`): `Promise`\<`void`\>
|
|
14
|
+
|
|
15
|
+
Defined in: [transitions.ts:33](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/transitions.ts#L33)
|
|
16
|
+
|
|
17
|
+
Do a grow transition for the given element. This is meant to be used as a
|
|
18
|
+
handler for the `create` property.
|
|
19
|
+
|
|
20
|
+
#### Parameters
|
|
21
|
+
|
|
22
|
+
##### el
|
|
23
|
+
|
|
24
|
+
`HTMLElement`
|
|
25
|
+
|
|
26
|
+
The element to transition.
|
|
27
|
+
|
|
28
|
+
The transition doesn't look great for table elements, and may have problems
|
|
29
|
+
for other specific cases as well.
|
|
30
|
+
|
|
31
|
+
#### Returns
|
|
32
|
+
|
|
33
|
+
`Promise`\<`void`\>
|
|
34
|
+
|
|
35
|
+
***
|
|
36
|
+
|
|
37
|
+
### shrink()
|
|
38
|
+
|
|
39
|
+
> **shrink**(`el`): `Promise`\<`void`\>
|
|
40
|
+
|
|
41
|
+
Defined in: [transitions.ts:56](https://github.com/vanviegen/aberdeen/blob/6b79c7fce769810bdcbd326b8ee6ef1c8e1fc011/src/transitions.ts#L56)
|
|
42
|
+
|
|
43
|
+
Do a shrink transition for the given element, and remove it from the DOM
|
|
44
|
+
afterwards. This is meant to be used as a handler for the `destroy` property.
|
|
45
|
+
|
|
46
|
+
#### Parameters
|
|
47
|
+
|
|
48
|
+
##### el
|
|
49
|
+
|
|
50
|
+
`HTMLElement`
|
|
51
|
+
|
|
52
|
+
The element to transition and remove.
|
|
53
|
+
|
|
54
|
+
The transition doesn't look great for table elements, and may have problems
|
|
55
|
+
for other specific cases as well.
|
|
56
|
+
|
|
57
|
+
#### Returns
|
|
58
|
+
|
|
59
|
+
`Promise`\<`void`\>
|
package/src/aberdeen.ts
CHANGED
|
@@ -1644,7 +1644,7 @@ function copyRecursive<T extends object>(dst: T, src: T, flags: number): boolean
|
|
|
1644
1644
|
const old = dst.get(k);
|
|
1645
1645
|
dst.delete(k);
|
|
1646
1646
|
if (flags & COPY_EMIT) {
|
|
1647
|
-
emit(dst, k,
|
|
1647
|
+
emit(dst, k, EMPTY, old);
|
|
1648
1648
|
}
|
|
1649
1649
|
changed = true;
|
|
1650
1650
|
}
|
|
@@ -1677,7 +1677,7 @@ function copyRecursive<T extends object>(dst: T, src: T, flags: number): boolean
|
|
|
1677
1677
|
const old = dst[k];
|
|
1678
1678
|
delete dst[k];
|
|
1679
1679
|
if (flags & COPY_EMIT && old !== undefined) {
|
|
1680
|
-
emit(dst, k,
|
|
1680
|
+
emit(dst, k, EMPTY, old);
|
|
1681
1681
|
}
|
|
1682
1682
|
changed = true;
|
|
1683
1683
|
}
|
|
@@ -1704,23 +1704,135 @@ export const NO_COPY = Symbol("NO_COPY");
|
|
|
1704
1704
|
(Promise.prototype as any)[NO_COPY] = true;
|
|
1705
1705
|
|
|
1706
1706
|
/**
|
|
1707
|
-
*
|
|
1708
|
-
*
|
|
1709
|
-
*
|
|
1710
|
-
*
|
|
1711
|
-
*
|
|
1707
|
+
* A reactive object containing CSS variable definitions.
|
|
1708
|
+
*
|
|
1709
|
+
* Any property you assign to `cssVars` becomes available as a CSS custom property throughout your application.
|
|
1710
|
+
*
|
|
1711
|
+
* Use {@link setSpacingCssVars} to optionally initialize `cssVars[1]` through `cssVars[12]` with an exponential spacing scale.
|
|
1712
|
+
*
|
|
1713
|
+
* When you reference a CSS variable in Aberdeen using the `$` prefix (e.g., `$primary`), it automatically resolves to `var(--primary)`.
|
|
1714
|
+
* For numeric keys (which can't be used directly as CSS custom property names), Aberdeen prefixes them with `m` (e.g., `$3` becomes `var(--m3)`).
|
|
1715
|
+
*
|
|
1716
|
+
* When you add the first property to cssVars, Aberdeen automatically creates a reactive `<style>` tag in `<head>`
|
|
1717
|
+
* containing the `:root` CSS custom property declarations. The style tag is automatically removed if cssVars becomes empty.
|
|
1718
|
+
*
|
|
1712
1719
|
* @example
|
|
1713
|
-
* ```
|
|
1720
|
+
* ```javascript
|
|
1721
|
+
* import { cssVars, setSpacingCssVars, $ } from 'aberdeen';
|
|
1722
|
+
*
|
|
1723
|
+
* // Optionally initialize spacing scale
|
|
1724
|
+
* setSpacingCssVars(); // Uses defaults: base=1, unit='rem'
|
|
1725
|
+
*
|
|
1726
|
+
* // Define custom colors - style tag is automatically created
|
|
1714
1727
|
* cssVars.primary = '#3b82f6';
|
|
1715
|
-
* cssVars
|
|
1716
|
-
*
|
|
1717
|
-
*
|
|
1728
|
+
* cssVars.danger = '#ef4444';
|
|
1729
|
+
*
|
|
1730
|
+
* // Use in elements with the $ prefix
|
|
1731
|
+
* $('button bg:$primary fg:white');
|
|
1732
|
+
*
|
|
1733
|
+
* // Use spacing (if setSpacingCssVars() was called)
|
|
1734
|
+
* $('div mt:$3'); // Sets margin-top to var(--m3)
|
|
1718
1735
|
* ```
|
|
1719
1736
|
*/
|
|
1720
1737
|
export const cssVars: Record<string, string> = optProxy({});
|
|
1721
1738
|
|
|
1722
|
-
|
|
1723
|
-
|
|
1739
|
+
/**
|
|
1740
|
+
* Initializes `cssVars[1]` through `cssVars[12]` with an exponential spacing scale.
|
|
1741
|
+
*
|
|
1742
|
+
* The scale is calculated as `2^(n-3) * base`, providing values from `0.25 * base` to `512 * base`.
|
|
1743
|
+
*
|
|
1744
|
+
* @param base - The base size for the spacing scale (default: 1). If unit is 'rem' or 'em', this is in that unit. If unit is 'px', this is the pixel value.
|
|
1745
|
+
* @param unit - The CSS unit to use (default: 'rem'). Can be 'rem', 'em', 'px', or any other valid CSS unit.
|
|
1746
|
+
*
|
|
1747
|
+
* @example
|
|
1748
|
+
* ```javascript
|
|
1749
|
+
* // Use default scale (0.25rem to 512rem)
|
|
1750
|
+
* setSpacingCssVars();
|
|
1751
|
+
*
|
|
1752
|
+
* // Use custom base size
|
|
1753
|
+
* setSpacingCssVars(16, 'px'); // 4px to 8192px
|
|
1754
|
+
*
|
|
1755
|
+
* // Use em units
|
|
1756
|
+
* setSpacingCssVars(1, 'em'); // 0.25em to 512em
|
|
1757
|
+
* ```
|
|
1758
|
+
*/
|
|
1759
|
+
export function setSpacingCssVars(base = 1, unit = 'rem'): void {
|
|
1760
|
+
for (let i = 1; i <= 12; i++) {
|
|
1761
|
+
cssVars[i] = 2 ** (i - 3) * base + unit;
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
const DIGIT_FIRST = /^\d/;
|
|
1766
|
+
function cssVarRef(name: string): string {
|
|
1767
|
+
// Prefix numeric keys with 'm' (CSS custom property names can't start with a digit)
|
|
1768
|
+
const varName = DIGIT_FIRST.test(name) ? `m${name}` : name;
|
|
1769
|
+
return `var(--${varName})`;
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
// Automatically mount cssVars style tag to document.head when cssVars is not empty
|
|
1773
|
+
if (typeof document !== "undefined") {
|
|
1774
|
+
leakScope(() => {
|
|
1775
|
+
$(() => {
|
|
1776
|
+
if (!isEmpty(cssVars)) {
|
|
1777
|
+
mount(document.head, () => {
|
|
1778
|
+
$('style', () => {
|
|
1779
|
+
let css = ":root {\n";
|
|
1780
|
+
for(const [key, value] of Object.entries(cssVars)) {
|
|
1781
|
+
const varName = DIGIT_FIRST.test(String(key)) ? `m${key}` : key;
|
|
1782
|
+
css += ` --${varName}: ${value};\n`;
|
|
1783
|
+
}
|
|
1784
|
+
css += "}";
|
|
1785
|
+
$(`#${css}`);
|
|
1786
|
+
});
|
|
1787
|
+
});
|
|
1788
|
+
}
|
|
1789
|
+
});
|
|
1790
|
+
});
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
/**
|
|
1794
|
+
* Returns whether the user's browser prefers a dark color scheme.
|
|
1795
|
+
*
|
|
1796
|
+
* This function is reactive - scopes that call it will re-execute when the
|
|
1797
|
+
* browser's color scheme preference changes (via the `prefers-color-scheme` media query).
|
|
1798
|
+
*
|
|
1799
|
+
* Use this in combination with {@link $} and {@link cssVars} to implement theme switching:
|
|
1800
|
+
*
|
|
1801
|
+
* @returns `true` if the browser prefers dark mode, `false` if it prefers light mode.
|
|
1802
|
+
*
|
|
1803
|
+
* @example
|
|
1804
|
+
* ```javascript
|
|
1805
|
+
* import { darkMode, cssVars, $, mount } from 'aberdeen';
|
|
1806
|
+
*
|
|
1807
|
+
* // Reactively set colors based on browser preference
|
|
1808
|
+
* $(() => {
|
|
1809
|
+
* if (darkMode()) { // Optionally override this with user settings
|
|
1810
|
+
* cssVars.bg = '#1a1a1a';
|
|
1811
|
+
* cssVars.fg = '#e5e5e5';
|
|
1812
|
+
* } else {
|
|
1813
|
+
* cssVars.bg = '#ffffff';
|
|
1814
|
+
* cssVars.fg = '#000000';
|
|
1815
|
+
* }
|
|
1816
|
+
* });
|
|
1817
|
+
* ```
|
|
1818
|
+
*/
|
|
1819
|
+
export function darkMode(): boolean {
|
|
1820
|
+
if (typeof window === 'undefined' || !window.matchMedia) return false;
|
|
1821
|
+
|
|
1822
|
+
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
|
1823
|
+
|
|
1824
|
+
// Read from proxy to establish reactivity
|
|
1825
|
+
const changed = proxy(false);
|
|
1826
|
+
changed.value; // Subscribe caller reactive scope
|
|
1827
|
+
function onChange() {
|
|
1828
|
+
changed.value = true;
|
|
1829
|
+
}
|
|
1830
|
+
mediaQuery.addEventListener('change', onChange);
|
|
1831
|
+
clean(() => {
|
|
1832
|
+
mediaQuery.removeEventListener('change', onChange);
|
|
1833
|
+
})
|
|
1834
|
+
|
|
1835
|
+
return mediaQuery.matches;
|
|
1724
1836
|
}
|
|
1725
1837
|
|
|
1726
1838
|
/**
|
|
@@ -2178,7 +2290,7 @@ function styleToCss(style: object, prefix: string): string {
|
|
|
2178
2290
|
);
|
|
2179
2291
|
}
|
|
2180
2292
|
} else {
|
|
2181
|
-
const val = v == null || v === false ? "" : typeof v === 'string' ? (v[0] === '
|
|
2293
|
+
const val = v == null || v === false ? "" : typeof v === 'string' ? (v[0] === '$' ? cssVarRef(v.substring(1)) : v) : String(v);
|
|
2182
2294
|
const expanded = CSS_SHORT[k] || k;
|
|
2183
2295
|
for (const prop of (Array.isArray(expanded) ? expanded : [expanded])) {
|
|
2184
2296
|
props += `${prop.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}:${val};`;
|
|
@@ -2207,7 +2319,7 @@ function applyArg(el: Element, key: string, value: any) {
|
|
|
2207
2319
|
} else if (key[0] === "$") {
|
|
2208
2320
|
// Style (with shortcuts)
|
|
2209
2321
|
key = key.substring(1);
|
|
2210
|
-
const val = value == null || value === false ? "" : typeof value === 'string' ? (value[0] === '
|
|
2322
|
+
const val = value == null || value === false ? "" : typeof value === 'string' ? (value[0] === '$' ? cssVarRef(value.substring(1)) : value) : String(value);
|
|
2211
2323
|
const expanded = CSS_SHORT[key] || key;
|
|
2212
2324
|
if (typeof expanded === "string") {
|
|
2213
2325
|
(el as any).style[expanded] = val;
|
|
@@ -2903,3 +3015,5 @@ export function withEmitHandler(
|
|
|
2903
3015
|
emit = oldEmitHandler;
|
|
2904
3016
|
}
|
|
2905
3017
|
}
|
|
3018
|
+
|
|
3019
|
+
|