kibana/packages/kbn-eslint-plugin-eui-a11y
Coen Warmer 54add717b6
Create eslint-plugin-eui-a11y plugin (#215859)
## Summary

This adds an additional custom ESLint rule package which checks certain
Eui elements for the existence of an `aria-label` prop.

If it exists, it will leave it untouched. If it doesn't, it will warn
the engineer it needs to be added, and offers a autofix suggestion for
those engineers who have fix on save enabled in their IDE.

<img width="739" alt="Screenshot 2025-03-25 at 13 59 28"
src="https://github.com/user-attachments/assets/0813b317-c752-40d7-b569-e866a3ecf6b0"
/>

<img width="804" alt="Screenshot 2025-03-25 at 13 59 36"
src="https://github.com/user-attachments/assets/3c45c49c-6db8-4740-b5de-89aa534c248b"
/>

This package is an offshoot of the `kbn-eslint-plugin-i18n` and
`kbn-eslint-plugin-telemetry` packages.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2025-03-26 23:00:31 +02:00
..
helpers Create eslint-plugin-eui-a11y plugin (#215859) 2025-03-26 23:00:31 +02:00
rules Create eslint-plugin-eui-a11y plugin (#215859) 2025-03-26 23:00:31 +02:00
index.ts Create eslint-plugin-eui-a11y plugin (#215859) 2025-03-26 23:00:31 +02:00
jest.config.js Create eslint-plugin-eui-a11y plugin (#215859) 2025-03-26 23:00:31 +02:00
kibana.jsonc Create eslint-plugin-eui-a11y plugin (#215859) 2025-03-26 23:00:31 +02:00
package.json Create eslint-plugin-eui-a11y plugin (#215859) 2025-03-26 23:00:31 +02:00
README.mdx Create eslint-plugin-eui-a11y plugin (#215859) 2025-03-26 23:00:31 +02:00
tsconfig.json Create eslint-plugin-eui-a11y plugin (#215859) 2025-03-26 23:00:31 +02:00

---
id: kibDevDocsOpsEslintPluginEuiA11y
slug: /kibana-dev-docs/ops/kbn-eslint-plugin-eui-a11y
title: '@kbn/eslint-plugin-eui-a11y'
description: Custom ESLint rules to support a11y on Eui components in the Kibana repository
tags: ['kibana', 'dev', 'contributor', 'operations', 'eslint', 'eui-a11y']
---

`@kbn/eslint-plugin-eui-a11y` is an ESLint plugin providing custom rules for validating JSXCode in the Kibana repo for accessibility (a11y) (i.e. aria) tags on Eui components.

## `@kbn/eui-a11y/eui_elements_should_have_aria_label_or_aria_labelledby_props`

This rule warns engineers to add `aria-label` to certain EUI components, and offers a suggestion for a value.

If an `aria-label` or `aria-labelledby` prop is already found, the rule does not warn. Existing values are never overwritten.

It is currently set to respond to a subset of EUI components which reasonably need an `aria-label`: `EuiButtonIcon`, `EuiButtonEmpty`, `EuiSelect`, `EuiSuperSelect`.

The rule attempts to create a reasonable suggestion for a value depending on the context of the component (i.e. app name, filename, function name, values inside children).

It then create a i18.translate function and uses the suggestion for a defaultMessage.

### Examples:

```
/* file is in `x-pack/solutions/observability/plugins/apm/public/components/fleet_integration/apm_agents/runtime_attachment/discovery_rule.tsx` */

<EuiButtonIcon
    iconType="pencil"
    onClick={() => {
        onEdit(id);
    }}
/>
```

turns into:

```
<EuiButtonIcon
    aria-label={i18n.translate('xpack.apm.discoveryRule.pencilButton.ariaLabel', {
        defaultMessage: 'Pencil Button',
    })}
    iconType="pencil"
    onClick={() => {
        onEdit(id);
    }}
/>
```

Another example:

```
/* file is in: `x-pack/solutions/observability/plugins/infra/public/components/beta_badge.tsx` */

export const InfraBetaBadge = ({ iconType, tooltipPosition, tooltipContent }: Props) => (
  <EuiBetaBadge
    label={i18n.translate('xpack.infra.common.tabBetaBadgeLabel', {
      defaultMessage: 'Beta',
    })}
    tooltipContent={tooltipContent}
    iconType={iconType}
    tooltipPosition={tooltipPosition}
    data-test-id="infra-beta-badge"
  />
);
```

gets transformed into:

```
export const InfraBetaBadge = ({ iconType, tooltipPosition, tooltipContent }: Props) => (
  <EuiBetaBadge
    aria-label={i18n.translate('xpack.infra.infraBetaBadge.betaBadge.ariaLabel', {
      defaultMessage: 'Beta Badge',
    })}
    label={i18n.translate('xpack.infra.common.tabBetaBadgeLabel', {
      defaultMessage: 'Beta',
    })}
    tooltipContent={tooltipContent}
    iconType={iconType}
    tooltipPosition={tooltipPosition}
    data-test-id="infra-beta-badge"
  />
);
```

### I want to add / remove this behavior for a particular EuiElement

See the `EUI_ELEMENTS` array in `packages/kbn-eslint-plugin-eui-a11y/rules/eui_elements_should_have_aria_label_or_aria_labelledby_props.ts` to see or adjust the list.

### The autofix provided is not 100% correct!

The autofix is provided as a time saver to save you from typing. Feel free to adjust it after applying.