-
Notifications
You must be signed in to change notification settings - Fork 30
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
Conversation
'--ripple-scale-end': '1.414', | ||
'--ripple-scale-start': '1.414', | ||
'--ripple-size': '100%', | ||
'--ripple-translate': '0, 0', |
There was a problem hiding this comment.
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' |
There was a problem hiding this comment.
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 } | ||
} |
There was a problem hiding this comment.
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.
Coverage reportTotal coverage
Show new covered files 🌑Coverage of new files
Report generated by 🧪jest coverage report action from 2f05c88 |
There was a problem hiding this 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) |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
Using
ReactDOMServer.renderToString()
on content containing anIconButton
generates a warning foruseLayoutEffect
: facebook/react#14927The
useLayoutEffect
hook comes intoIconButton
viauseRipple
->useMeasuredElement
->useResize
, but actually the last two hooks in that chain aren't necessary. I moved them to a separate hookuseBoundedRipple
that can be used for rectangular elements likeButton
andListItem
where measurement is necessary.I did look into the
useIsomorphicLayoutEffect
workaround but it wouldn't work in the specific situation that inspired this ticket, becausewindow
is still defined there.