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

Use React hoistable styles #3295

Draft
wants to merge 2 commits into
base: next
Choose a base branch
from
Draft

Use React hoistable styles #3295

wants to merge 2 commits into from

Conversation

Andarist
Copy link
Member

No description provided.

Copy link

changeset-bot bot commented Dec 15, 2024

⚠️ No Changeset found

Latest commit: 0439d5a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link

codesandbox-ci bot commented Dec 15, 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

codecov bot commented Dec 15, 2024

Codecov Report

Attention: Patch coverage is 97.50000% with 1 line in your changes missing coverage. Please review.

Project coverage is 92.58%. Comparing base (9717107) to head (c12a777).
Report is 1 commits behind head on use-react19.

Files with missing lines Patch % Lines
packages/jest/src/utils.js 90.90% 1 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
packages/cache/src/index.ts 93.75% <100.00%> (+0.06%) ⬆️
packages/primitives-core/src/styled.ts 100.00% <ø> (ø)
packages/primitives/src/styled.js 100.00% <ø> (ø)
packages/react/src/class-names.tsx 100.00% <100.00%> (ø)
packages/react/src/context.tsx 95.65% <ø> (ø)
packages/react/src/emotion-element.tsx 96.36% <100.00%> (-0.70%) ⬇️
packages/react/src/index.ts 30.76% <ø> (ø)
packages/react/src/jsx.ts 100.00% <ø> (ø)
packages/react/src/render-with-styles.tsx 100.00% <100.00%> (ø)
packages/react/src/theming.tsx 100.00% <ø> (ø)
... and 3 more

... and 6 files with indirect coverage changes

Base automatically changed from use-react19 to next December 15, 2024 11:01
@Andarist Andarist force-pushed the use-hoistable-styles branch from c12a777 to 19a5585 Compare December 15, 2024 11:02
Comment on lines +69 to +81
<style
data-href="1lrxbo5"
data-precedence="emotion-test"
>
.test-1lrxbo5{color:hotpink;}
</style>
<style
data-emotion="test-global"
data-s=""
>

/** test-global */body{width:0;}
</style>
Copy link
Member Author

Choose a reason for hiding this comment

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

their order flipped, we can fix this by using precedence with -global on global styles, that will allow people to control how they are grouped - they could render dummy styles in head with those precedence set and then React will insert new styles there

the problem is that with those hoistable styles... we lose the ability to unmount global styles, are we fine with that?

Copy link
Member

Choose a reason for hiding this comment

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

we lose the ability to unmount global styles, are we fine with that?

imo, yes, it's fine. I've never been a big fan of the unmounting styles is Global, it's kind of started out as "i guess we can do this so why not" but imo it's not really that valuable since people should just scope styles (whether that's literally with the css prop/etc. or with global styles but all the styles are scoped to some selector) and conditionally apply the selector rather than have conditional style insertion.

Copy link
Member Author

Choose a reason for hiding this comment

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

With this change - if we use hoistable styles everywhere - we won't need any SSR APIs at all (🎉). We can keep them for vanilla Emotion but then we'll need to tweak how our packages compose and how they are organized (like we'll have to move hydration paths to vanilla Emotion code parts instead of keeping them in @emotion/cache).

So at the moment, I didn't bother to tweak this. We need to first establish the baselines functionality~ for those hoistable styles

nonce={cache.sheet.nonce}
href={current.name}
>
{maybeStyles ?? styles}
Copy link
Member Author

Choose a reason for hiding this comment

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

this is nasty, when finalizing this PR we'll have to rethink the ssr/browser paths used by cache.insert. In the case of React we always want to get them back as the return value (so again, maybe we should rethink how our packages are composed)

Comment on lines +47 to +52
output.push(
<style
precedence={`emotion-${cache.key}`}
nonce={cache.sheet.nonce}
href={current.name}
/>
)
Copy link
Member Author

Choose a reason for hiding this comment

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

from what I understand we don't have to return the styles for already inserted styles... so I just return empty style tags here to keep the render returns somewhat idempotent (and that might have some impact on rehydration too, especially for useId as React is using sibling/descendant/whatever counters to rehydrate those correctly)

Copy link
Member Author

Choose a reason for hiding this comment

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

we might have to dust off our benchmarks... React doesn't use CSSOM APIs at all, we need to check how fine we are with that

@Andarist Andarist force-pushed the use-hoistable-styles branch from f505a06 to 09bcb47 Compare December 15, 2024 15:03
@Andarist Andarist force-pushed the use-hoistable-styles branch from 09bcb47 to 0439d5a Compare December 15, 2024 23:28
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.

2 participants