Skip to content

Virtual List adds padding to LitTemplate items #3900

Closed
vaadin/web-components
#4793

Description

@anne-k

Description

I'm using VirtualList with Lit items, instantiated in Java. The first time the list renders, everything looks good. But when I replace the data, every item gets a top padding equal to the item height. When scrolling, the problem goes away.

Virtual-List-padding.mov

Not shown in the video:

  • If the size of the item list is small enough that all items are retrieved at once (e.g. 20), the problem stays after scrolling, but can move to a different place in the list.
  • I can see it happens while scrolling the list normally, too, by keeping the 'elements' tab open. But then it gets corrected as soon as the list settles. The only visual effect of this is a bit of flickering.

None of this happens if I build the item view in Java, without Lit.
I don't know if it is actually caused by Lit itself or by something else that only happens when I build a component with Lit.

Expected outcome

I expect Virtual List to behave the same regardless of how the item view is built, and not add unexpected padding to the items.

Minimal reproducible example

This is adapted from the Virtual List example from the documentation.

In VirtualListBasic.java:

private final ComponentRenderer<Component, Person> litTemplatePersonCardRenderer = new ComponentRenderer<>(
		person -> {
			return new PersonCard(person);
		});
private final VirtualList<Person> list;

public VirtualListBasic() {
	this.setSizeFull();
	this.getStyle().set("overflow", "hidden");

	final Button button = new Button();
	button.setText("Refresh");
	button.addClickListener(this::onRefresh);
	add(button);

	list = new VirtualList<>();
	list.setItems(people);
	list.setRenderer(litTemplatePersonCardRenderer);
	list.setSizeFull();
	add(list);
}

private void onRefresh(final ClickEvent<Button> clickEvent) {
	list.setItems(people);
}

PersonCard.java:

@Tag("person-card")
@JsModule("./src/person-card.ts")
public class PersonCard extends LitTemplate {

	@Id("avatar")
	private Avatar avatar;

	@Id("name")
	private Element name;

	@Id("profession")
	private Div profession;

	@Id("email")
	private Div email;

	@Id("phoneNumber")
	private Div phoneNumber;

	public PersonCard(final Person person) {
		avatar.setName(person.getFullName());
		avatar.setImage(person.getPictureUrl());

		name.setText(person.getFullName());
		profession.add(new Text(person.getProfession()));
		email.add(new Text(person.getEmail()));
		phoneNumber.add(new Text(person.getAddress().getPhone()));
	}
}

person-card.ts:

@customElement('person-card')
export class PersonCard extends LitElement {
    render() {
        return html`
            <vaadin-horizontal-layout id="cardLayout" theme="margin spacing">
                <vaadin-avatar id="avatar" style="width: 64px; height: 64px;"></vaadin-avatar>
                <vaadin-vertical-layout id="infoLayout" class="p-0" style="height: 100%; width: 100%;">
                    <strong id="name"></strong>
                    <div id="profession"></div>
                    <vaadin-details>
                        <div>
                            <vaadin-vertical-layout style="width: 100%;">
                                <div id="email"></div>
                                <div id="phoneNumber"></div>
                            </vaadin-vertical-layout>
                        </div>
                        <span slot="summary">Contact information</span>
                    </vaadin-details>
                </vaadin-vertical-layout>
            </vaadin-horizontal-layout>
        `;
    }
}

Steps to reproduce

Add the code above to a project, run it, then click the 'Refresh' button. You'll see the behavior shown in the video.

Environment

Vaadin version(s): 23.1.1 (with java 11)
OS: macOS 12.3.1

Browsers

Chrome, Safari

Activity

  1. tomivirkki commented on Oct 21, 2022

    @tomivirkki
    Member

    Fix in vaadin/web-components#4793

    You can already work around the issue in your app by giving the person-card some minimum height, for example:

    export class PersonCard extends LitElement {
    
      static styles = css`
        :host {
          display: block;
          min-height: 96px;
        }
      `;
    
    ...
  2. anne-k commented on Oct 24, 2022

    @anne-k
    Author

    That was fast, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions