@use "@wordpress/base-styles/variables" as *;

.grid {
	// Nested Grids only use their own layout props.
	--wp-components-grid-align: initial;
	--wp-components-grid-justify: initial;
	--wp-components-grid-gap: initial;
	--wp-components-grid-template-columns: initial;
	--wp-components-grid-template-rows: initial;
	--wp-components-grid-row-gap: initial;
	--wp-components-grid-column-gap: initial;

	display: grid;
}

.grid:not(:where(
	.row-gap-inherit,
	.row-gap-initial,
	.row-gap-unset,
	.row-gap-revert,
	.row-gap-revert-layer
)) {
	row-gap: var(--wp-components-grid-row-gap, calc(#{$grid-unit-05} * var(--wp-components-grid-gap)));
}

.grid:not(:where(
	.column-gap-inherit,
	.column-gap-initial,
	.column-gap-unset,
	.column-gap-revert,
	.column-gap-revert-layer
)) {
	column-gap: var(--wp-components-grid-column-gap, calc(#{$grid-unit-05} * var(--wp-components-grid-gap)));
}

// Omitted props leave consumer stylesheet declarations intact.
.has-align:not(:where(
	.align-inherit,
	.align-initial,
	.align-unset,
	.align-revert,
	.align-revert-layer
)) {
	align-items: var(--wp-components-grid-align);
}

.has-justify:not(:where(
	.justify-inherit,
	.justify-initial,
	.justify-unset,
	.justify-revert,
	.justify-revert-layer
)) {
	justify-content: var(--wp-components-grid-justify);
}

.has-template-columns:not(:where(
	.template-columns-inherit,
	.template-columns-initial,
	.template-columns-unset,
	.template-columns-revert,
	.template-columns-revert-layer
)) {
	grid-template-columns: var(--wp-components-grid-template-columns);
}

.has-template-rows:not(:where(
	.template-rows-inherit,
	.template-rows-initial,
	.template-rows-unset,
	.template-rows-revert,
	.template-rows-revert-layer
)) {
	grid-template-rows: var(--wp-components-grid-template-rows);
}

.is-inline {
	display: inline-grid;
	vertical-align: middle;
}

// Preserve CSS-wide values without moving declarations into inline styles,
// where they would override consumer stylesheets.
@each $name, $property in (
	align: align-items,
	justify: justify-content,
	template-columns: grid-template-columns,
	template-rows: grid-template-rows,
	row-gap: row-gap,
	column-gap: column-gap
) {
	@each $keyword in (inherit, initial, unset, revert, revert-layer) {
		.#{$name}-#{$keyword} {
			#{$property}: $keyword;
		}
	}
}
