Skip to content

Commit 5e08c74

Browse files
blokku-chanfamilyfriendlymikey
authored andcommitted
Added docs for locals
1 parent 4943c0c commit 5e08c74

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

content/reference.md

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ Imba compiles to JavaScript. In the context of APIs you can think of it like Typ
77
The global `imba` object contains functions, constants and classes that are included with Imba. Imba also extends the built-in `Element` with certain properties. All items that are imba-specific are marked with a ★ symbol throughout the documentation.
88

99
### Properties
10+
1011
<api-list>imba.own.variables</api-list>
1112

1213
### Functions
14+
1315
<api-list>imba.own.functions</api-list>
1416

1517
### Decorators
18+
1619
<api-list>imba.own.decorators</api-list>
1720

1821
### Interfaces
22+
1923
<api-list>imba.own.interfaces</api-list>
2024

2125
## Global interfaces
26+
2227
<api-list>own.interfaces</api-list>
2328

2429
# /docs/css/properties/display
@@ -28,9 +33,11 @@ The display CSS property sets whether an element is treated as a block or inline
2833
Formally, the display property sets an element's inner and outer display types. The outer type sets an element's participation in flow layout; the inner type sets the layout of children. Some values of display are fully defined in their own individual specifications; for example the detail of what happens when display: flex is declared is defined in the CSS Flexible Box Model specification.
2934

3035
## Values
36+
3137
<api-list>css.display.all.cssvalues ^[^-]</api-list>
3238

3339
# /docs/css/properties/align-items
40+
3441
<api-list>css.align-items.all.cssvalues ^[^-]</api-list>
3542

3643
# /docs/css/values/timing-function
@@ -70,7 +77,44 @@ def load
7077

7178
# /api/imba/locals
7279

73-
Documentation coming
80+
`locals` is an instance of a JavaScript Proxy object that provides a convenient and efficient way to interact with the global `localStorage`. It wraps around a `Storage` instance, which in turn wraps around the `localStorage` object.
81+
82+
### Usage
83+
84+
#### Getting a Property
85+
86+
You can get a property from `locals` by simply accessing it like `imba.locals.prop`. This will call the getItem method of the Storage class, which retrieves the value from the `localStorage` (if it exists), or from the cache if it's already been retrieved before.
87+
88+
```imba
89+
let value = imba.locals.prop
90+
```
91+
92+
#### Setting a Property
93+
94+
You can set a property on `locals` by simply assigning a value to it like `imba.locals.prop = value`. This will call the `setItem` method of the `Storage` class, which stores the value in the `localStorage` and also updates the cache.
95+
96+
```imba
97+
imba.locals.prop = 'value'
98+
```
99+
100+
#### Deleting a Property
101+
102+
You can delete a property from `locals` by using the `delete` keyword like `delete locals.prop`. This will call the `deleteProperty` method of the `Storage` class, which removes the value from the `localStorage` and also deletes it from the cache.
103+
104+
```imba
105+
delete imba.locals.prop
106+
```
107+
108+
### Applying a Function
109+
110+
You can create a separate namespace or a sub-storage within your `localStorage` by calling it like imba.locals(name). This will call the `apply` method of the `Storage` class, which returns a child Proxy object associated with the given name.
111+
112+
Here's an example of how you can use this feature:
113+
114+
```imba
115+
let child = imba.locals('childName')
116+
child.prop = 'value'
117+
```
74118

75119
# /api/imba/session
76120

@@ -80,16 +124,16 @@ Documentation coming
80124

81125
In addition to the default [built in colors](/docs/css/values/color), `hue0`,`hue1`,`...`,`hue9` refers to the color scale set by the `hue` property. Setting `hue:amber` in css, will make `hue2` refer to `amber2` inside all elements that are affected by this style. The `hue(n)` color values supports an alpha value with the forward-slash syntax like `hue4/40`
82126

83-
84-
85127
### Syntax
128+
86129
```imba
87130
<div[hue:violet color:hue4]>
88131
```
89132

90133
### Usage
91134

92135
Let's say you have a button component with some basic styles:
136+
93137
```imba
94138
# [preview=md]
95139
import 'util/styles'
@@ -105,6 +149,7 @@ tag Button
105149
106150
imba.mount do <Button> "Button"
107151
```
152+
108153
This is all fine, but what if you want to also have a red button, and a green button? Duplicating all the styles involving colors is impractical, especially if you change the design of the button later. With `hue`, we can replace `blue2` with `hue2`, `blue8/70` with `hue8/70` and so on. Then we can set the color scale using the `hue` property.
109154

110155
```imba
@@ -117,7 +162,7 @@ tag Button
117162
text-shadow:0px 1px hue8/70
118163
bg:hue5 @hover:hue6
119164
bd:1px solid hue6
120-
165+
121166
<self> <span> <slot>
122167
123168
imba.mount do <>
@@ -138,12 +183,15 @@ This is some documentation for the parent!
138183
# /api/Element/data
139184

140185
### Syntax
186+
141187
```imba
142188
<element data=value>
143189
```
190+
144191
## Usage
145192

146193
The default convention in imba is to use the data property on elements to pass in the state/data they are supposed to represent. In this example we pass in the movie object through the data property:
194+
147195
```imba
148196
tag movie-item
149197
# using references to data
@@ -153,8 +201,7 @@ tag movie-item
153201
<movie-item data=movie>
154202
```
155203

156-
157-
However, __there is nothing special about the data property__, besides it being declared for all elements. It is just a plain property. If you prefer to use more descriptive properties you are free to do so:
204+
However, **there is nothing special about the data property**, besides it being declared for all elements. It is just a plain property. If you prefer to use more descriptive properties you are free to do so:
158205

159206
```imba
160207
tag movie-item
@@ -202,16 +249,19 @@ tag movie-item
202249
### Overriding
203250

204251
If you want to do something when setting data, you can easily create a setter and getter:
252+
205253
```imba
206254
tag movie-item
207255
set data value
208256
#data = value
209257
console.log "movie was set",value
210-
258+
211259
get data
212260
#data
213261
```
262+
214263
The memoized dom works in such a way that the setter will only be triggered when the value actually changes. If you set the property manually outside of rendering, the setter will be called every time, even if the value has not changed:
264+
215265
```imba
216266
tag app
217267
<self>
@@ -220,18 +270,22 @@ tag app
220270
<movie-item data=movie @click=($card.data = movie)>
221271
<movie-card$card>
222272
```
273+
223274
In this case, the movie-item data setter will only be called when their `data` actually changes, even if we render the list a million times. But if you click a movie-item 10 times, the `movie-card.data` property will be set 10 times. This is only really a challenge if you do something computationally expensive in the `data` setter itself. In that case you can just check if something has changed:
275+
224276
```imba
225277
tag movie-card
226278
set data value
227279
if #data != value
228280
#data = value
229281
# ... load more info about the movie?
230-
282+
231283
get data
232284
#data
233285
```
286+
234287
Since Imba embraces the concepts of memoization we have the `=?` operator that is shorthand for the pattern above.
288+
235289
```imba
236290
if item.propname =? value
237291
# item.propname is now set to value,
@@ -304,6 +358,7 @@ let length = 100px
304358
let progress = 87%
305359
let dynamic = (window.innerWidth)px
306360
```
361+
307362
Dimensions are numbers with a unit attached to it. They are compiled and treated as regular strings. When dealing with styles it is nice to be able to write `offset = (point.x)px` instead of `offset = "{point.x}px"`.
308363

309364
Time-based units `ms`, `s`, and `fps` are compiled to millisecond-based numbers.

0 commit comments

Comments
 (0)