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

[Security Solution][Data Quality Dashboard]: migrate styled-component… #205559

Merged

Conversation

kapral18
Copy link
Contributor

@kapral18 kapral18 commented Jan 4, 2025

…s to Emotion

Addresses #205449

This PR migrates ecs_data_quality_dashboard package from styled-components to emotion. In the process we also convert the kbn/ui-theme json tokens to euiTheme counterparts.

Additionally we decorate root babel-jest transform locally in security_solution/public/overview and ecs_data_quality_dashboard package folder to include @emotion/babel-preset-css-prop.

The reason for local babel-jest transforms is that root babel-jest transform doesn't include @emotion/babel-preset-css-prop which is necessary for proper compilation of emotion css prop in tests. Without it there is a warning
image
appearing in every test that tests a component that uses css prop with theme function passed into it. Other use cases seem to be compiling fine without this babel preset. But theme callback is a valid way of using emotion so we shouldn't avoid using it just because it's not added properly to the test compilation step. Hence I am adding it locally to ecs_data_quality_dashboard package and security_solution/public/overview.

@kapral18 kapral18 self-assigned this Jan 4, 2025
@kapral18 kapral18 force-pushed the feat/data-quality/205449-migrate-to-emotion branch 3 times, most recently from c5df37c to b91c385 Compare January 4, 2025 17:57
@kapral18 kapral18 marked this pull request as ready for review January 4, 2025 19:48
@kapral18 kapral18 requested review from a team as code owners January 4, 2025 19:48
@kapral18 kapral18 added release_note:skip Skip the PR/issue when compiling release notes backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) Team:Threat Hunting:Explore labels Jan 4, 2025
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-threat-hunting-explore (Team:Threat Hunting:Explore)

@kapral18 kapral18 force-pushed the feat/data-quality/205449-migrate-to-emotion branch 2 times, most recently from 33c4085 to 4d0d6da Compare January 5, 2025 01:33
Comment on lines +27 to +30
linkText: css({
display: 'flex',
gap: euiTheme.size.xs,
}),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using object notation is recommended for 2 reasons:

  1. emotion docs advise to use it for typescript https://emotion.sh/docs/typescript#emotionreact

image

  1. Free Autocomplete of keys and values.

Comment on lines +11 to +14
export const linkTextCss = ({ euiTheme }: UseEuiTheme): CSSObject => ({
display: 'flex',
gap: euiTheme.size.s,
});
Copy link
Contributor Author

@kapral18 kapral18 Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of defining shared useStyles hooks unnecessarily, I decided to export small nuggets of shared css snippets like this. When passed directly into css={} prop as a function emotion passes in the theme for free so no need for import, also no need to wrap the properties in css() call.

@@ -113,7 +113,7 @@ describe('LegacyHistoricalCheckFields', () => {
<TestExternalProviders>
<EuiMarkdownFormat>{tablesMarkdown}</EuiMarkdownFormat>
</TestExternalProviders>
).container.innerHTML
).container.children[0].innerHTML
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some reason the this container started producing an additional <span> I didn't spend time debugging why but I checked that it works as expected visually


