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

expose __unsafe_useEmotionCache from @emotion/react #2441

Merged
merged 3 commits into from Aug 8, 2021

Conversation

garronej
Copy link
Contributor

@garronej garronej commented Jul 23, 2021

What:

Expose __unsafe_useEmotionCache() from @emotion/react

Why:

Needed for seamless integration of tss-react with @emotion/react.
tss-react will be the new makeStyles in @material-ui v5.

How:

I exposed a proxy to useContext(EmotionCacheContext)

Checklist:

  • Documentation N.A? Being an interoperability feature, I don't think it has to be documented. But does it?
  • Tests
  • Code complete
  • Changeset Semantically it should be a minor but tell me if you feel that it should be a patch, I will rebase.

Hi @Andarist,
I implemented makeStyles on top of @emotion/react as you discussed with @oliviertassinari.
It's tss-react.
In v5 of material-ui makeStyle will be dropped and tss-react will be mentioned in the v4 to v5 migration guide for the teams that don't want to make the switch to the styled API.
It would be great if I could have access to the cache, tss-react have just been released and it has already been requested that it supports custom cache.
Of course I could expose my own provider but it would be much better if tss-react could blend in transparently with @emotion/react.

@changeset-bot
Copy link

changeset-bot bot commented Jul 23, 2021

🦋 Changeset detected

Latest commit: ac6b36b

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 Jul 23, 2021

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.

Latest deployment of this branch, based on commit ac6b36b:

Sandbox Source
Emotion Configuration

Comment on lines 4 to 8
export {
withEmotionCache,
CacheProvider,
useEmotionCacheContext
} from './context'

Choose a reason for hiding this comment

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

The naming here feels a bit strange:

  • Why do we have CacheProvider not mentioning "emotion" but withEmotionCache does?
  • Why do we have withEmotionCache not mentioning "context" but useEmotionCacheContext does?

Copy link
Contributor Author

@garronej garronej Jul 25, 2021

Choose a reason for hiding this comment

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

Agreed 😞 I'm stupid for putting 'Context',
However withEmotionCache and CacheProvider were named this way already. It's in the diff only because prettier rearranged the statement because it was becoming too long for a single line.
I assume it is out of the question to introduce breaking change for fixing name inconsistency.
That in mind what would you see as a better name for the hook?

  • useCache or
  • useEmotionCache

I personally prefer useEmotionCache, it is more consistent with withEmotionCache with which it shares the same goal of providing access to the cache.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have rebased to export useEmotionCache instead of useEmotionCacheContext

Choose a reason for hiding this comment

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

Ok, in any case, I'm not a maintainer of this GitHub repository

@garronej garronej force-pushed the expose_useEmotionCacheContext branch from 208ffa0 to 772ba3f Compare July 26, 2021 01:40
@garronej garronej changed the title expose useEmotionCacheContext in @emotion/react expose useEmotionCache from @emotion/react Jul 26, 2021
@thieryw
Copy link

thieryw commented Jul 28, 2021

It would be a goodness if this could be merged!

@Andarist
Copy link
Member

@mitchellhamilton what are your thoughts about this? It definitely shouldn't be encouraged to use this because there are not a lot of use cases this allows and they are mainly related to generating string class names that break 0-config SSR. OTOH, it's already possible to get access to the cache leveraging the public withEmotionCache - it's only way more problematic to do so (which we could treat as intentional). In the vein of being pragmatic over purist, I'm inclined to consent this - I'm pondering though if it shouldn't be exported with some kind of __unsafe prefix.

@garronej garronej force-pushed the expose_useEmotionCacheContext branch from 772ba3f to 4a1aa67 Compare July 28, 2021 09:45
@garronej
Copy link
Contributor Author

Hi @Andarist,
Thank you for taking the time to have a look.
I agree with your views. It's a shame to sacrifice 0-config SSR but unfortunately there is no other ways.
I am encline to use the __unsafe prefix, I think it's a good idea, I rebased.

