lwc icon indicating copy to clipboard operation
lwc copied to clipboard

Refactor `renderer.getClassList()` into `renderer.addClass()`, `renderer.removeClass()`

Open caridy opened this issue 3 years ago • 5 comments

Function setScopeTokenClassIfNecessary calls getClassList(elm) on an element, and assumes the shape of the result value to be a DOM Object and calls .add() on it. Probably we should have a renderer api for just the add operation itself in one go.

caridy avatar Mar 24 '22 23:03 caridy

This issue has been linked to a new work item: W-11568519

git2gus[bot] avatar Aug 08 '22 23:08 git2gus[bot]

Rather than exposing the entire classList:

https://github.com/salesforce/lwc/blob/fda8d6f3d800ab5b883a88f086ab7656169f0d65/packages/%40lwc/engine-core/src/framework/renderer.ts#L45

... the renderer should have hooks to add or remove classes.

nolanlawson avatar Aug 08 '22 23:08 nolanlawson

I'm not sure this is entirely possible, because of this code:

https://github.com/salesforce/lwc/blob/2ddff92f4cb7c223e63a1ad791ae6b9bf2d66a42/packages/%40lwc/engine-core/src/framework/base-lightning-element.ts#L449-L466

We need to expose this.classList inside of a LightningElement, and to do that we need to have a getClassList on the renderer that returns the entire DOMTokenList.

nolanlawson avatar Aug 22 '22 23:08 nolanlawson

Sure, but that doesn't mean we use that value to interact with, we just pass it along... then we can have new APIs just for the diffing. Basically, we have two separate buckets, APIs used for rendering, and APIs used for the element instance.

caridy avatar Aug 23 '22 13:08 caridy

@caridy What is the benefit in that case? We would have the following APIs on the renderer:

  • getClassList
  • addClass
  • removeClass
  • containsClass
  • classListLength

(This is based on the APIs we actually use in the engine.)

If we expose getClassList anyway, it's not clear to me why we should have the other APIs as well.

nolanlawson avatar Aug 23 '22 16:08 nolanlawson