Skip to content
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

Fix SSR for breadcrumbs and collapsible-nav #3970

Merged
merged 8 commits into from
Sep 2, 2020
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `28.3.0`.
**Bug fixes**

- Fix server-side rendering of `EuiBreadcrumbs` ([#3970](https://github.com/elastic/eui/pull/3970))
pugnascotia marked this conversation as resolved.
Show resolved Hide resolved

## [`28.3.0`](https://github.com/elastic/eui/tree/v28.3.0)

Expand Down
18 changes: 12 additions & 6 deletions src/components/breadcrumbs/breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,14 @@ export const EuiBreadcrumbs: FunctionComponent<EuiBreadcrumbsProps> = ({
max = 5,
...rest
}) => {
const innerWidth =
typeof window === 'undefined' ? -Infinity : window.innerWidth;
const [currentBreakpoint, setCurrentBreakpoint] = useState(
getBreakpoint(window.innerWidth)
getBreakpoint(innerWidth)
);

const functionToCallOnWindowResize = throttle(() => {
const newBreakpoint = getBreakpoint(window.innerWidth);
const newBreakpoint = getBreakpoint(innerWidth);
cchaos marked this conversation as resolved.
Show resolved Hide resolved
if (newBreakpoint !== currentBreakpoint) {
setCurrentBreakpoint(newBreakpoint);
}
Expand All @@ -200,11 +202,15 @@ export const EuiBreadcrumbs: FunctionComponent<EuiBreadcrumbsProps> = ({

// Add window resize handlers
useEffect(() => {
window.addEventListener('resize', functionToCallOnWindowResize);
if (typeof window !== 'undefined') {
window.addEventListener('resize', functionToCallOnWindowResize);

return () => {
window.removeEventListener('resize', functionToCallOnWindowResize);
};
return () => {
window.removeEventListener('resize', functionToCallOnWindowResize);
};
} else {
return () => {};
}
}, [responsive, functionToCallOnWindowResize]);

const breadcrumbElements = breadcrumbs.map((breadcrumb, index) => {
Expand Down
33 changes: 20 additions & 13 deletions src/components/collapsible_nav/collapsible_nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,17 @@ export const EuiCollapsibleNav: FunctionComponent<EuiCollapsibleNavProps> = ({
maskProps,
...rest
}) => {
const innerWidth =
typeof window === 'undefined' ? -Infinity : window.innerWidth;

const [flyoutID] = useState(id || htmlIdGenerator()('euiCollapsibleNav'));
const [windowIsLargeEnoughToDock, setWindowIsLargeEnoughToDock] = useState(
window.innerWidth >= dockedBreakpoint
innerWidth >= dockedBreakpoint
);
const navIsDocked = isDocked && windowIsLargeEnoughToDock;

const functionToCallOnWindowResize = throttle(() => {
if (window.innerWidth < dockedBreakpoint) {
if (innerWidth < dockedBreakpoint) {
setWindowIsLargeEnoughToDock(false);
} else {
setWindowIsLargeEnoughToDock(true);
Expand All @@ -110,19 +113,23 @@ export const EuiCollapsibleNav: FunctionComponent<EuiCollapsibleNavProps> = ({

// Watch for docked status and appropriately add/remove body classes and resize handlers
useEffect(() => {
window.addEventListener('resize', functionToCallOnWindowResize);
if (typeof window === 'undefined') {
return () => {};
} else {
window.addEventListener('resize', functionToCallOnWindowResize);

if (navIsDocked) {
document.body.classList.add('euiBody--collapsibleNavIsDocked');
} else if (isOpen) {
document.body.classList.add('euiBody--collapsibleNavIsOpen');
}
if (navIsDocked) {
document.body.classList.add('euiBody--collapsibleNavIsDocked');
} else if (isOpen) {
document.body.classList.add('euiBody--collapsibleNavIsOpen');
}

return () => {
document.body.classList.remove('euiBody--collapsibleNavIsDocked');
document.body.classList.remove('euiBody--collapsibleNavIsOpen');
window.removeEventListener('resize', functionToCallOnWindowResize);
};
return () => {
document.body.classList.remove('euiBody--collapsibleNavIsDocked');
document.body.classList.remove('euiBody--collapsibleNavIsOpen');
window.removeEventListener('resize', functionToCallOnWindowResize);
};
}
}, [navIsDocked, functionToCallOnWindowResize, isOpen]);

const onKeyDown = (event: KeyboardEvent) => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/breakpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const BREAKPOINT_KEYS = keysOf(BREAKPOINTS);
* that is less than or equal to the width
*
* @param {number} width Can either be the full window width or any width
* @param {EuiBreakpoints} breakpoints An object with keys for sizing and values for minimu width
* @param {EuiBreakpoints} breakpoints An object with keys for sizing and values for minimum width
* @returns {string | undefined} Name of the breakpoint key or `undefined` if a key doesn't exist
*/
export function getBreakpoint(
Expand Down