Repository navigation
feat(website): add Utilities > Provider page #8375
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
Merged
weronikaolejniczak
merged 7 commits into
elastic:main
from
weronikaolejniczak:feat/eui-plus-provider
Mar 7, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3381152
feat(website): add Utilities > Provider page
weronikaolejniczak d6bfeb9
chore(eui): export EuiComponentDefaultsProps to use in prop table
weronikaolejniczak 908482d
feat(docusaurus-theme): add showTitle prop to PropTable
weronikaolejniczak de8a08f
fix(website): fix Provider redirections in Getting Started
weronikaolejniczak fae790a
fix(eui): rename EuiComponentDefaultsProps to EuiProviderComponentDef…
weronikaolejniczak 2bf069e
fix(website): used the renamed EuiProviderComponentDefaultsProps
weronikaolejniczak 73cbef9
fix(website): add EuiSpacer between examples in Provider
weronikaolejniczak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
packages/website/docs/components/utilities/provider.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| --- | ||
| slug: /utilities/provider | ||
| id: utilities_provider | ||
| --- | ||
|
|
||
| import { EuiBetaBadge, EuiSpacer } from '@elastic/eui'; | ||
|
|
||
| # Provider | ||
|
|
||
| **EuiProvider** contains all necessary context providers required for full functionality and styling of EUI. A single instance of **EuiProvider** should exist at the top level of your app, where functionality will flow down the component tree. | ||
|
|
||
| ## Basic setup | ||
|
|
||
| For EUI to work correctly, set up **EuiProvider** at the root of your application: | ||
|
|
||
| ```jsx | ||
| import { EuiProvider } from '@elastic/eui'; | ||
|
|
||
| const MyApp = () => ( | ||
| <EuiProvider> | ||
| {/* Content */} | ||
| </EuiProvider> | ||
| ); | ||
| ``` | ||
|
|
||
| **EuiProvider** includes global reset and utilities styles and other app-wide contexts, functionality, and configuration options. It should only be instantiated **once**. This requirement is enforced internally - if another nested instance of **EuiProvider** is detected, that instance will return early without further processing, and will [warn if configured to do so](#enforce-usage). Nested instances of **EuiThemeProvider**, however, are valid. | ||
|
|
||
| ## Theming and global styles | ||
|
|
||
| To customize the global theme of your app, use the `theme`, `colorMode`, and `modify` props (documented in [EuiThemeProvider](../theming/theme_provider.mdx)). For instance, it's likely that you will want to implement color mode switching at the top level: | ||
|
|
||
| ```jsx | ||
| <EuiProvider colorMode={isDark ? 'dark' : 'light'} /> | ||
| ``` | ||
|
|
||
| If you do not wish your app to include EUI's default global reset CSS or [utility CSS classes](./css_utility_classes.mdx), this is configurable via the `globalStyles` or `utilityClasses` props. You can either pass in your own as a React component returning an <a href="https://emotion.sh/docs/globals" target="_blank" rel="noopener noreferrer">**Emotion Global**</a>, or remove them completely by setting the props to `false`: | ||
|
|
||
| ```jsx | ||
| <EuiProvider globalStyles={false} utilityClasses={false} /> | ||
| ``` | ||
|
|
||
| ### `@emotion/cache` customization | ||
|
|
||
| The <a href="https://emotion.sh/docs/@emotion/cache" target="_blank" rel="noopener noreferrer">**@emotion/cache**</a> library provides extra configuration options for EUI's CSS-in-JS behavior: | ||
|
|
||
| * **Browser prefixing**: By default, EUI uses CSS browser prefixes based on our <a href="https://www.elastic.co/support/matrix#matrix_browsers" target="_blank" rel="noopener noreferrer">**supported browsers matrix**</a> (latest evergreen only). Should you need to customize this, you can pass in your own prefix plugin via the `stylisPlugins` option. | ||
|
|
||
| * **Injection location**: In the case that your app has its own static stylesheet, Emotion's styles may not be injected into the correct location in the `<head>`, causing unintentional overrides or unapplied styles. You can use the `container` option and `<meta>` tags to achieve this. | ||
|
|
||
|
weronikaolejniczak marked this conversation as resolved.
Resolved
|
||
| <EuiSpacer /> | ||
|
|
||
| ```html | ||
| <!-- index.html --> | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>My App</title> | ||
| <meta name="eui-style-insert"> | ||
| <link name="compiled-css-here" /> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| </body> | ||
| </html> | ||
| ``` | ||
|
|
||
| <EuiSpacer /> | ||
|
|
||
| ```jsx | ||
| // App.js | ||
| import { EuiProvider, euiStylisPrefixer } from '@elastic/eui' | ||
|
|
||
| import createCache from '@emotion/cache'; | ||
|
|
||
| const euiCache = createCache({ | ||
| key: 'eui', | ||
| stylisPlugins: [euiStylisPrefixer], | ||
| container: document.querySelector('meta[name="eui-style-insert"]'), | ||
| }); | ||
|
|
||
| euiCache.compat = true; | ||
|
|
||
| <EuiProvider cache={euiCache}> | ||
| {/* Content */} | ||
| </EuiProvider> | ||
| ``` | ||
|
|
||
| For most applications, the above configuration will be enough to have styles ordered correctly. In advanced more cases, you may use the `default`, `global`, and `utility` properties on the `cache` prop to further define where specific styles should be inserted. See [the props documentation](#props) for details. | ||
|
|
||
| Any other options available with <a href="https://emotion.sh/docs/@emotion/cache#createcache" target="_blank" rel="noopener noreferrer">**the createCache API**</a> will be respected by EUI. Note that EUI does not include the `@emotion/cache` library, so you will need to add it to your application dependencies. | ||
|
|
||
| ## Component defaults <EuiBetaBadge alignment="middle" label="Beta" size="s" color="accent" /> | ||
|
|
||
| :::info Beta status | ||
|
|
||
| This functionality is still currently in beta, and the list of components as well as defaults that EUI will be supporting is still under consideration. If you have a component you would like to see added, feel free to <a href="https://github.com/elastic/eui/discussions/6922" target="_blank" rel="noopener noreferrer">**discuss that request in EUI's GitHub repo**</a>. | ||
| ::: | ||
|
|
||
| All EUI components ship with a set of baseline defaults that can usually be configured via props. For example, [EuiFocusTrap](./focus_trap.mdx) defaults to `crossFrame={false}` - i.e., it does not trap focus between iframes. If you wanted to change that behavior in your app across all instances of **EuiFocusTrap**, you would be stuck manually passing that prop over and over again, including in higher-level components (like modals, popovers, and flyouts) that utilize focus traps. | ||
|
|
||
| **EuiProvider** allows overriding some component defaults across all component usages globally via the `componentDefaults` prop like so: | ||
|
|
||
| ```jsx | ||
| <EuiProvider | ||
| componentDefaults={{ | ||
| EuiTable: { responsiveBreakpoint: 's', }, | ||
| EuiTablePagination: { itemsPerPage: 20, }, | ||
| EuiFocusTrap: { crossFrame: true }, | ||
| EuiPortal: { insert }, | ||
| }} | ||
| > | ||
| <App /> | ||
| </EuiProvider> | ||
| ``` | ||
|
|
||
| The above example would override EUI's default table pagination size (50) across all usages of EUI tables and data grids, all EUI focus traps would trap focus even from iframes, and all EUI portals would be inserted at a specified position (instead of the end of the document body). | ||
|
|
||
| The current list of supported components and the prop defaults they accept are: | ||
|
|
||
| import componentDefaultsDocgen from '@elastic/eui-docgen/dist/components/provider/component_defaults'; | ||
|
|
||
| <PropTable definition={componentDefaultsDocgen.EuiProviderComponentDefaultsProps} showTitle={false} /> | ||
|
|
||
| ## Enforce usage | ||
|
|
||
| For complex applications with multiple mount points or template wrappers, it may be beneficial to enable logging. Doing so will allow you to see warnings for duplicate **EuiProviders**, as well as when components do not have access to a parent **EuiProvider**. To enable logging or erroring, use `setEuiDevProviderWarning`: | ||
|
|
||
| ```jsx | ||
| import { setEuiDevProviderWarning } from '@elastic/eui'; | ||
|
|
||
| setEuiDevProviderWarning('warn'); | ||
| ``` | ||
|
|
||
| Examples of apps that would cause warnings: | ||
|
|
||
| ```jsx | ||
| const AppWithMissingProvider = () => ( | ||
| <EuiPageTemplate> | ||
| {/* Will render, but will warn about missing EuiProvider */} | ||
| </EuiPageTemplate> | ||
| ); | ||
|
|
||
| const App = () => ( | ||
| <EuiProvider> | ||
| {/* Content */} | ||
| </EuiProvider> | ||
| ); | ||
|
|
||
| const AppWithDuplicateProvider = () => ( | ||
| <EuiProvider> | ||
| {/* Will warn about multiple providers */} | ||
| <App /> | ||
| </EuiProvider> | ||
| ) | ||
| ``` | ||
|
|
||
| `setEuiDevProviderWarning` accepts three levels: | ||
|
|
||
| * `'log'`: uses `console.log` | ||
| * `'warn'`: uses `console.warn` | ||
| * `'error'`: `Throw`s an exception | ||
|
|
||
| It also accepts a callback function instead of a default warning level. The warning message string will be passed to your callback, where any custom action can be performed on it. Example usage: | ||
|
|
||
| ```jsx | ||
| import { setEuiDevProviderWarning } from '@elastic/eui'; | ||
|
|
||
| const customWarningHandler = (message: string) => { | ||
| sendWarningToTelemetryService(message); | ||
| console.debug(message); | ||
| }; | ||
|
|
||
| setEuiDevProviderWarning(customWarningHandler); | ||
| ``` | ||
|
|
||
| ## Props | ||
|
|
||
| import providerDocgen from '@elastic/eui-docgen/dist/components/provider'; | ||
|
|
||
| <PropTable definition={providerDocgen.EuiProvider} /> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.