Skip to curriculum
Jump to the editor

17.4. Build class names from variant options

Medium
Topics
20–35 min

A 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

JAVASCRIPT
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

  1. variants returns a function. A missing base, variants, defaults or compounds counts as empty.
  2. For each variant, in the order of config.variants, the value is the prop when it is not undefined, otherwise the default.
  3. A prop of null turns the variant off: no class, even when there is a default.
  4. Values are looked up as strings, so true, false and numbers find the keys "true", "false" and "2".
  5. A variant that has a "false" key and gets no prop and no default counts as false.
  6. A value that is not one of the variant's own keys (including "constructor" or "toString") throws a TypeError whose message names the variant. This also applies to a bad default, when it is used.
  7. Props that are not variants (other than class and className) are ignored. A variant may have any name, "__proto__" included, and works the same way in props, defaults and compounds.
  8. Each compound applies when every condition in when matches 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.
  9. The order is: base, the variant classes, the matching compounds in config order, then props.class and props.className.
  10. Class strings may hold several names; the result has each name once (at its first position), separated by single spaces.
  11. Calls are independent, and neither the config nor the props are changed.

Examples

JAVASCRIPT
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"
JAVASCRIPT
const panel = variants({ variants: { open: { true: "is-open", false: "is-closed" } } });
panel();                 // "is-closed": unset counts as false
panel({ open: true });   // "is-open"
JAVASCRIPT
button({ size: "xl" });  // throws TypeError: Unknown size "xl" …

Environment

  • Plain JavaScript; the tests call the global variants from variants.js.
  • Merging conflicting utility classes (deciding that p-2 beats p-4) is out of scope: repeats are removed, nothing else.

Hints

  1. Before you look at compounds, what should you have recorded for each variant?
  2. values[key] finds toString on 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…