-
Notifications
You must be signed in to change notification settings - Fork 4.7k
components: Add ZStack #31613
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
Merged
Merged
components: Add ZStack #31613
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
24b668d
components: Add ZStack
sarayourfriend 2807f2e
components: Export ZStack
sarayourfriend 00d4bfa
Use ReactNodeArray instead of duplicating it
sarayourfriend 92c5cca
Add explicit children prop
sarayourfriend 861ffb4
Update story and README
sarayourfriend 11b1c3d
Improve story adding more knobs and colors
sarayourfriend facfa7f
Rename offset to overlap
sarayourfriend 6434721
Update packages/components/src/z-stack/README.md
sarayourfriend a643d84
New approach to offset
ciampo 437ab9a
Fix ViewOwnProps import
sarayourfriend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # ZStack | ||
|
|
||
| > **Experimental!** | ||
|
|
||
| ## Usage | ||
|
|
||
| `ZStack` allows you to stack things along the Z-axis. | ||
|
|
||
| ```jsx | ||
| import { __experimentalZStack as ZStack } from '@wordpress/components'; | ||
|
|
||
| function Example() { | ||
| return ( | ||
| <ZStack offset={ 20 } isLayered> | ||
| <ExampleImage /> | ||
| <ExampleImage /> | ||
| <ExampleImage /> | ||
| </ZStack> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ## Props | ||
|
|
||
| ### `isLayered`: `boolean` | ||
|
|
||
| When `true`, the children are stacked on top of each other. When `false`, the children follow the normal flow of the layout. Defaults to `true`. | ||
|
|
||
| ### `isReversed`: `boolean` | ||
|
|
||
| Reverse the layer ordering. When `true`, the first child has the lowest `z-index` and the last child has the highest `z-index`. When `false`, the first child has the highest `z-index` and the last child has the lowest `z-index`. Defaults to `false`. | ||
|
|
||
| ### `offset`: `number` | ||
|
|
||
| The amount of space between each child element. Defaults to `0`. | ||
|
|
||
| ### `children`: `ReactNode` | ||
|
|
||
| The children to stack. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { css, cx } from 'emotion'; | ||
| // eslint-disable-next-line no-restricted-imports | ||
| import type { Ref, ReactNode } from 'react'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { isValidElement } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { getValidChildren } from '../ui/utils/get-valid-children'; | ||
| import { contextConnect, useContextSystem } from '../ui/context'; | ||
| // eslint-disable-next-line no-duplicate-imports | ||
| import type { PolymorphicComponentProps } from '../ui/context'; | ||
| import { View } from '../view'; | ||
| import * as styles from './styles'; | ||
| const { ZStackView } = styles; | ||
|
|
||
| export interface ZStackProps { | ||
| /** | ||
| * Layers children elements on top of each other (first: highest z-index, last: lowest z-index). | ||
| * | ||
| * @default true | ||
| */ | ||
| isLayered?: boolean; | ||
| /** | ||
| * Reverse the layer ordering (first: lowest z-index, last: highest z-index). | ||
| * | ||
| * @default false | ||
| */ | ||
| isReversed?: boolean; | ||
| /** | ||
| * The amount of offset between each child element. | ||
| * | ||
| * @default 0 | ||
| */ | ||
| offset?: number; | ||
sarayourfriend marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * Child elements. | ||
| */ | ||
| children: ReactNode; | ||
| } | ||
|
|
||
| function ZStack( | ||
| props: PolymorphicComponentProps< ZStackProps, 'div' >, | ||
| forwardedRef: Ref< any > | ||
| ) { | ||
| const { | ||
| children, | ||
| className, | ||
| isLayered = true, | ||
| isReversed = false, | ||
| offset = 0, | ||
| ...otherProps | ||
| } = useContextSystem( props, 'ZStack' ); | ||
|
|
||
| const validChildren = getValidChildren( children ); | ||
| const childrenLastIndex = validChildren.length - 1; | ||
|
|
||
| const clonedChildren = validChildren.map( ( child, index ) => { | ||
| const zIndex = isReversed ? childrenLastIndex - index : index; | ||
| const offsetAmount = offset * index; | ||
|
|
||
| const classes = cx( | ||
| isLayered ? styles.positionAbsolute : styles.positionRelative, | ||
| css( { | ||
| ...( isLayered | ||
| ? { marginLeft: offsetAmount } | ||
| : { right: offsetAmount * -1 } ), | ||
| } ) | ||
| ); | ||
|
|
||
| const key = isValidElement( child ) ? child.key : index; | ||
|
|
||
| return ( | ||
| <View | ||
| className={ classes } | ||
| key={ key } | ||
| style={ { | ||
| zIndex, | ||
| } } | ||
| > | ||
| { child } | ||
| </View> | ||
| ); | ||
| } ); | ||
|
|
||
| return ( | ||
| <ZStackView | ||
| { ...otherProps } | ||
| className={ className } | ||
| ref={ forwardedRef } | ||
| > | ||
| { clonedChildren } | ||
| </ZStackView> | ||
| ); | ||
| } | ||
|
|
||
| export default contextConnect( ZStack, 'ZStack' ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { default as ZStack } from './component'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { boolean, number } from '@storybook/addon-knobs'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { Elevation } from '../../ui/elevation'; | ||
| import { HStack } from '../../h-stack'; | ||
| import { View } from '../../view'; | ||
| import { ZStack } from '..'; | ||
|
|
||
| export default { | ||
| component: ZStack, | ||
| title: 'Components (Experimental)/ZStack', | ||
| }; | ||
|
|
||
| const Avatar = ( { backgroundColor } ) => { | ||
| return ( | ||
| <View> | ||
| <View | ||
| style={ { | ||
| border: '3px solid black', | ||
| borderRadius: '9999px', | ||
| height: '48px', | ||
| width: '48px', | ||
| backgroundColor, | ||
| } } | ||
| /> | ||
| <Elevation | ||
| borderRadius={ 9999 } | ||
| isInteractive={ false } | ||
| value={ 3 } | ||
| /> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| const AnimatedAvatars = () => { | ||
| const props = { | ||
| offset: number( 'offset', 20 ), | ||
| isLayered: boolean( 'isLayered', true ), | ||
| isReversed: boolean( 'isReversed', false ), | ||
| }; | ||
|
|
||
| return ( | ||
| <HStack> | ||
| <View> | ||
| <ZStack { ...props }> | ||
| <Avatar backgroundColor="#444" /> | ||
| <Avatar backgroundColor="#777" /> | ||
| <Avatar backgroundColor="#aaa" /> | ||
| <Avatar backgroundColor="#fff" /> | ||
| </ZStack> | ||
| </View> | ||
| </HStack> | ||
| ); | ||
| }; | ||
|
|
||
| export const _default = () => { | ||
| return ( | ||
| <View> | ||
| <AnimatedAvatars /> | ||
| </View> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { css } from 'emotion'; | ||
| import styled from '@emotion/styled'; | ||
|
|
||
| export const ZStackView = styled.div` | ||
| display: flex; | ||
| position: relative; | ||
| `; | ||
|
|
||
| export const positionAbsolute = css` | ||
| position: absolute; | ||
| `; | ||
|
|
||
| export const positionRelative = css` | ||
| position: relative; | ||
| `; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.