As we build a product and go through sprint after sprint,
we run into situations where we want the same component
to use a different tag,
a different style,
or different attributes.
The more sprints we go through, the more I realize I should design for extensibility from the start.
The images below are actual examples from our product.

We have buttons with various variations like these.
Clicking the cart button runs an action related to addToCart.
Clicking the login button takes you to the login page.
—> Considering semantic tags, the cart button should be a button, while the login button should be an a tag.
But they share the same button UI.
How can we handle semantic UI more intelligently?
Here’s how the existing code was written.
const FilledButton = styled(Button)<FillButtonProps>`
display: flex;
align-items: center;
justify-content: center;
height: auto;
padding: 9px 16px;
border-radius: 6px;
font-weight: 600;
letter-spacing: -0.14px;
&.ant-btn-text:disabled {
background-color: ${(props) => props.theme.new.colors.sol_gray_100} !important;
color: ${(props) => props.theme.new.colors.sol_gray_300} !important;
}
&${(props) => (props.variant === ButtonVariants.solid ? '.ant-btn-text' : '.unavailable')} {
color: ${(props) => props.theme.new.colors.white};
&.active,
&:hover {
background-color: ${(props) => props.theme.new.colors.sol_indigo_600};
color: ${(props) => props.theme.new.colors.white};
}
}
&${(props) => (props.variant === ButtonVariants.soft ? '.ant-btn-text' : '.unavailable')} {
background-color: ${(props) => props.theme.new.colors.sol_indigo_100};
color: ${(props) => props.theme.new.colors.sol_indigo_600};
&.active,
&:hover {
background-color: ${(props) => props.theme.new.colors.sol_indigo_200};
color: ${(props) => props.theme.new.colors.sol_indigo_600};
}
}
&${(props) => (props.variant === ButtonVariants.outlined ? '.ant-btn-text' : '.unavailable')} {
color: ${(props) => props.theme.new.colors.sol_indigo_500};
border: 1px solid ${(props) => props.theme.new.colors.sol_indigo_500};
&.active,
&:hover {
background-color: ${(props) => props.theme.new.colors.sol_indigo_50};
color: ${(props) => props.theme.new.colors.sol_indigo_500};
}
}
`
export default FilledButton
const FilledLink = styled(Link)<FillLinkProps>`
display: flex;
align-items: center;
justify-content: center;
height: auto;
padding: 9px 16px;
border-radius: 6px;
font-weight: 600;
letter-spacing: -0.14px;
&.disabled {
background-color: ${(props) => props.theme.new.colors.sol_gray_100} !important;
color: ${(props) => props.theme.new.colors.sol_gray_300} !important;
}
${(props) =>
props.variant === LinkVariants.solid &&
css`
background-color: ${(props) => props.theme.new.colors.sol_indigo_500};
color: ${(props) => props.theme.new.colors.white};
&.active,
&:hover {
background-color: ${(props) => props.theme.new.colors.sol_indigo_600};
color: ${(props) => props.theme.new.colors.white};
}
`}
${(props) =>
props.variant === LinkVariants.soft &&
css`
background-color: ${(props) => props.theme.new.colors.sol_indigo_100};
color: ${(props) => props.theme.new.colors.sol_indigo_600};
&.active,
&:hover {
background-color: ${(props) => props.theme.new.colors.sol_indigo_200};
color: ${(props) => props.theme.new.colors.sol_indigo_600};
}
`}
${(props) =>
props.variant === LinkVariants.outlined &&
css`
color: ${(props) => props.theme.new.colors.sol_indigo_500};
border: 1px solid ${(props) => props.theme.new.colors.sol_indigo_500};
&.active,
&:hover {
background-color: ${(props) => props.theme.new.colors.sol_indigo_50};
color: ${(props) => props.theme.new.colors.sol_indigo_500};
}
`}
`
export default FilledLink
We created separate FilledLink and FilledButton components.
- This approach has the following drawbacks.
- Because they look the same, people mostly used FilledButton instead of FilledLink, or wrapped FilledButton in an a tag.
- The same styles exist in two places, making design changes difficult to handle.
Polymorphic UI components
To sum it up, these are components that can have:
- Various semantic tags
- Various attributes
- Various styles
→ A component can become any element and use any attribute.
For example, this is a common pattern in MUI. Let’s look at its Button component.
// MUI Button component
<Button
variant="outlined"
size="small"
label={slots.libraryReviewButton.text}
endIcon={<ArrowRight1_5px />}
href={slots.libraryReviewButton.link} // Adding href changes it to an a tag.
target="_blank"
onClick={handleClickPromotionButton}
/>
How does it work?
export const Button = ({ as, ...props }) => {
const Element = as || "button";
return <Element style={{ backgroundColor: "red" }} {...props} />;
};
→ The element itself is injected through props.
Let’s apply this to our existing FilledButton
type ViewProps<T extends React.ElementType> = {
as?: T;
} & React.ComponentPropsWithoutRef<T>;
type ViewComponent = <C extends React.ElementType = "div">(
props: ViewProps<C> & {
ref?: React.ComponentPropsWithRef<C>["ref"];
}
) => React.ReactElement | null;
// Create a broadly reusable View component.
export const View: ViewComponent = forwardRef(
<T extends React.ElementType = "div">(
{ as, ...props }: ViewProps<T>,
ref: React.ComponentPropsWithRef<T>["ref"]
) => {
const Element = as || "div";
return <Element ref={ref} {...props} />;
}
);
// Set button as the default for View to create a polymorphic component.
// To add styles, use a styled View or apply styled to the button.
export const FilledButton = ({ as, ...props }) => {
return (
// Use the View component created above.
<View as={as || 'button'}
{...props}
/>
);
}
// Alternatively, use as directly inside FilledButton.
// If this pattern is used repeatedly, I think extracting a View module improves maintainability.
interface FilledButtonProps<T extends React.ElementType> {
as?: T
[key: string]: any
}
export const FilledButton = <T extends React.ElementType = 'button'>({ as, ...props }: FilledButtonProps<T>) => {
const Element = as || Button //mui button
return <Element {...props} />
}
- Problems I encountered while applying it to FilledButton
- Type inference is difficult when using it with other UI libraries such as MUI.
- Children sometimes failed to render when props weren’t specified correctly.
- Getting the types right seems to be the key. Whatever children are passed in, they need to receive props without problems.
- Other drawbacks
- Ambiguity around using as.
- Render delegation is a pattern that can address these drawbacks.
- https://kciter.so/posts/render-delegation-react-component/
Ultimately, types were the biggest barrier to using this pattern. Depending on the type configuration, children sometimes weren’t returned. For details on setting up the types, see Composition - Material UI and https://blog.logrocket.com/build-strongly-typed-polymorphic-components-react-typescript/ These pages explain it in detail.
References