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

Show visual label for Categories block in dropdown mode #56364

Merged
merged 20 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8e66c88
Add custom label and a toolbar button for showing or hiding the label
carolinan Nov 21, 2023
f85053b
Use the custmized label text in the visually hidden label
carolinan Nov 21, 2023
5a5fc47
Remove escaping of the label on the front, to allow RichText styles.
carolinan Nov 21, 2023
3979515
Update core__categories.json
carolinan Nov 21, 2023
d8c322f
Merge branch 'trunk' into add/categories-label
carolinan Nov 24, 2023
82dd25b
Merge branch 'trunk' into add/categories-label
carolinan Nov 27, 2023
ac396e9
Merge branch 'trunk' into add/categories-label
carolinan Jan 2, 2024
67642cd
Replace the toolbarGroup with a toggleControl in the sidebar (inspect…
carolinan Jan 3, 2024
d9364f9
Simplify condition in index.php
carolinan Jan 3, 2024
9282649
Merge branch 'trunk' into add/categories-label
carolinan Jan 25, 2024
5ba3fc3
Merge branch 'trunk' into add/categories-label
carolinan Feb 14, 2024
b371459
Merge branch 'trunk' into add/categories-label
carolinan Feb 22, 2024
59ac425
Merge branch 'trunk' into add/categories-label
carolinan Mar 8, 2024
92c4b91
Merge branch 'trunk' into add/categories-label
carolinan Apr 22, 2024
de5f767
Indent the control and disable it when displayAsDropdown is not enabled
carolinan Apr 22, 2024
86bd7d3
Merge branch 'trunk' into add/categories-label
carolinan May 21, 2024
5d043f6
Merge branch 'trunk' into add/categories-label
carolinan Jun 27, 2024
97face3
Merge branch 'trunk' into add/categories-label
carolinan Jul 11, 2024
a013b0e
Merge branch 'trunk' into add/categories-label
carolinan Jul 15, 2024
328977a
Hide the "show label" option until the dropdown option is enabled.
carolinan Jul 15, 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 @@ -79,7 +79,7 @@ Display a list of all categories. ([Source](https://github.com/WordPress/gutenbe
- **Name:** core/categories
- **Category:** widgets
- **Supports:** align, interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** displayAsDropdown, showEmpty, showHierarchy, showOnlyTopLevel, showPostCounts
- **Attributes:** displayAsDropdown, label, showEmpty, showHierarchy, showLabel, showOnlyTopLevel, showPostCounts

## Code

Expand Down
8 changes: 8 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,14 @@
"showEmpty": {
"type": "boolean",
"default": false
},
"label": {
"type": "string",
"__experimentalRole": "content"
},
"showLabel": {
"type": "boolean",
"default": true
}
},
"supports": {
Expand Down
36 changes: 32 additions & 4 deletions packages/block-library/src/categories/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
VisuallyHidden,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import {
InspectorControls,
useBlockProps,
RichText,
} from '@wordpress/block-editor';
import { decodeEntities } from '@wordpress/html-entities';
import { __ } from '@wordpress/i18n';
import { pin } from '@wordpress/icons';
Expand All @@ -27,6 +31,8 @@ export default function CategoriesEdit( {
showPostCounts,
showOnlyTopLevel,
showEmpty,
label,
showLabel,
},
setAttributes,
className,
Expand Down Expand Up @@ -92,9 +98,22 @@ export default function CategoriesEdit( {
const categoriesList = getCategoriesList( parentId );
return (
<>
<VisuallyHidden as="label" htmlFor={ selectId }>
{ __( 'Categories' ) }
</VisuallyHidden>
{ showLabel ? (
<RichText
className="wp-block-categories__label"
aria-label={ __( 'Label text' ) }
placeholder={ __( 'Categories' ) }
withoutInteractiveFormatting
value={ label }
onChange={ ( html ) =>
setAttributes( { label: html } )
}
/>
) : (
<VisuallyHidden as="label" htmlFor={ selectId }>
{ label ? label : __( 'Categories' ) }
</VisuallyHidden>
) }
<select id={ selectId }>
<option>{ __( 'Select Category' ) }</option>
{ categoriesList.map( ( category ) =>
Expand Down Expand Up @@ -148,6 +167,15 @@ export default function CategoriesEdit( {
checked={ displayAsDropdown }
onChange={ toggleAttribute( 'displayAsDropdown' ) }
/>
{ displayAsDropdown && (
<ToggleControl
__nextHasNoMarginBottom
className="wp-block-categories__indentation"
label={ __( 'Show label' ) }
checked={ showLabel }
onChange={ toggleAttribute( 'showLabel' ) }
/>
) }
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Show post counts' ) }
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/categories/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
[data-align="center"] .wp-block-categories {
text-align: center;
}

.wp-block-categories__indentation {
padding-left: 16px;
}
5 changes: 4 additions & 1 deletion packages/block-library/src/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ function render_block_core_categories( $attributes ) {
$id = 'wp-block-categories-' . $block_id;
$args['id'] = $id;
$args['show_option_none'] = __( 'Select Category' );
$wrapper_markup = '<div %1$s><label class="screen-reader-text" for="' . esc_attr( $id ) . '">' . __( 'Categories' ) . '</label>%2$s</div>';
$show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : '';
$default_label = __( 'Categories' );
$label_text = ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label;
Copy link
Member

Choose a reason for hiding this comment

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

See comment from @peterwilsoncc while syncing packages to WordPress core:

Escaping label text

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 will create a PR for this and add the backport to beta label.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you 👍🏻

$wrapper_markup = '<div %1$s><label class="wp-block-categories__label' . $show_label . '" for="' . esc_attr( $id ) . '">' . $label_text . '</label>%2$s</div>';
$items_markup = wp_dropdown_categories( $args );
$type = 'dropdown';

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/categories/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
&.wp-block-categories-dropdown.aligncenter {
text-align: center;
}
& .wp-block-categories__label {
width: 100%;
display: block;
}
}
3 changes: 2 additions & 1 deletion test/integration/fixtures/blocks/core__categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"showHierarchy": false,
"showPostCounts": false,
"showOnlyTopLevel": false,
"showEmpty": false
"showEmpty": false,
"showLabel": true
},
"innerBlocks": []
}
Expand Down
Loading