EUI publishes React UI components, JavaScript helpers called services, and utilities for writing Jest tests. Please refer to the Elastic UI Framework website for comprehensive info on what's available.
EUI is published through NPM as a dependency. We also provide a starter projects for:
You can import React components from the top-level EUI module.
import {
EuiButton,
EuiCallOut,
EuiPanel,
} from '@elastic/eui';
Most services are published from the lib/services
directory. Some are published from their module directories in this directory.
import { keys } from '@elastic/eui/lib/services';
import { Timer } from '@elastic/eui/lib/services/time';
Test utilities are published from the lib/test
directory.
import { findTestSubject } from '@elastic/eui/lib/test';
EUI expects that you polyfill ES2015 features, e.g. babel-polyfill
. Without an ES2015 polyfill your app might throw errors on certain browsers.
EUI also has moment
and @elastic/datemath
as dependencies itself. These are already loaded in most Elastic repos, but make sure to install them if you are starting from scratch.
The EUI CSS is included in Kibana's CSS bundle. To use EUI code in Kibana, simply import the components and services you want.
You can consume EUI in standalone projects, such as plugins and prototypes.
Most of the time, you just need the CSS, which provides the styling for the React components. In this case, you can use Webpack to import the compiled EUI CSS with the style
,css
, and postcss
loaders.
import '@elastic/eui/dist/eui_theme_light.css';
If you want access to the Sass variables, functions, and mixins in EUI then you'll need to import the SCSS file. This will require style
, css
, postcss
, and sass
loaders. You'll also want to import the SCSS file into one of your own SCSS files, to gain access to these variables, functions, and mixins.
// index.scss
@import '../node_modules/@elastic/eui/src/theme_light.scss';
By default, EUI ships with a font stack that includes some outside, open source fonts. If your system is internet available you can include these by adding the following imports to your SCSS/CSS files, otherwise you'll need to bundle the physical fonts in your build. EUI will drop to System Fonts (which you may prefer) in their absence.
// index.scss
@import url('https://fonts.googleapis.com/css?family=Roboto+Mono:400,400i,700,700i');
@import url('https://rsms.me/inter/inter-ui.css');
The Sass variables are also made available for consumption as json files. This enables reuse of values in css-in-js systems like styled-components. As the following example shows, it can also make the downstream components theme-aware without much extra effort:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import styled, { ThemeProvider } from 'styled-components';
import * as euiVars from '@elastic/eui/dist/eui_theme_light.json';
const CustomComponent = styled.div`
color: ${props => props.theme.euiColorPrimary};
border: ${props => props.theme.euiBorderThin};
`;
ReactDOM.render(
<ThemeProvider theme={euiVars}>
<CustomComponent>content</CustomComponent>
</ThemeProvider>
, document.querySelector('#renderTarget'));
If you get an error when importing a React component, you might need to configure Webpack's resolve.mainFields
to ['webpack', 'browser', 'main']
to import the components from lib
instead of src
. See the Webpack docs for more info.
To reduce EUI's impact to application bundle sizes, the icons are dynamically imported on-demand. This is problematic for some bundlers and/or deployments, so a method exists to preload specific icons an application needs.
import { appendIconComponentCache } from '@elastic/eui/es/components/icon/icon';
import { icon as EuiIconArrowDown } from '@elastic/eui/es/components/icon/assets/arrow_down';
import { icon as EuiIconArrowLeft } from '@elastic/eui/es/components/icon/assets/arrow_left';
// One or more icons are passed in as an object of iconKey (string): IconComponent
appendIconComponentCache({
arrowDown: EuiIconArrowDown,
arrowLeft: EuiIconArrowLeft,
});
We do not recommend customizing EUI components by applying styles directly to EUI classes, eg. .euiButton
. All components allow you to pass a custom className
prop directly to the component which will then append this to the class list. Utilizing the cascade feature of CSS, you can then customize by overriding styles so long as your styles are imported after the EUI import.
<EuiButton className="myCustomClass__button" />
// Renders as:
<button class="euiButton myCustomClass__button" />
EUI provides a separate babel-transformed and partially mocked commonjs build for testing environments in consuming projects. The output is identical to that of lib/
, but has transformed async functions and dynamic import statements, and also applies some useful mocks. This build mainly targets Kibana's Jest environment, but may be helpful for testing environments in other projects.
In Kibana's Jest configuration, the moduleNameMapper
option is used to resolve standard EUI import statements with test-env
aliases.
moduleNameMapper: {
'@elastic/eui$': '<rootDir>/node_modules/@elastic/eui/test-env'
}
This eliminates the need to polyfill or transform the EUI build for an environment that otherwise has no need for such processing.
Besides babel transforms, the test environment build consumes mocked component files of the type src/**/[name].testenv.*
. During the build, files of the type src/**/[name].*
will be replaced by those with the testenv
namespace. The purpose of this mocking is to further mitigate the impacts of time- and import-dependent rendering, and simplify environment output such as test snapshots. Information on creating mock component files can be found with testing documentation.