-
Notifications
You must be signed in to change notification settings - Fork 861
Add Emotion container query utilities #9264
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
tkajtoch
merged 8 commits into
elastic:main
from
tkajtoch:feat/emotion-container-query-utils
Dec 16, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bb8fb07
feat: Add container query Emotion utilities
tkajtoch 51fef7c
feat(EuiFlyout): Add container property
tkajtoch 71ce060
feat: export container query utilities
tkajtoch 391ad58
chore: mark container query utilities as beta
tkajtoch f00493b
feat: update `euiContainer` and `euiContainerCSS` API to require the …
tkajtoch 84ac571
fix: update `euiContainer` call in EuiFlyout styles
tkajtoch e25fe13
chore: add changelog
tkajtoch 8923fbf
fix: remove the container CSS property from EuiFlyout root element
tkajtoch 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Added beta `euiContainer()`, `euiContainerCSS()`, and `euiContainerQuery()` Emotion utilities to help work with CSS Container Queries |
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
153 changes: 153 additions & 0 deletions
153
packages/eui/src/global_styling/mixins/_container_query.test.tsx
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,153 @@ | ||
| /* | ||
| * 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 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import { render } from '../../test/rtl'; | ||
| import { | ||
| euiContainer, | ||
| euiContainerCSS, | ||
| euiContainerQuery, | ||
| } from './_container_query'; | ||
|
|
||
| describe('euiContainer', () => { | ||
| it('supports naming containers', () => { | ||
| expect(euiContainer('normal', 'my-unique-name')).toEqual( | ||
| 'container-name: my-unique-name' | ||
| ); | ||
| }); | ||
|
|
||
| it('supports non-scroll state container types', () => { | ||
| expect(euiContainer('size', 'my-unique-name')).toEqual( | ||
| 'container-name: my-unique-name;container-type: size' | ||
| ); | ||
| expect(euiContainer('inline-size', 'my-unique-name')).toEqual( | ||
| 'container-name: my-unique-name;container-type: inline-size' | ||
| ); | ||
| }); | ||
|
|
||
| it('supports scroll states via the scrollState argument', () => { | ||
| expect(euiContainer('size', 'my-unique-name', true)).toEqual( | ||
| 'container-name: my-unique-name;container-type: size scroll-state' | ||
| ); | ||
|
|
||
| expect(euiContainer('size', 'my-unique-name', false)).toEqual( | ||
| 'container-name: my-unique-name;container-type: size' | ||
| ); | ||
| }); | ||
|
|
||
| it('ignores the default "normal" container type', () => { | ||
| expect(euiContainer('normal', 'my-unique-name')).toEqual( | ||
| 'container-name: my-unique-name' | ||
| ); | ||
|
|
||
| expect(euiContainer('normal', 'my-unique-name', true)).toEqual( | ||
| 'container-name: my-unique-name;container-type: scroll-state' | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('euiContainerCSS', () => { | ||
| it('supports naming containers', () => { | ||
| const { container } = render( | ||
| <div css={euiContainerCSS('normal', 'my-unique-name')} /> | ||
| ); | ||
|
|
||
| expect(container.firstChild).toHaveStyleRule( | ||
| 'container-name', | ||
| 'my-unique-name' | ||
| ); | ||
| }); | ||
|
|
||
| it('supports non-scroll state container types', () => { | ||
| const { container, rerender } = render( | ||
| <div css={euiContainerCSS('size', 'my-unique-name')} /> | ||
| ); | ||
|
|
||
| expect(container.firstChild).toHaveStyleRule( | ||
| 'container-name', | ||
| 'my-unique-name' | ||
| ); | ||
| expect(container.firstChild).toHaveStyleRule('container-type', 'size'); | ||
|
|
||
| rerender(<div css={euiContainerCSS('inline-size', 'my-unique-name')} />); | ||
| expect(container.firstChild).toHaveStyleRule( | ||
| 'container-name', | ||
| 'my-unique-name' | ||
| ); | ||
| expect(container.firstChild).toHaveStyleRule( | ||
| 'container-type', | ||
| 'inline-size' | ||
| ); | ||
| }); | ||
|
|
||
| it('supports scroll states via the scrollState argument', () => { | ||
| const { container, rerender } = render( | ||
| <div css={euiContainerCSS('size', 'my-unique-name', true)} /> | ||
| ); | ||
|
|
||
| expect(container.firstChild).toHaveStyleRule( | ||
| 'container-name', | ||
| 'my-unique-name' | ||
| ); | ||
| expect(container.firstChild).toHaveStyleRule( | ||
| 'container-type', | ||
| 'size scroll-state' | ||
| ); | ||
|
|
||
| rerender(<div css={euiContainerCSS('size', 'my-unique-name', false)} />); | ||
| expect(container.firstChild).toHaveStyleRule( | ||
| 'container-name', | ||
| 'my-unique-name' | ||
| ); | ||
| expect(container.firstChild).toHaveStyleRule('container-type', 'size'); | ||
| }); | ||
|
|
||
| it('ignores the default "normal" container type', () => { | ||
| const { container, rerender } = render( | ||
| <div css={euiContainerCSS('normal', 'my-unique-name')} /> | ||
| ); | ||
|
|
||
| expect(container.firstChild).toHaveStyleRule( | ||
| 'container-name', | ||
| 'my-unique-name' | ||
| ); | ||
| expect(container.firstChild).not.toHaveStyleRule('container-type'); | ||
|
|
||
| rerender(<div css={euiContainerCSS('normal', 'my-unique-name', true)} />); | ||
| expect(container.firstChild).toHaveStyleRule( | ||
| 'container-name', | ||
| 'my-unique-name' | ||
| ); | ||
| expect(container.firstChild).toHaveStyleRule( | ||
| 'container-type', | ||
| 'scroll-state' | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('euiContainerQuery', () => { | ||
| it('supports any container conditions', () => { | ||
| expect(euiContainerQuery('(width > 150px)')).toEqual( | ||
| `@container (width > 150px)` | ||
| ); | ||
|
|
||
| expect(euiContainerQuery('(width > 150px) and (width < 300px)')).toEqual( | ||
| `@container (width > 150px) and (width < 300px)` | ||
| ); | ||
| }); | ||
|
|
||
| it('supports container names', () => { | ||
| expect(euiContainerQuery('(width > 150px)', 'my-container')).toEqual( | ||
| `@container my-container (width > 150px)` | ||
| ); | ||
|
|
||
| expect( | ||
| euiContainerQuery('(width > 150px) and (width < 300px)', 'my-container') | ||
| ).toEqual(`@container my-container (width > 150px) and (width < 300px)`); | ||
| }); | ||
| }); |
128 changes: 128 additions & 0 deletions
128
packages/eui/src/global_styling/mixins/_container_query.ts
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,128 @@ | ||
| /* | ||
| * 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 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import { css } from '@emotion/react'; | ||
|
|
||
| const CONTAINER_TYPES = ['normal', 'size', 'inline-size'] as const; | ||
|
|
||
| /** | ||
| * Type of container context used in container queries. | ||
| * @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/container-type} | ||
| */ | ||
| export type EuiContainerType = (typeof CONTAINER_TYPES)[number]; | ||
|
|
||
| /** | ||
| * Establish element as a query container. | ||
| * The scroll state is applied through the `scrollState` argument | ||
| * and not the `type` argument. | ||
| * | ||
| * @example | ||
| * // Export container name to use across the application | ||
| * export const PAGE_CONTENT_CONTAINER_NAME = 'my-app-page-content'; | ||
| * const pageContentStyles = css` | ||
| * ${euiContainer('inline-size', PAGE_CONTENT_CONTAINER_NAME)} | ||
| * margin: 0 auto; | ||
| * `; | ||
| * | ||
| * @returns A style string to be used inside Emotion's `css` template literal | ||
| * @beta | ||
| */ | ||
| export const euiContainer = ( | ||
| type: EuiContainerType, | ||
| name?: string, | ||
| scrollState?: boolean | ||
| ): string => { | ||
| let finalType = ''; | ||
| if (type !== 'normal') { | ||
| finalType += type; | ||
| } | ||
| if (scrollState) { | ||
| if (finalType.length) { | ||
| finalType += ' '; | ||
| } | ||
|
|
||
| finalType += 'scroll-state'; | ||
| } | ||
|
|
||
| return [ | ||
| !!name && `container-name: ${name}`, | ||
| !!finalType && `container-type: ${finalType}`, | ||
| ] | ||
| .filter(Boolean) | ||
| .join(';'); | ||
| }; | ||
|
|
||
| /** | ||
| * Establish element as a query container. | ||
| * The scroll state is applied through the `scrollState` argument | ||
| * and not the `type` argument. | ||
| * | ||
| * @example | ||
| * // Export container name to use across the application | ||
| * export const PAGE_CONTENT_CONTAINER_NAME = 'my-app-page-content'; | ||
| * const PageContent = ({ children }: PropsWithChildren) => ( | ||
| * <main css={euiContainerCSS('inline-size', PAGE_CONTENT_CONTAINER_NAME)}> | ||
| * {children} | ||
| * </main> | ||
| * ); | ||
| * @returns Emotion's `SerializedStyles` object to be passed to the `css` prop | ||
| * of a React component. | ||
| * @beta | ||
| */ | ||
| export const euiContainerCSS = ( | ||
| type: EuiContainerType, | ||
| name?: string, | ||
| scrollState?: boolean | ||
| ) => { | ||
| return css(euiContainer(type, name, scrollState)); | ||
| }; | ||
|
|
||
| /** | ||
| * Get a @container rule for given conditions and an optional container name. | ||
| * | ||
| * Container queries can be used to apply conditional styles based on container | ||
| * size, its scroll state or even its styles. | ||
| * | ||
| * It's hugely useful to conditionally show or hide information based | ||
| * on the **container** dimensions instead of the **viewport** dimensions. | ||
| * | ||
| * When container name is provided, it will be used to target the containment | ||
| * context. When skipped, it will target the nearest ancestor with containment. | ||
| * | ||
| * @example | ||
| * const itemDetailsStyles = css` | ||
| * background: red; | ||
| * | ||
| * ${euiContainerQuery('(width > 250px)')} { | ||
| * background: blue; | ||
| * } | ||
| * `; | ||
| * | ||
| * @param conditions one or many conditions to query the container with. | ||
| * Similarly to media queries, you can use | ||
| * size queries (e.g., `(width > 300px)`), | ||
| * scroll state queries (e.g., `(scroll-state(scrollable: top))`), | ||
| * or even style queries. | ||
| * You can use the `and`, `or` and `not` logical keywords to define container | ||
| * conditions. Note that all conditions must be wrapped in parentheses. | ||
| * | ||
| * @param containerName When provided, it will be used to target | ||
| * the containment context and run queries against it. Otherwise, the nearest | ||
| * ancestor with containment will be queried instead. | ||
| * | ||
| * @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@container} | ||
| * @beta | ||
| */ | ||
| export const euiContainerQuery = ( | ||
| conditions: string, | ||
| containerName?: string | ||
| ): string => { | ||
| return `@container ${containerName ?? ''}${ | ||
| containerName ? ' ' : '' | ||
| }${conditions}`; | ||
| }; | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[no action required] I like this API to make it easy to understand/follow the spec, when it has 2 values "one must be
scroll-stateand the other can beinline-sizeorsize" 👍