Themeable international component library
Definition and purpose
A component library defines a small stable styling contract: markup, semantic tokens, supported states, and layout in both text directions. Build a notice component whose theme is inherited from a container instead of duplicating every selector.
Light LTR notice
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> @layer reset, theme, components; @layer reset { * { box-sizing: border-box; } body { margin: 0; font: 16px/1.5 system-ui, sans-serif; } } @layer theme { :root { --surface: white; --ink: #172033; --accent: #1d4ed8; --action-ink: white; --space: 16px; } [data-theme="dark"] { --surface: #172033; --ink: white; --accent: #93c5fd; --action-ink: #172033; } } @layer components { .notice { background: var(--surface); color: var(--ink); display: flex; flex-wrap: wrap; align-items: center; gap: 12px; padding: var(--space); border-inline-start: 4px solid var(--accent); border-block: 1px solid currentColor; } .notice__action { margin-inline-start: auto; background: var(--accent); color: var(--action-ink); border: 1px solid currentColor; padding-inline: 12px; min-block-size: 44px; font: inherit; } .notice__action:focus-visible { outline: 3px solid currentColor; outline-offset: 2px; } .notice__action:disabled { background: var(--surface); color: var(--ink); } } </style> </head> <body> <section id="a" class="notice" lang="en" dir="ltr"> <span>New message</span> <button class="notice__action" id="act">Open</button> </section> </body></html>The accent border is on the left and the button moves toward the inline end.
Syntax and mental model
Order normal author layers as reset, theme, components. The theme supplies semantic surface, ink, accent and action-ink tokens; the component consumes them without knowing whether the palette is light or dark. The logical start border and auto inline-start margin follow direction from HTML dir; bdi isolates the Latin identifier inside Persian text. Keep the button a native button, including the disabled state, and use a visible focus indicator. Transfer task: create a story sheet with light/LTR, light/RTL, dark/LTR and dark/RTL cards, a disabled button, a long translation, and nested theme containers. Document allowed markup, tokens, defaults and expected states. Tab through enabled buttons and confirm the disabled button is skipped; inspect contrast in both palettes, including focus. Do not rely on the token names as evidence of contrast. Try an outer dark container and an inner light override, explicitly defining all palette tokens for the light override. Deliver the stylesheet, story sheet and component contract.
Dark RTL notice
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> @layer reset, theme, components; @layer reset { * { box-sizing: border-box; } body { margin: 0; font: 16px/1.5 system-ui, sans-serif; } } @layer theme { :root { --surface: white; --ink: #172033; --accent: #1d4ed8; --action-ink: white; --space: 16px; } [data-theme="dark"] { --surface: #172033; --ink: white; --accent: #93c5fd; --action-ink: #172033; } } @layer components { .notice { background: var(--surface); color: var(--ink); display: flex; flex-wrap: wrap; align-items: center; gap: 12px; padding: var(--space); border-inline-start: 4px solid var(--accent); border-block: 1px solid currentColor; } .notice__action { margin-inline-start: auto; background: var(--accent); color: var(--action-ink); border: 1px solid currentColor; padding-inline: 12px; min-block-size: 44px; font: inherit; } .notice__action:focus-visible { outline: 3px solid currentColor; outline-offset: 2px; } .notice__action:disabled { background: var(--surface); color: var(--ink); } } </style> </head> <body> <section id="a" class="notice" lang="fa" dir="rtl" data-theme="dark"> <span> پیام جدید <bdi>CSS-42</bdi> </span> <button class="notice__action" id="act">باز کردن</button> </section> </body></html>The same component rules mirror the border and change the palette; the markup supplies direction and language.
Common mistakes
Physical left/right rules can put decoration on the wrong side in RTL. Do not use a colored div in place of a button, and do not make all theme rules unlayered: normal unlayered styles outrank normal layered styles. Resetting only one inherited token can produce an unreadable mixed theme.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Responsive multi-page site. Next lesson: Animation and progressive enhancement project.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Themeable international component library — knowledge check
Five questions on themeable international component library.
Where should a component get palette values?
Which rule mirrors the accent edge in RTL?
Why use bdi around a Latin identifier?
What wins among normal author declarations: unlayered or layered?
What belongs in a component contract?