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

ZStack: fix component bounding box to match children #51836

Merged
merged 7 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -14,6 +14,7 @@
- `Button`: Remove unnecessary margin from dashicon ([#51395](https://github.com/WordPress/gutenberg/pull/51395)).
- `Autocomplete`: Announce how many results are available to screen readers when suggestions list first renders ([#51018](https://github.com/WordPress/gutenberg/pull/51018)).
- `ConfirmDialog`: Ensure onConfirm isn't called an extra time when submitting one of the buttons using the keyboard ([#51730](https://github.com/WordPress/gutenberg/pull/51730)).
- `ZStack`: ZStack: fix component bounding box to match children ([#51836](https://github.com/WordPress/gutenberg/pull/51836)).

### Internal

Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/z-stack/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function UnconnectedZStack(

const clonedChildren = validChildren.map( ( child, index ) => {
const zIndex = isReversed ? childrenLastIndex - index : index;
const offsetAmount = offset * index;
// Only when the component is layered, the offset needs to be multiplied
// the item's index, so that items can correctly stack at the right distance
const offsetAmount = isLayered ? offset * index : offset;

const key = isValidElement( child ) ? child.key : index;

Expand Down
29 changes: 14 additions & 15 deletions packages/components/src/z-stack/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import styled from '@emotion/styled';
/**
* Internal dependencies
*/
import { rtl } from '../utils';

export const ZStackView = styled.div`
display: flex;
display: inline-grid;
grid-auto-flow: column;
position: relative;
`;

Expand All @@ -19,21 +19,20 @@ export const ZStackChildView = styled.div< {
offsetAmount: number;
zIndex: number;
} >`
${ ( { isLayered, offsetAmount } ) =>
isLayered
? css( rtl( { marginLeft: offsetAmount } )() )
: css( rtl( { right: offsetAmount * -1 } )() ) }
position: relative;

${ ( { isLayered } ) =>
isLayered ? positionAbsolute : positionRelative }

${ ( { zIndex } ) => css( { zIndex } ) }
`;
isLayered
? // When `isLayered` is true, all items overlap in the same grid cell
css( { gridRowStart: 1, gridColumnStart: 1 } )
: undefined };

const positionAbsolute = css`
position: absolute;
`;
&:not( :first-of-type ) {
${ ( { offsetAmount } ) =>
css( {
marginInlineStart: offsetAmount,
} ) };
}

const positionRelative = css`
position: relative;
${ ( { zIndex } ) => css( { zIndex } ) };
`;