Designing Lightning Components for Reuse
This is a living document about a common sense approach to developing reusable Lightning components. It might change over time.
As well as the instance specific component library
- Components shall serve a single purpose, designed for reusability
- Components shall use the most feasible least code approach
- Components shall not contain country specific logic in the front-end
- Components shall be documented and tested
- Components shall use composition over inheritance. Inheritance is NOT forbidden, use it wisely
- Components shall observe case sensitivity even for non case sensitive item (e.g. field names)
- Components shall prefer component markup over html markup (e.g.
- Components shall use component navigation (if navigation is needed)
Related files and components need to be named so they appear close to each other. E.g. a component "VehicleInfoList" that depends on inner components. Those would also start with "VehicleInfo" e.g. "VehicleInfoCard" "VehicleInfoLineItem", "VehicleInfoInterested" etc.
Files should be named like this:
- A component shall only implement the interfaces that it actually uses.
- A component that relies on a current record, shall not use "availableForAllPageTypes" and must implement "hasRecordId" and the attribute "recordId".
- Components that are not used on a page layout, but rather inside other components shall not implement interfaces ("availableFor?") that make them appear in the page editor
- Components shall only implement the interfaces they actually use. Avoid interfaces the component "might use in future"
Components shall use the "least code" principles for data access. To be checked in this sequence:
- Does the component need data access or can the attributes of it provide all the input it requires?
- Can lightning:recordForm be used?
- Can lightning:recordEditForm and lightning:recordReadForm be used?
- Can force:recordData be used?
- Is a custom @AuraEnabled method in the controller needed for data provision?
This doesn't preclude fetching Meta data or configuration. Ensure to use storable actions where feasible. More principles:
- Use data change handlers where appropriate
- Use component events
This section probably will expand over time
- Code needs to be readable
- The controllers (both client and server side) shall be light modules that marshall actual work to helper classes and helper functions
- In Apex helper classes shall be instantiated using factory classes - this allows intoducing country specific behavior
- All Apex helper classes shall be based on Interfaces
- Methods and functions shall be single purpose and not exceed a page size. Break them down (makes them more testable anyway) if to big
- Don't copy/paste
- Operations that can fail need to be handled with try/catch or its equivalent
- Use @ApexDoc and @JSDoc style comments
- All components need test code: both for Apex (natural) and the client side component.
- A component is incomplete without a "Lightning testing service" test.
- Use assertions generously!
- Lightning components have a description
- Each lightning component comes with a documentation section - don't waste time documenting them outside Salesforce.
- Use the documentation to briefly explain what it does (no Pulitzer price for this writing!).
- Include at least one example in the documentation
- Components that can be dragged onto a page can benefit from having parameters the page maintainer can use to configure the component, thus increasing reusability and limit the number of components that need to show up in the palette.
- Parameter documentation - Check the documentation for details.
- If a component is usable only for a specific object page, add that to the Design Resource.
As usual YMMV