Skip to content

Commit

Permalink
Add in server-rendered output
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong committed Oct 13, 2022
1 parent dd8560d commit 27b2a43
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
61 changes: 61 additions & 0 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,65 @@ function gutenberg_register_layout_support( $block_type ) {
}
}

/**
* Generates the CSS for position support from the layout object.
*
* @param string $selector CSS selector.
* @param array $layout Layout object. The one that is passed has already checked
* the existence of default block layout.
* @return string CSS styles on success. Else, empty string.
*/
function gutenberg_get_layout_position_style( $selector, $layout ) {
$position_styles = array();

$position_type = _wp_array_get( $layout, array( 'position', 'type' ), '' );
$position_side = _wp_array_get( $layout, array( 'position', 'side' ), '' );

$offset_value = '0';

if (
in_array( $position_type, array( 'fixed', 'sticky' ), true ) &&
in_array( $position_side, array( 'top', 'right', 'bottom', 'left'), true )
) {
/*
* For fixed or sticky top positions,
* ensure the value includes an offset for the logged in admin bar.
*/
if (
'top' === $position_side &&
'fixed' === $position_type ||
'sticky' === $position_type
) {
$offset_value = 'var(--wp-admin--admin-bar--height, 0px)';
}

$position_styles[] =
array(
'selector' => "$selector",
'declarations' => array(
'position' => $position_type,
$position_side => $offset_value,
'z-index' => '250', // TODO: This hard-coded value should live somewhere else.
),
);
}

if ( ! empty( $position_styles ) ) {
/*
* Add to the style engine store to enqueue and render layout styles.
*/
return gutenberg_style_engine_get_stylesheet_from_css_rules(
$position_styles,
array(
'context' => 'block-supports',
'prettify' => false,
)
);
}

return '';
}

/**
* Generates the CSS corresponding to the provided layout.
*
Expand Down Expand Up @@ -406,6 +465,8 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
$block_spacing
);

$style .= gutenberg_get_layout_position_style( ".$block_classname.$container_class", $used_layout );

// Only add container class and enqueue block support styles if unique styles were generated.
if ( ! empty( $style ) ) {
$class_names[] = $container_class;
Expand Down
6 changes: 6 additions & 0 deletions lib/compat/wordpress-6.1/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ function gutenberg_safe_style_attrs_6_1( $attrs ) {
$attrs[] = 'margin-block-end';
$attrs[] = 'margin-inline-start';
$attrs[] = 'margin-inline-end';
$attrs[] = 'position';
$attrs[] = 'top';
$attrs[] = 'right';
$attrs[] = 'bottom';
$attrs[] = 'left';
$attrs[] = 'z-index';

return $attrs;
}
Expand Down

0 comments on commit 27b2a43

Please sign in to comment.