-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
components: Remove @emotion/css
from Elevation
#33098
Changes from all commits
36a68fa
d952243
1491235
832ba5d
9666395
14e3601
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line no-restricted-imports | ||
import type { Ref } from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
useContextSystem, | ||
contextConnect, | ||
PolymorphicComponentProps, | ||
} from '../ui/context'; | ||
import type { Props } from './types'; | ||
import { ElevationView, ElevationViewProps } from './styles'; | ||
|
||
const DEFAULT_PROPS: ElevationViewProps = { | ||
isInteractive: false, | ||
offset: 0, | ||
value: 0, | ||
active: null, | ||
focus: null, | ||
hover: null, | ||
borderRadius: 'inherit', | ||
}; | ||
|
||
function Elevation( | ||
props: PolymorphicComponentProps< Props, 'div', false >, | ||
forwardedRef: Ref< any > | ||
) { | ||
const contextProps = useContextSystem( props, 'Elevation' ); | ||
|
||
return ( | ||
<ElevationView | ||
ref={ forwardedRef } | ||
{ ...DEFAULT_PROPS } | ||
{ ...contextProps } | ||
aria-hidden="true" | ||
/> | ||
); | ||
} | ||
|
||
/** | ||
* `Elevation` is a core component that renders shadow, using the library's shadow system. | ||
* | ||
* The shadow effect is generated using the `value` prop. | ||
* | ||
* @example | ||
* ```jsx | ||
* import { | ||
* __experimentalElevation as Elevation, | ||
* __experimentalSurface as Surface, | ||
* __experimentalText as Text, | ||
* } from '@wordpress/components'; | ||
* | ||
* function Example() { | ||
* return ( | ||
* <Surface> | ||
* <Text>Code is Poetry</Text> | ||
* <Elevation value={ 5 } /> | ||
* </Surface> | ||
* ); | ||
* } | ||
* ``` | ||
*/ | ||
const ConnectedElevation = contextConnect( Elevation, 'Elevation' ); | ||
|
||
export default ConnectedElevation; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { default as Elevation } from './component'; | ||
export * from './hook'; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import styled from '@emotion/styled'; | ||
import { css, CSSObject } from '@emotion/react'; | ||
import type { Required } from 'utility-types'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { Props } from './types'; | ||
import { CONFIG, reduceMotion } from '../utils'; | ||
|
||
export type ElevationViewProps = Required< Props >; | ||
|
||
const getBoxShadow = ( value: number ) => { | ||
const boxShadowColor = `rgba(0 ,0, 0, ${ value / 20 })`; | ||
return `0 ${ value }px ${ value * 2 }px 0 | ||
${ boxShadowColor }`; | ||
}; | ||
|
||
const renderBoxShadow = ( { value }: ElevationViewProps ) => | ||
css( { boxShadow: getBoxShadow( value ) } ); | ||
|
||
const renderTransition = () => | ||
css( { | ||
transition: `box-shadow ${ CONFIG.transitionDuration } | ||
${ CONFIG.transitionTimingFunction }`, | ||
} ); | ||
|
||
const renderBorderRadius = ( { borderRadius }: ElevationViewProps ) => | ||
css( { borderRadius } ); | ||
|
||
const renderOffset = ( { offset }: ElevationViewProps ) => | ||
css( { bottom: offset, left: offset, right: offset, top: offset } ); | ||
|
||
const renderHoverActiveFocus = ( { | ||
isInteractive, | ||
active, | ||
hover, | ||
focus, | ||
value, | ||
}: ElevationViewProps ) => { | ||
let hoverValue: number | null = hover !== null ? hover : value * 2; | ||
let activeValue: number | null = active !== null ? active : value / 2; | ||
|
||
if ( ! isInteractive ) { | ||
hoverValue = hover; | ||
activeValue = active; | ||
} | ||
|
||
const cssObj: CSSObject = {}; | ||
|
||
if ( hoverValue !== null ) { | ||
cssObj[ '*:hover > &' ] = { | ||
boxShadow: getBoxShadow( hoverValue ), | ||
}; | ||
} | ||
|
||
if ( activeValue !== null ) { | ||
cssObj[ '*:active > &' ] = { | ||
boxShadow: getBoxShadow( activeValue ), | ||
}; | ||
} | ||
|
||
if ( focus !== null ) { | ||
cssObj[ '*focus > &' ] = { | ||
boxShadow: getBoxShadow( focus ), | ||
}; | ||
} | ||
|
||
return css( cssObj ); | ||
}; | ||
|
||
const DO_NOT_FORWARD = [ | ||
'value', | ||
'offset', | ||
'hover', | ||
'active', | ||
'focus', | ||
'borderRadius', | ||
'isInteractive', | ||
]; | ||
|
||
export const ElevationView = styled( 'div', { | ||
shouldForwardProp: ( propName ) => | ||
! DO_NOT_FORWARD.includes( propName as string ), | ||
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. Should we also add the |
||
} )< ElevationViewProps >` | ||
background: transparent; | ||
display: block; | ||
margin: 0 !important; | ||
pointer-events: none; | ||
position: absolute; | ||
will-change: box-shadow; | ||
opacity: ${ CONFIG.elevationIntensity }; | ||
${ renderTransition } | ||
${ reduceMotion( 'transition' ) } | ||
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. Was this added as part of this PR, or did the Elevation component already handle 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. It was added as part of this, the Elevation component incorrectly did not use this before. |
||
${ renderBoxShadow } | ||
${ renderBorderRadius } | ||
${ renderOffset } | ||
${ renderHoverActiveFocus } | ||
`; |
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.
Note to self:
This is the approach that was agreed in order to set the default values for props only once per component (these default values would otherwise be set also in when computing the styles) in a clear and explicit way.
The styles, in fact, assume that all props are always defined (and does so through the
Required
TypeScript utility)