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

Categories: Add a list style type option to the categories block #51344

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -79,7 +79,7 @@ Display a list of all categories. ([Source](https://github.com/WordPress/gutenbe
- **Name:** core/categories
- **Category:** widgets
- **Supports:** align, spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** displayAsDropdown, showEmpty, showHierarchy, showOnlyTopLevel, showPostCounts
- **Attributes:** displayAsDropdown, listStyleType, showEmpty, showHierarchy, showOnlyTopLevel, showPostCounts

## Code

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/categories/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"showEmpty": {
"type": "boolean",
"default": false
},
"listStyleType": {
"type": "string",
"default": "inherit"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if the default should just be an empty string.

}
},
"supports": {
Expand Down
22 changes: 22 additions & 0 deletions packages/block-library/src/categories/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Spinner,
ToggleControl,
VisuallyHidden,
SelectControl,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
Expand All @@ -23,6 +24,7 @@ import { useEntityRecords } from '@wordpress/core-data';
export default function CategoriesEdit( {
attributes: {
displayAsDropdown,
listStyleType,
showHierarchy,
showPostCounts,
showOnlyTopLevel,
Expand Down Expand Up @@ -136,6 +138,7 @@ export default function CategoriesEdit( {

const blockProps = useBlockProps( {
className: classes,
style: listStyleType ? { listStyleType } : undefined,
} );

return (
Expand All @@ -148,6 +151,25 @@ export default function CategoriesEdit( {
checked={ displayAsDropdown }
onChange={ toggleAttribute( 'displayAsDropdown' ) }
/>
{ ! displayAsDropdown && (
<SelectControl
__nextHasNoMarginBottom
label={ __( 'List style' ) }
onChange={ ( newListStyleType ) =>
setAttributes( {
listStyleType: newListStyleType,
} )
}
options={ [
{ label: __( 'Default' ), value: 'inherit' },
{ label: __( 'None' ), value: 'none' },
{ label: __( 'Disc' ), value: 'disc' },
{ label: __( 'Circle' ), value: 'circle' },
{ label: __( 'Square' ), value: 'square' },
] }
value={ listStyleType }
/>
) }
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Show post counts' ) }
Expand Down
14 changes: 9 additions & 5 deletions packages/block-library/src/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ function render_block_core_categories( $attributes ) {
);
}
} else {
$wrapper_markup = '<ul %1$s>%2$s</ul>';
$items_markup = wp_list_categories( $args );
$type = 'list';
$wrapper_markup = '<ul %1$s>%2$s</ul>';
$items_markup = wp_list_categories( $args );
$type = 'list';
$list_style_type = $attributes['listStyleType'];
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) );
$attribute_array = array( 'class' => "wp-block-categories-{$type}" );
if ( ! empty( $list_style_type ) && 'inherit' !== $list_style_type ) {
$attribute_array['style'] = "list-style-type: {$list_style_type}";
}
$wrapper_attributes = get_block_wrapper_attributes( $attribute_array );

return sprintf(
$wrapper_markup,
Expand Down
Loading