kibana/packages/kbn-eslint-plugin-css
Kibana Machine 91115abf8d
[8.x] Resolve false positives with eslint no color rule (#204848) (#204906)
# Backport

This will backport the following commits from `main` to `8.x`:
- [Resolve false positives with eslint no color rule
(#204848)](https://github.com/elastic/kibana/pull/204848)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Eyo O.
Eyo","email":"7893459+eokoneyo@users.noreply.github.com"},"sourceCommit":{"committedDate":"2024-12-19T10:26:48Z","message":"Resolve
false positives with eslint no color rule (#204848)\n\n##
Summary\r\n\r\nFixes error in lint rule that resulted in false
positives, also added a\r\ntest case to ascertain the issue has been
fixed. For context the error\r\nhappens in instances where specific CSS
declarations that are\r\nderivatives of shorthand declarations that can
apply color to the HTML\r\nelement or text nodes where found, because
the check we had simply\r\nchecked if we got a string back instead of
asserting that it was a falsy\r\nvalue.\r\n\r\n## Before\r\n![Screenshot
2024-12-17 at 10
27\r\n18 PM](https://github.com/user-attachments/assets/b0918d37-22f6-4778-a6d0-2cafe11b18e1)\r\n\r\n![Screenshot
2024-12-17 at 8
03\r\n33 PM](https://github.com/user-attachments/assets/d70c733d-e88f-42d6-956a-e266d53724f9)\r\n\r\n##
After\r\n\r\n<img width=\"755\" alt=\"Screenshot 2024-12-19 at 10 25
41\"\r\nsrc=\"https://github.com/user-attachments/assets/3e334785-d657-46ac-86c7-59d37f176c86\"\r\n/>\r\n\r\n\r\n<img
width=\"915\" alt=\"Screenshot 2024-12-19 at 10 23
33\"\r\nsrc=\"https://github.com/user-attachments/assets/87860189-92c5-4807-b5b0-2520b9e75778\"\r\n/>\r\n\r\n<!--\r\n###
Checklist\r\n\r\nCheck the PR satisfies following conditions.
\r\n\r\nReviewers should verify this PR satisfies this list as
well.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] If a plugin
configuration key changed, check if it needs to be\r\nallowlisted in the
cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This was checked for breaking HTTP API changes, and any
breaking\r\nchanges have been approved by the breaking-change committee.
The\r\n`release_note:breaking` label should be applied in these
situations.\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] The PR description includes
the appropriate Release Notes section,\r\nand the correct
`release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n###
Identify risks\r\n\r\nDoes this PR introduce any risks? For example,
consider risks like hard\r\nto test bugs, performance regression,
potential of data loss.\r\n\r\nDescribe the risk, its severity, and
mitigation for each identified\r\nrisk. Invite stakeholders and evaluate
how to proceed before merging.\r\n\r\n- [ ] [See some
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\r\n-
[ ]
...\r\n\r\n\r\n-->","sha":"7cbd9b0c1d19358470ad4c8a36dcc0a02b46f532","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:SharedUX","backport:prev-minor"],"title":"Resolve
false positives with eslint no color rule
","number":204848,"url":"https://github.com/elastic/kibana/pull/204848","mergeCommit":{"message":"Resolve
false positives with eslint no color rule (#204848)\n\n##
Summary\r\n\r\nFixes error in lint rule that resulted in false
positives, also added a\r\ntest case to ascertain the issue has been
fixed. For context the error\r\nhappens in instances where specific CSS
declarations that are\r\nderivatives of shorthand declarations that can
apply color to the HTML\r\nelement or text nodes where found, because
the check we had simply\r\nchecked if we got a string back instead of
asserting that it was a falsy\r\nvalue.\r\n\r\n## Before\r\n![Screenshot
2024-12-17 at 10
27\r\n18 PM](https://github.com/user-attachments/assets/b0918d37-22f6-4778-a6d0-2cafe11b18e1)\r\n\r\n![Screenshot
2024-12-17 at 8
03\r\n33 PM](https://github.com/user-attachments/assets/d70c733d-e88f-42d6-956a-e266d53724f9)\r\n\r\n##
After\r\n\r\n<img width=\"755\" alt=\"Screenshot 2024-12-19 at 10 25
41\"\r\nsrc=\"https://github.com/user-attachments/assets/3e334785-d657-46ac-86c7-59d37f176c86\"\r\n/>\r\n\r\n\r\n<img
width=\"915\" alt=\"Screenshot 2024-12-19 at 10 23
33\"\r\nsrc=\"https://github.com/user-attachments/assets/87860189-92c5-4807-b5b0-2520b9e75778\"\r\n/>\r\n\r\n<!--\r\n###
Checklist\r\n\r\nCheck the PR satisfies following conditions.
\r\n\r\nReviewers should verify this PR satisfies this list as
well.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] If a plugin
configuration key changed, check if it needs to be\r\nallowlisted in the
cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This was checked for breaking HTTP API changes, and any
breaking\r\nchanges have been approved by the breaking-change committee.
The\r\n`release_note:breaking` label should be applied in these
situations.\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] The PR description includes
the appropriate Release Notes section,\r\nand the correct
`release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n###
Identify risks\r\n\r\nDoes this PR introduce any risks? For example,
consider risks like hard\r\nto test bugs, performance regression,
potential of data loss.\r\n\r\nDescribe the risk, its severity, and
mitigation for each identified\r\nrisk. Invite stakeholders and evaluate
how to proceed before merging.\r\n\r\n- [ ] [See some
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\r\n-
[ ]
...\r\n\r\n\r\n-->","sha":"7cbd9b0c1d19358470ad4c8a36dcc0a02b46f532"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/204848","number":204848,"mergeCommit":{"message":"Resolve
false positives with eslint no color rule (#204848)\n\n##
Summary\r\n\r\nFixes error in lint rule that resulted in false
positives, also added a\r\ntest case to ascertain the issue has been
fixed. For context the error\r\nhappens in instances where specific CSS
declarations that are\r\nderivatives of shorthand declarations that can
apply color to the HTML\r\nelement or text nodes where found, because
the check we had simply\r\nchecked if we got a string back instead of
asserting that it was a falsy\r\nvalue.\r\n\r\n## Before\r\n![Screenshot
2024-12-17 at 10
27\r\n18 PM](https://github.com/user-attachments/assets/b0918d37-22f6-4778-a6d0-2cafe11b18e1)\r\n\r\n![Screenshot
2024-12-17 at 8
03\r\n33 PM](https://github.com/user-attachments/assets/d70c733d-e88f-42d6-956a-e266d53724f9)\r\n\r\n##
After\r\n\r\n<img width=\"755\" alt=\"Screenshot 2024-12-19 at 10 25
41\"\r\nsrc=\"https://github.com/user-attachments/assets/3e334785-d657-46ac-86c7-59d37f176c86\"\r\n/>\r\n\r\n\r\n<img
width=\"915\" alt=\"Screenshot 2024-12-19 at 10 23
33\"\r\nsrc=\"https://github.com/user-attachments/assets/87860189-92c5-4807-b5b0-2520b9e75778\"\r\n/>\r\n\r\n<!--\r\n###
Checklist\r\n\r\nCheck the PR satisfies following conditions.
\r\n\r\nReviewers should verify this PR satisfies this list as
well.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] If a plugin
configuration key changed, check if it needs to be\r\nallowlisted in the
cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This was checked for breaking HTTP API changes, and any
breaking\r\nchanges have been approved by the breaking-change committee.
The\r\n`release_note:breaking` label should be applied in these
situations.\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] The PR description includes
the appropriate Release Notes section,\r\nand the correct
`release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n###
Identify risks\r\n\r\nDoes this PR introduce any risks? For example,
consider risks like hard\r\nto test bugs, performance regression,
potential of data loss.\r\n\r\nDescribe the risk, its severity, and
mitigation for each identified\r\nrisk. Invite stakeholders and evaluate
how to proceed before merging.\r\n\r\n- [ ] [See some
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)\r\n-
[ ]
...\r\n\r\n\r\n-->","sha":"7cbd9b0c1d19358470ad4c8a36dcc0a02b46f532"}}]}]
BACKPORT-->

Co-authored-by: Eyo O. Eyo <7893459+eokoneyo@users.noreply.github.com>
2024-12-19 06:23:17 -06:00
..
src/rules [8.x] Resolve false positives with eslint no color rule (#204848) (#204906) 2024-12-19 06:23:17 -06:00
index.ts
jest.config.js
kibana.jsonc
package.json
README.mdx
tsconfig.json

---
id: kibSharedUXEslintPluginCSS
slug: /kibana-dev-docs/shared-ux/packages/kbn-eslint-plugin-css
title: '@kbn/eslint-plugin-design-tokens'
description: Custom ESLint rules to guardrails for using eui in the Kibana repository
date: 2024-11-19
tags: ['kibana', 'dev', 'contributor', 'shared_ux', 'eslint', 'eui']
---

# Summary

`@kbn/eslint-plugin-css` is an ESLint plugin providing custom ESLint rules to help setup guardrails for using eui in the Kibana repo especially around styling.

The aim of this package is to help engineers to modify EUI components in a much complaint way.

If a rule does not behave as you expect or you have an idea of how these rules can be improved, please reach out to the Shared UX team.

# Rules

## `@kbn/css/no_css_color`

This rule warns engineers to not use literal css color in the codebase, particularly for CSS properties that apply color to 
either the html element or text nodes, but rather urge users to defer to using the color tokens provided by EUI.

This rule kicks in on the following JSXAttributes; `style`, `className` and `css` and supports various approaches to providing styling declarations.

### Example

The following code:

```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { EuiText } from '@elastic/eui';

function MyComponent() {
    return (
        <EuiText style={{ color: 'red' }}>You know, for search</EuiText>
    )
}
```

```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { EuiText } from '@elastic/eui';

function MyComponent() {

    const style = {
        color: 'red'
    }

    return (
        <EuiText style={{ color: style.color }}>You know, for search</EuiText>
    )
}
```

```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { EuiText } from '@elastic/eui';

function MyComponent() {
    const colorValue = '#dd4040';

    return (
        <EuiText style={{ color: colorValue }}>You know, for search</EuiText>
    )
}
```

will all raise an eslint report with an appropriate message of severity that matches the configuration of the rule, further more all the examples above
will also match for when the attribute in question is `css`. The `css` attribute will also raise a report the following cases below;

```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { css } from '@emotion/css';
import { EuiText } from '@elastic/eui';

function MyComponent() {
    return (
        <EuiText css={css`color: '#dd4040' `}>You know, for search</EuiText>
    )
}
```

```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { EuiText } from '@elastic/eui';

function MyComponent() {
    return (
        <EuiText css={() => ({ color: '#dd4040' })}>You know, for search</EuiText>
    )
}
```

A special case is also covered for the `className` attribute, where the rule will also raise a report for the following case below;


```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { css } from '@emotion/css';
import { EuiText } from '@elastic/eui';

function MyComponent() {
    return (
        <EuiText className={css`color: '#dd4040'`}>You know, for search</EuiText>
    )
}
```

it's worth pointing out that although the examples provided are specific to EUI components, this rule applies to all JSX elements.

## `@kbn/css/prefer_css_attributes_for_eui_components`

This rule warns engineers to use the `css` attribute for EUI components instead of the `style` attribute.