Skip to content

Instantly share code, notes, and snippets.

@adityanaag3
Created March 25, 2021 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adityanaag3/62e4aecc718db9e9aefb7a9d0d8ede8d to your computer and use it in GitHub Desktop.
Save adityanaag3/62e4aecc718db9e9aefb7a9d0d8ede8d to your computer and use it in GitHub Desktop.
Style LWC by assigning the value of a JS Public Property to a CSS Custom Property
lightning-card{
--my-color: black;
}
div{
padding: 0.5rem;
margin-bottom: 0.5rem;
}
.myClass {
color: var(--my-color);
}
.myOtherClass {
border: 1px solid;
border-color: var(--my-color);
}
.myBadge{
--sds-c-badge-color-background: var(--my-color);
}
<template>
<lightning-card title="JS Properties in CSS">
<div class="container">
<div class="myClass">
Some colored text.
</div>
</div>
<div class="myOtherClass">
Text with border
</div>
<div class="myBadge">
<lightning-badge label="Badge Base Component">
</lightning-badge>
</div>
</lightning-card>
</template>
import { LightningElement, api } from "lwc";
export default class JsInCss extends LightningElement {
@api customColor;
renderedCallback() {
this.template
.querySelector("lightning-card")
.style.setProperty("--my-color", this.customColor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment