Skip to content

Commit c0ce40a

Browse files
committed
bottom bar var
1 parent 6c3c997 commit c0ce40a

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

packages/eui/src/components/bottom_bar/bottom_bar.test.tsx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,83 @@ describe('EuiBottomBar', () => {
8989
expect(baseElement).toMatchSnapshot();
9090
});
9191
});
92+
93+
describe('CSS variables', () => {
94+
test('sets the correct css variable for the bottom bar', () => {
95+
const { unmount } = render(
96+
<EuiBottomBar affordForDisplacement={true} usePortal={true}>
97+
Content
98+
</EuiBottomBar>
99+
);
100+
101+
// The CSS variable should be set on the document root
102+
const cssVarValue = getComputedStyle(document.documentElement).getPropertyValue(
103+
'--euiBottomBarOffset'
104+
);
105+
expect(cssVarValue).toBeTruthy();
106+
expect(cssVarValue).toMatch(/\d+px/);
107+
108+
unmount();
109+
110+
// After unmounting, the CSS variable should be cleared
111+
const clearedValue = getComputedStyle(document.documentElement).getPropertyValue(
112+
'--euiBottomBarOffset'
113+
);
114+
expect(clearedValue.trim()).toBe('');
115+
});
116+
117+
test('does not set css variable when affordForDisplacement is false', () => {
118+
render(
119+
<EuiBottomBar affordForDisplacement={false}>
120+
Content
121+
</EuiBottomBar>
122+
);
123+
124+
const cssVarValue = getComputedStyle(document.documentElement).getPropertyValue(
125+
'--euiBottomBarOffset'
126+
);
127+
expect(cssVarValue.trim()).toBe('');
128+
});
129+
130+
test('does not set css variable when usePortal is false', () => {
131+
render(
132+
<EuiBottomBar affordForDisplacement={true} usePortal={false}>
133+
Content
134+
</EuiBottomBar>
135+
);
136+
137+
const cssVarValue = getComputedStyle(document.documentElement).getPropertyValue(
138+
'--euiBottomBarOffset'
139+
);
140+
expect(cssVarValue.trim()).toBe('');
141+
});
142+
143+
test('does not set css variable for non-fixed positions', () => {
144+
const { unmount: unmountSticky } = render(
145+
<EuiBottomBar position="sticky">
146+
Content
147+
</EuiBottomBar>
148+
);
149+
150+
let cssVarValue = getComputedStyle(document.documentElement).getPropertyValue(
151+
'--euiBottomBarOffset'
152+
);
153+
expect(cssVarValue.trim()).toBe('');
154+
155+
unmountSticky();
156+
157+
const { unmount: unmountStatic } = render(
158+
<EuiBottomBar position="static">
159+
Content
160+
</EuiBottomBar>
161+
);
162+
163+
cssVarValue = getComputedStyle(document.documentElement).getPropertyValue(
164+
'--euiBottomBarOffset'
165+
);
166+
expect(cssVarValue.trim()).toBe('');
167+
168+
unmountStatic();
169+
});
170+
});
92171
});

packages/eui/src/components/bottom_bar/bottom_bar.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import React, {
1818
import {
1919
useCombinedRefs,
2020
useEuiMemoizedStyles,
21+
useEuiThemeCSSVariables,
2122
EuiThemeProvider,
2223
} from '../../services';
2324
import { EuiScreenReaderOnly } from '../accessibility';
@@ -120,6 +121,7 @@ const _EuiBottomBar = forwardRef<
120121
ref
121122
) => {
122123
const styles = useEuiMemoizedStyles(euiBottomBarStyles);
124+
const { setGlobalCSSVariables } = useEuiThemeCSSVariables();
123125

124126
// Force some props if `fixed` position, but not if the user has supplied these
125127
affordForDisplacement =
@@ -134,6 +136,11 @@ const _EuiBottomBar = forwardRef<
134136
useEffect(() => {
135137
if (affordForDisplacement && usePortal) {
136138
document.body.style.paddingBottom = `${dimensions.height}px`;
139+
140+
// EUI doesn't use this css variable, but it is useful for consumers
141+
setGlobalCSSVariables({
142+
'--euiBottomBarOffset': `${dimensions.height}px`,
143+
});
137144
}
138145

139146
if (bodyClassName) {
@@ -143,13 +150,16 @@ const _EuiBottomBar = forwardRef<
143150
return () => {
144151
if (affordForDisplacement && usePortal) {
145152
document.body.style.paddingBottom = '';
153+
setGlobalCSSVariables({
154+
'--euiBottomBarOffset': null,
155+
});
146156
}
147157

148158
if (bodyClassName) {
149159
document.body.classList.remove(bodyClassName);
150160
}
151161
};
152-
}, [affordForDisplacement, usePortal, dimensions, bodyClassName]);
162+
}, [affordForDisplacement, usePortal, dimensions, bodyClassName, setGlobalCSSVariables]);
153163

154164
const classes = classNames(
155165
'euiBottomBar',

packages/website/docs/components/containers/bottom-bar.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,21 @@ export default () => {
209209
};
210210
```
211211

212+
### Relative positioning
213+
214+
When `affordForDisplacement` is enabled and the bottom bar is positioned as `fixed`, **EuiBottomBar** automatically sets a global CSS variable that you can use to position other fixed elements in your application relative to the bottom bar's height:
215+
216+
- `--euiBottomBarOffset` (Set when `affordForDisplacement={true}` and `position="fixed"`)
217+
218+
This variable is automatically updated when the bottom bar resizes and is removed when the bottom bar is closed.
219+
220+
```css
221+
.custom-fixed-footer {
222+
/* Adjust footer position when bottom bar is visible */
223+
padding-bottom: var(--euiBottomBarOffset, 0);
224+
}
225+
```
226+
212227
## Props
213228

214229
import docgen from '@elastic/eui-docgen/dist/components/bottom_bar';

0 commit comments

Comments
 (0)