export interface Props {
docsCount: number;
ilmPhase?: string;
sizeInBytes?: number;
sameFamilyFieldsCount?: number;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleaning up leftovers from before

className="eui-yScroll"
$width={LEGEND_WIDTH}
>
<div css={styles.legendContainer} data-test-subj="legend" className="eui-scrollBar">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this className change is intentional and is needed because eui-yScroll is added on <style> level and previously <LegendContainer> styled-components styles were also added on style level and took precedence.

But now after replacing with emotion, emotion styles are now coming from compiled css bundle and are being overshadowed by eui-yScroll class's block-size property which completely breaks the height restriction of the container and renders the list unrestricted, so scroll is removed.

I found the eui-scrollBar which is less opinionated and doesn't enforce block-size but applies same basic styling for scrolling https://eui.elastic.co/#/utilities/scroll%23scroll-bar-style I just needed to add overflowY: auto explicitly in this case to achieve parity with eui-yScroll behavior.

@@ -25,7 +25,6 @@ module.exports = {
/x-pack[\/\\]solutions[\/\\]observability[\/\\]plugins[\/\\]synthetics[\/\\]/,
/x-pack[\/\\]solutions[\/\\]observability[\/\\]plugins[\/\\]uptime[\/\\]/,
/x-pack[\/\\]solutions[\/\\]observability[\/\\]plugins[\/\\]ux[\/\\]/,
/x-pack[\/\\]solutions[\/\\]security[\/\\]packages[\/\\]ecs_data_quality_dashboard[\/\\]/,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line was the puzzle piece that helped me debug what happened before with my faulty setup last time I tried to convert to emotion and failed. Turns out we have to uncomment this line to allow proper emotion compilation on webpack side. Now everything works as expected.

cc: @semd @angorayc @agusruidiazgd

@kapral18 kapral18 force-pushed the feat/data-quality/205449-migrate-to-emotion branch from 80a70e3 to b7d2a9d Compare January 5, 2025 11:46
@kapral18 kapral18 requested a review from a team as a code owner January 5, 2025 11:46
</MockKibanaContextProvider>
</I18nProvider>
<MockKibanaContextProvider startServices={startServices}>
<KibanaRenderContextProvider {...coreMock.createStart()}>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this turned out to cause issues with existing tests, so I moved this to data_quality.test only for now with latest commit

@kapral18 kapral18 force-pushed the feat/data-quality/205449-migrate-to-emotion branch from c4ebd35 to 90240b3 Compare January 5, 2025 15:53
kapral18 and others added 5 commits January 5, 2025 17:01
…s to Emotion

Addresses elastic#205449

- Replaced styled-components with Emotion for styling components
- Updated all styled-components imports to Emotion
- Refactored styles using Emotion's `css` and `useEuiTheme` hooks
- Ensured all components are using Emotion's `css` prop for styling
- Updated tsconfig to include Emotion types and configuration
- Removed ecs_data_quality_dashboard package from
packages/kbn-babel-preset/styled_components_files.js so that emotion is
properly compiled
The whole point of this change is to make babel-jest transform options
shared and reusable by other packages' jest.config transforms that need
to add their own custom stuff. For example here
ecs_data_quality_dashboard
makes use of that to add @emotion/babel-preset-css-prop that is needed
to properly compile emotion css prop in jest tests.
- Add a new babel transformer for jest in the security solution plugin
  to enable overview/data_quality.test.tsx to pass without compilation
  warnings
- Integrate `@emotion/babel-preset-css-prop` with custom label format
- Update `test_providers.tsx` to include `KibanaRenderContextProvider` and `coreMock`
- Modify jest configuration in `overview` to use the new babel transformer
- Ensure compatibility with existing jest preset and module name mapping
- Removed KibanaRenderContextProvider from TestProvidersComponent due to
  issues in current overview tests
- Updated test setup in data_quality.test.tsx to include KibanaRenderContextProvider instead
@kapral18 kapral18 force-pushed the feat/data-quality/205449-migrate-to-emotion branch from 90240b3 to 5ab5bfe Compare January 5, 2025 16:03
@kapral18
Copy link
Contributor Author

kapral18 commented Jan 5, 2025

/ci

@elasticmachine
Copy link
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

Metrics [docs]

‼️ ERROR: no builds found for mergeBase sha [211d4a6]

History

cc @kapral18

@kapral18 kapral18 added ci:cloud-deploy Create or update a Cloud deployment ci:cloud-persist-deployment Persist cloud deployment indefinitely ci:project-deploy-security Create a Security Serverless Project ci:project-persist-deployment Persist project deployment indefinitely labels Jan 5, 2025
Copy link
Member

@pheyos pheyos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kbn-test changes LGTM

Comment on lines +77 to +78
<MockKibanaContextProvider startServices={startServices}>
<I18nProvider>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come these needed to be swapped?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good question. Initially I checked the root of security_solution app and it was wrapped in KibanaContextProvider without anything above it. But now after double checking I see that I18nProvider is not even present anywhere, only in tests. But we can clean it up later wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, can be cleaned up later. You already have 2 approvals here :)

Copy link
Member

@KDKHD KDKHD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ for security_solution files!

@kapral18 kapral18 removed ci:cloud-deploy Create or update a Cloud deployment ci:cloud-persist-deployment Persist cloud deployment indefinitely ci:project-deploy-security Create a Security Serverless Project ci:project-persist-deployment Persist project deployment indefinitely labels Jan 6, 2025
@kapral18 kapral18 merged commit 72980e1 into elastic:main Jan 6, 2025
20 checks passed
@kapral18 kapral18 deleted the feat/data-quality/205449-migrate-to-emotion branch January 6, 2025 13:48
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.x

https://github.com/elastic/kibana/actions/runs/12633676216

@kibanamachine
Copy link
Contributor

💔 All backports failed

Status Branch Result
8.x Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 205559

Questions ?

Please refer to the Backport tool documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) release_note:skip Skip the PR/issue when compiling release notes Team:Threat Hunting:Explore v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants