Skip to content

Conversation

@ENvironmentSet
Copy link
Contributor

What: Let css prop attachment be distributed over union types. So that branches with className prop are extended with css prop.

Why: Some components may accept className prop conditionally. For example, Link of wouter does. Emotion skips these cases, however, and does not add css prop in anywhere. As far as I know, there is no runtime or compilation-time problem stopping us from supporting these kind of components. Thus I propose this change to enhance expressiveness of the library.

fixes #3185

How: Made LibraryManagedAttributes be distributed over a prop type in cases it is a union.

Checklist:

  • Documentation "N/A"
  • Tests
  • Code complete
  • Changeset

Should I add test cases for this change? Please let me know if you need.

@changeset-bot
Copy link

changeset-bot bot commented Aug 1, 2024

🦋 Changeset detected

Latest commit: f181c72

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@emotion/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 1, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link
Member

@Andarist Andarist left a comment

Choose a reason for hiding this comment

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

We'd need some type-level tests to take in this change

@ENvironmentSet
Copy link
Contributor Author

Indeed, I'll add some. Would you please give me a guideline to add type level tests or example to refer to? I could not find either an example of type-level test or a setting relevant to it in emotion repository.

@Andarist
Copy link
Member

Andarist commented Aug 2, 2024

@ENvironmentSet
Copy link
Contributor Author

Thanks, I added a test. Let me know if you need more.

@ENvironmentSet
Copy link
Contributor Author

It seems dtslint compares type too strictly. I'll find other ways to express validity of the implementation.

Comment on lines 247 to 253
type _HasCssPropAsIntended7 = [
// $ExpectType { foo: boolean; } | ({ css?: Interpolation<Theme>; } & { foo: number; className: string; })
EmotionJSX.LibraryManagedAttributes<
{},
{ foo: number; className: string } | { foo: boolean }
>
]
Copy link
Member

Choose a reason for hiding this comment

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

This is like a unit test. I am more interested in an integration test for this change. How did you come across this to be an issue? Maybe it was related to code like this?

type DiscriminatedClassNameSupportProp =
  | {
      variant: "foo";
      className?: string;
    }
  | {
      variant: "bar";
    };

function DiscriminatedClassNameSupportComponent(
  props: DiscriminatedClassNameSupportProp,
) {
  return null;
}

<DiscriminatedClassNameSupportComponent
  variant="foo"
  // this should be OK
  css={{ color: "hotpink" }}
/>;

<DiscriminatedClassNameSupportComponent
  variant="bar"
  // this shouldn't be OK
  css={{ color: "hotpink" }}
/>;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that was my case. May I add tests like that?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. Please replace the tests you have here with something closer to the one above :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I'll update PR tonight. Thank you for comments!

@Andarist Andarist merged commit 0ce3ed0 into emotion-js:main Aug 5, 2024
@github-actions github-actions bot mentioned this pull request Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The css prop is not attached to a type of props if it is a disjoint union type on className field.

2 participants