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

[styled-engine][styled-engine-sc] Add support for tagged template styles with tags #36403

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/mui-styled-engine-sc/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export default function styled(tag, options) {
return stylesFactory;
}

Object.keys(scStyled).forEach((tag) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to update the type of the styled utility too, to extend ThemedStyledComponentFactories<Theme>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure which type needs to be updated, but if you let me know, I can make that change!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to how the types for the @mui/styled-engine are updated. The changes would need to be made in packages/mui-styled-engine-sc/src/index.d.ts

styled[tag] = scStyled[tag];
});

// eslint-disable-next-line @typescript-eslint/naming-convention
export const internal_processStyles = (tag, processor) => {
// Styled-components attaches an instance to `componentStyle`.
Expand Down
5 changes: 5 additions & 0 deletions packages/mui-styled-engine-sc/src/styled.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { expect } from 'chai';
import scStyled from 'styled-components';
import { createRenderer } from 'test/utils';
import styled from '@mui/styled-engine-sc';

Expand Down Expand Up @@ -39,6 +40,10 @@ describe('styled', () => {
expect(typeof styled('span').attrs).to.equal('undefined');
});

it('has primitive', () => {
expect(styled.div).to.equal(scStyled.div);
});

// The babel-plugin-styled-components depends on the withConfig option to be defined
it("should allow styled-components's APIs: .withConfig", () => {
expect(typeof styled('span').withConfig).to.equal('function');
Expand Down
14 changes: 12 additions & 2 deletions packages/mui-styled-engine/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as CSS from 'csstype';
import { StyledComponent, StyledOptions } from '@emotion/styled';
import { StyledComponent, StyledOptions, StyledTags as EmotionStyledTags } from '@emotion/styled';
import { PropsOf } from '@emotion/react';

export * from '@emotion/styled';
Expand Down Expand Up @@ -136,11 +136,21 @@ export interface CreateStyledComponent<
): StyledComponent<ComponentProps & AdditionalProps, SpecificComponentProps, JSXProps>;
}

export type StyledTags<Theme> = {
[Tag in keyof JSX.IntrinsicElements]: CreateStyledComponent<
{
theme?: Theme;
as?: React.ElementType;
},
JSX.IntrinsicElements[Tag]
>;
};

export interface CreateMUIStyled<
MUIStyledCommonProps extends {},
MuiStyledOptions,
Theme extends object,
> {
> extends StyledTags<Theme> {
<
C extends React.ComponentClass<React.ComponentProps<C>>,
ForwardedProps extends keyof React.ComponentProps<C> = keyof React.ComponentProps<C>,
Expand Down
4 changes: 4 additions & 0 deletions packages/mui-styled-engine/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export default function styled(tag, options) {
return stylesFactory;
}

Object.keys(emStyled).forEach((tag) => {
styled[tag] = emStyled[tag];
});

// eslint-disable-next-line @typescript-eslint/naming-convention
export const internal_processStyles = (tag, processor) => {
// Emotion attaches all the styles as `__emotion_styles`.
Expand Down
5 changes: 5 additions & 0 deletions packages/mui-styled-engine/src/styled.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import emStyled from '@emotion/styled';
import styled from './index';

describe('styled', () => {
Expand All @@ -11,4 +12,8 @@ describe('styled', () => {
styled('span')(undefined, { color: 'red' });
}).toErrorDev('MUI: the styled("span")(...args) API requires all its args to be defined');
});

it('has primitive', () => {
expect(styled.div).to.equal(emStyled.div);
});
});