@garronej garronej changed the title expose useEmotionCache from @emotion/react expose __unsafe_useEmotionCache from @emotion/react Jul 28, 2021
Comment on lines 23 to 27
export let __unsafe_useEmotionCache =
function __unsafe_useEmotionCache(): EmotionCache | null {
return useContext(EmotionCacheContext)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
export let __unsafe_useEmotionCache =
function __unsafe_useEmotionCache(): EmotionCache | null {
return useContext(EmotionCacheContext)
}
export function __unsafe_useEmotionCache(): EmotionCache | null {
return useContext(EmotionCacheContext)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know, this is what I would write but I am trying to respect the repo conventions.

image

Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't really bother about the existing convention here. The main reason why withEmotionCache is defined like that is because of the fact that it might be reassigned a few lines below - this doesn't apply to the __unsafe_useEmotionCache

Copy link
Contributor Author

@garronej garronej Jul 29, 2021

Choose a reason for hiding this comment

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

Ah yes I see!
However I rebased to do it like this:
image
Because it is meant to be imported like this:
import { __unsafe_useEmotionCache as useEmotionCache } from "@emotion/react";

@garronej garronej force-pushed the expose_useEmotionCacheContext branch from 4a1aa67 to ae143e2 Compare July 28, 2021 10:42
@Andarist
Copy link
Member

Thank you for taking the time to have a look.

No problem, I will still wait on @mitchellhamilton's opinion though, before moving this forward.

@emmatown
Copy link
Member

I'm fine with exposing this as __unsafe_useEmotionCache

@oliviertassinari
Copy link

Seeing this export make me think about something else related to emotion's cache & React context.
In @material-ui/styled-engine, the package has a direct dependency on @emotion/cache so it can access createCache. However, it seems to come with a few downsides:

  1. All the developers that use Material-UI with styled-components download @emotion/cache unless they use an npm package alias. Proof: install and check the lock file.
  2. The package size reported by bundlephobia is harder to follow, it does show how the dependencies are deduplicated once installed. Proof: https://bundlephobia.com/package/@material-ui/styled-engine@5.0.0-beta.1

Would it make sense to reexport the API of the packages that @emotion/react depends on? Now, maybe it doesn't make sense. For instance, for Material-UI, we have talked about making @emotion/styled a direct dependency instead of a peer dependency as it has no singletons.
So I guess I wished there we were a single emotion package to install and use, not multiples.

@garronej
Copy link
Contributor Author

garronej commented Aug 5, 2021

I'm fine with exposing this as __unsafe_useEmotionCache

@Andarist Is there any remaining blocking for this PR to be merged?
I hope I don't came out as being too pushy 😣, I promised emotion cache support to tss-react users weeks ago and I am embarrassed not to have delivered yet.

@Andarist
Copy link
Member

Andarist commented Aug 5, 2021

Sorry for not handling this yet. Gonna go through all pending PRs tomorrow and ship a new version

@Jack-Works
Copy link
Contributor

I'm fine with exposing this as __unsafe_useEmotionCache

@Andarist Is there any remaining blocking for this PR to be merged?
I hope I don't came out as being too pushy 😣, I promised emotion cache support to tss-react users weeks ago and I am embarrassed not to have delivered yet.

Don't feel embarrassed! I do not so hurry for that issue to be resolved. Thanks for all of your work!

@garronej
Copy link
Contributor Author

garronej commented Aug 6, 2021

Sorry for not handling this yet. Gonna go through all pending PRs tomorrow and ship a new version

No need to apologize, I can't imagine the amount of work required to maintain a project as widely used as emotion.

Don't feel embarrassed! I do not so hurry for that issue to be resolved. Thanks for all of your work!

Thanks for your understanding 😊 You are welcome.

@Andarist Andarist merged commit 24557d9 into emotion-js:main Aug 8, 2021
@github-actions github-actions bot mentioned this pull request Aug 8, 2021
@Andarist Andarist mentioned this pull request Aug 8, 2021
4 tasks
@garronej
Copy link
Contributor Author

garronej commented Aug 9, 2021

Thank you 😊

@ciampo
Copy link

ciampo commented Aug 10, 2021

Hey @garronej and @Andarist , thank you for working on this! Would it be possible to include __unsafe_useEmotionCache() in the the TypeScript types for the @emotion/react package?

Thank you!

@garronej
Copy link
Contributor Author

garronej commented Aug 10, 2021

Hey @garronej and @Andarist , thank you for working on this! Would it be possible to include __unsafe_useEmotionCache() in the the TypeScript types for the @emotion/react package?

Thank you!

Since the use of this feature is discouraged if not for interoperability, I doubt @Andarist will be encline to let the type definitions be added for it. But maybe I'm wrong?
What about using module augmentation to import it?

@ciampo
Copy link

ciampo commented Aug 10, 2021

Hey @garronej and @Andarist , thank you for working on this! Would it be possible to include __unsafe_useEmotionCache() in the the TypeScript types for the @emotion/react package?
Thank you!

Since the use of this feature is discouraged if not for interoperability, I doubt @Andarist will be encline to add the types definition for it.
What about using module augmentation to import it?

Sounds good, thank you for the advice!

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.

None yet

7 participants