Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
17.4. Build class names from variant options
Medium
Topics
This question is in
17. DOM: changing the page and eventsA design system's Button takes options like size, tone and block, and each option value maps to some classes. Some classes only apply to a combination ("a large danger button gets a stronger shadow"). Instead of if statements in every component, the mapping is written once as a config, and a helper turns the props of each render into the class string.
Write variants(config) in variants.js. It returns a function that takes props and returns the class names.
Signature
function variants(config) // returns (props) => string
config = {
base: "btn", // optional: always first
variants: { // option name -> value -> classes
size: { sm: "btn-sm", md: "btn-md", lg: "btn-lg" },
block: { true: "w-full", false: "" },
},
defaults: { size: "md" }, // optional
compounds: [ // optional
{ when: { size: ["md", "lg"], block: false }, class: "px-wide" },
],
};The returned function takes one props object (or nothing) and returns a string.
Requirements
variantsreturns a function. A missingbase,variants,defaultsorcompoundscounts as empty.- For each variant, in the order of
config.variants, the value is the prop when it is notundefined, otherwise the default. - A prop of
nullturns the variant off: no class, even when there is a default. - Values are looked up as strings, so
true,falseand numbers find the keys"true","false"and"2". - A variant that has a
"false"key and gets no prop and no default counts asfalse. - A value that is not one of the variant's own keys (including
"constructor"or"toString") throws aTypeErrorwhose message names the variant. This also applies to a bad default, when it is used. - Props that are not variants (other than
classandclassName) are ignored. A variant may have any name,"__proto__"included, and works the same way in props, defaults and compounds. - Each compound applies when every condition in
whenmatches the resolved value (after defaults and rule 5). An array in a condition means any of its values. A variant that is off matches no condition. - The order is:
base, the variant classes, the matching compounds in config order, thenprops.classandprops.className. - Class strings may hold several names; the result has each name once (at its first position), separated by single spaces.
- Calls are independent, and neither the config nor the props are changed.
Examples
const button = variants({
base: "btn",
variants: {
size: { sm: "btn-sm", md: "btn-md", lg: "btn-lg" },
tone: { neutral: "btn-neutral", danger: "btn-danger" },
},
defaults: { size: "md", tone: "neutral" },
compounds: [{ when: { tone: "danger", size: ["md", "lg"] }, class: "shadow-danger" }],
});
button(); // "btn btn-md btn-neutral"
button({ tone: "danger" }); // "btn btn-md btn-danger shadow-danger"
button({ size: "sm", class: "mt-2" }); // "btn btn-sm btn-neutral mt-2"
button({ size: null }); // "btn btn-neutral"const panel = variants({ variants: { open: { true: "is-open", false: "is-closed" } } });
panel(); // "is-closed": unset counts as false
panel({ open: true }); // "is-open"button({ size: "xl" }); // throws TypeError: Unknown size "xl" …Environment
- Plain JavaScript; the tests call the global
variantsfromvariants.js. - Merging conflicting utility classes (deciding that
p-2beatsp-4) is out of scope: repeats are removed, nothing else.
Hints
- Before you look at compounds, what should you have recorded for each variant?
values[key]findstoStringon any object. Which check sees only the keys written in the config?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it
Back to the question
Loading the editor…