Skip to content

as prop drops parent styles in styled-component chains when using Preact (works in React, works in SC v5) #5736

Closed

Description

@rbrochot

First, a quick question: is Preact officially supported? I couldn't find a clear answer in the docs. We're running a production app that uses Preact and styled-components, and we've hit a regression migrating from v5 to v6.

Environment

  • styled-components: 6.4.1 (broken) / 5.3.11 (works)
  • preact: 10.29.1
  • vite: 5.4.1
  • Node: 22.22.0

Also tested with latest React — both versions of styled-components work fine with it.

Reproduction

import React from "react";
import styled from "styled-components";

const Box = styled.div`
  background: "#222";
  color: "#fff";
  padding: "2px 6px";
`;

const Box1 = styled(Box)`
  padding: 20px;
  margin: 8px 0;
  background: #c8e6ff;
  border: 4px solid #0055cc;
`;

const Box2 = styled(Box1)`
  border-color: #cc0077;
  background: #e6660b;
`;

const Box3 = styled(Box2)`
  color: darkblue;
  font-weight: bold;
`;

export default function App() {
  return (
    <div>
      <h1>styled-components v6 — <code>as</code> prop in chains</h1>

      <h2>1 extension + as="section" (✓ works)</h2>
      <Box1 as="section">Content</Box1>

      <h2>2 extensions + as="section" (❌ broken: Box1 styles missing)</h2>
      <Box2 as="section">Content</Box2>

      <h2>3 extensions + as="section" (❌ broken: Box1 and Box2 styles missing)</h2>
      <Box3 as="section">Content</Box3>

      <h2>3 extensions WITHOUT as="section" (✓ works: all styles applied)</h2>
      <Box3>Content</Box3>
    </div>
  );
}

Setup uses @preact/preset-vite which aliases react → preact/compat.

Steps to reproduce

  1. Set up a Vite project with preact and styled-components@6, using @preact/preset-vite
  2. Create a styled-component chain of 2+ extensions (styled(styled(styled.div)))
  3. Render the outermost component with as="section" (or any valid tag)
  4. Styles from intermediate chain levels are not applied

Here are screenshots for SC5 and SC6 on Preact:

working on SC5

Image

SC6 drops intermediate styles

Image

Expected behavior

All styles from every level of the chain are applied, regardless of whether as is used — consistent with the behavior without as, and with styled-components v5.

Actual behavior

When as is passed to a component that extends 2 or more styled-components, styles from the intermediate chain levels are dropped. Without as, the same component renders correctly with all styles applied.

This regression does not occur with styled-components v5 (tested on 5.3.11) or without Preact (plain React is unaffected).

Workaround

Instead of relying on as to change the root element, use a custom renderAs prop on the underlying component and pass it explicitly:

const Box = ({ renderAs = "div", className, children }) =>
  React.createElement(renderAs, { className }, children);

const Box3 = styled(styled(styled(Box))`...`)`...`;

// ❌ broken — `as` is consumed by styled-components, Box never receives it
<Box3 as="section" />

// ✓ workaround — renderAs is a plain prop, not intercepted by SC
<Box3 renderAs="section" />

Activity

  1. self-assigned this
    on May 14, 2026
  2. quantizor commented on May 14, 2026

    @quantizor
    Contributor

    Could you try [email protected] when you get a chance?

  3. rbrochot commented on May 18, 2026

    @rbrochot
    Author

    Looks good to me with [email protected]

    Thanks a lot, for this issue, and your work in general :)

  4. quantizor commented on May 19, 2026

    @quantizor
    Contributor

    Fix released with 6.4.2 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions