Skip to content

Commit

Permalink
Pass empty names option to Sheet when using StyleSheetManager with …
Browse files Browse the repository at this point in the history
…custom target (#3159)

* Reset the names Map when reconstructing Sheet with custom target

* Modify StyleSheetManager test to catch previous bug

* add failing test

* Update CHANGELOG.md
  • Loading branch information
eramdam committed Jul 29, 2020
1 parent 1311e17 commit b19d17f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ _The format is based on [Keep a Changelog](http://keepachangelog.com/) and this

## Unreleased

### Bugfixes

- Make sure `StyleSheetManager` renders all styles in iframe / child windows (see [#3159](https://github.com/styled-components/styled-components/pull/3159)) thanks @eramdam!

## [v5.1.1] - 2020-04-07

### New Functionality
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-components/src/models/StyleSheetManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function StyleSheetManager(props: Props) {
// eslint-disable-next-line prefer-destructuring
sheet = props.sheet;
} else if (props.target) {
sheet = sheet.reconstructWithOptions({ target: props.target });
sheet = sheet.reconstructWithOptions({ target: props.target }, false);
}

if (props.disableCSSOMInjection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,62 @@ describe('StyleSheetManager', () => {
div.parentElement.removeChild(div);
});

// https://github.com/styled-components/styled-components/issues/2973
it('should inject common styles into both the main document and a child frame', async () => {
const CommonTitle = styled.h1`
color: palevioletred;
`;

// Injects the stylesheet into the document available via context
const SheetInjector = ({ children, target }) => (
<StyleSheetManager target={target}>{children}</StyleSheetManager>
);

class Main extends React.Component {
componentDidMount() {
const styles = this.props.document.querySelector('style').textContent;
expect(styles.includes('palevioletred')).toEqual(true);
}

render() {
return this.props.children;
}
}

class Child extends React.Component {
componentDidMount() {
const styles = this.props.document.querySelector('style').textContent;
expect(styles.includes(`palevioletred`)).toEqual(true);
}

render() {
return <CommonTitle />;
}
}

const div = document.body.appendChild(document.createElement('div'));

render(
<Main document={document}>
<div>
<CommonTitle />
<Frame>
<FrameContextConsumer>
{({ document }) => (
<SheetInjector target={document.head}>
<Child document={document} />
</SheetInjector>
)}
</FrameContextConsumer>
</Frame>
</div>
</Main>,
div
);

div.parentElement.removeChild(div);
});

it('should render styles in correct order when styled(StyledComponent) and StyleSheetManager is used', () => {
const Red = styled.div`
color: red;
Expand Down
8 changes: 6 additions & 2 deletions packages/styled-components/src/sheet/Sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ export default class StyleSheet implements Sheet {
}
}

reconstructWithOptions(options: SheetConstructorArgs) {
return new StyleSheet({ ...this.options, ...options }, this.gs, this.names);
reconstructWithOptions(options: SheetConstructorArgs, withNames?: boolean = true) {
return new StyleSheet(
{ ...this.options, ...options },
this.gs,
(withNames && this.names) || undefined
);
}

allocateGSInstance(id: string) {
Expand Down

0 comments on commit b19d17f

Please sign in to comment.