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(IconButton): Remove useLayoutEffect to avoid SSR warning #2831

Merged
merged 3 commits into from
Sep 15, 2021

Conversation

mdodgelooker
Copy link
Contributor

@mdodgelooker mdodgelooker commented Sep 14, 2021

Using ReactDOMServer.renderToString() on content containing an IconButton generates a warning for useLayoutEffect: facebook/react#14927

The useLayoutEffect hook comes into IconButton via useRipple -> useMeasuredElement -> useResize, but actually the last two hooks in that chain aren't necessary. I moved them to a separate hook useBoundedRipple that can be used for rectangular elements like Button and ListItem where measurement is necessary.

I did look into the useIsomorphicLayoutEffect workaround but it wouldn't work in the specific situation that inspired this ticket, because window is still defined there.

@google-cla google-cla bot added the cla: yes CLA manual verification label Sep 14, 2021
'--ripple-scale-end': '1.414',
'--ripple-scale-start': '1.414',
'--ripple-size': '100%',
'--ripple-translate': '0, 0',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A square IconButton now uses the size prop instead of a measurement to make the ripple extend to the corners.

@@ -24,9 +24,65 @@

*/

import type { ExtendedStatefulColor } from '@looker/design-tokens'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved types from useRipple into this file.

const [{ height, width }] = useMeasuredElement(element)
const result = useRipple({ ...props, bounded: true, height, width })
return { ...result, ref }
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently not used but will be for Button, ListItem, etc.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 14, 2021

Coverage report

Total coverage

Status Category Percentage Covered / Total
🟢 Statements 94.86% 7220/7611
🟡 Branches 87.15% 4598/5276
🟢 Functions 93.65% 2080/2221
🟢 Lines 98.96% 25287/25553

Status of coverage: 🟢 - ok, 🟡 - slightly more than threshold, 🔴 - under the threshold

Show new covered files 🌑

Coverage of new files

Status Filename Statements Branches Functions Lines
🟢 packages/components/src/Ripple/useRipple.ts 96.55% 89.29% 100% 100%
🟢 packages/components/src/Ripple/useBoundedRipple.ts 100% 100% 100% 100%

Status of coverage: 🟢 - ok, 🟡 - slightly more than threshold, 🔴 - under the threshold

Report generated by 🧪jest coverage report action from 2f05c88

@mdodgelooker mdodgelooker requested a review from a user September 15, 2021 00:06
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Nice fix! :)


export const useBoundedRipple = (props: UseBoundedRippleProps) => {
const [element, ref] = useCallbackRef()
const [{ height, width }] = useMeasuredElement(element)
Copy link

Choose a reason for hiding this comment

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

With an eye to the (not-too-distant) future do you think useMeasuredElement could be refactored to be more "defensive"? Basically just check if window exists and skip calculation if not?

I went ahead and logged a ticket to keep track of this requirement for that work: https://b.corp.google.com/issues/199953481 :)

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 think so. But in order to make that change, we should set up a test app that uses SSR. It sounds like neither useEffect nor useLayoutEffect should be used with SSR so I'm curious to see what falls apart with our components.

@mdodgelooker mdodgelooker merged commit b647328 into main Sep 15, 2021
@mdodgelooker mdodgelooker deleted the mdodge/fix-uselayouteffect branch September 15, 2021 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes CLA manual verification
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant