Skip to content
Merged
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- `Divider`: Complete TypeScript migration ([#41991](https://github.com/WordPress/gutenberg/pull/41991)).
- `Divider`, `Flex`, `Spacer`: Improve documentation for the `SpaceInput` prop ([#42376](https://github.com/WordPress/gutenberg/pull/42376)).
- `Elevation`: Convert to TypeScript ([#42302](https://github.com/WordPress/gutenberg/pull/42302)).

## 19.15.0 (2022-07-13)

Expand Down
16 changes: 8 additions & 8 deletions packages/components/src/card/test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ Object {
will-change: box-shadow;
border-radius: inherit;
bottom: 0;
box-shadow: 0 0px 0px 0 rgba(0 ,0, 0, 0);
box-shadow: 0 0px 0px 0 rgba(0, 0, 0, 0);
opacity: 1;
left: 0;
right: 0;
Expand Down Expand Up @@ -629,7 +629,7 @@ Object {
will-change: box-shadow;
border-radius: inherit;
bottom: 0;
box-shadow: 0 0px 0px 0 rgba(0 ,0, 0, 0);
box-shadow: 0 0px 0px 0 rgba(0, 0, 0, 0);
opacity: 1;
left: 0;
right: 0;
Expand Down Expand Up @@ -786,15 +786,15 @@ Snapshot Diff:
</div>
<div
aria-hidden="true"
- class="components-elevation css-1b4rkmt-View-Elevation-sx-Base-elevationClassName em57xhy0"
+ class="components-elevation css-91yjwm-View-Elevation-sx-Base-elevationClassName em57xhy0"
- class="components-elevation css-1lsfy80-View-Elevation-sx-Base-elevationClassName em57xhy0"
+ class="components-elevation css-15t1t3g-View-Elevation-sx-Base-elevationClassName em57xhy0"
data-wp-c16t="true"
data-wp-component="Elevation"
/>
<div
aria-hidden="true"
- class="components-elevation css-o8otup-View-Elevation-sx-Base-elevationClassName em57xhy0"
+ class="components-elevation css-91yjwm-View-Elevation-sx-Base-elevationClassName em57xhy0"
- class="components-elevation css-18cl04p-View-Elevation-sx-Base-elevationClassName em57xhy0"
+ class="components-elevation css-15t1t3g-View-Elevation-sx-Base-elevationClassName em57xhy0"
data-wp-c16t="true"
data-wp-component="Elevation"
/>
Expand Down Expand Up @@ -829,7 +829,7 @@ exports[`Card Card component should warn when the isElevated prop is passed 1`]
will-change: box-shadow;
border-radius: inherit;
bottom: 0;
box-shadow: 0 1px 2px 0 rgba(0 ,0, 0, 0.05);
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
opacity: 1;
left: 0;
right: 0;
Expand All @@ -854,7 +854,7 @@ exports[`Card Card component should warn when the isElevated prop is passed 1`]
will-change: box-shadow;
border-radius: inherit;
bottom: 0;
box-shadow: 0 2px 4px 0 rgba(0 ,0, 0, 0.1);
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
opacity: 1;
left: 0;
right: 0;
Expand Down
30 changes: 16 additions & 14 deletions packages/components/src/elevation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,45 @@ function Example() {

Size of the shadow value when active (see the `value` and `isInteractive` props).

- Required: No
- Required: No

### `borderRadius`: `CSSProperties[ 'borderRadius' ]`

Renders the border-radius of the shadow.

- Required: No
- Default: `inherit`
- Required: No
- Default: `inherit`

### `focus`: `number`

Size of the shadow value when focused (see the `value` and `isInteractive` props).
Size of the shadow value when focused (see the `value` and `isInteractive`props).

- Required: No
- Required: No

### `hover`: `number`

Size of the shadow value when hovered (see the `value` and `isInteractive` props).
Size of the shadow value when hovered (see the `value` and `isInteractive` props).

- Required: No
- Required: No

### `isInteractive`: `boolean`

Determines if `hover`, `active`, and `focus` shadow values should be automatically calculated and rendered.

- Required: No
- Default: `false`
- Required: No
- Default: `false`

### `offset`: `number`

Dimensional offsets (margin) for the shadow.

- Required: No
- Default: `0`
- Required: No
- Default: `0`

### `value`

**Type**: `number`
### `value`: `number`

Size of the shadow, based on the Style system's elevation system. The `value` determines the strength of the shadow, which sense of depth.

In the example below, `isInteractive` is activated to give a better sense of depth.

```jsx
Expand All @@ -86,3 +85,6 @@ function Example() {
);
}
```

- Required: No
- Default: `0`
43 changes: 0 additions & 43 deletions packages/components/src/elevation/component.js

This file was deleted.

48 changes: 48 additions & 0 deletions packages/components/src/elevation/component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';

/**
* Internal dependencies
*/
import { contextConnect, WordPressComponentProps } from '../ui/context';
import { View } from '../view';
import { useElevation } from './hook';
import type { ElevationProps } from './types';

function UnconnectedElevation(
props: WordPressComponentProps< ElevationProps, 'div' >,
forwardedRef: ForwardedRef< any >
) {
const elevationProps = useElevation( props );

return <View { ...elevationProps } ref={ forwardedRef } />;
}

/**
* `Elevation` is a core component that renders shadow, using the component
* system's shadow system.
*
* The shadow effect is generated using the `value` prop.
*
* ```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>
* );
* }
* ```
*/
export const Elevation = contextConnect( UnconnectedElevation, 'Elevation' );

export default Elevation;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { css } from '@emotion/react';
import { css, SerializedStyles } from '@emotion/react';

/**
* WordPress dependencies
Expand All @@ -11,28 +11,24 @@ import { useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import { useContextSystem } from '../ui/context';
import { useContextSystem, WordPressComponentProps } from '../ui/context';
import * as styles from './styles';
import { CONFIG, reduceMotion } from '../utils';
import { useCx } from '../utils/hooks/use-cx';
import { isValueDefined } from '../utils/values';
import type { ElevationProps } from './types';

/**
* @param {number} value
* @return {string} The box shadow value.
*/
export function getBoxShadow( value ) {
const boxShadowColor = `rgba(0 ,0, 0, ${ value / 20 })`;
export function getBoxShadow( value: number ) {
const boxShadowColor = `rgba(0, 0, 0, ${ value / 20 })`;
const boxShadow = `0 ${ value }px ${ value * 2 }px 0
${ boxShadowColor }`;

return boxShadow;
}

/**
* @param {import('../ui/context').WordPressComponentProps<import('./types').Props, 'div'>} props
*/
export function useElevation( props ) {
export function useElevation(
props: WordPressComponentProps< ElevationProps, 'div' >
) {
const {
active,
borderRadius = 'inherit',
Expand All @@ -48,10 +44,12 @@ export function useElevation( props ) {
const cx = useCx();

const classes = useMemo( () => {
/** @type {number | undefined} */
let hoverValue = isValueDefined( hover ) ? hover : value * 2;
/** @type {number | undefined} */
let activeValue = isValueDefined( active ) ? active : value / 2;
let hoverValue: number | undefined = isValueDefined( hover )
? hover
: value * 2;
let activeValue: number | undefined = isValueDefined( active )
? active
: value / 2;

if ( ! isInteractive ) {
hoverValue = isValueDefined( hover ) ? hover : undefined;
Expand All @@ -60,7 +58,12 @@ export function useElevation( props ) {

const transition = `box-shadow ${ CONFIG.transitionDuration } ${ CONFIG.transitionTimingFunction }`;

const sx = {};
const sx: {
Base?: SerializedStyles;
hover?: SerializedStyles;
active?: SerializedStyles;
focus?: SerializedStyles;
} = {};

sx.Base = css(
{
Expand Down
Loading