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

components: Remove @emotion/css from Elevation #33098

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions packages/components/src/elevation/component.js

This file was deleted.

69 changes: 69 additions & 0 deletions packages/components/src/elevation/component.tsx
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',
};
Comment on lines +18 to +26
Copy link
Contributor

@ciampo ciampo Jul 2, 2021

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)


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;
120 changes: 0 additions & 120 deletions packages/components/src/elevation/hook.js

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';
16 changes: 0 additions & 16 deletions packages/components/src/elevation/styles.js

This file was deleted.

102 changes: 102 additions & 0 deletions packages/components/src/elevation/styles.ts
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 ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also add the isPropValid() check, following the example in the official docs?

} )< ElevationViewProps >`
background: transparent;
display: block;
margin: 0 !important;
pointer-events: none;
position: absolute;
will-change: box-shadow;
opacity: ${ CONFIG.elevationIntensity };
${ renderTransition }
${ reduceMotion( 'transition' ) }
Copy link
Contributor

Choose a reason for hiding this comment

The 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 reduced-motion ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 }
`;
Loading