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

Component System: Add Elevation Component #28593

Merged
merged 8 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
70 changes: 70 additions & 0 deletions packages/components/src/ui/elevation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Elevation

`Elevation` is a core component that renders shadow, using the component system's shadow system.

## Usage

The shadow effect is generated using the `value` prop.

```jsx live
import {
__experimentalElevation as Elevation,
__experimetnalSurface as Surface,
__experimetnalText as Text,
__experimetnalView as View,
} from '@wp-g2/components';

function Example() {
return (
<Surface>
<Text>Elevation</Text>
<Elevation value={ 5 } />
</Surface>
);
}
```

## Props

##### active

**Type**: `boolean`

Renders the active (interaction) shadow value.

##### borderRadius

**Type**: `string`,`number`

Renders the border-radius of the shadow.

##### focus

**Type**: `boolean`

Renders the focus (interaction) shadow value.

##### hover

**Type**: `boolean`

Renders the hover (interaction) shadow value.

##### isInteractive

**Type**: `boolean`

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

##### offset

**Type**: `number`

Dimensional offsets (margin) for the shadow.

##### value

**Type**: `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.
13 changes: 13 additions & 0 deletions packages/components/src/ui/elevation/elevation-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* External dependencies
*/
import { css } from '@wp-g2/styles';

export const Elevation = css`
background: transparent;
display: block;
margin: 0 !important;
pointer-events: none;
position: absolute;
will-change: box-shadow;
`;
44 changes: 44 additions & 0 deletions packages/components/src/ui/elevation/elevation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* External dependencies
*/
import { contextConnect } from '@wp-g2/context';

/**
* Internal dependencies
*/
import { View } from '../view';
import { useElevation } from './use-elevation';

/**
*
* @param {import('@wp-g2/create-styles').ViewOwnProps<import('./types').Props, 'div'>} props
* @param {import('react').Ref<any>} forwardedRef
*/
function Elevation( props, forwardedRef ) {
const otherProps = useElevation( props );

return <View aria-hidden { ...otherProps } ref={ forwardedRef } />;
Copy link
Member

@diegohaz diegohaz Feb 11, 2021

Choose a reason for hiding this comment

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

Does this component accept a children prop? Is it possible that someone uses this component like this by mistake?

<Elevation>
  <p>Paragraph</p>
  <button>Button</button>
</Elevation>

If that's the case, I would whether not accept a children prop or warn against it. If there's a use case for children prop, I would use role="presentation" instead of aria-hidden (the difference is that aria-hidden will disable the whole subtree, whereas role="presentation" will only disable that element).

Copy link
Author

Choose a reason for hiding this comment

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

Ooo! Thanks for the feedback. Elevation actually shouldn't accept children 😱 .

(@sarayourfriend how would we adjust the types for that? I think this may be one of the few new components that don't accept children)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, interesting. I'll do some tinkering.

}

/**
* `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
* function Example() {
* return (
* <View css={ [ ui.padding( 5 ) ] }>
* <Surface css={ [ ui.padding( 5 ) ] }>
* <Text>Into The Unknown</Text>
* <Elevation value={ 5 } />
* </Surface>
* </View>
* );
* }
* ```
*/
const ConnectedElevation = contextConnect( Elevation, 'Elevation' );

export default ConnectedElevation;
2 changes: 2 additions & 0 deletions packages/components/src/ui/elevation/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as Elevation } from './elevation';
export * from './use-elevation';
40 changes: 40 additions & 0 deletions packages/components/src/ui/elevation/stories/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* External dependencies
*/
import { number } from '@storybook/addon-knobs';
/**
* Internal dependencies
*/
import { Elevation } from '../index';
import { View } from '../../view';

export default {
component: Elevation,
title: 'G2 Components (Experimental)/Elevation',
};

export const _default = () => {
const value = number( 'value', 5 );
const hover = number( 'hover', undefined );
const focus = number( 'focus', undefined );
const active = number( 'active', undefined );

return (
<View
css={ {
padding: '20vh',
position: 'relative',
margin: '20vh auto 10vw',
maxWidth: 400,
} }
>
<Elevation
isInteractive
value={ value }
hover={ hover }
focus={ focus }
active={ active }
/>
</View>
diegohaz marked this conversation as resolved.
Show resolved Hide resolved
);
};
Loading