Skip to content

Two plugins with the same name produce the same class names, so one subtree gets the other's CSS #5828

Closed

Description

@quantizor

Class names fold in the plugin configuration so that a component under different plugins gets different classes. Each plugin contributes only its name to that hash, so two different plugins that share a name hash identically. Whichever subtree renders first decides the CSS for both. This happens on the client, in classic server rendering, and in server components.

Minimal repro (main, plugins prop):

import styled, { StyleSheetManager } from 'styled-components';

const upper = { name: 'p', decl: (prop, value) => ({ prop, value: value.toUpperCase() }) };
const lower = { name: 'p', decl: (prop, value) => ({ prop, value: value.toLowerCase() }) };

const Box = styled.div`
  color: Red;
`;

<>
  <StyleSheetManager plugins={[upper]}><Box>A</Box></StyleSheetManager>
  <StyleSheetManager plugins={[lower]}><Box>B</Box></StyleSheetManager>
</>;

On 6.x the same happens with stylisPlugins and two middleware functions that share a name.

Expected: each subtree's Box gets its own class with its own plugin's output (color:RED and color:red).

Actual: both get the same class and a single rule, color:RED.

Related on 6.x: a plugin with no name is supposed to throw error 15, but the error is created and never thrown, so anonymous plugins are accepted silently. Two inline arrow-function plugins then share a class with each other and with the same component rendered without plugins. On main the error throws as intended.

A fix would derive the plugin part of the hash from something unique per plugin rather than its name, or warn in development when two different plugins in use share a name.

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

Metadata

Metadata

Assignees

No one assigned

    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