- From: Christian Landgren <notifications@github.com>
- Date: Thu, 22 Jun 2023 23:09:31 -0700
- To: whatwg/dom <dom@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/dom/issues/1213@github.com>
In the context of modern web development, many frameworks and libraries rely on the concept of a virtual DOM (VDOM) to manage and efficiently update the actual DOM of the web application. The ability to directly apply changes from a VDOM representation to the actual DOM in a standardized, efficient way could help in developing lighter and more performant libraries and applications. To support this, I propose the addition of a new method to the Element interface. This method would take a VDOM representation (as an object or JSON structure) and efficiently apply changes to the element and its descendants to match the provided VDOM. Each node in the VDOM could optionally include an id or key attribute. The browser would use these keys to determine which elements in the real DOM correspond to nodes in the VDOM. When synchronizing, if the method encounters a node in the VDOM that has the same key but is in a different position, it would move the corresponding element in the actual DOM to match the VDOM. If a node in the VDOM has a key that doesn’t exist in the actual DOM, the method would create a new element. If a node in the actual DOM has a key that doesn’t exist in the VDOM, the method would remove the corresponding element. Potential method names could include syncWithVDOM, applyVDOM, updateFromVDOM, or renderFromVDOM. The best choice would depend on the specific semantics and behavior of the method. For example, the usage could look like this: ```js let vdom = { tagName: 'div', attributes: { id: 'root' }, children: [ { tagName: 'h1', attributes: { key: 'heading' }, children: ['Hello, world!'] }, { tagName: 'p', attributes: { key: 'paragraph' }, children: ['Welcome to my app.'] } ] }; let element = document.querySelector('#root'); element.syncWithVDOM(vdom); ``` In this example, syncWithVDOM would ensure that the element matches the structure and content of the vdom object. If there were differences, it would make the minimum number of changes to the actual DOM to bring it into line with the vdom. This feature would not replace the need for full-fledged frameworks like React or Vue, but it would provide a useful tool for developers building lightweight applications or libraries. It could also potentially increase the performance of DOM updates by leveraging browser internals and reducing reliance on JavaScript. Please consider this feature for future improvements to the dom spec? Thank you for your attention to this matter. -- Reply to this email directly or view it on GitHub: https://github.com/whatwg/dom/issues/1213 You are receiving this because you are subscribed to this thread. Message ID: <whatwg/dom/issues/1213@github.com>
Received on Friday, 23 June 2023 06:09:36 UTC