box('margin', $attributes['margin'] ?? [], [Style::class, 'length'], ['top', 'bottom']) ->add('button-align', $attributes['buttonAlign'] ?? [], static fn($value): string => Style::oneOf($value, self::ALIGNMENTS)) ->add('button-gap', $attributes['buttonGap'] ?? [], [Style::class, 'length']); $placement = $this->placement($attributes['buttonLayout'] ?? [], $attributes['buttonAlign'] ?? []); $styles ->add('button-direction', $placement['direction'], static fn($value): string => Style::oneOf($value, ['row', 'column'])) ->add('button-items', $placement['items'], static fn($value): string => Style::oneOf($value, ['center', 'start', 'end'])); return sprintf( '
%2$s
', get_block_wrapper_attributes([ 'class' => implode(' ', array_filter([$styles->classNames(), ...DesignStyles::hiddenClasses($attributes)])), 'style' => $styles->style(), ] + Anchor::wrapperAttributes($attributes)), $content ); } /** * The flex direction and cross-axis alignment each device shows, '' where * it matches the next larger device. Mirrors buttonsPlacement() in * buttons-styles.js. * * @param mixed $layout Saved buttonLayout per device. * @param mixed $align Saved buttonAlign per device. * @return array{direction: array, items: array} flex-direction and align-items per device, or ''. */ private function placement(mixed $layout, mixed $align): array { $placement = ['direction' => [], 'items' => []]; $previous = ['direction' => 'row', 'items' => 'center']; foreach (array_keys(ResponsiveStyles::DEVICES) as $device) { $isStacked = Style::oneOf(ResponsiveStyles::effectiveValue($layout, $device), self::LAYOUTS) === 'stack'; $shownAlign = Style::oneOf(ResponsiveStyles::effectiveValue($align, $device), self::ALIGNMENTS); $current = [ 'direction' => $isStacked ? 'column' : 'row', 'items' => $isStacked ? (in_array($shownAlign, ['start', 'center', 'end'], true) ? $shownAlign : 'start') : 'center', ]; foreach (['direction', 'items'] as $key) { $placement[$key][$device] = $current[$key] === $previous[$key] ? '' : $current[$key]; } $previous = $current; } return $placement; } }