mirror of
https://github.com/elastic/kibana.git
synced 2025-06-28 11:05:39 -04:00
## Summary Closes https://github.com/elastic/kibana-team/issues/1272 This PR adds implementation for eslint rules to help facilitate the migration away from SASS files to leveraging the design tokens EUI provides for styling. The introduced rules in this PR are as follows; - #### No CSS Color values Consider; ```tsx <EuiCode style={{ color: '#dd4040' }}>Hello World!</EuiCode> ``` this expression because it specifies the css color property, with a valid [CSS color value](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value), when the aforementioned rule is enabled depending on the set report level set the user would get a feedback, see screenshot below; <img width="735" alt="Screenshot 2024-11-20 at 12 46 17" src="https://github.com/user-attachments/assets/d2f608dc-782c-4d83-88e6-92dfdd8f6101"> This rule also works for variables defined elsewhere in the code and referenced as a value to the style prop, see screenshot below; <img width="1658" alt="Screenshot 2024-11-26 at 13 29 45" src="https://github.com/user-attachments/assets/f8aadf6b-318b-4c6a-b7c9-bb44fb867b58"> feedback will also be provided when some variable that is a literal value is specified as a value for any earmarked property that should not specify literal values. <img width="1730" alt="Screenshot 2024-11-28 at 19 00 08" src="https://github.com/user-attachments/assets/bc3a8674-9469-4c7a-b0c9-7a2bfa7f08dc"> feedback will be provided for referencing a member prop of some object defined elsewhere as a value to any earmarked property that we have deemed to not specify literal values <img width="1676" alt="Screenshot 2024-11-29 at 11 36 44" src="https://github.com/user-attachments/assets/c4537fbf-b2d8-46bb-ad5f-8582e8c9a932"> Supports; - object values - object references - template literals - tagged templates This approach does not penalize variable declarations, only the usages of any said variable when it doesn't conform to expectation - #### Prefer CSS attributes for EUI components (optional) Consider; ```tsx <EuiCode style={{ someCSSProperty: 'propertyValue' }}>Hello World!</EuiCode> ``` A declaration like the one above, will be regarded as an error and can be fixed, when it's fixed it will be re-written as ```tsx <EuiCode css={{ someCSSProperty: 'propertyValue' }}>Hello World!</EuiCode> ``` <!-- ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_node:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ... --> --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> |
||
---|---|---|
.. | ||
src/rules | ||
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.