@lightningjs/blits 1.8.3 → 1.9.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## v1.9.0
4
+
5
+ _4 nov 2024_
6
+
7
+ - Added Layout component
8
+
3
9
  ## v1.8.3
4
10
 
5
11
  _31 oct 2024_
package/docs/_sidebar.md CHANGED
@@ -23,6 +23,7 @@
23
23
  - Built-in components
24
24
  - [Directives](/built-in/directives.md)
25
25
  - [For loop](/built-in/for-loop.md)
26
+ - [Layout](/built-in/layout.md)
26
27
  - Plugins
27
28
  - [Text-to-Speech / Announcer](/plugins/text-to-speech-announcer.md)
28
29
  - [Language](/plugins/language.md)
@@ -0,0 +1,76 @@
1
+ # Layout
2
+
3
+ A big portion of building a pixel perfect App is all about correctly positioning Elements and Components on screen. In Lightning and Blits _absolute_ positions are used. This fits the whole concept of building a TV app where the viewport dimensions are known and fixed. Absolute positioning (versus spacing Elements relative to each other) is also more performant.
4
+
5
+ Imagine a complex design with several nested layers. Relying solely on _relative_ positioning and elements automatically floating as space is available, may lead to heavy and recursive calculations. Then throw some dynamically changing dimensions and animations in the mix as well, and your App may be starting to noticeably drop frames due to expensive re-calculations.
6
+
7
+ That being said, there are often cases where it becomes very cumbersome to calculate all the positions manually. Especially when dimensions are not known in advance and elements may load asynchronously.
8
+
9
+ If you come from a CSS background, you may be tempted to say: "_this is why we have Flexbox!_". And while Flexbox is great and versatile in CSS and running on a powerful device, a full flexbox implementation does come at a cost and the risk of slowing down your App, when applied extensively or incorrectly.
10
+
11
+ In order to address the core problem "_how do I easily position a bunch of Elements and Components, without writing a lot of code with `@loaded` event handlers_", Blits offers the built-in `<Layout>`-component - a basic and performant solution that automatically puts your Elements in the right place.
12
+
13
+ ## How to use
14
+
15
+ Since the Layout-component is a built-in component, there is no need to register it separately. You can use the `<Layout>`-tag anywhere in your templates, just as you would use `<Element>` and `<Text>`.
16
+
17
+ The Layout component is a wrapper component, that encloses a number of children. All the direct children that are wrapped in a `<Layout>`-tag are automatically positioned in order, one next to the other.
18
+
19
+ Whenever the dimensions of a child change, the positioning of the next children is automatically recalculated and re-applied.
20
+
21
+ ```xml
22
+ <Layout>
23
+ <Element w="40" h="40" color="red" />
24
+ <Element w="80" h="40" color="blue" />
25
+ <Text>Hello world</Text>
26
+ <Element w="40" h="40" color="green" />
27
+ </Layout>
28
+ ```
29
+
30
+ ### Horizontal and vertical layout
31
+
32
+ By default the Layout component lays out it's contents _horizontally_. The Layout component accepts a `direction` attribute which allows you to control the direction.
33
+
34
+ In order to align vertically use `<Layout direction="vertical"></Layout>`. And use `<Layout direction="horizontal"></Layout>` to explicitly apply the default horizontal layout.
35
+
36
+ ### Spacing between children
37
+
38
+ By default the Layout-component places each Element directly besides (or below) the previous one. By adding the `gap`-attribute you can control how much space will be added between each Element or Component. The `gap`-attribute accepts a number in pixels.
39
+
40
+ ### Dynamic dimensions
41
+
42
+ For the Layout component to work properly, the direct children all need to have dimensions (i.e `w` and `h` attributes). The exception here being Text elements.
43
+
44
+ Due to the asynchronous nature of rendering text, the final text dimensions are not known beforehand. The width and height of Text elements are automatically set when the text has been loaded. This fires a `@loaded` event, that you can tap into as described [here](../essentials/displaying_text#text-dimensions).
45
+
46
+ The Layout component also uses this event, to execute its calculation and position the text and other elements around it properly.
47
+
48
+ When the children of the Layout-component have _reactive_ dimensions (i.e. `<Element :w="$mywidth" :h="$myheight" />), the Layout component ensures that all child elements are properly re-positioned whenever a dimension changes.
49
+
50
+ ### Nesting Layouts
51
+
52
+ It's also possible to nest layouts. Simply place a new `<Layout>`-tag, with it's own children in between the children of another Layout-component. The Layout-component itself will grow automatically with the dimensions of it's children. In other words, it's not required to specify a width (`w`) or height (`h`) on the `<Layout>`-tag itself.
53
+
54
+ And of course you can nest _vertical_ Layouts inside a _horizontal_ one - and vice versa.
55
+
56
+ ### Transitioning layouts
57
+
58
+ The `<Layout>`-component can also take into account when dimensions of children change with a transition applied to it. The component will recalculate the position of its children as the transition progresses, making sure that elements are always perfectly positioned relative to one another.
59
+
60
+ ## Performance
61
+
62
+ The Layout-component is a very useful utility, that will make it a lot easier to put together complex layouts. It will reduce the code required, it means less hardcoding of values and removes the need to manually wire up `@loaded` events when working with dynamic / asynchronous elements.
63
+
64
+ We've done our best to make the Component as performant as possible, by deliberately keeping it simple and straight forward. But it's important to understand that the Layout component does come at a cost. Depending on the use case these costs may be negligible, but there are cases where the Layout component might prove to be a bottle-neck.
65
+
66
+ The performance costs may always be there to some degree. Whether it's the Layout component that does the complex calculations, or you do it in your custom code. So please feel encouraged to use the `<Layout>`-tag!
67
+
68
+ Some pointers to keep in mind, though:
69
+
70
+ - Every change in the dimensions of a child, triggers the Layout component to recalculate and reposition. If your child elements change frequently, the Layout component may have a performance impact.
71
+
72
+ - The Layout component support transitions, but beware that when transitioning child dimensions a recalculation happens every frame-tick. If you see frame-drops in your App, you may want to see if the `<Layout>`-tag has an impact on this.
73
+
74
+ - Be mindful to not apply unnecessary deep nesting of Layout tags. Each change in a deep child, will trigger recalculations up the tree of Layout tags.
75
+
76
+ - When using the `<Layout>`-tag with a for-loop, it may be more performant to use the `$index` of the for-loop and manually position the elements yourself (i.e. `<Element :for="(item, index) in $items" :$x="$item * 200" w="150" h="150" />`). Especially if the dimensions are know before hand and possibly are the same for each child.
package/docs/sidebar.json CHANGED
@@ -101,6 +101,10 @@
101
101
  {
102
102
  "text": "For loop",
103
103
  "link": "/built-in/for-loop"
104
+ },
105
+ {
106
+ "text": "Layout",
107
+ "link": "/built-in/layout.md"
104
108
  }
105
109
  ]
106
110
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightningjs/blits",
3
- "version": "1.8.3",
3
+ "version": "1.9.0",
4
4
  "description": "Blits: The Lightning 3 App Development Framework",
5
5
  "bin": "bin/index.js",
6
6
  "exports": {
@@ -19,12 +19,10 @@ import Circle from './Circle.js'
19
19
  import RouterView from './RouterView.js'
20
20
  import Sprite from './Sprite.js'
21
21
  import FPScounter from './FPScounter.js'
22
- import Layout from './Layout.js'
23
22
 
24
23
  export default () => ({
25
24
  Circle: Circle(),
26
25
  RouterView: RouterView(),
27
26
  Sprite: Sprite(),
28
27
  FPScounter: FPScounter(),
29
- Layout: Layout(),
30
28
  })
@@ -19,12 +19,10 @@ import Circle from './Circle.js'
19
19
  import RouterView from './RouterView.js'
20
20
  import Sprite from './Sprite.js'
21
21
  import FPScounter from './FPScounter.js'
22
- import Layout from './Layout.js'
23
22
 
24
23
  export default () => ({
25
24
  Circle: Circle(),
26
25
  RouterView: RouterView(),
27
26
  Sprite: Sprite(),
28
27
  FPScounter: FPScounter(),
29
- Layout: Layout(),
30
28
  })
@@ -22,6 +22,38 @@ import { Log } from '../../lib/log.js'
22
22
  import symbols from '../../lib/symbols.js'
23
23
  import Settings from '../../settings.js'
24
24
 
25
+ const layoutFn = function (config) {
26
+ let offset = 0
27
+ const xy = config.direction === 'vertical' ? 'y' : 'x'
28
+ const wh = config.direction === 'vertical' ? 'height' : 'width'
29
+ const opositeWh = config.direction === 'vertical' ? 'width' : 'height'
30
+
31
+ const children = this.children
32
+ const childrenLength = children.length
33
+ let otherDimension = 0
34
+ for (let i = 0; i < childrenLength; i++) {
35
+ const node = children[i]
36
+ node[xy] = offset
37
+ // todo: temporary text check, due to 1px width of empty text node
38
+ if (wh === 'width') {
39
+ offset += node.width + (node.width !== ('text' in node ? 1 : 0) ? config.gap : 0)
40
+ } else if (wh === 'height') {
41
+ offset +=
42
+ 'text' in node
43
+ ? node.width > 1
44
+ ? node.height + config.gap
45
+ : 0
46
+ : node.height !== 0
47
+ ? node.height + config.gap
48
+ : 0
49
+ }
50
+ otherDimension = Math.max(otherDimension, node[opositeWh])
51
+ }
52
+ // adjust the size of the layout container
53
+ this[wh] = offset - (config.gap || 0)
54
+ this[opositeWh] = otherDimension
55
+ }
56
+
25
57
  const isTransition = (value) => {
26
58
  return value !== null && typeof value === 'object' && 'transition' in value === true
27
59
  }
@@ -305,11 +337,16 @@ const Element = {
305
337
  })
306
338
  }
307
339
 
308
- // if (this.component !== undefined && '___layout' in this.component) {
309
- // this.node.on('loaded', () => {
310
- // this.component.___layout()
311
- // })
312
- // }
340
+ if (props.__layout === true) {
341
+ this.triggerLayout = layoutFn.bind(this.node)
342
+ }
343
+
344
+ if (this.config.parent.props !== undefined && this.config.parent.props.__layout === true) {
345
+ this.config.parent.triggerLayout(this.config.parent.props)
346
+ this.node.on('loaded', () => {
347
+ this.config.parent.triggerLayout(this.config.parent.props)
348
+ })
349
+ }
313
350
  },
314
351
  set(prop, value) {
315
352
  if (value === undefined) return
@@ -338,10 +375,9 @@ const Element = {
338
375
  }
339
376
  }
340
377
 
341
- // todo: review naming
342
- // if (this.component && this.component.___layout) {
343
- // this.component.___layout()
344
- // }
378
+ if (this.config.parent.props && this.config.parent.props.__layout === true) {
379
+ this.config.parent.triggerLayout(this.config.parent.props)
380
+ }
345
381
  },
346
382
  animate(prop, value, transition) {
347
383
  // check if a transition is already scheduled to run on the same prop
@@ -393,6 +429,12 @@ const Element = {
393
429
  })
394
430
  }
395
431
 
432
+ if (this.config.parent.props && this.config.parent.props.__layout === true) {
433
+ f.on('tick', () => {
434
+ this.config.parent.triggerLayout(this.config.parent.props)
435
+ })
436
+ }
437
+
396
438
  if (transition.progress !== undefined && typeof transition.progress === 'function') {
397
439
  let prevProgress = 0
398
440
  f.on('tick', (_node, { progress }) => {
@@ -414,9 +414,6 @@ const generateForLoopCode = function (templateObject, parent) {
414
414
  `)
415
415
  }
416
416
  })
417
- // if(elms[${forStartCounter}][0] && elms[${forStartCounter}][0].forComponent && elms[${forStartCounter}][0].forComponent.___layout) {
418
- // elms[${forStartCounter}][0].forComponent.___layout()
419
- // }
420
417
  ctx.renderCode.push(`
421
418
  }
422
419
  }`)
@@ -498,11 +495,15 @@ const generateCode = function (templateObject, parent = false, options = {}) {
498
495
  if (
499
496
  childTemplateObject[Symbol.for('componentType')] === 'Element' ||
500
497
  childTemplateObject[Symbol.for('componentType')] === 'Slot' ||
501
- childTemplateObject[Symbol.for('componentType')] === 'Text'
498
+ childTemplateObject[Symbol.for('componentType')] === 'Text' ||
499
+ childTemplateObject[Symbol.for('componentType')] === 'Layout'
502
500
  ) {
503
501
  if (childTemplateObject[Symbol.for('componentType')] === 'Text') {
504
502
  childTemplateObject.__textnode = 'true'
505
503
  }
504
+ if (childTemplateObject[Symbol.for('componentType')] === 'Layout') {
505
+ childTemplateObject.__layout = 'true'
506
+ }
506
507
  generateElementCode.call(this, childTemplateObject, parent, options)
507
508
  } else {
508
509
  generateComponentCode.call(this, childTemplateObject, parent, options)
@@ -1,50 +0,0 @@
1
- import Component from '../component.js'
2
-
3
- export default () => {
4
- return Component('Layout', {
5
- /* eslint-disable no-unused-vars */
6
- code: {
7
- render: function anonymous(parent, component, context, components, effect, getRaw) {
8
- const elms = []
9
- let componentType
10
- const rootComponent = component
11
- let propData
12
- const elementConfig0 = {}
13
-
14
- elms[0] = this.element({ parent: parent || 'root' }, component)
15
-
16
- elementConfig0[Symbol.for('isSlot')] = true
17
- elementConfig0['ref'] = 'slot'
18
- elms[0].populate(elementConfig0)
19
- return elms
20
- },
21
- effects: [],
22
- context: {},
23
- },
24
- props: [
25
- 'direction',
26
- {
27
- key: 'gap',
28
- default: 0,
29
- },
30
- ],
31
- hooks: {
32
- ready() {
33
- this.___layout()
34
- },
35
- },
36
- methods: {
37
- // todo: review naming
38
- ___layout() {
39
- let offset = 0
40
-
41
- this.$select('slot').children.forEach((el) => {
42
- el.set(this.direction === 'vertical' ? 'y' : 'x', offset)
43
- // todo: grab width from interface, not directly from node
44
- offset +=
45
- (el.node[this.direction === 'vertical' ? 'height' : 'width'] || 0) + (this.gap || 0)
46
- })
47
- },
48
- },
49
- })
50
- }
@@ -1,34 +0,0 @@
1
- import Component from '../component.js'
2
-
3
- export default () => {
4
- return Component('Layout', {
5
- template: `
6
- <Slot ref="slot" />
7
- `,
8
- props: [
9
- 'direction',
10
- {
11
- key: 'gap',
12
- default: 0,
13
- },
14
- ],
15
- hooks: {
16
- ready() {
17
- this.___layout()
18
- },
19
- },
20
- methods: {
21
- // todo: review naming
22
- ___layout() {
23
- let offset = 0
24
-
25
- this.$select('slot').children.forEach((el) => {
26
- el.set(this.direction === 'vertical' ? 'y' : 'x', offset)
27
- // todo: grab width from interface, not directly from node
28
- offset +=
29
- (el.node[this.direction === 'vertical' ? 'height' : 'width'] || 0) + (this.gap || 0)
30
- })
31
- },
32
- },
33
- })
34
- }