The core of Battery is the propertyConfig. These objects configure Battery to recognize valid class names from your Atomic CSS library and then convert them into full CSS classes. Here's an example of a propertyConfig for background-color.
const backgroundColor = {
property: "background-color",
propertyIdentifier: "bg",
valueSeparator: "-",
values: {
black: "#000000",
white: "#FFFFFF",
transparent: "transparent"
}
};The code example below is an extremely simplified version how Battery converts the contents of a propertyConfig into CSS.
Object.entries(backgroundColor.values).forEach(([valueIdentifier, value]) => {
const { propertyIdentifier, valueSeparator, property } = backgroundColor;
const className = `${propertyIdentifier}${valueSeparator}${valueIdentifier}`;
const cssClass = `.${className} { ${property}: ${value} }`;
console.log(cssClass);
});// Output
.bg-black { background-color: #000000 }
.bg-white { background-color: #FFFFFF }
.bg-transparent { background-color: transparent }There are several other properties in our library that use colors. Below we'll create propertyConfigs for each of them.
const textColor = {
property: "color",
propertyIdentifier: "text",
valueSeparator: "-",
values: {
black: "#000000",
white: "#FFFFFF",
transparent: "transparent"
}
};const fillColor = {
property: "fill",
propertyIdentifier: "fill",
valueSeparator: "-",
values: {
black: "#000000",
white: "#FFFFFF",
transparent: "transparent"
}
};With these propertyConfigs Battery can now recognize and generate the following classes:
const input = [
"bg-black",
"bg-white",
"bg-transparent",
"text-black",
"text-white",
"text-transparent",
"fill-black",
"fill-white",
"fill-transparent"
];// Output
.bg-black { background-color: #000000 }
.bg-white { background-color: #FFFFFF }
.bg-transparent { background-color: transparent }
.text-black { color: #000000 }
.text-white { color: #FFFFFF }
.text-transparent { color: transparent }
.fill-black { fill: #000000 }
.fill-white { fill: #FFFFFF }
.fill-transparent { fill: transparent }The propertyConfigs for background-color, color and fill above work, but they introduce a lot of duplication. In the next section we'll learn how to DRY this code up by using Battery's plugins.