|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import { css, cx } from 'emotion'; |
| 5 | +// eslint-disable-next-line no-restricted-imports |
| 6 | +import type { Ref, ReactNode } from 'react'; |
| 7 | + |
| 8 | +/** |
| 9 | + * WordPress dependencies |
| 10 | + */ |
| 11 | +import { isValidElement } from '@wordpress/element'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Internal dependencies |
| 15 | + */ |
| 16 | +import { getValidChildren } from '../ui/utils/get-valid-children'; |
| 17 | +import { contextConnect, useContextSystem } from '../ui/context'; |
| 18 | +// eslint-disable-next-line no-duplicate-imports |
| 19 | +import type { PolymorphicComponentProps } from '../ui/context'; |
| 20 | +import { View } from '../view'; |
| 21 | +import * as styles from './styles'; |
| 22 | +const { ZStackView } = styles; |
| 23 | + |
| 24 | +export interface ZStackProps { |
| 25 | + /** |
| 26 | + * Layers children elements on top of each other (first: highest z-index, last: lowest z-index). |
| 27 | + * |
| 28 | + * @default true |
| 29 | + */ |
| 30 | + isLayered?: boolean; |
| 31 | + /** |
| 32 | + * Reverse the layer ordering (first: lowest z-index, last: highest z-index). |
| 33 | + * |
| 34 | + * @default false |
| 35 | + */ |
| 36 | + isReversed?: boolean; |
| 37 | + /** |
| 38 | + * The amount of offset between each child element. |
| 39 | + * |
| 40 | + * @default 0 |
| 41 | + */ |
| 42 | + offset?: number; |
| 43 | + /** |
| 44 | + * Child elements. |
| 45 | + */ |
| 46 | + children: ReactNode; |
| 47 | +} |
| 48 | + |
| 49 | +function ZStack( |
| 50 | + props: PolymorphicComponentProps< ZStackProps, 'div' >, |
| 51 | + forwardedRef: Ref< any > |
| 52 | +) { |
| 53 | + const { |
| 54 | + children, |
| 55 | + className, |
| 56 | + isLayered = true, |
| 57 | + isReversed = false, |
| 58 | + offset = 0, |
| 59 | + ...otherProps |
| 60 | + } = useContextSystem( props, 'ZStack' ); |
| 61 | + |
| 62 | + const validChildren = getValidChildren( children ); |
| 63 | + const childrenLastIndex = validChildren.length - 1; |
| 64 | + |
| 65 | + const clonedChildren = validChildren.map( ( child, index ) => { |
| 66 | + const zIndex = isReversed ? childrenLastIndex - index : index; |
| 67 | + const offsetAmount = offset * index; |
| 68 | + |
| 69 | + const classes = cx( |
| 70 | + isLayered ? styles.positionAbsolute : styles.positionRelative, |
| 71 | + css( { |
| 72 | + ...( isLayered |
| 73 | + ? { marginLeft: offsetAmount } |
| 74 | + : { right: offsetAmount * -1 } ), |
| 75 | + } ) |
| 76 | + ); |
| 77 | + |
| 78 | + const key = isValidElement( child ) ? child.key : index; |
| 79 | + |
| 80 | + return ( |
| 81 | + <View |
| 82 | + className={ classes } |
| 83 | + key={ key } |
| 84 | + style={ { |
| 85 | + zIndex, |
| 86 | + } } |
| 87 | + > |
| 88 | + { child } |
| 89 | + </View> |
| 90 | + ); |
| 91 | + } ); |
| 92 | + |
| 93 | + return ( |
| 94 | + <ZStackView |
| 95 | + { ...otherProps } |
| 96 | + className={ className } |
| 97 | + ref={ forwardedRef } |
| 98 | + > |
| 99 | + { clonedChildren } |
| 100 | + </ZStackView> |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +export default contextConnect( ZStack, 'ZStack' ); |
0 commit comments