Skip to content

Commit 9554450

Browse files
authored
[EuiCollapsibleNavBeta] Add global CSS variable for width offset (#7248)
1 parent 10a50bb commit 9554450

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/components/collapsible_nav_beta/collapsible_nav_beta.stories.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { Meta, StoryObj } from '@storybook/react';
1111

1212
import { EuiHeader, EuiHeaderSection, EuiHeaderSectionItem } from '../header';
1313
import { EuiPageTemplate } from '../page_template';
14+
import { EuiBottomBar } from '../bottom_bar';
1415
import { EuiFlyout } from '../flyout';
1516
import { EuiButton } from '../button';
1617
import { EuiTitle } from '../title';
@@ -469,6 +470,24 @@ export const FlyoutInFixedHeaders: Story = {
469470
},
470471
};
471472

473+
export const GlobalCSSVariable: Story = {
474+
render: ({ ...args }) => (
475+
<>
476+
<EuiHeader position="fixed">
477+
<EuiHeaderSection>
478+
<EuiCollapsibleNavBeta {...args}>
479+
This story tests the global `--euiCollapsibleNavOffset` CSS variable
480+
</EuiCollapsibleNavBeta>
481+
</EuiHeaderSection>
482+
</EuiHeader>
483+
<EuiBottomBar left="var(--euiCollapsibleNavOffset, 0)">
484+
This text should be visible at all times and the bar position should
485+
update dynamically based on the nav width (including on mobile)
486+
</EuiBottomBar>
487+
</>
488+
),
489+
};
490+
472491
export const CollapsedStateInLocalStorage: Story = {
473492
render: () => {
474493
const key = 'EuiCollapsibleNav__isCollapsed';

src/components/collapsible_nav_beta/collapsible_nav_beta.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import React, {
1818
} from 'react';
1919
import classNames from 'classnames';
2020

21-
import { useEuiTheme, useGeneratedHtmlId, throttle } from '../../services';
21+
import {
22+
useEuiTheme,
23+
useEuiThemeCSSVariables,
24+
useGeneratedHtmlId,
25+
throttle,
26+
} from '../../services';
2227

2328
import { CommonProps } from '../common';
2429
import { EuiFlyout, EuiFlyoutProps } from '../flyout';
@@ -88,6 +93,7 @@ const _EuiCollapsibleNavBeta: FunctionComponent<EuiCollapsibleNavBetaProps> = ({
8893
focusTrapProps: _focusTrapProps,
8994
...rest
9095
}) => {
96+
const { setGlobalCSSVariables } = useEuiThemeCSSVariables();
9197
const euiTheme = useEuiTheme();
9298
const headerHeight = euiHeaderVariables(euiTheme).height;
9399

@@ -138,9 +144,17 @@ const _EuiCollapsibleNavBeta: FunctionComponent<EuiCollapsibleNavBetaProps> = ({
138144
const width = useMemo(() => {
139145
if (isOverlayFullWidth) return '100%';
140146
if (isPush && isCollapsed) return headerHeight;
141-
return _width;
147+
return `${_width}px`;
142148
}, [_width, isOverlayFullWidth, isPush, isCollapsed, headerHeight]);
143149

150+
// Other UI elements may need to account for the nav width -
151+
// set a global CSS variable that they can use
152+
useEffect(() => {
153+
setGlobalCSSVariables({
154+
'--euiCollapsibleNavOffset': isOverlay ? '0' : width,
155+
});
156+
}, [width, isOverlay, setGlobalCSSVariables]);
157+
144158
/**
145159
* Prop setup
146160
*/

0 commit comments

Comments
 (0)