Replies: 5 comments
-
What's the difference between the
|
Beta Was this translation helpful? Give feedback.
-
Can I use both
|
Beta Was this translation helpful? Give feedback.
-
Should I use the
|
Beta Was this translation helpful? Give feedback.
-
Migrating from Sass to CSS-in-JSThis section of the FAQ is meant as a handy reference/cheatsheet on how to migrate specific Sass variables and mixins to their Emotion counterparts. VariablesSizesFor a full list of tokens, see https://eui.elastic.co/#/theming/sizing/values
Performing math on size tokensIt's worth highlighting that these size tokens return CSS strings and not numbers. This means something like
const width = `${useEuiTheme().euiTheme.base * 10}px`; // '160px'
import { mathWithUnits } from '@elastic/eui';
const SomeComponent = () => {
const { euiTheme } = useEuiTheme();
const width = mathWithUnits(euiTheme.size.base, (x) => x * 10); // Returns '160px'
// The util also accepts multiple tokens, e.g.
const height = mathWithUnits([euiTheme.size.xl, euiTheme.size.xs], (x, y) => x + y); // Returns '44px'
}
ColorsFor a full list of tokens, see https://eui.elastic.co/#/theming/colors/values
Color palettes:
import { euiColorPaletteBlind, euiPaletteColorBlindBehindText } from '@elastic/eui';
// Note that these utils can be called statically / outside of hooks or react components
const visColors = euiColorPaletteBlind();
const visBackgroundColors = euiPaletteColorBlindBehindText();
const SomeComponent = () => <div css={{ color: visColors[0], backgroundColor: visBackgroundColors[1] }} /> For a full list of arguments, see https://eui.elastic.co/#/utilities/color-palettes.
TypographyFor a full list of tokens, see https://eui.elastic.co/#/theming/typography/values
Unfortunately, font sizes are no longer static tokens, and must be calculated using a function/hook instead. See the below "Typography" mixin section. Z-index levelsFor a full list of tokens, see https://eui.elastic.co/#/theming/more-tokens#levels
A quick note about math with z-index levels - while they're typed in our theme as potentially being either a string or number (the default type for CSS properties), the returned tokens are numbers and can have math performed on them as-is (e.g. addition). BordersFor a full list of tokens, see https://eui.elastic.co/#/theming/borders
AnimationsFor a full list of tokens, see https://eui.elastic.co/#/theming/more-tokens#animation
Form variables
|
Sass | CSS-in-JS |
---|---|
@include euiFontSizeS /L /etc |
useEuiFontSize('xxxs' through 'xxl') |
@include euiFont |
Deprecated. Use font-family: ${euiTheme.font.family}; or font-weight: ${euiTheme.font.weight.regular}; if necessary instead |
@include euiCodeFont |
Deprecated. Use font-family: ${euiTheme.font.familyCode}; instead |
@include euiText |
Deprecated. Use <EuiText /> or if necessary, color: ${euiTheme.colors.text} instead |
@include euiTitle |
Deprecated. Use <EuiTitle /> instead (strongly recommended)If absolutely necessary, you may reach into the EuiTitle component via import { euiTitle } from '@elastic/eui/lib/components/title/title.styles'; , but this is generally not recommended as Kibana has issues typing @elastic/eui/lib |
@include euiTextTruncate and @include euiTextBreakWord |
Consider simply applying .eui-textTruncate or .eui-textBreakWord as a vanilla className (simplest option). Or, import { euiTextBreakWord, euiTextTruncate } from '@elastic/eui'; and use it as css={euiTextTruncate()} |
See https://eui.elastic.co/#/theming/typography/utilities for a full list of className
utilities and documentation.
Responsive
EUI's breakpoint utilities have been generally enhanced to exceed their Sass capabilities. With that in mind, we recommend a more nuanced approach to migrating media queries:
Sass | CSS-in-JS |
---|---|
@include euiBreakpoint('s', 'm') |
useEuiBreakpoint(['s', 'm']) |
Media queries ending with @include euiBreakpoint(..., 'xl') e.g., @include euiBreakpoint('m,' 'l', 'xl') |
useEuiMinBreakpoint('m') We recommend preferring a single min/max breakpoint, as this helps account for themes with custom breakpoints |
Media queries beginning with @include euiBreakpoint('xs', ...) e.g., @include euiBreakpoint('xs', 's') |
useEuiMaxBreakpoint('m') See above. If converted from @euiBreakpoint , should be a size higher than the ending size |
Example usage:
import { useEuiMinBreakpoint, useEuiMaxBreakpoint, useEuiBreakpoint } from '@elastic/eui';
const SomeComponent = () => {
const cssStyles = css`
${useEuiMaxBreakpoint('m')} {
color: red; /* Text will be red under the 'm' breakpoint */
}
${useEuiBreakpoint(['m', 'l'])} {
color: yellow; /* Text will be yellow between the 'm and 'xl' breakpoints */
}
${useEuiMinBreakpoint('xl')} {
color: green; /* Text will be green above the 'xl' breakpoint */
}
`
return <div css={cssStyles />;
}
For a list of all default breakpoints and visual demonstration of these utilities, see https://eui.elastic.co/#/theming/breakpoints/values
Shadows
For a visual demonstration of these mixins, see https://eui.elastic.co/#/theming/more-tokens#shadow
Sass | CSS-in-JS |
---|---|
@include euiSlightShadow |
useEuiShadow('xs') |
@include euiBottomShadowSmall |
useEuiShadow('s') |
@include euiBottomShadowMedium |
useEuiShadow('m') |
@include euiBottomShadow |
useEuiShadow('l') |
@include euiBottomShadowLarge |
useEuiShadow('xl') |
@include euiBottomShadowFlat |
useEuiShadowFlat() |
@include euiSlightShadowHover |
useEuiSlightShadowHover() |
Scrolling
For a visual demonstration of these mixins, see https://eui.elastic.co/#/utilities/scroll
Sass | CSS-in-JS |
---|---|
@include euiScrollBar |
useEuiScrollBar() , or apply the .eui-scrollBar className |
@include euiYScroll |
useEuiOverflowScroll('y') , or apply the .eui-yScroll className |
@include euiYScrollWithShadows |
useEuiOverflowScroll('y', true) |
@include euiXScroll |
useEuiOverflowScroll('x') , or apply the .eui-xScroll className |
@include euiXScrollWithShadows |
useEuiOverflowScroll('x', true) |
Non-hook utilities
If using a hook is onerous to migrate within the existing code for any reason (e.g. conditional returns, nested hooks, etc.), you may use non-hook versions of each utility. For example, you can import euiFontSize
instead of useEuiFontSize
, euiBreakpoint
instead of useEuiBreakpoint
, euiShadow
instead of useEuiShadow
, and so on and so forth.
import { euiFontSize, useEuiTheme } from '@elastic/eui';
import { css } from '@emotion/react';
const SomeComponent = ({ isLoading, hasButton }) => {
const euiThemeContext = useEuiTheme();
// Conditional return before css`` is used
if (isLoading) return <LoadingComponent />;
// Nested hook usage
const someButton = useMemo(() => {
if (!hasButton) return null;
const cssStyles = css`
${euiFontSize(euiThemeContext, 'l')}
color: red;
`;
return <button css={cssStyles}>Conditional button</button>;
}, [hasButton, euiThemeContext]);
return <>{someButton}{children}</>
};
Alternatively, you can also take advantage of Emotion's theme context API to obtain the current EUI theme without useEuiTheme()
as a nice syntactical sugar shortcut. See also: https://eui.elastic.co/#/theming/theme-provider#consuming-with-emotions-theming
import { euiFontSize } from '@elastic/eui';
import { css } from '@emotion/react';
const SomeComponent = ({ isLoading }) => {
if (isLoading) return null;
return (
<div
css={(euiThemeContext) => css`
${euiFontSize(euiThemeContext, 'l')}
color: red;
`
/>
);
}
Please note that you may have to set up an emotion.d.ts file for your Kibana plugin for the parameter in css
functions to be properly typed as EUI's theme. See the osquery plugin for an example of their tsconfig setup.
Beta Was this translation helpful? Give feedback.
-
Theming and Emotion's
|
Beta Was this translation helpful? Give feedback.
-
👋 I'm starting this discussion as a general Q&A location for EUI Emotion (or other general CSS-in-JS) usage in Kibana. The goal is of this discussion to be a one-stop linkable resource for Kibana devs.
While I may end up converting this to a wiki document at some point, I opted for a GitHub discussion now to allow others to ask questions, as well as to account for the fact that things are currently in flux (e.g. EUI's Emotion migration is still ongoing, and Kibana currently supports more than one CSS-in-JS but may not in the future).
Beta Was this translation helpful? Give feedback.
All reactions