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

Add ability to toggle bullets on and off for Latest Posts Block via new "Layout" option. #32806

Closed
41 changes: 35 additions & 6 deletions packages/block-library/src/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Spinner,
ToggleControl,
ToolbarGroup,
ToolbarDropdownMenu,
} from '@wordpress/components';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
Expand All @@ -32,7 +33,7 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { pin, list, grid } from '@wordpress/icons';
import { pin, list, formatListBullets, grid } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';

/**
Expand Down Expand Up @@ -411,6 +412,8 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
'has-dates': displayPostDate,
'has-author': displayAuthor,
[ `columns-${ columns }` ]: postLayout === 'grid',
'has-no-bullets': 'bullet-list' !== postLayout,
'has-bullets': 'bullet-list' === postLayout,
} ),
} );

Expand All @@ -435,28 +438,54 @@ export default function LatestPostsEdit( { attributes, setAttributes } ) {
? latestPosts.slice( 0, postsToShow )
: latestPosts;

function applyLayout( layout ) {
return () =>
setAttributes( {
postLayout: layout,
} );
}

const layoutControls = [
{
layout: 'list',
icon: list,
title: __( 'List view' ),
onClick: () => setAttributes( { postLayout: 'list' } ),
title: __( 'List' ),
onClick: applyLayout( 'list' ),
isActive: postLayout === 'list',
},
{
layout: 'bullet-list',
icon: formatListBullets,
title: __( 'Bulleted List' ),
onClick: applyLayout( 'bullet-list' ),
isActive: postLayout === 'bullet-list',
},
{
layout: 'grid',
icon: grid,
title: __( 'Grid view' ),
onClick: () => setAttributes( { postLayout: 'grid' } ),
title: __( 'Grid' ),
onClick: applyLayout( 'grid' ),
isActive: postLayout === 'grid',
},
];

const dateFormat = __experimentalGetSettings().formats.date;

const activePostLayoutIcon =
layoutControls.find( ( layout ) => layout.layout === postLayout )
?.icon || 'list';

return (
<div>
{ inspectorControls }
<BlockControls>
<ToolbarGroup controls={ layoutControls } />
<ToolbarGroup>
<ToolbarDropdownMenu
icon={ activePostLayoutIcon }
label={ __( 'Layout' ) }
controls={ layoutControls }
/>
</ToolbarGroup>
</BlockControls>
<ul { ...blockProps }>
{ displayPosts.map( ( post, i ) => {
Expand Down
13 changes: 12 additions & 1 deletion packages/block-library/src/latest-posts/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@
margin-left: 2em;
}
&.wp-block-latest-posts__list {
list-style: none;

// Ensure bullets are visible even if block is "full-width"
// or "wide" alignment.
&.has-bullets {
padding-left: revert;
list-style: initial; // needed in case Theme Styles are disabled.
}

&.has-no-bullets {
list-style: none;
padding-left: 0; // needed in case Theme Styles are disabled.
}

li {
clear: both;
Expand Down