Skip to content

Commit

Permalink
♿️ Mark invalid fields via area for screen readers
Browse files Browse the repository at this point in the history
Closes #25
  • Loading branch information
MatzeKitt committed Aug 6, 2023
1 parent a20e653 commit a0b97df
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions assets/js/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,38 @@ document.addEventListener( 'DOMContentLoaded', function() {
} );
};
};

const newFormErrorObserver = new MutationObserver( function( mutations, observer ) {
const formErrors = form.querySelectorAll( '.form-error' );
const oldFormErrors = form.querySelectorAll( '.form-block__element:not(.form-error) [aria-invalid="true"]' );

if ( formErrors ) {
for ( const formError of formErrors ) {
if ( formError.classList.contains( 'wp-block-form-block-input' ) ) {
formError.querySelector( 'input' ).ariaInvalid = true;
}
else if ( formError.classList.contains( 'wp-block-form-block-select' ) ) {
formError.querySelector( 'select' ).ariaInvalid = true;
}
else if ( formError.classList.contains( 'wp-block-form-block-textarea' ) ) {
formError.querySelector( 'textarea' ).ariaInvalid = true;
}
};
}

if ( oldFormErrors ) {
for ( const oldFormError of oldFormErrors ) {
oldFormError.ariaInvalid = false;
}
}
} );

newFormErrorObserver.observe(
form,
{
attributeFilter: [ 'class' ],
subtree: true,
}
);
};
} );

0 comments on commit a0b97df

Please sign in to comment.