Last active
May 7, 2020 20:55
-
-
Save loktevich/292e27404786136c9f80302041b5a207 to your computer and use it in GitHub Desktop.
LWC. Interesting approach to style standard LWC components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Dark lightning datatable without header actions simplified example */ | |
/* Better to create selectors with your entire component (c-my-component) */ | |
c-my-component lightning-datatable .slds-th__action-button { | |
display: none; | |
} | |
/* Sometimes it needs to create more specific selectors as standard components may contain another componenets */ | |
c-my-component lightning-datatable th .slds-cell-fixed { | |
background-color: #333; | |
color: #fff; | |
} | |
c-my-component lightning-datatable th .slds-cell-fixed:hover { | |
color: #111; | |
} | |
c-my-component lightning-datatable .slds-table tr { | |
background-color: #555; | |
color: #fff; | |
} | |
c-my-component lightning-datatable .slds-table tr:hover { | |
color: #111; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { LightningElement, api, track } from "lwc"; | |
import myComponent from "./myComponent.css"; | |
export default class SearchResults extends LightningElement { | |
// Adds a style element (only once) | |
renderedCallBack() { | |
if (!this.hasRendered) { | |
const style = document.createElement("style"); | |
// imported myComponent contains an object with a function(t,e,s) | |
// which can return a css content | |
style.innerText = searchResults["0"](null, ""); | |
this.template | |
.querySelector("lightning-datatable") | |
.appendChild(style); | |
this.hasRendered = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Appending
style
element it's not ideal and you can break shadow DOM encapsulation. To keep advantages of shadow DOM it is better to create selectors starting from the component itself: