feature: Add field for ARIA label in button block

This commit is contained in:
Keith Solomon
2025-11-25 08:50:39 -06:00
parent f086c17d26
commit a1d2b59919
3 changed files with 133 additions and 70 deletions

View File

@@ -26,7 +26,8 @@
"other_choice": 0,
"allow_in_bindings": 0,
"layout": "horizontal",
"save_other_choice": 0
"save_other_choice": 0,
"wpml_cf_preferences": 1
},
{
"key": "field_5f7f85a71151d",
@@ -42,7 +43,8 @@
"class": "",
"id": ""
},
"return_format": "array"
"return_format": "array",
"wpml_cf_preferences": 1
},
{
"key": "field_5f7f85bd1151e",
@@ -71,7 +73,10 @@
"allow_in_bindings": 1,
"ui": 0,
"ajax": 0,
"placeholder": ""
"placeholder": "",
"create_options": 0,
"save_options": 0,
"wpml_cf_preferences": 1
},
{
"key": "field_5f7f85e41151f",
@@ -97,7 +102,10 @@
"ui": 0,
"return_format": "value",
"ajax": 0,
"placeholder": ""
"placeholder": "",
"create_options": 0,
"save_options": 0,
"wpml_cf_preferences": 1
},
{
"key": "field_5f807a47dd2a8",
@@ -125,7 +133,10 @@
"allow_in_bindings": 1,
"ui": 0,
"ajax": 0,
"placeholder": ""
"placeholder": "",
"create_options": 0,
"save_options": 0,
"wpml_cf_preferences": 1
},
{
"key": "field_605924972a98a",
@@ -154,7 +165,32 @@
"allow_in_bindings": 1,
"ui": 0,
"ajax": 0,
"placeholder": ""
"placeholder": "",
"create_options": 0,
"save_options": 0,
"wpml_cf_preferences": 1
},
{
"key": "field_6924c83e85da9",
"label": "ARIA Label",
"name": "aria_label",
"aria-label": "",
"type": "text",
"instructions": "Used to give the link an accessible name if using \"Read more\" as the title",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"wpml_cf_preferences": 0,
"default_value": "",
"maxlength": "",
"allow_in_bindings": 0,
"placeholder": "",
"prepend": "",
"append": ""
}
],
"location": [
@@ -175,5 +211,7 @@
"active": true,
"description": "",
"show_in_rest": 0,
"modified": 1742236256
"display_title": "",
"acfml_field_group_mode": "translation",
"modified": 1764018311
}

View File

