Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass empty names option to Sheet when using StyleSheetManager with custom target #3159

Merged
merged 4 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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