Skip to content

Commit d0c3fe1

Browse files
committed
fix(ui): use button instead of anchor for Quick Create
Replace <a href="#"> anti-pattern with a semantic <button> element. Reset button styles via CSS, add aria-label for accessibility, and replace the button with an <a> edit link on success.
1 parent 9c81a5d commit d0c3fe1

5 files changed

Lines changed: 24 additions & 23 deletions

File tree

assets/css/msls.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/msls-quick-create.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/Component/Component.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ abstract class Component {
1212
const INPUT_PREFIX = 'msls_input_';
1313

1414
const ALLOWED_HTML = array(
15-
'a' => array(
16-
'href' => true,
17-
'title' => true,
15+
'button' => array(
16+
'type' => true,
1817
'class' => true,
19-
'target' => true,
18+
'title' => true,
19+
'aria-label' => true,
2020
'data-target-blog-id' => true,
2121
'data-source-post-id' => true,
2222
'data-source-blog-id' => true,

includes/MslsAdminIcon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ protected function get_quick_create_a(): string {
245245
$title = sprintf( $format, $this->language );
246246

247247
return sprintf(
248-
'<a class="msls-quick-create" href="#" title="%1$s" data-target-blog-id="%2$d" data-source-post-id="%3$d" data-source-blog-id="%4$d">%5$s</a>&nbsp;',
248+
'<button type="button" class="msls-quick-create" title="%1$s" aria-label="%1$s" data-target-blog-id="%2$d" data-source-post-id="%3$d" data-source-blog-id="%4$d">%5$s</button>&nbsp;',
249249
esc_attr( $title ),
250250
$target_blog_id,
251251
$this->id,

src/msls-quick-create.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,37 @@ jQuery( document ).ready(
33
$( document ).on(
44
'click',
55
'.msls-quick-create',
6-
function ( e ) {
7-
e.preventDefault();
6+
function () {
7+
var $button = $( this );
88

9-
var $link = $( this );
10-
11-
if ( $link.hasClass( 'msls-loading' ) ) {
9+
if ( $button.hasClass( 'msls-loading' ) ) {
1210
return;
1311
}
1412

15-
$link.addClass( 'msls-loading' );
16-
$link.find( '.dashicons' ).removeClass( 'dashicons-plus' ).addClass( 'dashicons-update' );
13+
$button.addClass( 'msls-loading' );
14+
$button.find( '.dashicons' ).removeClass( 'dashicons-plus' ).addClass( 'dashicons-update' );
1715

1816
wp.apiFetch(
1917
{
2018
path: '/msls/v1/create-translation',
2119
method: 'POST',
2220
data: {
23-
source_post_id: parseInt( $link.data( 'source-post-id' ), 10 ),
24-
source_blog_id: parseInt( $link.data( 'source-blog-id' ), 10 ),
25-
target_blog_id: parseInt( $link.data( 'target-blog-id' ), 10 )
21+
source_post_id: parseInt( $button.data( 'source-post-id' ), 10 ),
22+
source_blog_id: parseInt( $button.data( 'source-blog-id' ), 10 ),
23+
target_blog_id: parseInt( $button.data( 'target-blog-id' ), 10 )
2624
}
2725
}
2826
).then(
2927
function ( response ) {
30-
$link.removeClass( 'msls-quick-create msls-loading' );
31-
$link.removeAttr( 'data-target-blog-id data-source-post-id data-source-blog-id' );
32-
$link.attr( 'href', response.edit_url );
33-
$link.attr( 'title', $link.attr( 'title' ).replace( /Create/, 'Edit' ) );
28+
var $link = $( '<a>' )
29+
.attr( 'href', response.edit_url )
30+
.attr( 'title', $button.attr( 'title' ).replace( /Create/, 'Edit' ) )
31+
.html( $button.html() );
32+
3433
$link.find( '.dashicons' ).removeClass( 'dashicons-update dashicons-plus' ).addClass( 'dashicons-edit' );
3534

35+
$button.replaceWith( $link );
36+
3637
var $container = $link.closest( 'li' );
3738
if ( ! $container.length ) {
3839
return;
@@ -52,8 +53,8 @@ jQuery( document ).ready(
5253
}
5354
).catch(
5455
function () {
55-
$link.removeClass( 'msls-loading' );
56-
$link.find( '.dashicons' ).removeClass( 'dashicons-update' ).addClass( 'dashicons-plus' );
56+
$button.removeClass( 'msls-loading' );
57+
$button.find( '.dashicons' ).removeClass( 'dashicons-update' ).addClass( 'dashicons-plus' );
5758
}
5859
);
5960
}

0 commit comments

Comments
 (0)