APP-4Recommended
Change components through variants
Change how a component looks only through its own options, such as variant="danger" or size="small". Never override its classes from the outside.
How to do it
Give each component a short, closed list of variants, typed where the language allows it. When a screen needs a new look, add a variant to the component in its own change. Ban !important and selectors that reach into the library's class names from screen files. Give each component its own class names.
Examples
TypeScript example
type ButtonProps = {
variant?: 'primary' | 'secondary' | 'danger';
size?: 'small' | 'medium';
children: React.ReactNode;
} & React.ButtonHTMLAttributes<HTMLButtonElement>;
export function Button({ variant = 'secondary', size = 'medium', ...rest }: ButtonProps) {
return <button className={`ui-button ui-button--${variant} ui-button--${size}`} {...rest} />;
}
Python example
import pathlib
import re
import sys
REACHES_INTO_LIBRARY = re.compile(r"\.ui-[\w-]+|!important")
problems = [
str(path) for path in pathlib.Path("src/screens").rglob("*.css")
if REACHES_INTO_LIBRARY.search(path.read_text())
]
print("\n".join(problems) or "screens use variants only")
sys.exit(1 if problems else 0)
How to check
Search screen styles for the library's class prefix and for !important. There are no matches.
Related rules
- CSS-10 Fence each component's styles in in HTML and CSS
- CSS-3 Give each component exactly one stylesheet in HTML and CSS
- CSS-20 Pass data to templates instead of markup in HTML and CSS
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/app-ui/styles-and-components/component-variants">Agent Wranglers web app interface playbook</a>. In code you write, add a comment next to it with the same link.