-
Notifications
You must be signed in to change notification settings - Fork 32
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,65 @@ | |
|
||
*/ | ||
|
||
import type { ExtendedStatefulColor } from '@looker/design-tokens' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved types from |
||
import type { CSSProperties } from 'react' | ||
|
||
export type RippleCallbacks = { | ||
endBG: () => void | ||
endFG: () => void | ||
startBG: () => void | ||
startFG: () => void | ||
} | ||
|
||
export type UseBoundedRippleProps = { | ||
/** | ||
* Change the color of the ripple background and foreground | ||
* @default neutral | ||
*/ | ||
color?: ExtendedStatefulColor | ||
/** | ||
* Decimal multiplier, e.g. 1.5. | ||
* Use for unbounded ripple that needs to extend past element dimensions, | ||
* don't use with overflow: hidden | ||
* @default 1 | ||
*/ | ||
size?: number | ||
} | ||
|
||
export type UseRippleProps = UseBoundedRippleProps & { | ||
/** | ||
* Use for elements where the ripple disappears at the edges of | ||
* a visible rectangle, e.g. a default Button | ||
* @default false | ||
*/ | ||
bounded?: boolean | ||
/** | ||
* Internal use only | ||
* @private | ||
*/ | ||
width?: number | ||
/** | ||
* Internal use only | ||
* @private | ||
*/ | ||
height?: number | ||
} | ||
|
||
export type UseRippleResponse = { | ||
/** | ||
* The start and end functions for the background and foreground | ||
*/ | ||
callbacks: RippleCallbacks | ||
/** | ||
* The class names used in rippleStyle to trigger the animations | ||
*/ | ||
className: string | ||
/** | ||
* ref is only used for bounded ripple, to detect element dimensions | ||
*/ | ||
ref?: (node: HTMLElement | null) => void | ||
/** | ||
* style contains CSS variables to control the animation | ||
*/ | ||
style: CSSProperties | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
|
||
import { renderWithTheme } from '@looker/components-test-utils' | ||
import { screen } from '@testing-library/react' | ||
import { ThemeProvider } from 'styled-components' | ||
import React from 'react' | ||
import type { UseBoundedRippleProps } from './types' | ||
import { useBoundedRipple } from './useBoundedRipple' | ||
|
||
const RippleInner = (props: UseBoundedRippleProps) => { | ||
const { | ||
callbacks: { startBG, endBG, startFG, endFG }, | ||
className, | ||
ref, | ||
style, | ||
} = useBoundedRipple(props) | ||
return ( | ||
<div ref={ref}> | ||
<div data-testid="startBG" onClick={startBG} /> | ||
<div data-testid="endBG" onClick={endBG} /> | ||
<div data-testid="startFG" onClick={startFG} /> | ||
<div data-testid="endFG" onClick={endFG} /> | ||
<div data-testid="className">{className}</div> | ||
<div style={style}>style</div> | ||
</div> | ||
) | ||
} | ||
|
||
// TODO: Remove this when we change brandAnimation default to true | ||
// (then just change the value below to use this for the brandAnimation: false scenario) | ||
const RippleComponent = (props: UseBoundedRippleProps) => ( | ||
<ThemeProvider | ||
theme={(theme) => ({ | ||
...theme, | ||
defaults: { ...theme.defaults, brandAnimation: true }, | ||
})} | ||
> | ||
<RippleInner {...props} /> | ||
</ThemeProvider> | ||
) | ||
|
||
/* eslint-disable-next-line @typescript-eslint/unbound-method */ | ||
const globalGetBoundingClientRect = Element.prototype.getBoundingClientRect | ||
|
||
beforeEach(() => { | ||
/* eslint-disable-next-line @typescript-eslint/unbound-method */ | ||
Element.prototype.getBoundingClientRect = jest.fn(() => { | ||
return { | ||
bottom: 0, | ||
height: 30, | ||
left: 0, | ||
right: 0, | ||
toJSON: jest.fn(), | ||
top: 0, | ||
width: 360, | ||
x: 0, | ||
y: 0, | ||
} | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
/* eslint-disable-next-line @typescript-eslint/unbound-method */ | ||
Element.prototype.getBoundingClientRect = globalGetBoundingClientRect | ||
}) | ||
|
||
describe('useRipple', () => { | ||
test('bounded animation values', () => { | ||
renderWithTheme(<RippleComponent />) | ||
expect(screen.getByText('style')).toHaveStyle({ | ||
'--ripple-color': '#71767a', | ||
'--ripple-overflow': 'hidden', | ||
'--ripple-scale-end': '12.041594578792294', | ||
'--ripple-scale-start': '1', | ||
'--ripple-size': '30px', | ||
'--ripple-translate': '165px, 0', | ||
}) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
|
||
MIT License | ||
|
||
Copyright (c) 2021 Looker Data Sciences, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
*/ | ||
import { useMeasuredElement, useCallbackRef } from '../utils' | ||
import { useRipple } from './useRipple' | ||
import type { UseBoundedRippleProps } from './types' | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. With an eye to the (not-too-distant) future do you think 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 commentThe 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 |
||
const result = useRipple({ ...props, bounded: true, height, width }) | ||
return { ...result, ref } | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently not used but will be for |
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 thesize
prop instead of a measurement to make the ripple extend to the corners.