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

Post Navigation Link: Add border and spacing block support #64258

Open
wants to merge 21 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a02df3f
Add border support to post mavigation block
akasunil Aug 5, 2024
f331538
Add box sizing style to post navigation link block
akasunil Aug 5, 2024
e9fff19
Return empty if the adjacent post is unavailable
akasunil Aug 7, 2024
6baaa15
Update comment text
akasunil Aug 7, 2024
de7485c
Merge branch 'trunk' of personal.github.com:WordPress/gutenberg into …
akasunil Aug 8, 2024
700f22c
Add spacing support to post navigation link
akasunil Aug 9, 2024
d9d51b7
Merge branch 'trunk' of personal.github.com:WordPress/gutenberg into …
akasunil Aug 13, 2024
4c25d1a
Merge branch 'trunk' of personal.github.com:WordPress/gutenberg into …
akasunil Aug 13, 2024
6aa39df
Revert changes of render function
akasunil Aug 13, 2024
0551091
Add selectors for border and spacing support
akasunil Aug 14, 2024
a14512f
Merge branch 'trunk' of personal.github.com:WordPress/gutenberg into …
akasunil Aug 16, 2024
c46823f
Remove styles from empty post navigation link element
akasunil Aug 16, 2024
f15548f
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/post-…
akasunil Sep 25, 2024
311f5d8
Skip serialization of border and spacing support
akasunil Sep 26, 2024
6404d13
Apply border and spacing support manully
akasunil Sep 26, 2024
3fca4e4
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/post-…
akasunil Sep 26, 2024
b0fdd00
Update comment texts
akasunil Sep 26, 2024
8dcec4d
Fix coding standards
akasunil Sep 26, 2024
c656936
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/post-…
akasunil Oct 3, 2024
b027a57
Fix inline support style issue
akasunil Oct 3, 2024
2654826
Merge branch 'trunk' of github.com:WordPress/gutenberg into add/post-…
akasunil Oct 19, 2024
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ Displays the next or previous post link that is adjacent to the current post. ([

- **Name:** core/post-navigation-link
- **Category:** theme
- **Supports:** color (background, link, text), interactivity (clientNavigation), typography (fontSize, lineHeight), ~~html~~, ~~reusable~~
- **Supports:** color (background, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~, ~~reusable~~
- **Attributes:** arrow, label, linkLabel, showTitle, taxonomy, textAlign, type

## Post Template
Expand Down
20 changes: 20 additions & 0 deletions packages/block-library/src/post-navigation-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,27 @@
},
"interactivity": {
"clientNavigation": true
},
"spacing": {
"__experimentalSkipSerialization": true,
"margin": true,
"padding": true,
"__experimentalDefaultControls": {
"margin": false,
"padding": false
}
},
"__experimentalBorder": {
"__experimentalSkipSerialization": true,
"radius": true,
"color": true,
"width": true,
"style": true
}
},
"selectors": {
"border": ".wp-block-post-navigation-link:not(:empty)",
"spacing": ".wp-block-post-navigation-link:not(:empty)"
},
"style": "wp-block-post-navigation-link"
}
27 changes: 17 additions & 10 deletions packages/block-library/src/post-navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ import {
BlockControls,
AlignmentToolbar,
useBlockProps,
__experimentalUseBorderProps as useBorderProps,
__experimentalGetSpacingClassesAndStyles as useSpacingProps,
} from '@wordpress/block-editor';
import { __, _x } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';

