UmbLitElement vs UmbElementMixin(LitElement) πŸ₯Š
# package-development
s
Quick question folks - what's the difference between extending
UmbElementMixin(LitElement)
and
UmbLitElement
? Should I be doing one or another in my package?
I have both in my code, mainly because I've been looking at a few different packages as well as core to figure out how to do things πŸ‘€ I dunno if there's a situation you have to use one or the other
l
According to CoPilot: Extending your component with UmbElementMixin(LitElement) wraps LitElement with additional Umbraco-specific functionality. For example, the mixin provides built‑in support for consuming context (with methods like consumeContext) and observing context changes, which helps integrate your component into the Umbraco backoffice ecosystem. If you simply extend LitElement, you'd lose those helper methods and integration points, requiring you to implement that functionality manually. I can confirm that if I change UmbElementMixin(LitElement) to simply LitElement, functions and properties like consumeContext and localize stop working. So I guess CoPilot is right.
s
Yes, that is correct for LitElement, but I am talking about UmbLitElement
I have switched some classes from UmbLitElement to UmbElementMixin(LitElement) and vice versa, without any noticeable changes
l
Ah ok. According to CoPilot (again): *The short answer is that UmbElementMixin is a mixin function that augments LitElement with additional Umbraco functionality, while UmbLitElement is a higher-level base class that bundles that mixin (and possibly other common functionality) so you don’t have to apply it manually each time. In other words, extending: UmbElementMixin(LitElement) β€’ Gives you explicit control over how mixins are applied. β€’ You can combine it with other mixins or change the order if needed. UmbLitElement β€’ Provides a preconfigured component class that already includes the mixin functionality. β€’ It’s a convenience base class for when you simply want the default Umbraco element behavior without extra customization. The choice depends on whether you need the flexibility of composing your own mixin chain or prefer using a ready-to-go base component.* So I guess we need someone to confirm if that's correct πŸ˜›
But if it's true, I guess using UmbLitElement in most cases would make sense
s
Huge if true
πŸ˜‚
Makes sense mostly
l
I think this would make a good forum post. That way other people can find it easier and it gets indexed by search engines
l
If CoPilot is correct, both should do exactly the same I guess πŸ˜„
s
So I was wondering if it's best to use one or the other in certain situations
l
I mostly use UmbElementMixin(LitElement), but only because I did it that way once and stuck with it. But if you don't do anything special, UmbLitElement would do the same I guess? With this knowledge I'd probably would have used just UmbLitElement
j
UmbElementMixin(LitElement)
is two imports, whereas
UmbLitElement
is only one import πŸ˜‰ Joking aside, they do the same. The former gives you more control if you wanted to use something else than Lit, and the latter is just a magic box fixing your trouble.
UmbLitElement was initially not exported because it was only thought to be an internal wrapper, but we decided to make it easy for people. Kudos for ChatGPT for giving a somewhat nuanced and mostly correct answer, though...
s
Thanks Jacob, I've switched over to
UmbLitElement
across my codebase, one less import is nice as they can stack up..
I think it was a team effort, ChatGPT + Luuk
CoPilot even
l
Hehe thanks
Github copilot to be exact πŸ˜†
j
πŸ˜„
2 Views