Vanilla Extract made me realize that applying design tokens with sprinkles was repetitive while building a design system. Each time, css.ts file had to be created, and Tailwind could make applying styles based on props more efficient.
In this process, I started thinking about a highly extensible way to pass styles as props, and while looking for patterns in various UI libraries to solve this problem, Polymorphic components — an interesting concept.
Box Using this component, as I was able to render various HTML elements via the prop while also passing design tokens as props based on sprinkles.
Through this, I believed I could apply Vanilla Extract's design tokens more flexibly and type-safely.
The following headless UI libraries already used Polymorphic components.
So, Polymorphic what exactly is a component?
What is a Polymorphic Component?
Polymorphic means having multiple forms.
In React, a Polymorphic component refers to a component that can be rendered as different HTML elements or components depending on the props it receives.
In other words, it can be seen as the most abstracted form of a component that can transform into any other component.
<Box as="h1">Heading</Box>
<Box as="p">Paragraph</Box>
<Box as="button">Button</Box>
✔ Expressing various elements with a single component
→ <h1>, <p>, <button> and other elements can be reused with the same style
✔ Minimize unnecessary code repetition
→ No need to write the same styles redundantly in multiple places, managed with a consistent component
✔ Flexible style extension
→ Maintain a semantic HTML structure while applying styles according to the design system
Basic Polymorphic Component
A basic polymorphic component changes its rendered element using the as prop.
const Box = <C extends ElementType = 'div'>({ as, ...props }: { as?: C }) => {
const Component = as || 'div';
return <Component {...props} />;
};
<Box as="button" onClick={() => alert('Clicked!')}>Button</Box>
This method is simple, but lacks type safety.
Box as="button" should support onClick when used, but it's not guaranteed by the current type system.
To solve this, Polymorphic types must be defined.
Implementing Polymorphic Types
To implement type-safe Polymorphic types, the following factors must be considered.
asprop should correctly change the HTML element.- It must also support the default properties of that element.
- e.g.,
buttonshould allowonClickwhen used
- e.g.,
refmust function correctly.asEven if the prop changes,refshould be correctly connected to the element
- In TypeScript,
asshould only allow attributes that match the prop.
Importing Base Types
First, import the necessary React types.
import type {
ComponentPropsWithRef,
ComponentPropsWithoutRef,
ElementType,
PropsWithChildren,
} from 'react';Let's look at why these types are needed:
ElementType: Represents HTML tag or React component typesComponentPropsWithRef: Props type of a component, including refComponentPropsWithoutRef: Props type of a component, excluding refPropsWithChildren: Type including the children prop
Defining PolymorphicRef Type
First, define the ref type.
export type PolymorphicRef<C extends ElementType> = ComponentPropsWithRef<C>['ref'];// Ref type for a button
type ButtonRef = PolymorphicRef<'button'>;
// Ref type for a div
type DivRef = PolymorphicRef<'div'>;
// Example usage
const buttonRef: ButtonRef = useRef<HTMLButtonElement>(null);This type is necessary to extract the ref type of any HTML element.
Defining AsProp Type
Next, define the type of the as prop.
export type AsProp<C extends ElementType> = {
as?: C;
};as prop is optional and restricted to only accept HTML element or React component types.
Through this as props type, HTML elements can be changed. It's a simple but important type.
Usage Example
// Prop type for rendering as a button
type ButtonAsProp = AsProp<'button'>;
// { as?: 'button' }
// Inside the component
const Component = ({ as: Tag = 'div' }: AsProp<'div' | 'button'>) => {
return <Tag>Content</Tag>;
};Defining PropsToOmit Utility Type
Create a utility type to remove duplicate prop keys in a component.
Keys of props to omit
export type PropsToOmit<C extends ElementType, P> = keyof (AsProp<C> & P);Usage Example
// 1. Native HTML button props
interface HTMLButtonProps {
onClick: (e: MouseEvent) => void;
className: string;
type: 'submit' | 'button' | 'reset';
disabled?: boolean;
// ... Other props
}
// 2. Custom button props
type CustomButtonProps = {
variant: 'primary' | 'secondary'; // New prop
onClick: () => void; // ⚠️ Overlaps with native HTML button props
className: string; // ⚠️ Overlaps with native HTML button props
}type ButtonPropsToOmit = PropsToOmit<'button', CustomButtonProps>;
// Result: "as" | "onClick" | "className" | "variant"In other words, PropsToOmit is a type that tells us which props to exclude from an HTML button and use our own.
PolymorphicComponentProp
Now, using PropsToOmit and AsProp, we create PolymorphicComponentProp.
Create the props type for a basic polymorphic component without ref.
export type PolymorphicComponentProp<
C extends ElementType,
Props = Record<string, unknown>
> = PropsWithChildren<Props & AsProp<C>> &
Omit<ComponentPropsWithoutRef<C>, PropsToOmit<C, Props>>;
PropsWithChildren<Props & AsProp<C>>:childrenandasprop including propsOmit<ComponentPropsWithoutRef<C>, PropsToOmit<C, Props>>: Remove duplicate props
- Combine custom props and as prop
- Add children prop
- Remove duplicates from HTML element's default props
type CustomButtonProps = {
variant: 'primary' | 'secondary';
};
type ButtonComponentProps = PolymorphicComponentProp<'button', CustomButtonProps>;
PolymorphicComponentPropWithRef
Add the ref prop to the base PolymorphicComponentProp type.
export type PolymorphicComponentPropWithRef<
C extends ElementType,
Props = Record<string, unknown>
> = PolymorphicComponentProp<C, Props> & { ref?: PolymorphicRef<C> };
Now, it becomes the final type that supports ref.
Full Code
import type {
ComponentPropsWithRef,
ComponentPropsWithoutRef,
ElementType,
PropsWithChildren,
} from 'react';
/**
* Define the ref type for a polymorphic component
* @template C - Default HTML element type of the component
*/
export type PolymorphicRef<C extends ElementType> =
ComponentPropsWithRef<C>['ref'];
/**
* Define the type of the 'as' prop
* @template C - Element type to render
*/
export type AsProp<C extends ElementType> = {
as?: C;
};
/**
* Utility type for removing overlapping prop keys
* @template C - Default HTML element type of the component
* @template P - Additional component props
*/
export type PropsToOmit<C extends ElementType, P> = keyof (AsProp<C> & P);
/**
* Props for a polymorphic component without a ref
* @template C - Default HTML element type of the component
* @template Props - Additional component props
*/
export type PolymorphicComponentProp<
C extends ElementType,
Props = Record<string, unknown>,
> = PropsWithChildren<Props & AsProp<C>> &
Omit<ComponentPropsWithoutRef<C>, PropsToOmit<C, Props>>;
/**
* Props for a polymorphic component with a ref
* @template C - Default HTML element type of the component
* @template Props - Additional component props
*/
export type PolymorphicComponentPropWithRef<
C extends ElementType,
Props = Record<string, unknown>,
> = PolymorphicComponentProp<C, Props> & { ref?: PolymorphicRef<C> };
Final Box Component Implementation
Now that all types are ready, we can create a Polymorphic Box component as an example.
import { PolymorphicComponentPropWithRef, PolymorphicRef } from './types'
const Box = forwardRef(<C extends ElementType = 'div'>(
props: PolymorphicComponentPropWithRef<C, { color?: string } accelerators
ref?: PolymorphicRef<C>
) => {
const { as: Component = 'div', color, children, ...rest } = props;
return (
<Component
ref={ref}
style={{ color }}
{...rest}
>
{children}
</Component>
);
});
This implementation allows us to create a type-safe and highly reusable Box Polymorphic component.
// Usage
<Box>Default text</Box>
<Box as="h1" color="red">Red heading</Box>
<Box as="button" onClick={() => alert('Clicked!')}>Button</Box>
Now that we've created the Box component, let's try using Vanilla Extract's design tokens as props, similar to Tailwind.
Extending Styles by Combining with Vanilla Extract
To combine Polymorphic components with Vanilla Extract, I referred to the method proposed by Sandro Roth.
The operating principle is as follows:
- When the
Boxcomponent receivesprops extractAtomsseparates style-relatedpropssprinklesgenerates CSS classes corresponding to thoseprops- The generated classes are applied to the component
This way, while maintaining Vanilla Extract's type safety, styles can be applied intuitively, like Tailwind.
Configuring Sprinkles
First, I created a sprinkles file. Although more tokens were needed in reality, I only wrote the basic ones for this example.
import { createSprinkles, defineProperties } from '@vanilla-extract/sprinkles';
const responsiveProperties = defineProperties({
conditions: {
mobile: {},
tablet: { '@media': 'screen and (min-width: 768px)' },
desktop: { '@media': 'screen and (min-width: 1024px)' },
},
defaultCondition: 'mobile',
properties: {
display: ['none', 'flex', 'block', 'inline'],
flexDirection: ['row', 'column'],
padding: {
none: '0',
small: '8px',
medium: '16px',
large: '24px',
},
margin: {
none: '0',
small: '8px',
medium: '16px',
large: '24px',
},
},
shorthands: {
p: ['padding'],
m: ['margin'],
}
});
const baseProperties = defineProperties({
properties: {
cursor: ['default', 'pointer'],
borderRadius: {
none: '0',
small: '4px',
medium: '8px',
large: '16px',
}
}
});
// 3. Create the Sprinkles function
export const sprinkles = createSprinkles(
responsiveProperties,
baseProperties
);
// 4. Extract the type
export type Sprinkles = Parameters<typeof sprinkles>[0];
Separating style props with extractAtoms
Create an atoms.ts file to define utility functions that can utilize Sprinkles.
const keys = Array.from(sprinkles.properties.keys());
export const extractAtoms = <P extends object>(props: P) => [
pick(props, keys),
omit(props, keys),
];
sprinkles.properties.keys()
- Retrieves all style property keys defined in sprinkles.
- In the example code,
['display', 'flexDirection', 'padding', 'margin', 'cursor', 'borderRadius']can be retrieved.
extractAtoms function
- Separates component props into style props and general props, returning them as a tuple.
- Uses generics to handle props of any type.
Create the generateClassNames function.
function generateClassNames(atoms: Atoms) {
const { reset, className, ...rest } = atoms;
const sprinklesClassNames = sprinkles(rest);
return clsx(
sprinklesClassNames,
className,
reset ? [elementResets[reset]] : null,
);
}reset: Key to reset the default styles of an HTML element (e.g., 'button', 'input')className: Custom class directly passed by the userrest: Remaining style properties (properties to be handled by sprinkles)
const sprinklesClassNames = sprinkles(rest) generates atomic classes.
Combining classes with clsx
return clsx(
sprinklesClassNames, // Classes generated by Sprinkles
className, // Custom class
reset ? [elementResets[reset]] : null // Reset class, if present
);
generateClassNames({
reset: 'button',
className: 'custom-button',
padding: 'medium',
margin: 'large'
});
// Result: 'sprinkles_padding_medium sprinkles_margin_large custom-button button-reset'
atoms function
export function atoms(atoms: Atoms) {
return generateClassNames(atoms);
}
- Hides the internal implementation (
generateClassNames) and provides a simple public API. Encapsulation. - Generates class names while ensuring type safety.
Full Code
import { elementResets, sprinkles } from '../styles';
import clsx from 'clsx';
import omit from 'lodash/omit';
import pick from 'lodash/pick';
import type { Atoms } from '../types/atoms';
const keys = Array.from(sprinkles.properties.keys());
export const extractAtoms = <P extends object>(props: P) => [
pick(props, keys),
omit(props, keys),
];
function generateClassNames(atoms: Atoms) {
const { reset, className, ...rest } = atoms;
const sprinklesClassNames = sprinkles(rest);
return clsx(
sprinklesClassNames,
className,
reset ? [elementResets[reset]] : null,
);
}
export function atoms(atoms: Atoms) {
return generateClassNames(atoms);
}
Applying Sprinkles to the Box Component
Now, let's introduce it in a polymorphic component.
import {
type ElementType,
type HTMLAttributes,
type ReactNode,
forwardRef,
} from 'react';
import type { Atoms } from './atoms';
import type { PolymorphicComponentPropWithRef } from './types/polymorhpic';
import { atoms, extractAtoms } from './utils/atoms';
type HTMLProperties = Omit<
HTMLAttributes<HTMLElement>,
'className' | 'color' | 'height' | 'width' | 'size'
>;
export type BoxProps<C extends ElementType = 'div'> =
PolymorphicComponentPropWithRef<C, HTMLProperties & Atoms>;
type BoxComponent = <C extends ElementType = 'div'>(
props: BoxProps<C>,
) => ReactNode;
export const Box: BoxComponent = forwardRef<HTMLElement, BoxProps<ElementType>>(
({ as, ...restProps }, ref) => {
const [atomsProps, propsToForward] = extractAtoms(restProps);
const Component: ElementType = as || 'div';
const className = atoms({
className: propsToForward?.className,
reset: typeof Component === 'string' ? Component : 'div',
...atomsProps,
});
return <Component {...propsToForward} className={className} ref={ref} />;
},
);
Let's examine the code piece by piece:
type HTMLProperties = Omit<
HTMLAttributes<HTMLElement>,
'className' | 'color' | 'height' | 'width' | 'size'
>;
Exclude attributes that may conflict with Atoms from HTML base properties
Box Props Type Definition
export type BoxProps<C extends ElementType = 'div'> =
PolymorphicComponentPropWithRef<C, HTMLProperties & Atoms>;
- Combine HTML attributes and Atoms attributes
- Includes ref type
Managing the Type System
Here, I encountered the first challenge. If the type system is not properly managed, props auto-completion doesn't work.
It was especially important to explicitly define the BoxComponent type.
If the type of BoxComponent is not specified, auto-completion for props will not work.
// 1. Define the BoxComponent type
type BoxComponent = <C extends ElementType = 'div'>(
props: BoxProps<C>,
) => ReactNode;
// 2. Apply the type annotation to Box
export const Box: BoxComponent = forwardRef(
<C extends ElementType = 'div'>(
{ as, ...restProps }: BoxProps<C>,
ref?: PolymorphicRef<C>
) => {
// ... Implementation
}
);
This way, generic types can be preserved.
// Without the BoxComponent type:
const Box = forwardRef(...)
// ⚠️ The generic is fixed to a specific type
// Props are not inferred correctly
// With the BoxComponent type:
const Box: BoxComponent = forwardRef(...)
// ✅ The generic type is preserved
// Props are inferred correctly
Sprinkles Props Type Inference
// Without the BoxComponent type:
<Box
padding={} // ⚠️ No autocomplete
margin={} // ⚠️ Valid values are not inferred
/>
// With the BoxComponent type:
<Box
padding="medium" // ✅ Autocomplete shows valid values
margin={['small', 'medium']} // ✅ Responsive values are inferred correctly
/>
as prop type inference
// Accurate inference with the BoxComponent type:
<Box as="button" onClick={() => {}} /> // ✅ Button props are inferred correctly
<Box as="a" href="/home" /> // ✅ Anchor props are inferred correctly
<Box as={CustomComponent} customProp="" /> // ✅ Custom component props are inferred
// ✅ All prop types are inferred correctly
<Box
as="button"
padding="medium"
margin={['small', 'medium']}
display={{ mobile: 'block', desktop: 'flex' }}
onClick={(e: React.MouseEvent) => {}}
>
Button
</Box>
// ✅ Custom component props are also inferred correctly
interface CustomProps {
custom: string;
}
const Custom = ({ custom }: CustomProps) => <div>{custom}</div>;
<Box
as={Custom}
custom="value" // ✅ CustomProps type check
padding="medium"
>
Custom
</Box>
The type of BoxComponent must be explicitly defined for type inference like the following to work.
- Generic type preservation
- Sprinkles props type inference
- Accurate props type inference based on as prop
- Improve DX (autocomplete, type checking)
Extending Button Component using Box
import { type ButtonHTMLAttributes, type ReactNode, forwardRef } from 'react';
import ClipLoader from 'react-spinners/ClipLoader';
import { Box } from '..';
import type { AtomProps } from './types/atoms';
import * as styles from './Button.css';
// 1. Define props specifically for Button
export interface ButtonProps
extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'prefix' | 'color'>,
AtomProps {
variant?: 'primary' | 'secondary' | 'ghost' | 'outline';
size?: 'sm' | 'md' | 'lg' | 'zero';
selected?: boolean;
prefix?: ReactNode;
suffix?: ReactNode;
loading?: boolean;
}
// 2. Implement the Button component
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
(
{
variant = 'primary',
size = 'md',
selected = false,
disabled = false,
prefix,
suffix,
loading = false,
children,
...restProps
},
ref,
) => {
return (
<Box // Render Box as a button element
as='button'
className={styles.button({
variant,
size,
disabled,
loading,
selected,
})}
disabled={disabled || loading}
aria-pressed={selected}
aria-busy={loading}
ref={ref}
{...restProps}
>
{loading && <ClipLoader color={'#0142C0'} size={14} role='status' />}
{prefix && prefix}
{children}
{suffix && suffix}
</Box>
);
},
);
The Button component implemented this way:
Box's style props can all be used as-isButton's unique features (loading, prefix/suffix, etc.) have been added- Maintains perfect type safety
- ARIA attributes are also handled automatically