export default function PostNavigationLinkEdit( {
context: { postType },
attributes: {
type,
label,
showTitle,
textAlign,
linkLabel,
arrow,
taxonomy,
},
attributes,
setAttributes,
} ) {
const { type, label, showTitle, textAlign, linkLabel, arrow, taxonomy } =
attributes;

const isNext = type === 'next';
let placeholder = isNext ? __( 'Next' ) : __( 'Previous' );

const borderProps = useBorderProps( attributes );
const spacingProps = useSpacingProps( attributes );

const arrowMap = {
none: '',
arrow: isNext ? '→' : '←',
Expand Down Expand Up @@ -184,7 +184,14 @@ export default function PostNavigationLinkEdit( {
} }
/>
</BlockControls>
<div { ...blockProps }>
<div
{ ...blockProps }
style={ {
...blockProps.style,
...borderProps.style,
...spacingProps.style,
} }
>
Comment on lines +187 to +194
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only applies styles from the border and spacing props. Border support can define CSS classes to apply as well e.g. has-border-color etc.

You can see this in action in the editor if you apply a border to the Group block. An example that maintains the classes from useBorderProps can be seen in the Avatar block.

One option would be to pass borderProps.className etc in as classes for the useBlockProps call here.

The reason this doesn't present visually in the editor is that the border block support forces inline styles for the colors in the event that a theme doesn't correctly load their color stylesheets in the editor. Even still, we should maintain parity between the frontend and editor for classes wherever possible.

{ ! isNext && displayArrow && (
<span
className={ `wp-block-post-navigation-link__arrow-previous is-arrow-${ arrow }` }
Expand Down
91 changes: 86 additions & 5 deletions packages/block-library/src/post-navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ function render_block_core_post_navigation_link( $attributes, $content ) {
if ( isset( $attributes['textAlign'] ) ) {
$classes .= " has-text-align-{$attributes['textAlign']}";
}
$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => $classes,
)
);

// Set default values.
$format = '%link';
$link = 'next' === $navigation_type ? _x( 'Next', 'label for next post link' ) : _x( 'Previous', 'label for previous post link' );
Expand Down Expand Up @@ -116,13 +112,98 @@ function render_block_core_post_navigation_link( $attributes, $content ) {
$content = $get_link_function( $format, $link );
}

$support_styles = block_core_post_navigation_link_get_border_and_spacing_attributes( $attributes );
$classes .= ! empty( $support_styles['class'] ) ? " {$support_styles['class']}" : '';
Comment on lines +115 to +116
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the editor side flagged above, this change doesn't take into account that the border support can apply color CSS classes e.g. has-border-color, has-accent-1-border-color etc for preset colors for "flat" borders.

The result is the issue raised in my last review is still present.


// Get the wrapper attributes.
$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => $classes,
'style' => ! empty( $content ) && $support_styles['style'] ? $support_styles['style'] : '',
)
);

return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
$content
);
}

/**
* Generates class names and styles to apply the border and spacing support styles for
* the Post Navigation Link block.
*
* @since 6.7.0
*
* @param array $attributes The block attributes.
* @return array The border and spacing related classnames and styles for the block.
*/
function block_core_post_navigation_link_get_border_and_spacing_attributes( $attributes ) {
$border_styles = array();
$sides = array( 'top', 'right', 'bottom', 'left' );

// Border radius.
if ( isset( $attributes['style']['border']['radius'] ) ) {
$border_styles['radius'] = $attributes['style']['border']['radius'];
}

// Border style.
if ( isset( $attributes['style']['border']['style'] ) ) {
$border_styles['style'] = $attributes['style']['border']['style'];
}

// Border width.
if ( isset( $attributes['style']['border']['width'] ) ) {
$border_styles['width'] = $attributes['style']['border']['width'];
}

// Border color.
$preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
$custom_color = $attributes['style']['border']['color'] ?? null;
$border_styles['color'] = $preset_color ? $preset_color : $custom_color;

// Individual border styles e.g. top, left etc.
foreach ( $sides as $side ) {
$border = $attributes['style']['border'][ $side ] ?? null;
$border_styles[ $side ] = array(
'color' => isset( $border['color'] ) ? $border['color'] : null,
'style' => isset( $border['style'] ) ? $border['style'] : null,
'width' => isset( $border['width'] ) ? $border['width'] : null,
);
}

// Individual padding styles e.g. top, left etc.
$padding_style = array();
foreach ( $sides as $side ) {
$padding_style[ $side ] = $attributes['style']['spacing']['padding'][ $side ] ?? null;
}

// Individual margin styles e.g. top, left etc.
$margin_style = array();
foreach ( $sides as $side ) {
$margin_style[ $side ] = $attributes['style']['spacing']['margin'][ $side ] ?? null;
}

$styles = wp_style_engine_get_styles(
array(
'border' => $border_styles,
'spacing' => array(
'padding' => $padding_style,
'margin' => $margin_style,
),
)
);
$attributes = array();
if ( ! empty( $styles['classnames'] ) ) {
$attributes['class'] = $styles['classnames'];
}
if ( ! empty( $styles['css'] ) ) {
$attributes['style'] = $styles['css'];
}
return $attributes;
}

/**
* Registers the `core/post-navigation-link` block on the server.
*
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/post-navigation-link/style.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.wp-block-post-navigation-link {
// This block has customizable padding, border-box makes that more predictable.
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
box-sizing: border-box;

.wp-block-post-navigation-link__arrow-previous {
display: inline-block;
Expand Down
Loading