To install the Elastic UI Framework into an existing project, use the yarn
CLI (npm
is not supported).
yarn add @elastic/eui
Note that EUI has several peerDependencies
requirements that will also need to be installed if starting with a blank project.
yarn add @elastic/eui @elastic/datemath @emotion/react moment prop-types
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.
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';
Test utilities are published from the lib/test
directory.
import { findTestSubject } from '@elastic/eui/lib/test';
The Sass variables are also made available for consumption as json files. This enables reuse of values in css-in-js systems like Emotion. As the following example shows, it can also make the downstream components theme-aware without much extra effort:
import React from 'react';
import { css } from '@emotion/react';
import * as euiVars from '@elastic/eui/dist/eui_theme_light.json';
const styles = css`
color: ${euiVars.euiColorPrimary};
border: ${euiVars.euiBorderThin};
`;
export default () => (
<div css={styles} />
)
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,
});
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.
The optimize
output directory is an opt-in intermediate step as we work towards dedicated, formal build output for development and production environments.
When compiling with webpack, use the resolve.alias
configuration to target the desired directory:
resolve: {
alias: {
'@elastic/eui$': '@elastic/eui/optimize/lib'
}
}
Designated "Beta" as included babel plugins may change and the output directory is likely to be renamed.
- Absence of
propTypes
- Significant bundle size decrease
- Likely not suitable for development environments
- Runtime transforms
- Slight bundle size decrease
- Requires
@babel/runtime
be a consumer dependency