Skip to content

Commit

Permalink
fix(ui-virtualization): fix empty array edge cases (#1759)
Browse files Browse the repository at this point in the history
better & more stable overall handling of the virtualization.
  • Loading branch information
Lakerfield committed May 12, 2023
1 parent 7a71d43 commit 7a2f17f
Show file tree
Hide file tree
Showing 40 changed files with 676 additions and 409 deletions.
14 changes: 12 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ executors:
docker:
- image: "cimg/node:<< parameters.node >>-browsers"



docker_release:
working_directory: *working_dir
resource_class: large
Expand Down Expand Up @@ -845,6 +843,12 @@ workflows:
path: "packages/__e2e__/i18n"
requires:
- build_release
- e2e_test:
<<: *filter_ignore_develop_release
name: e2e_virtual_repeat
path: "packages/__e2e__/ui-virtualization"
requires:
- build_release
- tacho_benchmark_prep:
<<: *filter_ignore_develop_release
requires:
Expand Down Expand Up @@ -996,6 +1000,12 @@ workflows:
path: "packages/__e2e__/i18n"
requires:
- build_release
- e2e_test:
<<: *filter_ignore_develop_release
name: e2e_virtual_repeat
path: "packages/__e2e__/ui-virtualization"
requires:
- build_release

# - lint_packages:
# <<: *filter_only_tag
Expand Down
1 change: 1 addition & 0 deletions docs/user-docs/TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
* [Logging](getting-to-know-aurelia/logging.md)
* [Building plugins](developer-guides/building-plugins.md)
* [Web Components](developer-guides/web-components.md)
* [UI virtualization](developer-guides/ui-virtualization.md)
* [Errors](developer-guides/error-messages/README.md)
* [0001 to 0015](developer-guides/error-messages/0001-to-0015/README.md)
* [AUR0001](developer-guides/error-messages/0001-to-0015/aur0001.md)
Expand Down
102 changes: 102 additions & 0 deletions docs/user-docs/developer-guides/ui-virtualization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

## Installation

{% hint style="info" %}
This plugin is in development, expect unstability.
{% endhint %}

Install via npm

```
npm install @aurelia/ui-virtualization
```

Load the plugin

```javascript
import { Aurelia } from 'aurelia';
import { DefaultVirtualizationConfiguration } from '@aurelia/ui-virtualization';

Aurelia
.register(DefaultVirtualizationConfiguration)
.app(...)
```

## Using the plugin

Simply bind an array to `virtual-repeat` like you would with the standard `repeat`. The repeated rows are expected to have equal height throughout the list, and one item per row.

### div
```html
<template>
<div virtual-repeat.for="item of items">
${$index} ${item}
</div>
</template>
```

### list
```html
<template>
<ul>
<li virtual-repeat.for="item of items">
${$index} ${item}
</li>
</ul>
</template>
```

### table
```html
<template>
<table>
<tr virtual-repeat.for="item of items">
<td>${$index}</td>
<td>${item}</td>
</tr>
</table>
</template>
```

```javascript
export class MyVirtualList {
items = ['Foo', 'Bar', 'Baz'];
}
```

With a surrounding fixed height container with overflow scroll. Note that `overflow: scroll` styling is inlined on the elemenet. It can also be applied from CSS.
An error will be thrown if no ancestor element with style `overflow: scroll` is found.

## Caveats

1. `<template/>` is not supported as root element of a virtual repeat template. This is due to the difficulty of technique employed: item height needs to be calculatable. With `<tempate/>`, there is no easy and performant way to acquire this value.
2. Similar to (1), other template controllers cannot be used in conjunction with `virtual-repeat`, unlike `repeat`. I.e: built-in template controllers: `with`, `if`, etc... cannot be used with `virtual-repeat`. This can be workaround'd by nesting other template controllers inside the repeated element, with `<template/>` element, for example:

```html
<template>
<h1>${message}</h1>
<div virtual-repeat.for="person of persons">
<template with.bind="person">
${Name}
</template>
</div>
</template>
```
3. Beware of CSS selector `:nth-child` and similar selectors. Virtualization requires appropriate removing and inserting visible items, based on scroll position. This means DOM elements order will not stay the same, thus creating unexpected `:nth-child` CSS selector behavior. To work around this, you can use contextual properties `$index`, `$odd`, `$even` etc... to determine an item position, and apply CSS classes/styles against it, like the following example:

```html
<template>
<div virtual-repeat.for="person of persons" class="${$odd ? 'odd' : 'even'}-row">
${person.name}
</div>
</template>
```
4. Similar to (3), virtualization requires appropriate removing and inserting visible items, so not all views will have their lifecycle invoked repeatedly. Rather, their binding contexts will be updated accordingly when the virtual repeat reuses the view and view model. To work around this, you can have your components work in a reactive way, which is natural in an Aurelia application. An example is to handle changes in change handler callback.

## Tobe implemented feature list

- [ ] ability to configure height & many aspects of the virtual repeat
- [ ] infinite scroll
- [ ] horizontal scrolling
- [ ] variable height
- [ ] scrolling direction
2 changes: 1 addition & 1 deletion examples/realworld-advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"css-loader": "^6.7.1",
"html-webpack-plugin": "^5.5.0",
"mocha": "10.0.0",
"playwright": "^1.21.1",
"playwright": "^1.33.0",
"rimraf": "^3.0.0",
"style-loader": "^3.3.1",
"ts-loader": "^9.3.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/realworld-conventions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"css-loader": "^6.7.1",
"html-webpack-plugin": "^5.5.0",
"mocha": "10.0.0",
"playwright": "^1.21.1",
"playwright": "^1.33.0",
"rimraf": "^3.0.0",
"style-loader": "^3.3.1",
"ts-loader": "^9.3.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/realworld/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"css-loader": "^6.7.1",
"html-webpack-plugin": "^5.5.0",
"mocha": "10.0.0",
"playwright": "^1.21.1",
"playwright": "^1.33.0",
"rimraf": "^3.0.0",
"style-loader": "^3.3.1",
"ts-loader": "^9.3.0",
Expand Down

0 comments on commit 7a2f17f

Please sign in to comment.