Skip to content
Merged
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 .plugin-data
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "1.0.0",
"version": "1.1.0",
"slug": "blockparty-button-icon"
}
2 changes: 1 addition & 1 deletion .wordpress-org/blueprints/blueprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"pluginData": {
"resource": "git:directory",
"url": "https://github.com/BeAPI/blockparty-button-icon",
"ref": "1.0.0",
"ref": "1.1.0",
"refType": "tag"
},
"options": {
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.1.0 - 2026-11-25

- Add an **Icon only** position: the label is visually hidden but kept for screen readers, and edited from a required sidebar field
- In icon-only mode, the icon stays clickable to replace it like in other positions; the required screen-reader label is also shown in the icon inspector
- Mark icons rendered inside buttons as decorative (`aria-hidden="true"`) when the button label has text; buttons with an empty label keep the icon alternative text as accessible name
- Explain in the icon inspector that its alternative text is only a fallback inside a button
- Restore keyboard focus on the button after closing the link popover in icon-only mode

## 1.0.0 - 2026-10-15

- Initial release
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Extend the WordPress **core Button** block (`core/button`) with optional icons,

- **Core block extension**: No new block name — editors keep using the familiar Button block
- **Optional icon**: Insert an icon as an inner block (defaults to `core/icon` on WordPress 7.0+)
- **Icon position**: Display the icon to the left or right of the label
- **Icon position**: Display the icon to the left or right of the label, or show the icon only (label kept for screen readers)
- **Block gap**: Control horizontal spacing between icon and text via block spacing settings
- **Valid markup**: Icons render inside links with a `span` wrapper on the front end (phrasing content)
- **Extensible**: Filter which icon block types may be inserted into a button
Expand Down Expand Up @@ -42,7 +42,9 @@ composer require beapi/blockparty-button-icon
1. Add or select a **Button** block in the editor
2. Open the block **Styles** panel (sidebar)
3. Turn on **Show icon**
4. Choose **Left** or **Right** for icon position
4. Choose **Left**, **Right** or **Icon only** for icon position
- With **Icon only**, the label is hidden in the canvas and on the front end; fill the required **Label (screen readers)** field in the sidebar
- This field is plain text: editing it removes any formatting from the label (bold, italic, `<span lang>`). Switching back to **Left** or **Right** keeps the plain text
5. Use the block toolbar **Icon** control to pick or replace the icon
6. Adjust **Block spacing → Block gap** (horizontal) to change the space between icon and label

Expand All @@ -67,6 +69,8 @@ The first block type in the filtered list is used when the editor creates a new

- Button label text is wrapped in `<span class="wp-block-button__label">` when an icon is present, keeping a clear text node for assistive technologies
- On the front end, `core/icon` outer wrappers are output as `span` elements when rendered inside button links
- Icons inside buttons get `aria-hidden="true"` when the button label has text: the label then provides the accessible name. When the label is empty (for example a v1.0.0 icon-only button relying on the icon alternative text), the icon is left exposed so the button keeps a name
- **Icon only** buttons keep the label in the markup and hide it visually (`.blockparty-button-icon--only .wp-block-button__label`), so links and buttons keep an accessible name

## Generated markup example

Expand All @@ -79,6 +83,17 @@ The first block type in the filtered list is used when the editor creates a new
</div>
```

Icon only:

```html
<div class="wp-block-button blockparty-button-icon blockparty-button-icon--only">
<a class="wp-block-button__link wp-element-button" href="#">
<span class="wp-block-icon" aria-hidden="true">…</span>
<span class="wp-block-button__label">Next slide</span>
</a>
</div>
```

## Development

### Prerequisites
Expand Down
4 changes: 2 additions & 2 deletions blockparty-button-icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Blockparty Button Icon
* Description: Easily add icons to button blocks in WordPress.
* Version: 1.0.0
* Version: 1.1.0
* Requires at least: 7.0
* Requires PHP: 8.1
* Author: Be API Technical Team
Expand All @@ -27,7 +27,7 @@

require_once __DIR__ . '/vendor/autoload.php';

define( 'BLOCKPARTY_BUTTON_ICON_VERSION', '1.0.0' );
define( 'BLOCKPARTY_BUTTON_ICON_VERSION', '1.1.0' );
define( 'BLOCKPARTY_BUTTON_ICON_URL', plugin_dir_url( __FILE__ ) );
define( 'BLOCKPARTY_BUTTON_ICON_DIR', plugin_dir_path( __FILE__ ) );
define( 'BLOCKPARTY_BUTTON_ICON_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
Expand Down
78 changes: 75 additions & 3 deletions classes/IconBlockRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
namespace Blockparty\ButtonIcon;

/**
* Converts core/icon wrappers so markup stays valid inside button links.
* Converts core/icon wrappers so markup stays valid inside button links
* and marks them as decorative.
*/
class IconBlockRenderer {

Expand Down Expand Up @@ -76,11 +77,56 @@ public function enter_button_with_icon_context( $pre_render, array $parsed_block
* @return string Unchanged HTML.
*/
public function leave_button_with_icon_context( string $block_content, array $block ): string {
if ( $this->is_button_with_icon( $block ) && $this->button_with_icon_depth > 0 ) {
if ( ! $this->is_button_with_icon( $block ) ) {
return $block_content;
}

if ( $this->button_with_icon_depth > 0 ) {
--$this->button_with_icon_depth;
}

return $block_content;
// Without label text, the icon (and its aria-label) is the only accessible
// name left: v1.0.0 icon-only buttons relied on it, so keep it exposed.
if ( ! self::button_label_has_text( $block_content ) ) {
return $block_content;
}

return self::hide_icon_from_assistive_technologies( $block_content );
}

/**
* Whether the button label span contains visible text.
*
* @param string $html Rendered button block HTML.
* @return bool True when `.wp-block-button__label` holds non-whitespace text.
*/
public static function button_label_has_text( string $html ): bool {
$processor = new \WP_HTML_Tag_Processor( $html );

if ( ! $processor->next_tag( array( 'class_name' => 'wp-block-button__label' ) ) ) {
return false;
}

// Track nested spans (e.g. <span lang="en">) to stop at the label closer.
$depth = 1;
$text = '';

while ( $depth > 0 && $processor->next_token() ) {
$token = $processor->get_token_name();

if ( '#text' === $token ) {
$text .= $processor->get_modifiable_text();
continue;
}

if ( 'SPAN' !== $token ) {
continue;
}

$depth += $processor->is_tag_closer() ? -1 : 1;
}

return '' !== trim( (string) preg_replace( '/[\s\x{00A0}]+/u', ' ', $text ) );
}

/**
Expand All @@ -105,6 +151,32 @@ public function render_icon_as_span( string $block_content, array $block ): stri
return self::convert_icon_wrapper_to_span( $block_content );
}

/**
* Marks the icon wrapper as decorative (aria-hidden="true").
*
* Only called when the button label has text: the label then provides the
* accessible name, even when visually hidden (icon only), and an icon
* aria-label would otherwise be announced in addition to it.
*
* @param string $html Rendered icon or button block HTML.
* @return string HTML with aria-hidden on the icon wrapper when applicable.
*/
public static function hide_icon_from_assistive_technologies( string $html ): string {
$processor = new \WP_HTML_Tag_Processor( $html );

if ( ! $processor->next_tag( array( 'class_name' => 'wp-block-icon' ) ) ) {
return $html;
}

if ( 'true' === $processor->get_attribute( 'aria-hidden' ) ) {
return $html;
}

$processor->set_attribute( 'aria-hidden', 'true' );

return $processor->get_updated_html();
}

/**
* Whether the parsed block is a core/button with icon support enabled.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"translation-revision-date":"2026-10-15 22:34+0200","generator":"WP-CLI\/2.11.0","source":"build\/index.js","domain":"messages","locale_data":{"messages":{"":{"domain":"messages","lang":"fr_FR","plural-forms":"nplurals=2; plural=(n != 1);"},"Remove icon":["Retirer l\u2019ic\u00f4ne"],"Icon":["Ic\u00f4ne"],"Show icon":["Afficher l\u2019ic\u00f4ne"],"Icon position":["Position de l\u2019ic\u00f4ne"],"Left":["Gauche"],"Right":["Droite"],"Replace Icon":["Remplacer l\u2019ic\u00f4ne"],"Mark as nofollow":["Marquer en nofollow"],"Button text":["Texte du bouton"],"Add text\u2026":["Ajouter du texte\u2026"],"Link":["Lien"]}}}
{"translation-revision-date":"2026-10-15 22:34+0200","generator":"WP-CLI\/2.12.0","source":"build\/index.js","domain":"messages","locale_data":{"messages":{"":{"domain":"messages","lang":"fr_FR","plural-forms":"nplurals=2; plural=(n != 1);"},"Remove icon":["Retirer l\u2019ic\u00f4ne"],"Icon":["Ic\u00f4ne"],"Show icon":["Afficher l\u2019ic\u00f4ne"],"Icon position":["Position de l\u2019ic\u00f4ne"],"Left":["Gauche"],"Right":["Droite"],"Replace Icon":["Remplacer l\u2019ic\u00f4ne"],"Mark as nofollow":["Marquer en nofollow"],"Button text":["Texte du bouton"],"Add text\u2026":["Ajouter du texte\u2026"],"Link":["Lien"],"Icon only":["Ic\u00f4ne seule"],"Label (screen readers)":["Libell\u00e9 (lecteurs d\u2019\u00e9cran)"],"Required. Hidden on screen, announced by assistive technologies.":["Obligatoire. Masqu\u00e9 \u00e0 l\u2019\u00e9cran, annonc\u00e9 par les technologies d\u2019assistance."],"A label is required for an icon-only button to be accessible.":["Un libell\u00e9 est obligatoire pour qu\u2019un bouton ic\u00f4ne seule soit accessible."],"Inside a button, this icon is decorative: the button label is its accessible name. The icon alternative text is only used when the button label is empty.":["Dans un bouton, cette ic\u00f4ne est d\u00e9corative : le libell\u00e9 du bouton sert de nom accessible. Le texte alternatif de l\u2019ic\u00f4ne n\u2019est utilis\u00e9 que si le libell\u00e9 du bouton est vide."]}}}
Binary file modified languages/blockparty-button-icon-fr_FR.mo
Binary file not shown.
20 changes: 20 additions & 0 deletions languages/blockparty-button-icon-fr_FR.po
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,23 @@ msgstr "Ajouter du texte…"
#: build/index.js:1
msgid "Link"
msgstr "Lien"

#: build/index.js:1
msgid "Icon only"
msgstr "Icône seule"

#: build/index.js:1
msgid "Label (screen readers)"
msgstr "Libellé (lecteurs d’écran)"

#: build/index.js:1
msgid "Required. Hidden on screen, announced by assistive technologies."
msgstr "Obligatoire. Masqué à l’écran, annoncé par les technologies d’assistance."

#: build/index.js:1
msgid "A label is required for an icon-only button to be accessible."
msgstr "Un libellé est obligatoire pour qu’un bouton icône seule soit accessible."

#: build/index.js:1
msgid "Inside a button, this icon is decorative: the button label is its accessible name. The icon alternative text is only used when the button label is empty."
msgstr "Dans un bouton, cette icône est décorative : le libellé du bouton sert de nom accessible. Le texte alternatif de l’icône n’est utilisé que si le libellé du bouton est vide."
24 changes: 22 additions & 2 deletions languages/blockparty-button-icon.pot
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2026-10-15T20:12:02+00:00\n"
"POT-Creation-Date: 2026-11-25T17:40:37+00:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.11.0\n"
"X-Generator: WP-CLI 2.12.0\n"
"X-Domain: blockparty-button-icon\n"

#. Plugin Name of the plugin
Expand Down Expand Up @@ -58,6 +58,22 @@ msgstr ""
msgid "Right"
msgstr ""

#: build/index.js:1
msgid "Icon only"
msgstr ""

#: build/index.js:1
msgid "Label (screen readers)"
msgstr ""

#: build/index.js:1
msgid "Required. Hidden on screen, announced by assistive technologies."
msgstr ""

#: build/index.js:1
msgid "A label is required for an icon-only button to be accessible."
msgstr ""

#: build/index.js:1
msgid "Replace Icon"
msgstr ""
Expand All @@ -77,3 +93,7 @@ msgstr ""
#: build/index.js:1
msgid "Link"
msgstr ""

#: build/index.js:1
msgid "Inside a button, this icon is decorative: the button label is its accessible name. The icon alternative text is only used when the button label is empty."
msgstr ""
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blockparty-button-icon",
"version": "1.0.0",
"version": "1.1.0",
"description": "Easily add icons to button blocks in WordPress.",
"author": "Be API Technical Team",
"license": "GPL-2.0-or-later",
Expand Down
10 changes: 8 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: block, button, icon, gutenberg
Requires at least: 7.0
Tested up to: 7.0
Requires PHP: 8.1
Stable tag: 1.0.0
Stable tag: 1.1.0
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -18,7 +18,7 @@ Blockparty Button Icon enhances the WordPress core Button block (`core/button`)

* Extends the core Button block (no new block name)
* Optional icon via inner blocks (defaults to `core/icon`)
* Icon position: left or right
* Icon position: left, right or icon only (label kept for screen readers)
* Horizontal block gap for icon ↔ label spacing
* Valid HTML inside links (`span` wrapper for icons on the front end)
* Filter `blockparty_button_icon_allowed_icon_blocks` to allow other icon block types
Expand All @@ -37,5 +37,11 @@ By default, only `core/icon` is allowed inside the button. Use the `blockparty_b

== Changelog ==

= 1.1.0 =
* Add an "Icon only" button position: the label is visually hidden but kept for screen readers
* Keep the icon clickable to replace it in icon-only mode
* Mark icons inside buttons as decorative when the button label has text
* Restore keyboard focus on the button after closing the link popover in icon-only mode

= 1.0.0 =
* Initial release
7 changes: 7 additions & 0 deletions src/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@
}
}
}

.blockparty-button-icon-notice {
display: flex;
flex-direction: column;
gap: 16px;
padding: 0 16px 16px;
}
25 changes: 24 additions & 1 deletion src/extend-core-button/button-icon-controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ import {
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from '@wordpress/components';
import { ICON_POSITION_LEFT, ICON_POSITION_RIGHT } from './constants';
import {
ICON_POSITION_LEFT,
ICON_POSITION_ONLY,
ICON_POSITION_RIGHT,
} from './constants';
import IconToolbarSvg from './icon-toolbar-svg';
import { getInnerIconClientId } from './get-inner-icon-client-id';
import { openIconInserterForBlock } from './open-icon-inserter';
import IconOnlyLabelControl from './icon-only-label-control';

/**
* Toolbar and inspector controls for button icons.
Expand All @@ -25,6 +30,7 @@ import { openIconInserterForBlock } from './open-icon-inserter';
* @param {string} props.iconPosition Icon position relative to label.
* @param {boolean} props.hasIconBlock Whether core/icon is available.
* @param {string} props.clientId Button block client id.
* @param {string} [props.text] Button label (rich-text HTML).
* @param {( attributes: Object ) => void} props.setAttributes Attribute updater.
* @return {Element} Controls fragment.
*/
Expand All @@ -33,12 +39,14 @@ export default function ButtonIconControls( {
iconPosition,
hasIconBlock,
clientId,
text,
setAttributes,
} ) {
const iconClientId = useSelect(
( select ) => getInnerIconClientId( select, clientId ),
[ clientId ]
);
const isIconOnly = hasIcon && iconPosition === ICON_POSITION_ONLY;

return (
<>
Expand Down Expand Up @@ -105,7 +113,22 @@ export default function ButtonIconControls( {
'blockparty-button-icon'
) }
/>
<ToggleGroupControlOption
value={ ICON_POSITION_ONLY }
label={ __(
'Icon only',
'blockparty-button-icon'
) }
/>
</ToggleGroupControl>
{ isIconOnly && (
<IconOnlyLabelControl
text={ text }
onChange={ ( value ) => {
setAttributes( { text: value } );
} }
/>
) }
<Button
variant="secondary"
__next40pxDefaultSize
Expand Down
Loading
Loading