LWC Base Components Have Problems

Join Crudspace - Manage Salesforce Records, Permissions & Metadata in One Place

Problem with Lightning Base Components

Lightning-based components are great at building Salesforce UI, but what happens when you want to build a UI that doesn't look like Salesforce? Its extremely difficult.

I have been working on Lightning Web Components development since 2018, the time when it was initially launched. 

Over the years there were many instances where I had to take the SLDS blueprint and re-build a base component from scratch.

LWC gives options to compose components using slots, expose the properties, and design tokens. Lightning Base Components don't utilize the slots as much as it should be. That's why customizing the base lightning components is difficult. You need custom development in some cases, and creating genuinely custom components with custom design still requires a lot of CSS and markup work.

[However there is a legit reason for why those components were built in black box manner, at the beginning the LWC framework wasn't mature and developers of base library had to use a lot of work-arounds for simple things like dynamic components.]

If you are working with LWR OSS, like building an independent website using Lightning Web Components, it is notoriously complicated, and there are so many quirks around using the base components in the LWR OSS. 

That's why I created Fandry UI, an opinionated component design system layer on top of the LWC framework. It is not actually meant  to replace the Lightning Base Components, but it has a different use case - "building a custom UI that doesn't look like Salesforce". 

The base lightning components are black box by design, you cannot see what is the behavior inside the component other than the properties that are exposed by the library. Overriding the default behavior of the Lightning Base Components is very difficult. 

That's why, while building Fandry UI, my main goal was to give preference for extension and composition over properties. Working with components which has 10 or 20 different properties is difficult, but simple components which can be extended or overridden with the help of slots, parts, and CSS token. 

The idea is composition rather than dozens of configuration properties. 


Example: Styling fandry button with Parts

Let's see a simple example, how I have applied this composition and extensibility over the properties and blackbox components. There are around 40 base components on the Fandry UI. Let's check the button component. If you go to the button component, this is what Fandry ships out of the box.

Screenshot of Base Button Fandry UI

 
It is pretty simple and basic stuff. But imagine you want to give this button a different look. In that case you want your website to look like your brand, so how does Fandry UI gives you ability to change the behavior and the look and feel of the components?

See this screenshot:

Screenshot of Stylized fandry ui buttons

This is very good use case for building external experience sites on top of Salesforce, like D2C or B2B website which are selling some stuff online.

These aren't different components built from scratch, but its the same base button. That is stylized with the help of Parts. 

The part is a standard browser API that exposes the internal elements so that they can be styled out of the shadow boundary, by the parent/ancestor components.

You can see the full example code here: fandry button component, Customization is pretty straightforward with Fandry UI. The library exposes parts so that they can be styled outside the shadow DOM i.e wrapper/caller component.

Fandry uses native shadow DOM, not the synthetic shadow DOM unlike Lightning Base Components. Styling these components is easy, just the CSS. Best part is that you still retain the default implementation and behavior of the component.

You know it's not easy to get that rainbow button out of the standard Lightning Button!


Example: Changing behavior with slots

How can you change the behavior of the button? Fandry UI exposes relevant slots for each primitive component, they have built-in "batteries-included" default behavior. Which can be overwritten with the help of slots.

Changing Fandry UI button behavior with slots


If you click on the button from the Example: A loading state, with a slot section. It changes the state to "Saving", the label reverts to "Save" once the saving is completed. The change was simple. I just used the same Fandry button component, but with a slot inside that, and added a slotted Fandry spinner.

The spinner is displayed whenever the is saving is true. No blueprint needed, no need to build the component from ground up.


Architecture of the Fandry UI vs Lightning Base Components

Image - Architecture of the Fandry UI


Lightning Base Components

Now let's talk about the architecture of the library. See the Lightning-base-components section. Lightning-base-components provide three ways to customize those components. The first and the most flexible way is to rebuild component from scratch using the SLDS blueprint. 

This means you handle all the events, behaviors, all the accessibility and everything in your custom implementation.

Then the second way of extend customizing those components was using properties, but this was limited because you can customize only the properties that are exposed by the base component library.

And, third is the SLDS design token, which can only change the appearance and it still has some limitations for animation and some CSS props. 

Lightning base component gives you enterprise stability and they are optimized for building the standard Salesforce UI but they are closed for extension and customization.


Fandry UI

Now, let's see the Fandry UI, it uses a different approach, and provides three different ways to customize the components. 

First is using parts: You just saw the button example, like how the button can be styled differently using the parts. 

Second thing is slots. Slots are basically extending or overriding the behavior of the component, like swapping the entire HTML markup inside a component. 

And third is tokens. Tokens are very similar to the SLDS design token, but they are also used for theming.

One more option with the Fandry UI is that the component are "source available". You can download the code for those base components, customize, and own it. If you need heavy customization and flexibility, similar to rebuilding the component from SLDS blueprint. 

What are the downsides of the Fandry UI? It is still in the developmental phase and limited features. It is not as stable as the Lightning base components. But I'm continuously working on the library to make it stable. 

Do you see any use cases where a library like Fandry UI can be used?


Related



No comments :
Post a Comment

Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com