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

[Infra] Migrate infra from styled-components to @emotion #202405

Merged
14 changes: 14 additions & 0 deletions x-pack/plugins/observability_solution/infra/emotion.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import '@emotion/react';
import type { UseEuiTheme } from '@elastic/eui';

declare module '@emotion/react' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Theme extends UseEuiTheme {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Threshold', () => {

expect((Metric as jest.Mock).mock.calls[0][0].data[0][0]).toMatchInlineSnapshot(`
Object {
"color": "#f8e9e9",
"color": "#BD271E",
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this potentially be an issue for flakiness should there be any further changes to the design system or theming?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought so, I will check if we can actually not pass the color for the test, or another option

jennypavlova marked this conversation as resolved.
Show resolved Hide resolved
"extra": <React.Fragment>
Alert when &gt;= 7%
<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import React from 'react';
import { Chart, Metric, Settings } from '@elastic/charts';
import { EuiIcon, EuiPanel, useEuiBackgroundColor } from '@elastic/eui';
import { EuiIcon, EuiPanel, UseEuiTheme, useEuiTheme } from '@elastic/eui';
MiriamAparicio marked this conversation as resolved.
Show resolved Hide resolved
import type { PartialTheme, Theme } from '@elastic/charts';
import { i18n } from '@kbn/i18n';
import { COMPARATORS } from '@kbn/alerting-comparators';

export interface ChartProps {
theme?: PartialTheme;
theme?: UseEuiTheme<{}>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this work? {} is the default value of the generic parameter

Suggested change
theme?: UseEuiTheme<{}>;
theme?: UseEuiTheme;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so, i'll fix it on the next PR

baseTheme: Theme;
}

Expand Down Expand Up @@ -41,7 +41,8 @@ export const Threshold = ({
valueFormatter,
warning,
}: Props) => {
const color = useEuiBackgroundColor('danger');
const { euiTheme } = useEuiTheme();
const color = euiTheme.colors.danger;
mgadewoll marked this conversation as resolved.
Show resolved Hide resolved

return (
<EuiPanel
Expand All @@ -56,7 +57,7 @@ export const Threshold = ({
data-test-subj={`threshold-${thresholds.join('-')}-${value}`}
>
<Chart>
<Settings theme={theme} baseTheme={baseTheme} locale={i18n.getLocale()} />
<Settings theme={theme as PartialTheme} baseTheme={baseTheme} locale={i18n.getLocale()} />
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to use as PartialTheme here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The types coming from the emotion Theme and the type requested for Settings theme doesn't match, we need to cast it as PartialTheme to work, I'm open to suggestions on how to fix it in a different way

<Metric
id={id}
data={[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import React from 'react';
import { AnnotationDomainType, LineAnnotation, Position } from '@elastic/charts';
import moment from 'moment';
import { EuiIcon } from '@elastic/eui';
import { EuiIcon, useEuiTheme } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { euiThemeVars } from '@kbn/ui-theme';
import { UI_SETTINGS } from '@kbn/data-plugin/public';
import { useKibanaContextForPlugin } from '../../../../../hooks/use_kibana';

export function AlertAnnotation({ alertStarted }: { alertStarted: number }) {
const { uiSettings } = useKibanaContextForPlugin().services;
const { euiTheme } = useEuiTheme();

return (
<LineAnnotation
Expand All @@ -32,7 +33,7 @@ export function AlertAnnotation({ alertStarted }: { alertStarted: number }) {
style={{
line: {
strokeWidth: 3,
stroke: euiThemeVars.euiColorDangerText,
stroke: euiTheme.colors.textDanger,
opacity: 1,
},
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import React from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui';
import { LEGACY_LIGHT_THEME } from '@elastic/charts';
import { LIGHT_THEME } from '@elastic/charts';
import { EuiPanel } from '@elastic/eui';
import {
ALERT_CONTEXT,
Expand Down Expand Up @@ -93,7 +93,7 @@ const AlertDetailsAppSection = ({ rule, alert }: AlertDetailsAppSectionProps) =>
<EuiSpacer size="s" />
<Threshold
title={`Threshold breached`}
chartProps={{ theme, baseTheme: LEGACY_LIGHT_THEME }}
chartProps={{ theme, baseTheme: LIGHT_THEME }}
comparator={ComparatorToi18nSymbolsMap[rule.params.count.comparator]}
id={'threshold-ratio-chart'}
thresholds={[rule.params.count.value]}
Expand Down Expand Up @@ -160,7 +160,7 @@ const AlertDetailsAppSection = ({ rule, alert }: AlertDetailsAppSectionProps) =>
<EuiSpacer size="s" />
<Threshold
title={`Threshold breached`}
chartProps={{ theme, baseTheme: LEGACY_LIGHT_THEME }}
chartProps={{ theme, baseTheme: LIGHT_THEME }}
comparator={ComparatorToi18nSymbolsMap[rule.params.count.comparator]}
id="logCountThreshold"
thresholds={[rule.params.count.value]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { omit } from 'lodash';
import React, { PropsWithChildren, useCallback, useMemo, useState } from 'react';
import { euiStyled } from '@kbn/kibana-react-plugin/common';
import styled from '@emotion/styled';
import {
AggregationType,
IErrorObject,
Expand Down Expand Up @@ -46,13 +46,15 @@ interface ExpressionRowProps {
setRuleParams(id: number, params: MetricExpression): void;
}

const NegativeHorizontalMarginDiv = euiStyled.div`margin: 0 -4px;`;
const NegativeHorizontalMarginDiv = styled.div`
margin: 0 -4px;
mgadewoll marked this conversation as resolved.
Show resolved Hide resolved
`;

const StyledExpression = euiStyled.div`
const StyledExpression = styled.div`
padding: 0 4px;
`;

const StyledHealth = euiStyled(EuiHealth)`
const StyledHealth = styled(EuiHealth)`
margin-left: 4px;
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { AppMountParameters, CoreStart } from '@kbn/core/public';
import React, { FC, PropsWithChildren } from 'react';
import { EuiThemeProvider } from '@kbn/kibana-react-plugin/common';
import { EuiThemeProvider } from '@elastic/eui';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { KibanaRenderContextProvider } from '@kbn/react-kibana-context-render';
import { Storage } from '@kbn/kibana-utils-plugin/public';
Expand All @@ -34,10 +34,11 @@ export const CommonInfraProviders: FC<
}>
> = ({ children, triggersActionsUI, setHeaderActionMenu, appName, storage, theme$ }) => {
const darkMode = useIsDarkMode();
const colorMode = darkMode ? 'DARK' : 'LIGHT';

return (
<TriggersActionsProvider triggersActionsUI={triggersActionsUI}>
<EuiThemeProvider darkMode={darkMode}>
<EuiThemeProvider colorMode={colorMode}>
<DataUIProviders appName={appName} storage={storage}>
<HeaderActionMenuProvider setHeaderActionMenu={setHeaderActionMenu} theme$={theme$}>
<NavigationWarningPromptProvider>{children}</NavigationWarningPromptProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
EuiButton,
EuiSpacer,
} from '@elastic/eui';
import { euiStyled } from '@kbn/kibana-react-plugin/common';
import styled from '@emotion/styled';
import useToggle from 'react-use/lib/useToggle';
import { type Message } from '@kbn/observability-ai-assistant-plugin/public';
import { useKibanaContextForPlugin } from '../../../../hooks/use_kibana';
Expand Down Expand Up @@ -199,34 +199,33 @@ const explainProcessMessageTitle = i18n.translate(
}
);

const ExpandedRowDescriptionList = euiStyled(EuiDescriptionList).attrs({
compressed: true,
})`
const ExpandedRowDescriptionList = styled(EuiDescriptionList)`
width: 100%;
`;

const CodeListItem = euiStyled(EuiCode).attrs({
transparentBackground: true,
})`
ExpandedRowDescriptionList.defaultProps = { compressed: true };

const CodeListItem = styled(EuiCode)`
padding: 0 !important;
& code.euiCodeBlock__code {
white-space: nowrap !important;
vertical-align: middle;
}
`;

const ExpandedCommandLine = euiStyled(EuiCode).attrs({
transparentBackground: true,
})`
CodeListItem.defaultProps = { transparentBackground: true };

const ExpandedCommandLine = styled(EuiCode)`
padding: 0 !important;
margin-bottom: ${(props) => props.theme.eui.euiSizeS};
margin-bottom: ${(props) => props.theme.euiTheme.size.s};
`;

const ExpandedRowCell = euiStyled(EuiTableRowCell).attrs({
textOnly: false,
colSpan: 6,
})`
padding-top: ${(props) => props.theme.eui.euiSizeM} !important;
padding-bottom: ${(props) => props.theme.eui.euiSizeM} !important;
background-color: ${(props) => props.theme.eui.euiColorLightestShade};
ExpandedCommandLine.defaultProps = { transparentBackground: true };

const ExpandedRowCell = styled(EuiTableRowCell)`
padding-top: ${(props) => props.theme.euiTheme.size.m} !important;
padding-bottom: ${(props) => props.theme.euiTheme.size.m} !important;
background-color: ${(props) => props.theme.euiTheme.colors.lightestShade};
`;

ExpandedRowCell.defaultProps = { textOnly: false, colSpan: 6 };
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ const columns: Array<{
];

const CodeLine = ({ command }: { command: string }) => {
const euiTheme = useEuiTheme();
const { euiTheme } = useEuiTheme();
return (
<div
css={css`
Expand All @@ -379,8 +379,8 @@ const CodeLine = ({ command }: { command: string }) => {
<EuiCode
transparentBackground
css={css`
color: ${euiTheme.euiTheme.colors.text};
font-weight: ${euiTheme.euiTheme.font.weight.medium};
color: ${euiTheme.colors.textParagraph};
font-weight: ${euiTheme.font.weight.medium};
`}
>
{command}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { EuiFieldSearch, EuiOutsideClickDetector, EuiPanel } from '@elastic/eui';
import React from 'react';
import { QuerySuggestion } from '@kbn/unified-search-plugin/public';
import { euiStyled } from '@kbn/kibana-react-plugin/common';
import styled from '@emotion/styled';
import { composeStateUpdaters } from '../../utils/typed_react';
import { SuggestionItem } from './suggestion_item';

Expand Down Expand Up @@ -302,19 +302,18 @@ const withUnfocused = (state: AutocompleteFieldState) => ({
isFocused: false,
});

const AutocompleteContainer = euiStyled.div`
const AutocompleteContainer = styled.div`
position: relative;
`;

const SuggestionsPanel = euiStyled(EuiPanel).attrs(() => ({
paddingSize: 'none',
hasShadow: true,
}))`
const SuggestionsPanel = styled(EuiPanel)`
position: absolute;
width: 100%;
margin-top: 2px;
overflow-x: hidden;
overflow-y: scroll;
z-index: ${(props) => props.theme.eui.euiZLevel1};
z-index: ${(props) => props.theme.euiTheme.levels.maskBelowHeader};
max-height: 322px;
`;

SuggestionsPanel.defaultProps = { paddingSize: 'none', hasShadow: true };
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import React from 'react';
import { EuiIcon } from '@elastic/eui';
import { euiStyled } from '@kbn/kibana-react-plugin/common';
import styled from '@emotion/styled';
import { Theme } from '@emotion/react';
MiriamAparicio marked this conversation as resolved.
Show resolved Hide resolved
import { QuerySuggestion, QuerySuggestionTypes } from '@kbn/unified-search-plugin/public';
import { transparentize } from 'polished';

Expand Down Expand Up @@ -36,51 +37,51 @@ SuggestionItem.defaultProps = {
isSelected: false,
};

const SuggestionItemContainer = euiStyled.div<{
const SuggestionItemContainer = styled.div<{
isSelected?: boolean;
}>`
display: flex;
flex-direction: row;
font-size: ${(props) => props.theme.eui.euiFontSizeS};
height: ${(props) => props.theme.eui.euiSizeXL};
font-size: ${({ theme }) => theme.euiTheme.size.m};
mgadewoll marked this conversation as resolved.
Show resolved Hide resolved
height: ${({ theme }) => theme.euiTheme.size.xl};
white-space: nowrap;
background-color: ${(props) =>
props.isSelected ? props.theme.eui.euiColorLightestShade : 'transparent'};
props.isSelected ? props.theme.euiTheme.colors.lightestShade : 'transparent'};
`;

const SuggestionItemField = euiStyled.div`
const SuggestionItemField = styled.div`
align-items: center;
cursor: pointer;
display: flex;
flex-direction: row;
height: ${(props) => props.theme.eui.euiSizeXL};
padding: ${(props) => props.theme.eui.euiSizeXS};
height: ${({ theme }) => theme.euiTheme.size.xl};
padding: ${({ theme }) => theme.euiTheme.size.xs};
`;

const SuggestionItemIconField = euiStyled(SuggestionItemField)<{
const SuggestionItemIconField = styled(SuggestionItemField)<{
suggestionType: QuerySuggestionTypes;
}>`
background-color: ${(props) =>
transparentize(0.9, getEuiIconColor(props.theme, props.suggestionType))};
color: ${(props) => getEuiIconColor(props.theme, props.suggestionType)};
background-color: ${({ theme, suggestionType }) =>
transparentize(0.9, getEuiIconColor(theme, suggestionType))};
color: ${({ theme, suggestionType }) => getEuiIconColor(theme, suggestionType)};
flex: 0 0 auto;
justify-content: center;
width: ${(props) => props.theme.eui.euiSizeXL};
width: ${({ theme }) => theme.euiTheme.size.xl};
`;

const SuggestionItemTextField = euiStyled(SuggestionItemField)`
const SuggestionItemTextField = styled(SuggestionItemField)`
flex: 2 0 0;
font-family: ${(props) => props.theme.eui.euiCodeFontFamily};
font-family: ${({ theme }) => theme.euiTheme.font.familyCode};
`;

const SuggestionItemDescriptionField = euiStyled(SuggestionItemField)`
const SuggestionItemDescriptionField = styled(SuggestionItemField)`
flex: 3 0 0;

p {
display: inline;

span {
font-family: ${(props) => props.theme.eui.euiCodeFontFamily};
font-family: ${({ theme }) => theme.euiTheme.font.familyCode};
}
}
`;
Expand All @@ -102,18 +103,18 @@ const getEuiIconType = (suggestionType: QuerySuggestionTypes) => {
}
};

const getEuiIconColor = (theme: any, suggestionType: QuerySuggestionTypes): string => {
const getEuiIconColor = (theme: Theme, suggestionType: QuerySuggestionTypes): string => {
switch (suggestionType) {
case QuerySuggestionTypes.Field:
return theme?.eui.euiColorVis7;
return theme?.euiTheme.colors.vis.euiColorVis7;
case QuerySuggestionTypes.Value:
return theme?.eui.euiColorVis0;
return theme?.euiTheme.colors.vis.euiColorVis0;
case QuerySuggestionTypes.Operator:
return theme?.eui.euiColorVis1;
return theme?.euiTheme.colors.vis.euiColorVis1;
case QuerySuggestionTypes.Conjunction:
return theme?.eui.euiColorVis2;
return theme?.euiTheme.colors.vis.euiColorVis2;
case QuerySuggestionTypes.RecentSearch:
default:
return theme?.eui.euiColorMediumShade;
return theme?.euiTheme.colors.mediumShade;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import { EuiButton, EuiEmptyPrompt } from '@elastic/eui';
import React from 'react';

import { euiStyled } from '@kbn/kibana-react-plugin/common';
import styled from '@emotion/styled';

interface NoDataProps {
titleText: string;
Expand Down Expand Up @@ -44,6 +43,6 @@ export const NoData: React.FC<NoDataProps> = ({
/>
);

const CenteredEmptyPrompt = euiStyled(EuiEmptyPrompt)`
const CenteredEmptyPrompt = styled(EuiEmptyPrompt)`
align-self: center;
`;
Loading
Loading