Connectors
CSS engine adapters — switch between Styler, styled-components, or Emotion.
Connectors are adapter packages that standardize how different CSS-in-JS libraries integrate with the Vitus Labs UI System. They allow switching between CSS engines without changing component code.
Why Connectors?
All vitus-labs components depend on @vitus-labs/core, not on a specific CSS-in-JS library. The connector system:
- Decouples components from CSS engines — write once, run with any supported engine
- Provides a unified interface — all connectors implement the same
CSSEngineConnectorshape - Enables runtime engine selection — call
init()at startup to choose your engine - Supports lazy initialization — destructured functions work before
init()is called
CSSEngineConnector Interface
Every connector exports these functions:
| Export | Type | Required | Description |
|---|---|---|---|
css | Tagged template | Yes | Create composable CSS fragments |
styled | Component factory | Yes | styled(tag)\...`` → React component |
provider | FC<{ theme, children }> | Yes | ThemeProvider component |
keyframes | Tagged template | No | Define keyframe animations |
createGlobalStyle | Tagged template | No | Create global CSS |
useTheme | () => theme | No | Access current theme |
Engine-Agnostic Result Type
css()'s return type is CSSEngineResult — an empty interface exposed by @vitus-labs/core that connectors augment via TypeScript module declaration merging. Each connector declares its concrete result shape so consumers' css`...` calls type-check against whichever engine they init()'d with, without core depending on any engine package. See Core › CSSEngineResult for details.
Available Connectors
connector-styler (Recommended)
Uses the built-in @vitus-labs/styler engine (~7.0 KB min+gz, React external) — purpose-built for the UI system.
npm install @vitus-labs/connector-stylerimport { init } from '@vitus-labs/core'
import {
css,
styled,
provider,
keyframes,
createGlobalStyle,
useTheme,
} from '@vitus-labs/connector-styler'
init({ css, styled, provider, keyframes, createGlobalStyle, useTheme })Implementation: Pure pass-through — re-exports @vitus-labs/styler functions directly (provider is styler's ThemeProvider). It additionally exports styler's useCSS hook, which the other web connectors don't provide. It also augments core's CSSEngineResult with styler's CSSResult.
Peer dependencies: @vitus-labs/core, @vitus-labs/styler, react >= 19
See Styler › What's new in 2.9 for engine behaviour changes in this release (styled(StyledComponent) folding, mount-scoped global styles, SSR keyframes).
connector-styled-components
Uses styled-components (v6+) for applications already using it.
npm install @vitus-labs/connector-styled-componentsimport { init } from '@vitus-labs/core'
import {
css,
styled,
provider,
keyframes,
createGlobalStyle,
useTheme,
} from '@vitus-labs/connector-styled-components'
init({ css, styled, provider, keyframes, createGlobalStyle, useTheme })Implementation: Pure pass-through — re-exports styled-components functions directly. Does not export useCSS (styled-components v6 has no public API to turn a css result into a standalone class name).
Peer dependencies: styled-components >= 6, @vitus-labs/core, react >= 19
connector-emotion
Uses @emotion/react + @emotion/styled with an adapter layer for CSS composition compatibility.
npm install @vitus-labs/connector-emotionimport { init } from '@vitus-labs/core'
import {
css,
styled,
provider,
keyframes,
createGlobalStyle,
useTheme,
} from '@vitus-labs/connector-emotion'
init({ css, styled, provider, keyframes, createGlobalStyle, useTheme })Implementation: Adapter wrapping Emotion's APIs to match styled-components' CSS composition pattern.
The adapter handles:
- Static templates → returns plain string (fast path, no overhead)
- Static values (no function or array interpolations) → builds string directly (second fast path)
- Dynamic templates (with function or array interpolations) → returns a
(props) => …thunk that Emotion's styled resolves at render time - Style objects (e.g. Emotion
keyframes) → kept as objects, not stringified; the result is serialized through Emotion's owncsssoanimation: ${fadeIn}works createGlobalStyle→ wraps Emotion's<Global>component to match the styled-components API, injecting the context theme so({ theme }) => …interpolations resolve (an explicitthemeprop wins)
Internally, interpolation values are resolved recursively:
null/false/true→ empty string- Functions → called with props, then recursed
- Arrays → each element recursed and concatenated
- Objects (style objects / keyframes) → passed through to Emotion
- Primitives → converted to string
The Emotion connector does not export useCSS (Emotion only inserts a serialized rule when it passes through @emotion/styled or <Global>), and it does not augment core's CSSEngineResult — css returns a string or a function, which interface merging can't describe.
Peer dependencies: @emotion/react >= 11, @emotion/styled >= 11, react >= 19
How init() Works
The init() function from @vitus-labs/core registers the CSS engine:
import { init } from '@vitus-labs/core'
init({
css, // Required: tagged template for CSS fragments
styled, // Required: component factory
provider, // Required: ThemeProvider
keyframes, // Optional: keyframe animations
createGlobalStyle, // Optional: global styles
useTheme, // Optional: theme hook
})init() also accepts the platform options component, textComponent and createMediaQueries (used by the React Native connector). It can be called more than once: a call that supplies a full engine (css + styled) resets the optional members (keyframes, createGlobalStyle, useTheme) the new engine doesn't provide, while a partial call (e.g. only component) keeps them.
Lazy Delegate Pattern
The core uses a lazy delegate pattern, so you can destructure config at module level before init() is called:
import { config } from '@vitus-labs/core'
// These work even before init() — they delegate to the engine when called
const { styled, css } = config
// Later, in your app entry:
init({ css: engineCss, styled: engineStyled, provider: engineProvider })
// Now all styled/css calls from any module use the registered engineChoosing a Connector
| Connector | Platform | Size (gzipped) | When to Use |
|---|---|---|---|
| connector-styler | Web | ~7.0 KB (engine) + ~0.2 KB (adapter) | New projects; built-in engine, no external dep |
| connector-styled-components | Web | ~17.9 KB (engine) + ~0.2 KB (adapter) | Existing styled-components codebase |
| connector-emotion | Web | ~16.6 KB (engine) + ~0.7 KB (adapter) | Existing Emotion codebase |
| connector-native | React Native | ~2 KB | React Native applications |
Sizes are minified + gzipped with React external. Engine figures for styled-components / Emotion (@emotion/react + @emotion/styled) are the same snapshot used on the styler page.
Per-scenario ops/sec numbers comparing all three engines via the in-repo bench live on the styler page.
Web Connector Compatibility
All web connectors support:
- CSS-in-CSS nesting (tagged template composition)
- Dynamic interpolations (functions receiving props)
- Theme context via provider
- Keyframes and global styles
Only connector-styler exports useCSS.
React Native
For React Native, use connector-native which parses CSS template literals into style objects instead of generating CSS class names. See the dedicated React Native connector page for details.
Switching Engines
To switch engines, just change the import and reinstall dependencies:
// Before: styled-components
import * as connector from '@vitus-labs/connector-styled-components'
// After: styler
import * as connector from '@vitus-labs/connector-styler'
// Same init call
init(connector)No component code changes required.
API Reference
CSSEngineConnector
Prop
Type
Last updated on