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

Fix useDynamicTextareaHeight initial render with slots #3196

Merged
merged 11 commits into from
Jun 20, 2023

Conversation

iansan5653
Copy link
Contributor

@iansan5653 iansan5653 commented Apr 19, 2023

Sometimes, useDynamicTextareaHeight cannot determine what the height of the textarea should be on initial render. This can be seen in Projects in the single-select editor dialog, which is contained inside a Dialog v2 component:

Screen.Recording.2023-04-19.at.3.03.18.PM.mov

Notice how the description field initially has extra height, but then collapses down to the correct height after the user starts typing. This is jarring and annoying.

It appears that this is caused by an interaction with the 'slots' pattern where the input is initially hidden on first render, then becomes visible after the layout effect has already run. The layout effect does not rerun when the slots resolve, causing the height to be incorrectly calculated on first render.

We could update the effect to re-run on every render, but because it sets state this would cause a cyclical infinite render loop. Alternatively, we could change the effect to a regular effect (non-layout) which would cause it to always be correct, but this CSS update in an effect would cause distracting flashes when not using the slots pattern.

So this is a hacky workaround to resolve issues with slots, which is itself a hacky workaround: we run a regular effect in addition to a layout effect on first render, then only layout effects from then on.

@iansan5653 iansan5653 requested a review from a team April 19, 2023 19:08
@iansan5653 iansan5653 self-assigned this Apr 19, 2023
@changeset-bot
Copy link

changeset-bot bot commented Apr 19, 2023

🦋 Changeset detected

Latest commit: 1026d29

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

This PR includes changesets to release 1 package
Name Type
@primer/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

@github-actions github-actions bot temporarily deployed to storybook-preview-3196 April 19, 2023 19:14 Inactive
@iansan5653 iansan5653 temporarily deployed to github-pages April 19, 2023 19:15 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3196 April 19, 2023 19:15 Inactive
@josepmartins josepmartins temporarily deployed to github-pages April 20, 2023 06:31 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3196 April 20, 2023 06:31 Inactive
@josepmartins
Copy link
Contributor

Het @iansan5653, there are some linting errors with unnecessary dependencies and the UseEffect declaration. Can you take a look at those? 🙏

@github-actions
Copy link
Contributor

github-actions bot commented Apr 20, 2023

size-limit report 📦

Path Size
dist/browser.esm.js 101.01 KB (0%)
dist/browser.umd.js 101.57 KB (0%)

@iansan5653 iansan5653 temporarily deployed to github-pages April 20, 2023 16:47 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3196 April 20, 2023 16:48 Inactive
@iansan5653
Copy link
Contributor Author

Should be good now!

Copy link
Contributor

@josepmartins josepmartins left a comment

Choose a reason for hiding this comment

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

LGMT! I adding @rezrah as a reviewer as well 🙏

@josepmartins josepmartins temporarily deployed to github-pages April 21, 2023 09:15 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3196 April 21, 2023 09:15 Inactive
@colebemis
Copy link
Contributor

@iansan5653 Is this still necessary with the new implementation of slots? https://github.com/primer/react/blob/main/src/hooks/useSlots.ts

@iansan5653
Copy link
Contributor Author

Probably not - let's keep an eye on it after we update dotcom and close this out if it resolves itself.

@iansan5653
Copy link
Contributor Author

@colebemis it looks like we haven't shipped slots updates for Dialog (v2) yet? Maybe this would still be useful to merge to fix the bug there for now - then we can revert it once the old slots is entirely gone.

@joshblack
Copy link
Member

bump @colebemis wanted to check in if this was something we still need to include in the release or if it's already been addressed by the slots updates mentioned above 👀

@iansan5653
Copy link
Contributor Author

iansan5653 commented May 23, 2023

😕 Actually I think I was wrong - doesn't look like DialogV2 uses slots. The only slots I can see here are FormControl which already got the update and should be live in dotcom. So maybe we do still need to merge this fix - maybe I was just wrong about it being caused by slots.

Copy link
Contributor

@colebemis colebemis left a comment

Choose a reason for hiding this comment

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

Thanks for your patience! These changes look good to me 👍

@mperrotti mperrotti temporarily deployed to github-pages June 19, 2023 14:58 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3196 June 19, 2023 14:58 Inactive
@mperrotti mperrotti temporarily deployed to github-pages June 19, 2023 15:34 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3196 June 19, 2023 15:35 Inactive
@iansan5653 iansan5653 temporarily deployed to github-pages June 19, 2023 16:50 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3196 June 19, 2023 16:50 Inactive
@joshblack joshblack enabled auto-merge June 20, 2023 16:14
@github-actions github-actions bot temporarily deployed to storybook-preview-3196 June 20, 2023 16:21 Inactive
@joshblack joshblack temporarily deployed to github-pages June 20, 2023 16:21 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3196 June 20, 2023 16:22 Inactive
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.

5 participants