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

fix: prevent unwarranted a11y warnings on custom Button subclasses #3238

Merged
merged 4 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions js/src/admin/components/UploadImageButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Button from '../../common/components/Button';

export default class UploadImageButton extends Button {
loading = false;
ignoreNoChildrenWarning = true;

view(vnode) {
this.attrs.loading = this.loading;
Expand Down
11 changes: 10 additions & 1 deletion js/src/common/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ export interface IButtonAttrs extends ComponentAttrs {
* styles can be applied by providing `className="Button"` to the Button component.
*/
export default class Button<CustomAttrs extends IButtonAttrs = IButtonAttrs> extends Component<CustomAttrs> {
/**
* Prevents firing of accessibility warnings for this Button component.
*
* This should be set to `true` when creating a custom Button class component which
* does not expect any children to be provided to it because, for example, it has a
* custom `view` method with its own content baked-in.
*/
protected ignoreNoChildrenWarning: boolean = false;

view(vnode: Mithril.VnodeDOM<CustomAttrs, this>) {
let { type, title, 'aria-label': ariaLabel, icon: iconName, disabled, loading, className, class: _class, ...attrs } = this.attrs;

Expand Down Expand Up @@ -107,7 +116,7 @@ export default class Button<CustomAttrs extends IButtonAttrs = IButtonAttrs> ext

const { 'aria-label': ariaLabel } = this.attrs;

if (!ariaLabel && !extractText(vnode.children) && !this.element?.getAttribute?.('aria-label')) {
davwheat marked this conversation as resolved.
Show resolved Hide resolved
if (!this.ignoreNoChildrenWarning && !ariaLabel && !extractText(vnode.children) && !this.element?.getAttribute?.('aria-label')) {
fireDebugWarning(
'[Flarum Accessibility Warning] Button has no content and no accessible label. This means that screen-readers will not be able to interpret its meaning and just read "Button". Consider providing accessible text via the `aria-label` attribute. https://web.dev/button-name',
this.element
Expand Down