Giter Club home page Giter Club logo

lit-grid-layout's People

Contributors

zsarnett avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

lit-grid-layout's Issues

Deeper integration with lit-element

First of all, thank you for creating this library and expanding the lit-element ecosystem.

I would like to discuss a problem I found trying to integrate this library into a lit-element project and the solution that we found to said problem.

lit-grid-layout loads elements from this.items or form this.children

get _childrenElements(): LayoutItemElement[] {
return this.items.concat(
...Array.prototype.filter.call(this.children, (e: LayoutItemElement) =>
e.classList.contains("grid-item")
)
);
}

Then, the elements are attached to the DOM here, which forcefully detaches them from their previous position in the DOM.

>
${element}
</lit-grid-item>

So if this.children is used, the elements will be detached from this.children in order to attach them under lit-grid-item.

This makes lit-element usage problematic. Let's say that the parent is in charge of rendering and passing the elements to lit-grid-layout,

  html`
    <lit-grid-layout
      .layout="this.layout">
      ${this.gridItems.map(gridItem => {
        return html`<div>${gridItem.title}</div>`;
      })}
    </lit-grid-layout>
  `;

Now when the parent re-renders, it finds that children of lit-grid-layout are missing. So lit-html cannot patch those nodes and has to create and initialize new nodes. Not only is this more expensive, but since the new nodes replace the old nodes, the internal state of the old nodes just disappears.

To improve reactivity and integration I thought about taking another approach. The idea is to delegate rendering to lit-grid-layout and avoid having to detach elements from a DOM position to attach them in another place.

              <lit-grid-layout
                .layout="${this.layout}"
                .dragHandle="${dragHandle}"
                .sortStyle="${sortStyle}"
                .itemRenderer="${this.itemRenderer}"
                @layout-changed="${this.onLayoutChanged}"
                @item-changed="${this.onLayoutChanged}">
              </lit-grid-layout>

The usage is similar, except that there are no this.items or children. Instead itemRenderer is used to create the elements from within lit-grid-layout and thanks to the repeat directive of lit-html re-renders will find old elements and patch them.

        itemRenderer (itemKey) {
          return html`<my-item
              title="${itemKey}">
            </my-item>`;
        }

Take a look at the demo of this working branch

  1. npm run start and go to 127.0.0.1:8000
  2. Click on the title/number of some items to activate them. They will turn green.
  3. Click on "Switch to sunny mode" or "New Layout"

Notice also that code style becomes more friendly to lit-element and lit-html.

Demo not working

When going to the demo page I get this error:

Uncaught TypeError: Failed to resolve module specifier "lit-element". Relative references must start with either "/", "./", or "../".

Checked on Chrome 84.0.4147.105 (Win10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.