@@ -1,76 +1,99 @@
class ButtonComponent extends HTMLElement {
/**
* Parameters
* - btnClasses: Additional classes to add to the block.
* - element: The element to use for the button. Defaults to 'a'.
* - url: The URL to link to.
* - target: The target for the link.
* - title: The text to display on the button.
* - color: The color of the button.
* - variant: The variant of the button.
* - size: The size of the button.
* - width: The width of the button.
*
*/
/**
* Parameters
* - btnClasses: Additional classes to add to the block.
* - element: The element to use for the button. Defaults to 'a'.
* - url: The URL to link to.
* - target: The target for the link.
* - title: The text to display on the button.
* - ariaLabel: The ARIA label for the button.
* - color: The color of the button.
* - variant: The variant of the button.
* - size: The size of the button.
* - width: The width of the button.
*
*/
connectedCallback() {
if (!this.querySelector(this.getAttribute('element'))) {
this.append(document.createElement(this.getAttribute('element')));
}
connectedCallback() {
if (!this.querySelector(this.getAttribute('element'))) {
this.append(document.createElement(this.getAttribute('element')));
}
this.update();
}
this.update();
}
static get observedAttributes() {
return [
'btnClasses',
'el',
'element',
'type',
'url',
'target',
'title',
'color',
'variant',
'size',
'width',
]
}
static get observedAttributes() {
return [
'btnClasses',
'el',
'element',
'type',
'url',
'target',
'title',
'ariaLabel',
'color',
'variant',
'size',
'width',
]
}
attributeChangedCallback() {
this.update();
}
attributeChangedCallback() {
this.update();
}
update() {
const btn = this.querySelector(this.getAttribute('element'));
update() {
const btn = this.querySelector(this.getAttribute('element'));
if (btn) {
btn.classList = this.getAttribute('btnClasses') || '';
// console.log('[ButtonComponent] attributes', {
// btnClasses: this.getAttribute('btnClasses'),
// element: this.getAttribute('element'),
// type: this.getAttribute('type'),
// url: this.getAttribute('url'),
// target: this.getAttribute('target'),
// title: this.getAttribute('title'),
// ariaLabel: this.getAttribute('ariaLabel'),
// color: this.getAttribute('color'),
// variant: this.getAttribute('variant'),
// size: this.getAttribute('size'),
// width: this.getAttribute('width'),
// });
if (this.getAttribute('element') == 'a') {
btn.href = this.getAttribute('url') || '#';
if (btn) {
btn.classList = this.getAttribute('btnClasses') || '';
if (btn.target) {
btn.target = 'target="${this.getAttribute(target)}"';
}
}
if (this.getAttribute('element') == 'a') {
btn.href = this.getAttribute('url') || '#';
const type = this.getAttribute('type');
if (type && this.getAttribute('element') !== 'a') {
btn.type = type;
}
if (btn.target) {
btn.target = 'target="${this.getAttribute(target)}"';
}
}
btn.title = this.getAttribute('title') || '';
btn.textContent = this.getAttribute('title') || '';
const type = this.getAttribute('type');
if (type && this.getAttribute('element') !== 'a') {
btn.type = type;
}
btn.setAttribute('data-button-color', this.getAttribute('color'));
btn.setAttribute('data-button-variant', this.getAttribute('variant'));
btn.setAttribute('data-button-size', this.getAttribute('size'));
btn.setAttribute('data-button-width', this.getAttribute('width'));
}
}
btn.title = this.getAttribute('title') || '';
btn.textContent = this.getAttribute('title') || '';
if (!this.getAttribute('ariaLabel') && this.getAttribute('url')) {
btn.setAttribute('aria-label', `Link to ${this.getAttribute('url')}`);
} else {
btn.setAttribute('aria-label', this.getAttribute('ariaLabel'));
}
btn.setAttribute('aria-label', this.getAttribute('ariaLabel'));
btn.setAttribute('data-button-color', this.getAttribute('color'));
btn.setAttribute('data-button-variant', this.getAttribute('variant'));
btn.setAttribute('data-button-size', this.getAttribute('size'));
btn.setAttribute('data-button-width', this.getAttribute('width'));
}
}
}
export const registerButtonComponent = () => {
customElements.define('x-button', ButtonComponent);
customElements.define('x-button', ButtonComponent);
}

View File

@@ -10,9 +10,10 @@ namespace BasicWP;
// Retrieve ACF fields
$element = get_field( 'element' ) ? get_field( 'element' ) : 'a';
$btnLink = get_field( 'link' );
$url = $btnLink['url'] ? $btnLink['url'] : '';
$target = $btnLink['target'] ? $btnLink['target'] : '';
$btnTitle = $btnLink['title'] ? $btnLink['title'] : 'Button';
$url = isset( $btnLink['url'] ) ? $btnLink['url'] : '';
$target = isset( $btnLink['target'] ) ? $btnLink['target'] : '';
$btnTitle = isset( $btnLink['title'] ) ? $btnLink['title'] : 'Button';
$btnAria = get_field( 'aria_label' ) ? get_field( 'aria_label' ) : '';
$color = get_field( 'color' );
$variant = get_field( 'variant' );
$size = get_field( 'size' );
@@ -44,6 +45,7 @@ if ( ! $is_preview ) {
url="<?php echo esc_url( $url ); ?>"
target="<?php echo esc_attr( $target ); ?>"
title="<?php echo esc_attr( $btnTitle ); ?>"
ariaLabel="<?php echo esc_attr( $btnAria ); ?>"
color="<?php echo esc_attr( $color ); ?>"
variant="<?php echo esc_attr( $variant ); ?>"
size="<?php echo esc_attr( $size ); ?>"