Conclusion
Changes after application
Adopting the Polymorphic component pattern brought about several positive changes.
- The first thing I noticed was improved developer experience. The code became much more intuitive, and thanks to TypeScript's powerful type system, stability was also ensured. In particular, the reusability of components increased, allowing us to leverage existing code extensively when adding new features.
- From a design system perspective, there were also significant advantages. Design tokens could be used consistently, and Box components could be added easily. Most importantly, style extension became flexible, allowing quick responses to design-change requests.
- As a result, overall productivity significantly improved. I wrote less repetitive code and spent less time building components. In the long run, easier maintenance brought even greater value.
Remaining Challenges
Of course, there is still much room for improvement. Going forward, I plan to focus on the following areas:
- Enhance accessibility (a11y): Aim to provide a better user experience by automating ARIA attributes and improving keyboard interaction
- Performance optimization: Will improve bundle size and rendering performance to ensure faster loading speeds
- Documentation improvement: Plan to integrate Storybook and write detailed usage guides to increase team adoption
- Strengthen testing: Will expand unit and E2E tests to further enhance stability
Implementing Polymorphic components was not easy, but I learned a lot from it. In particular, I realized the importance of in-depth TypeScript usage and component design.
Initially, I started with the simple idea of wanting to use Vanilla Extract as conveniently as Tailwind. As a result, I discovered greater value. This pattern was particularly useful in building the design system. Component reusability increased, and development productivity significantly improved.
I hope this experience can be of some help to developers facing similar challenges. The journey to create a better design system will continue.
References
