@lightningjs/blits 0.9.6 → 0.9.8
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/CHANGELOG.md +19 -1
- package/boilerplate/common/eslint/.eslintrc.cjs +1 -1
- package/boilerplate/common/git/gitignore +4 -1
- package/boilerplate/js/default/.env.example +4 -0
- package/boilerplate/js/default/src/pages/Home.js +1 -1
- package/boilerplate/js/default/vite.config.js +2 -0
- package/docs/_sidebar.md +1 -0
- package/docs/built-in/directives.md +11 -3
- package/docs/built-in/for-loop.md +172 -0
- package/docs/components/component_state.md +1 -2
- package/docs/components/computed_properties.md +3 -4
- package/docs/components/methods.md +3 -3
- package/docs/components/props.md +1 -1
- package/docs/components/user_input.md +2 -2
- package/docs/components/watchers.md +1 -1
- package/docs/essentials/application_root.md +1 -1
- package/docs/essentials/components.md +3 -3
- package/docs/essentials/displaying_images.md +2 -2
- package/docs/essentials/displaying_text.md +12 -12
- package/docs/essentials/element_attributes.md +19 -13
- package/docs/essentials/template_syntax.md +8 -8
- package/docs/getting_started/file_structure.md +10 -11
- package/docs/getting_started/intro.md +3 -3
- package/docs/plugins/text-to-speech-announcer.md +2 -2
- package/docs/sidebar.json +4 -0
- package/docs/transitions_animations/transitions.md +10 -10
- package/package.json +2 -2
- package/src/component.js +1 -1
- package/src/components/Circle.js +30 -27
- package/src/components/Circle.orig.js +3 -29
- package/src/components/FPScounter.js +1078 -1024
- package/src/components/FPScounter.orig.js +41 -1024
- package/src/components/Image.js +21 -18
- package/src/components/Image.orig.js +2 -20
- package/src/components/RouterView.js +20 -17
- package/src/components/RouterView.orig.js +3 -19
- package/src/components/Sprite.js +27 -23
- package/src/components/Sprite.orig.js +3 -25
- package/src/components/Text.js +57 -53
- package/src/components/Text.orig.js +21 -55
- package/src/engines/L3/element.js +3 -0
- package/src/engines/L3/launch.js +1 -0
- package/src/launch.d.ts +11 -1
- package/src/lib/reactivity/effect.js +1 -0
- package/src/lib/reactivity/reactive.js +10 -1
- package/src/lib/setup/base.js +7 -3
- package/src/lib/setup/state.js +1 -1
- package/src/router/router.js +0 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## v0.9.8
|
|
4
|
+
|
|
5
|
+
_25 apr 2024_
|
|
6
|
+
|
|
7
|
+
- Fixed issue with removing children during navigation with the router
|
|
8
|
+
- Added support for overflow (as an inversed alias of clipping)
|
|
9
|
+
- Upgraded to latest version of renderer (0.8.3)
|
|
10
|
+
- Introduced `renderMode` setting (`webgl` or `canvas`)
|
|
11
|
+
|
|
12
|
+
## v0.9.7
|
|
13
|
+
|
|
14
|
+
_15 apr 2024_
|
|
15
|
+
|
|
16
|
+
- Added support for direct assignment of an Array element and triggering reactivity
|
|
17
|
+
- Added support for .env in boilerplate
|
|
18
|
+
- Fixed issue with Blits component being wrapped in a Proxy when assigned to a state variable
|
|
19
|
+
- Fixed built-in `hasFocus`-state to also work for components that don't have state in their config object
|
|
20
|
+
|
|
3
21
|
## v0.9.6
|
|
4
22
|
|
|
5
23
|
_4 apr 2024_
|
|
@@ -29,7 +47,7 @@ _25 mar 2024_
|
|
|
29
47
|
|
|
30
48
|
- Added support for mount / pivot on Text elements and Sprites.
|
|
31
49
|
|
|
32
|
-
|
|
50
|
+
## v0.9.2
|
|
33
51
|
|
|
34
52
|
_21 mar 2024_
|
|
35
53
|
|
package/docs/_sidebar.md
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
## Directives
|
|
4
4
|
|
|
5
|
-
Blits comes with a few built-in attributes that tap into specific behaviour provided by the framework - also
|
|
5
|
+
Blits comes with a few built-in attributes that tap into specific behaviour provided by the framework - also known as _directives_. Most of them can be used in the template on both Elements and Components.
|
|
6
6
|
|
|
7
7
|
### Show-directive
|
|
8
8
|
|
|
9
|
-
The `show
|
|
9
|
+
The `show` attribute allows you to conditionally show and hide Components and Elements.
|
|
10
10
|
|
|
11
|
-
When passed a _truthy_ value, such as `true` or `1`, the element will be made visible. And when passed a
|
|
11
|
+
When passed a _truthy_ value, such as `true` or `1`, the element will be made visible. And when passed a _falsey_ value (`false` or `0`) it will not be visible.
|
|
12
12
|
|
|
13
13
|
```xml
|
|
14
14
|
<Element>
|
|
@@ -39,6 +39,14 @@ export default Blits('DynamicComponents', {
|
|
|
39
39
|
|
|
40
40
|
At the moment it's not possible to use the `is`-attribute as a _reactive_ attribute and have a component change type after instantiation.
|
|
41
41
|
|
|
42
|
+
### Ref-attribute
|
|
43
|
+
|
|
44
|
+
The declarative coding style that is promoted in Blits components, should generally remove the need to directly interact with individual Elements or Components in your template. However, in some cases you may need to reference them directly. For example, in order to delegate the focus.
|
|
45
|
+
|
|
46
|
+
For this use case, you can specify a `ref`-attribute on Elements (or Components). And by using the helper function `this.select()`, which is vailable on every Blits component, you can gain access to the child Element or Component inside your business logic.
|
|
47
|
+
|
|
48
|
+
The `select()`-function accepts the `ref`-value that you are looking for as a single argument, and returns the Element or Component if found.
|
|
49
|
+
|
|
42
50
|
<!-- ### If-directive
|
|
43
51
|
|
|
44
52
|
Todo -->
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# Blits - Lightning 3 App Development Framework
|
|
2
|
+
|
|
3
|
+
## For loop
|
|
4
|
+
|
|
5
|
+
The for loop is technically also a [directive](../directives.md), but such an important one that it deserves it's own section.
|
|
6
|
+
|
|
7
|
+
The for loop directive can be used when you want to repeat multiple instances of an Element or a Component, without having to specify them hardcoded one by one in your template.
|
|
8
|
+
|
|
9
|
+
The for loop take an `Array` of data, loops over it and for each Array-item an Element or Component is created. The `Array` can be a fixed one, but it' can also dynamically be filled or modified and have it's changes reflect in the rendered result.
|
|
10
|
+
|
|
11
|
+
> Important: the for loop is a powerful and often used directive. Within Blits it's optimized to be as performant as possible. But if used incorrectly, it can be the cause of slow performance. So please read carefully through this entire section.
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Basic syntax
|
|
15
|
+
|
|
16
|
+
The for-loop directive should be added as an attribute on the Element or Component that you want to repeat for each item in your Array. In the template syntax a `for ... in` construct is used. This ultimately transpiles down to a classic, optimized for loop.
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
export default Blits('ForLoop', {
|
|
20
|
+
template: `
|
|
21
|
+
<Element>
|
|
22
|
+
<Element :for="item in $items" w="100" h="100" color="lime" />
|
|
23
|
+
</Element>
|
|
24
|
+
`,
|
|
25
|
+
state() {
|
|
26
|
+
return {
|
|
27
|
+
items: ['Item 1', 'Item 2', 'Item 3']
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The example above will generate 3 _lime green_ elements, based on the array of `items` in the Component state. They will appear on top of each other though, since they have no `x` and `y` positioning (which default to 0).
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
### Using the index
|
|
37
|
+
|
|
38
|
+
In order to fix the example above and make all 3 elements visible, we can use the `index` of the Array, to position each item correctly.
|
|
39
|
+
|
|
40
|
+
All we need to do is declare the `index` in the `for`-directive and subsequently use it in the the `x`-attribute, with a simple calculation.
|
|
41
|
+
|
|
42
|
+
You may choose any name for the `index` variable (like `i` or `forIndex`). This may be useful when you already have the `index`-key specified as a `state` or `prop` variable on your component, causing a conflict.
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
export default Blits('ForLoop', {
|
|
46
|
+
template: `
|
|
47
|
+
<Element>
|
|
48
|
+
<Element :for="(item, index) in $items" w="100" h="100" color="lime" :x="$index * 150" />
|
|
49
|
+
</Element>
|
|
50
|
+
`,
|
|
51
|
+
state() {
|
|
52
|
+
return {
|
|
53
|
+
items: ['Item 1', 'Item 2', 'Item 3']
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
You will now see that each Element is positioned next to each other, with a 50px space in between.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
### Using variables inside a for loop
|
|
63
|
+
|
|
64
|
+
When iterating over an Array in the for loop, you can reference the current Array item under the name specified in the for-in construct, prefixed with a `$`-sign. Besides this scoped Array item context, you _also_ have access to any `prop`, `state` or `computed` variable as you loop over an Array.
|
|
65
|
+
|
|
66
|
+
Consider the example below, that loops over an array of _Objects_, and _also_ references the `alpha` state variable.
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
export default Blits('ForLoop', {
|
|
70
|
+
template: `
|
|
71
|
+
<Element>
|
|
72
|
+
<Element
|
|
73
|
+
:for="(item, index) in $items"
|
|
74
|
+
w="100"
|
|
75
|
+
h="100"
|
|
76
|
+
:color="$item.color"
|
|
77
|
+
:x="$index * 150"
|
|
78
|
+
alpha="$alpha"
|
|
79
|
+
/>
|
|
80
|
+
</Element>
|
|
81
|
+
`,
|
|
82
|
+
state() {
|
|
83
|
+
return {
|
|
84
|
+
alpha: 0.5,
|
|
85
|
+
items: [{
|
|
86
|
+
id: 1,
|
|
87
|
+
color: 'green'
|
|
88
|
+
}, {
|
|
89
|
+
id: 2,
|
|
90
|
+
color: 'blue'
|
|
91
|
+
}, {
|
|
92
|
+
id: 3,
|
|
93
|
+
color: 'yellow'
|
|
94
|
+
}]
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
})
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
This will generate 3 squares aligned next to each other, each with a differrent color and they will use the _alpha_ specific in the Component state.
|
|
101
|
+
|
|
102
|
+
### The importance of using the key attribute
|
|
103
|
+
|
|
104
|
+
The examples above have demonstrated the basic usage of the for-loop. But they are all missing one _very important_ ingredient to make sure that for-loops that are subject to change, continue to perform well. Meet the `key`-attribute.
|
|
105
|
+
|
|
106
|
+
For perfomance reasons, it is essential to be able track the _identity_ of an Element or Component, whenever changes are made to the Array in the for loop.
|
|
107
|
+
|
|
108
|
+
Think of _identity_ as the field that makes an item in the Array unique, such as an `id` or a `hash`. It's basically the thing that allows you to distinguish between the poster of _The Matrix_ and the poster of _Frozen 2_, for example.
|
|
109
|
+
|
|
110
|
+
Providing this information, allows the for-loop to decide whether it should _update_ an existing Component or Element instance. Or whether we're dealing with a new Array item, and a new instance should be _created_.
|
|
111
|
+
|
|
112
|
+
Correctly using the `key`-attribute enables Blits to _reuse_ existing instances whenever possible - which obviously is good for performance.
|
|
113
|
+
|
|
114
|
+
It's important that the `key`-attribute is _unique_ for each Array item. Also beware that we can't rely on the `index` parameter provided in for loop, because that only indentifies the position in the Array, and not the actual item itself.
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
export default Blits('ForLoop', {
|
|
119
|
+
template: `
|
|
120
|
+
<Element>
|
|
121
|
+
<Element
|
|
122
|
+
:for="(item, index) in $items"
|
|
123
|
+
w="100"
|
|
124
|
+
h="100"
|
|
125
|
+
:color="$item.color"
|
|
126
|
+
:x="$index * 150"
|
|
127
|
+
alpha="$alpha"
|
|
128
|
+
key="$item.id"
|
|
129
|
+
/>
|
|
130
|
+
<!-- ^^ id is the unique field that identifies each array item -->
|
|
131
|
+
</Element>
|
|
132
|
+
`,
|
|
133
|
+
state() {
|
|
134
|
+
return {
|
|
135
|
+
alpha: 0.5,
|
|
136
|
+
items: [{
|
|
137
|
+
id: 1,
|
|
138
|
+
color: 'green'
|
|
139
|
+
}, {
|
|
140
|
+
id: 2,
|
|
141
|
+
color: 'blue'
|
|
142
|
+
}, {
|
|
143
|
+
id: 3,
|
|
144
|
+
color: 'yellow'
|
|
145
|
+
}]
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
hooks: {
|
|
149
|
+
ready() {
|
|
150
|
+
let count = 3
|
|
151
|
+
this.$setInterval(() => {
|
|
152
|
+
this.items.push({id: count++, color: randomColor()})
|
|
153
|
+
}, 2000)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
In the example above we have added the `key`-attribute in the template. Now whenever we push a new item to the `items`-array, the for-loop will reuse the Elements previously created and only instantiate a new Element for the newly added item.
|
|
160
|
+
|
|
161
|
+
If we would ommit the `key`-attribute, then whenever we push a new item to the Array, the for-loop would dispose all the old Elements and create new instances for _each_ item in the array.
|
|
162
|
+
|
|
163
|
+
### Referencing elements in the for loop
|
|
164
|
+
|
|
165
|
+
When you specify a `for`-attribute on an Element or a Component, it will effectively create multiple Elements / Components depending
|
|
166
|
+
on the length of the Array.
|
|
167
|
+
|
|
168
|
+
Generally it's not recommended to access and interact directly with Elements in a template, and that also goes for items in a For loop. But in some cases you may need to, for example to delegate focus to a specific item in the For loop.
|
|
169
|
+
|
|
170
|
+
The `ref`-attribute can be used in combination with the `for`-attribute as well.
|
|
171
|
+
|
|
172
|
+
Setting the _ref_ in a for loop context, can be done by either passing a string (i.e. `myitem`) and as Elements get generated in the for loop, the `index` value will be appended to it (i.e. `myitem0`, `myitem1`, etc.). Alternatively you can make the attribute interpolated and define the value for each item yourself (i.e. `:ref="'item' + $item.id"`).
|
|
@@ -47,8 +47,7 @@ For nested objects, you can use _dot notation_ (e.g., `$style.dimensions.w`).
|
|
|
47
47
|
In your component's code, you can reference state variables directly within the `this`-scope. For instance, a state variable named `color` can be accessed (and modified) by referencing `this.color`.
|
|
48
48
|
|
|
49
49
|
It's important to note that unlike in the template, you should _not_ use the dollar sign when accessing state variables within the component's code.
|
|
50
|
-
|
|
51
|
-
Also remember that there is no need to explicitely reference the `state`-key. Blits automatically maps all state variables directly on the `this`-scope, for easy access.
|
|
50
|
+
Also, remember that there is no need to explicitly reference the `state`-key. Blits automatically maps all state variables directly on the `this`-scope, for easy access.
|
|
52
51
|
|
|
53
52
|
The example below gives a full example of defining and using a component's state:
|
|
54
53
|
|
|
@@ -4,9 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
So far we've learned how to utilize a Component's [internal state](./component_state.md) and [props](./props.md) passed by a parent into a child component.
|
|
6
6
|
|
|
7
|
-
We've also seen how we can perform simple operations like `Math.max()` or `str.toUpperCase()` directly within an argument of a template on the Component's internal state or passed props
|
|
8
|
-
|
|
9
|
-
However as these operations become more complex it's more clear and more maintainable to abstract these operations into what we call _computed properties_.
|
|
7
|
+
We've also seen how we can perform simple operations like `Math.max()` or `str.toUpperCase()` directly within an argument of a template on the Component's internal state or passed props.
|
|
8
|
+
However, as these operations become more complex it's more clear and more maintainable to abstract these operations into what we call _computed properties_.
|
|
10
9
|
|
|
11
10
|
Computed properties are a powerful tool for enhancing the readability of your component code. By abstracting complex or frequently used calculations from your template into computed properties, you can make your code more concise and easier to understand.
|
|
12
11
|
|
|
@@ -17,7 +16,7 @@ Within the `computed`-key of the Component configuration object, you can specify
|
|
|
17
16
|
|
|
18
17
|
A computed property function should always _return_ a value.
|
|
19
18
|
|
|
20
|
-
In your template, you can reference these computed properties exactly the same as you would with _state_
|
|
19
|
+
In your template, you can reference these computed properties exactly the same as you would with _state_ variables and _props_, by prefixing them with a dollar sign (e.g., `$offset`).
|
|
21
20
|
|
|
22
21
|
In the rest of your app's code, you can access these computed properties (but not modify them!) using `this.offset`. Note that similar to Component state and props, you do not need to prefix with `computed` to access the computed property.
|
|
23
22
|
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
## Methods
|
|
4
4
|
|
|
5
|
-
Within a Blits component you have quite some freedom where to put specific logic.
|
|
5
|
+
Within a Blits component, you have quite some freedom where to put specific logic.
|
|
6
6
|
|
|
7
|
-
Template related logic can be either
|
|
7
|
+
Template related logic can be either placed directly in a _reactive / interpolated_ attribute or you can move it out to a _computed property_ if the logic is more complex.
|
|
8
8
|
|
|
9
9
|
Business logic for your App can be placed directly in a _watcher_, a _lifecycle hook_ or an _input handling_ function.
|
|
10
10
|
|
|
@@ -12,7 +12,7 @@ If you notice that you're duplicating logic in these different places, or if you
|
|
|
12
12
|
|
|
13
13
|
You can reference your Component's methods in the template by using a `$`-sign (for example in the `@loaded`-attribute when your element has a `src` attribute).
|
|
14
14
|
|
|
15
|
-
In the javascript code of a Component you can reference methods directly on the `this`-scope (i.e. `this.getData()`). Similar
|
|
15
|
+
In the javascript code of a Component, you can reference methods directly on the `this`-scope (i.e. `this.getData()`). Similar to `internal` state and `props`, there is no need to prefix with `methods`, for easy access.
|
|
16
16
|
|
|
17
17
|
```js
|
|
18
18
|
export default Blits('Carousel', {
|
package/docs/components/props.md
CHANGED
|
@@ -22,7 +22,7 @@ The simplest way to define props is to just list their names within the `props`
|
|
|
22
22
|
|
|
23
23
|
Once specified, you can refer to these props inside the template of your component using the `$` sign, similar to how you would reference variables defined within the component's [internal state](./component_state.md) (i.e. `<Element color="$color" />`).
|
|
24
24
|
|
|
25
|
-
You can
|
|
25
|
+
You can also access a prop inside a component's code using `this.color` (without a dollar sign!). And similar to component `state` variables,
|
|
26
26
|
there is no need to specifically reference the `props`-key. Blits automatically maps all props directly on the `this`-scope, for easy access.
|
|
27
27
|
|
|
28
28
|
Since props are used to pass information from a parent to a child, it's important not to attempt to _modify_ props inside your child component. If changes based on the prop from the parent are needed, you should probably use the prop in a so called [computed property](./computed_properties.md).
|
|
@@ -11,7 +11,7 @@ Blits offers an intuitive and straightforward interface to handle key input in C
|
|
|
11
11
|
|
|
12
12
|
Before diving into the specifics of key handling, it is important to understand the basic concept of _focus_.
|
|
13
13
|
|
|
14
|
-
In a Blits app, there is always _one_ Component that has the focus. By default this will be the root Application component.
|
|
14
|
+
In a Blits app, there is always _one_ Component that has the focus. By default, this will be the root Application component.
|
|
15
15
|
|
|
16
16
|
The component that has focus, is the one that is responsible for handling the user input at that moment.
|
|
17
17
|
|
|
@@ -81,7 +81,7 @@ Blits comes with a default keycode mapping. This mapping is a sensible default t
|
|
|
81
81
|
|
|
82
82
|
But it's possible that the keycodes and mapping of your target device are slightly or even completely different.
|
|
83
83
|
|
|
84
|
-
In Blits you can easily configure the key mapping to match your needs. In the `src/index.js` file where we
|
|
84
|
+
In Blits, you can easily configure the key mapping to match your needs. In the `src/index.js` file where we instantiate the App via the `Blits.Launch` function, we can add an extra key, called `keys`, to the _settings object_.
|
|
85
85
|
|
|
86
86
|
The `keys` item should be an object literal, where you map a `key` or `keyCode` (from the `KeyboardEvent`) to an event name that you can use in your Components.
|
|
87
87
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
In some cases, you may want to execute specific actions whenever the value of a state variable, a prop, or a computed property changes. These actions could involve dispatching an event or updating another state variable.
|
|
6
6
|
|
|
7
|
-
You might be tempted to handle this functionality inside a computed property, but this is not recommended. Computed properties should not have side effects, to prevent the risk of falling into
|
|
7
|
+
You might be tempted to handle this functionality inside a computed property, but this is not recommended. Computed properties should not have side effects, to prevent the risk of falling into an endless loop.
|
|
8
8
|
|
|
9
9
|
Instead, Blits allows you to specify **watchers** to trigger functionality when certain variables change.
|
|
10
10
|
|
|
@@ -6,7 +6,7 @@ Every Blits App starts with a base Application component.
|
|
|
6
6
|
|
|
7
7
|
Ultimately this Application is a component like any regular Blits component. But it is augmented with some extra functionality. `Blits.Application` is responsible for setting up the listeners for keyhandling for example.
|
|
8
8
|
|
|
9
|
-
You can only have 1 Application component per App. By default this file is named `App.js` and it is placed in the root of the `src`-folder.
|
|
9
|
+
You can only have 1 Application component per App. By default, this file is named `App.js` and it is placed in the root of the `src`-folder.
|
|
10
10
|
|
|
11
11
|
`src/App.js` will look something like this:
|
|
12
12
|
|
|
@@ -20,11 +20,11 @@ Let's see how to create a new component and explore the basic anatomy of a compo
|
|
|
20
20
|
|
|
21
21
|
A new component is created using the `Blits.Component()` function that is exported from the `@lightningjs/blits` package. This function accepts two arguments:
|
|
22
22
|
|
|
23
|
-
- The first argument is the _name_ of the component. This name will be used in debug log messages, so make sure to choose a unique and descriptive name for your component, such as `Homepage`, `Loader
|
|
23
|
+
- The first argument is the _name_ of the component. This name will be used in debug log messages, so make sure to choose a unique and descriptive name for your component, such as `Homepage`, `Loader`, or `SidebarMenuItem`.
|
|
24
24
|
|
|
25
|
-
- The second argument is a _Component configuration object_, represented as an `object literal`. This Component configuration object can contain a predefined set of key-value pairs, that
|
|
25
|
+
- The second argument is a _Component configuration object_, represented as an `object literal`. This Component configuration object can contain a predefined set of key-value pairs, that define how your Component looks and behaves.
|
|
26
26
|
|
|
27
|
-
Some of the
|
|
27
|
+
Some of the commonly used Component configuration options include:
|
|
28
28
|
|
|
29
29
|
- `template`: to define the template for the component.
|
|
30
30
|
- `state`: to specify the component instance's internal state.
|
|
@@ -25,7 +25,7 @@ For the best performance, it's important to keep your source images as small as
|
|
|
25
25
|
|
|
26
26
|
You also have the option to _colorize_ an image on the fly. Just add a `color` attribute to the Element with a `src` attribute. You can use a single color, or apply a gradient.
|
|
27
27
|
|
|
28
|
-
By default all Elements with a `src` attribute get
|
|
28
|
+
By default, all Elements with a `src` attribute get a solid white background, with the result that the actual colors of the image will be shown.
|
|
29
29
|
|
|
30
30
|
```xml
|
|
31
31
|
<Element
|
|
@@ -66,4 +66,4 @@ Considering the template above you would do something like the following in the
|
|
|
66
66
|
}
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
The `loaded` event receives
|
|
69
|
+
The `loaded` event receives image dimensions as its argument and the `error` event receives an error message explaining the failure.
|
|
@@ -19,19 +19,19 @@ You can use the Text-tag anywhere in your template, without the need to explicit
|
|
|
19
19
|
|
|
20
20
|
### Available attributes on the Text tag
|
|
21
21
|
|
|
22
|
-
The Text-tag accepts the following
|
|
22
|
+
The Text-tag accepts the following attributes:
|
|
23
23
|
|
|
24
|
-
- `content` - the text to be displayed. Can be a hardcoded text, a dynamic value or a reactive value
|
|
24
|
+
- `content` - the text to be displayed. Can be a hardcoded text, a dynamic value, or a reactive value
|
|
25
25
|
- `font` - the font family, defaults to `lato`, or the default font specified in the launch settings
|
|
26
26
|
- `size` - the font size, defaults to `32`
|
|
27
|
-
- `color` - the color to display for the text, defaults to `white` and can be any of the supported Blits color formats (
|
|
27
|
+
- `color` - the color to display for the text, defaults to `white` and can be any of the supported Blits color formats (HTML, hexadecimal or rgb(a))
|
|
28
28
|
- `letterspacing` - letterspacing in pixels, defaults to `0`
|
|
29
|
-
- `align` - the alignment of the text, can be `left`, `right
|
|
30
|
-
- `wordwrap` - the max length of a line of text in pixels, words surpassing this length will be broken and wrapped onto the next line. This
|
|
29
|
+
- `align` - the alignment of the text, can be `left`, `right`, or `center`, defaults to `left`. Centering text and aligning text to the right requires the `wordwrap` attribute to be set as well.
|
|
30
|
+
- `wordwrap` - the max length of a line of text in pixels, words surpassing this length will be broken and wrapped onto the next line. This attribute is required when aligning center or right
|
|
31
31
|
- `maxlines` - maximum number of lines that will be displayed
|
|
32
32
|
- `maxheight` - maximum height of a text block, lines that don't fit within this height will not be displayed
|
|
33
33
|
- `lineheight` - the spacing between lines in pixels
|
|
34
|
-
- `contain` - the strategy for containing text within the bounds, can be `none` (default), `width
|
|
34
|
+
- `contain` - the strategy for containing text within the bounds, can be `none` (default), `width`, or `both`. In most cases, the value of this attribute will automatically be set by Blits, based on the other specified attributes
|
|
35
35
|
- `textoverflow` - the suffix to be added when text is cropped due to bounds limits, defaults to `...`
|
|
36
36
|
|
|
37
37
|
|
|
@@ -39,23 +39,23 @@ The Text-tag accepts the following of attributes:
|
|
|
39
39
|
|
|
40
40
|
Compared to Lightning 2, texts have improved a lot in Lightning 3, thanks to the SDF (Signed Distance Field) Text renderer.
|
|
41
41
|
|
|
42
|
-
With the SDF text renderer, texts appear a lot _sharper_ on screen. The SDF technique also allows for better scaling of texts, without them becoming blurry - a well
|
|
42
|
+
With the SDF text renderer, texts appear a lot _sharper_ on screen. The SDF technique also allows for better scaling of texts, without them becoming blurry - a well-known painpoint in Lightning 2 Apps.
|
|
43
43
|
|
|
44
|
-
In general it's recommended to use the SDF text renderer, but Lightning 3 still has a Canvas2d text renderer as a backup, and you can use both text renderers within the same App.
|
|
44
|
+
In general, it's recommended to use the SDF text renderer, but Lightning 3 still has a Canvas2d text renderer as a backup, and you can use both text renderers within the same App.
|
|
45
45
|
|
|
46
46
|
### Using custom fonts
|
|
47
47
|
|
|
48
|
-
The `font`-attribute on the `<Text>`-tag is used define which font family should be used for a certain piece of text.
|
|
48
|
+
The `font`-attribute on the `<Text>`-tag is used to define which font family should be used for a certain piece of text.
|
|
49
49
|
|
|
50
50
|
When you create a new Blits app using the available [getting started boilerplate](../getting_started/getting_started.md) you'll be able to use the `lato` (Lato regular) and `raleway` (Raleway ExtraBold) fonts out of the box.
|
|
51
51
|
|
|
52
|
-
But of course you can also use any custom font that you want, to give your App the unique look and feel that fits with the design.
|
|
52
|
+
But of course, you can also use any custom font that you want, to give your App the unique look and feel that fits with the design.
|
|
53
53
|
|
|
54
|
-
Adding is custom font to a Blits App is quite straightforward. First you'll need to place a `.ttf` version of your font in the `public` folder (i.e. `public/fonts/comic-sans.ttf`).
|
|
54
|
+
Adding is custom font to a Blits App is quite straightforward. First, you'll need to place a `.ttf` version of your font in the `public` folder (i.e. `public/fonts/comic-sans.ttf`).
|
|
55
55
|
|
|
56
56
|
Then you'll need to register the custom font in the Launch settings of your app (in `src/index.js`). The `fonts`-key in the settings is an `Array` that specifies all available fonts in your App.
|
|
57
57
|
|
|
58
|
-
Just add a new font object with the
|
|
58
|
+
Just add a new font object with the necessary details:
|
|
59
59
|
|
|
60
60
|
```js
|
|
61
61
|
fonts: [
|
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
|
|
5
5
|
The core building block of a Blits template is the `<Element>`-tag. The Element tag corresponds directly to a node in the Lightning 3 Renderer. You can style and position Elements via _attributes_, much like you would do in plain HTML.
|
|
6
6
|
|
|
7
|
-
Blits Elements have a specific set of attributes that can be used. The available attributes are loosely modeled after the properties of a Lightning 3 renderer node. In certain cases Blits provides more developer friendly names or accepts a wider range of values, and transparently takes care of the translation into L3 renderer instructions.
|
|
7
|
+
Blits Elements have a specific set of attributes that can be used. The available attributes are loosely modeled after the properties of a Lightning 3 renderer node. In certain cases, Blits provides more developer friendly names or accepts a wider range of values, and transparently takes care of the translation into L3 renderer instructions.
|
|
8
8
|
|
|
9
9
|
### Positioning and Dimensions
|
|
10
10
|
|
|
11
11
|
In order to position and set the dimensions of an Element, the following attributes can be used.
|
|
12
12
|
|
|
13
|
-
- `x` - the x position of the Element in pixels, relative to
|
|
14
|
-
- `y` - the y position of the Element in pixels, relative to
|
|
13
|
+
- `x` - the x position of the Element in pixels, relative to its parent - allows negative values and decimals
|
|
14
|
+
- `y` - the y position of the Element in pixels, relative to its parent - allows negative values and decimals
|
|
15
15
|
- `z` - the z index of the element (optionally `zIndex` can be used as an alias)
|
|
16
16
|
- `w` - the width of the element in pixels (optionally `width` can be used as an alias)
|
|
17
17
|
- `h` - the height of the element in pixels (optionally `height` can be used as an alias)
|
|
@@ -40,9 +40,9 @@ And the percentage specified for `h` and `y` will use the _height_ (`h`) of the
|
|
|
40
40
|
|
|
41
41
|
### Colors
|
|
42
42
|
|
|
43
|
-
By default Elements have a transparent background color. The `color` attribute can be used to give an Element a color.
|
|
43
|
+
By default, Elements have a transparent background color. The `color` attribute can be used to give an Element a color.
|
|
44
44
|
|
|
45
|
-
If you are familiar with Lightning 2: colors have gotten a lot easier with Blits. Under the hood the Lightning 3 renderer still uses the somewhat unfamiliar (but efficient) `0xffc0ffee` syntax. In Blits you can specify colors as you are used to in HTML and CSS.
|
|
45
|
+
If you are familiar with Lightning 2: colors have gotten a lot easier with Blits. Under the hood, the Lightning 3 renderer still uses the somewhat unfamiliar (but efficient) `0xffc0ffee` syntax. In Blits, you can specify colors as you are used to in HTML and CSS.
|
|
46
46
|
|
|
47
47
|
Blits accepts the following color formats and makes sure that they are converted in a way the Lightning 3 renderer can understand.
|
|
48
48
|
|
|
@@ -83,9 +83,9 @@ Again, you can use "normal" notation for the colors (like hexadecimal or rgba) a
|
|
|
83
83
|
|
|
84
84
|
### Alpha and visibility
|
|
85
85
|
|
|
86
|
-
The opacity of an Element
|
|
86
|
+
The opacity of an Element can be controlled by setting the `alpha` attribute. This attribute accepts a value between `0` (fully transparent) and `1` (completely visible).
|
|
87
87
|
|
|
88
|
-
The value of alpha is also
|
|
88
|
+
The value of alpha is also applied recursively to the children of the Element that has its alpha set. If you just want the background color of an Element to be semi-transparent, you should set the alpha channel in the `color` instead of applying the `alpha` attribute.
|
|
89
89
|
|
|
90
90
|
```xml
|
|
91
91
|
<Element w="200" h="200" color="blue" alpha="0.8">
|
|
@@ -107,13 +107,13 @@ The rotation of an Element is also automatically applied to any children down th
|
|
|
107
107
|
|
|
108
108
|
For scaling an Element the `scale` attribute is used.
|
|
109
109
|
|
|
110
|
-
This attribute either accepts a single numeric value for scaling evenly
|
|
110
|
+
This attribute either accepts a single numeric value for scaling evenly across width and height. Or an _object literal_, if you want to apply a different scaling for the `x` and the `y` axis.
|
|
111
111
|
|
|
112
112
|
The value should be higher than `0` and the default value is `1`, which means no scaling.
|
|
113
113
|
|
|
114
114
|
Any value below `1` will scale _down_ the element and values greater than `1` will scale the element _up_.
|
|
115
115
|
|
|
116
|
-
Similar to rotation, scale is also applied recursively to children down the tree of the Element that has
|
|
116
|
+
Similar to rotation, scale is also applied recursively to children down the tree of the Element that has its `scale` attribute set.
|
|
117
117
|
|
|
118
118
|
```xml
|
|
119
119
|
<Element w="200" h="200" color="blue" scale="0.5" />
|
|
@@ -123,13 +123,13 @@ Similar to rotation, scale is also applied recursively to children down the tree
|
|
|
123
123
|
|
|
124
124
|
### Mounting point
|
|
125
125
|
|
|
126
|
-
For advanced positioning the `mount` attribute can come in handy. By default when you set the `x` and `y` position of an Element, the _top-left_ corner will be set to that position. But in some cases you may want to align the position starting at a different corner, or even any arbitrary point in between.
|
|
126
|
+
For advanced positioning, the `mount` attribute can come in handy. By default when you set the `x` and `y` position of an Element, the _top-left_ corner will be set to that position. But in some cases, you may want to align the position starting at a different corner, or even any arbitrary point in between.
|
|
127
127
|
|
|
128
128
|
The `mount` attribute accepts an _object literal_ that allows you to precisely control the mount position on the _x-axis_ and the mount position on the _y-axis_. The default value is `{x: 0, y: 0}`, which refers to the _top-left_ corner.
|
|
129
129
|
|
|
130
130
|
In order to align the position starting at the _bottom-right_ corner, we would set `mount` to `{x: 1, y: 1}` and `{x: 0.5, y: 0.5}` would align the position at the _center_ of the Element.
|
|
131
131
|
|
|
132
|
-
If you omit either the `x` or the `y` key from the _object literal_,
|
|
132
|
+
If you omit either the `x` or the `y` key from the _object literal_, its value will default to `0`.
|
|
133
133
|
|
|
134
134
|
In the case where the `x` and `y` values are the same (i.e. centering with `{x: 0.5, y: 0.5}`), you can also just supply a single value (`mount="0.5"`) instead of the object literal notation.
|
|
135
135
|
|
|
@@ -143,7 +143,7 @@ In the case where the `x` and `y` values are the same (i.e. centering with `{x:
|
|
|
143
143
|
|
|
144
144
|
The pivot point of an Element comes into play when it's rotated or scaled. The pivot point defaults to the _center_ of the Element, which means that when setting `rotation` it rotates around the middle. And when the Element is scaled it scales from the center out.
|
|
145
145
|
|
|
146
|
-
But sometimes you may want rotate around the left corner, or scale from the right side out. This can be controlled by adding the `pivot` attribute to the Element and, similar to the `mount` attribute, specify an _object literal_ with
|
|
146
|
+
But sometimes you may want to rotate around the left corner, or scale from the right side out. This can be controlled by adding the `pivot` attribute to the Element and, similar to the `mount` attribute, specify an _object literal_ with an `x` and a `y` key.
|
|
147
147
|
|
|
148
148
|
Both `y` and `x` values should be anything between `0` and `1`, where `{x: 0, y: 0}` sets the pivot point to the _top-left_ corner and `{x: 1, y: 1}` refers to the _bottom-right_ corner. The default pivot value is `{x: 0.5, y: 0.5}` (i.e. the center) and if you omit `x` or `y` in your pivot object, it will default to `0.5`.
|
|
149
149
|
|
|
@@ -156,4 +156,10 @@ In the case where the `x` and `y` values are the same, you can also just supply
|
|
|
156
156
|
<Element w="200" h="200" x="800" y="700" pivot="0.9" rotation="42" />
|
|
157
157
|
```
|
|
158
158
|
|
|
159
|
-
|
|
159
|
+
### Clipping / overflow
|
|
160
|
+
|
|
161
|
+
By default contents inside an Element (i.e. child Elements) will overflow the boundaries of the parent, even when you give the parent Element fixed dimensions.
|
|
162
|
+
|
|
163
|
+
In order to contain / cut off the content inside an Elements' `w` and `h`, you can add the `clipping="true"`-attribute. Setting `clipping` to `false` restores the default behaviour of content overflowing.
|
|
164
|
+
|
|
165
|
+
Alternatively you can also use the `overflow`-attribute (and pass it `true` or `false`), which works similar to clipping just mapped inversly (i.e. `overflow="false"` ensures content that surpasses the parent dimensions is clipped-off).
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
Blits uses an easy-to-read _XML-style_ template syntax. The syntax is inspired by frameworks like VueJS, so if you are familiar with Vue, you will probably recognize certain concepts.
|
|
6
6
|
|
|
7
|
-
The `template` key in the _Component configuration object_ is used to specify a string with this XML-like template structure. Templates can often span multiple lines. In those cases it's advisable to use `template literals` (enclosed by backticks).
|
|
7
|
+
The `template` key in the _Component configuration object_ is used to specify a string with this XML-like template structure. Templates can often span multiple lines. In those cases, it's advisable to use `template literals` (enclosed by backticks).
|
|
8
8
|
|
|
9
9
|
Similar to HTML you can use arguments and nested tags. Self-closing tags and HTML-style comments are also supported in Blits templates.
|
|
10
10
|
|
|
11
|
-
The default tag that can be used
|
|
11
|
+
The default tag that can be used in templates is the `<Element>` tag. The Element tag corresponds to a node in the Lightning 3 renderer.
|
|
12
12
|
|
|
13
13
|
For each element, there is a set of predefined render properties, such as `x`, `y`, `w`, `color` that define how the element looks and where it is positioned. Detailed information on all the attributes supported by the `<Element>` tag can be found [here](./element_attributes.md).
|
|
14
14
|
|
|
@@ -40,13 +40,13 @@ on screen. For that you will need to use _reactive_ arguments.
|
|
|
40
40
|
### Reactive arguments
|
|
41
41
|
|
|
42
42
|
If you look at the template example above you'll notice that certain arguments are prefixed with a colon sign (`:`). Prefixing an argument
|
|
43
|
-
with a colon makes it not only dynamic
|
|
43
|
+
with a colon makes it not only dynamic but also _reactive_.
|
|
44
44
|
|
|
45
45
|
Reactive arguments are set with the initial state value when your component first renders, exactly as a dynamic argument does.
|
|
46
46
|
|
|
47
47
|
The difference is that a _re-render_ of (a portion of) the template will be triggered whenever the referenced value in your Component's internal state changes.
|
|
48
48
|
|
|
49
|
-
This concept of _reactive data binding_ eliminates the need for manual template patching
|
|
49
|
+
This concept of _reactive data binding_ eliminates the need for manual template patching and allows for a declarative way of programming.
|
|
50
50
|
|
|
51
51
|
Reactive arguments also support _interpolation_. This enables the use of simple JavaScript instructions, such as ternary conditions or basic String and Math manipulations, right inside your template arguments.
|
|
52
52
|
|
|
@@ -54,15 +54,15 @@ For more complex logic, it's recommended to abstract this into a [Component meth
|
|
|
54
54
|
|
|
55
55
|
## Abstracting template portions to custom components
|
|
56
56
|
|
|
57
|
-
As your template grows in complexity, you may want to
|
|
57
|
+
As your template grows in complexity, you may want to organize your codebase with custom components, abstracting a complex template into smaller, reusable pieces.
|
|
58
58
|
|
|
59
|
-
When you've
|
|
59
|
+
When you've abstracted a portion of a template into a separate component, a _Menu_ component for example, you'll first need to `import` the file that contains the Menu component.
|
|
60
60
|
|
|
61
|
-
Next you have to _register_ the imported custom component. The `components`-key in the _configuration object_ is used for this.
|
|
61
|
+
Next, you have to _register_ the imported custom component. The `components`-key in the _configuration object_ is used for this.
|
|
62
62
|
|
|
63
63
|
The `components` key is an _object literal_, where the value is the reference to the custom Blits component and the key will correspond with the _tag_ that can be used in the template to insert the component.
|
|
64
64
|
|
|
65
|
-
Once this is done, the tag (`<Menu>` for example) is available for use in your component's template. You can use the component tag as many times as you want. For each tag a new Component instance will be created.
|
|
65
|
+
Once this is done, the tag (`<Menu>` for example) is available for use in your component's template. You can use the component tag as many times as you want. For each tag, a new Component instance will be created.
|
|
66
66
|
|
|
67
67
|
The example below shows how to use a custom `Menu` and `Button` (aliased to `MyButton`) component inside a template.
|
|
68
68
|
|