Skip to content

Commit

Permalink
Fix SSR for breadcrumbs and collapsible-nav (#3970)
Browse files Browse the repository at this point in the history
Closes #3687. Put guards around references to `window` in the breadcrumbs and collapsible nav
components, so that EUI can still render in an SSR environment.
  • Loading branch information
pugnascotia committed Sep 2, 2020
1 parent b82e8e4 commit fc3a93c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fixed bug in `EuiComboBox` where the input was dropping to the next line when a `EuiBadge` had a very long text ([#3968](https://github.com/elastic/eui/pull/3968))
- Fixed type mismatch between `EuiSelectable` options extended via `EuiSelectableOption` and internal option types ([#3983](https://github.com/elastic/eui/pull/3983))
- Fixed `EuiButton` CSS for RTL languages by using `margin-inline-[pos]` instead of `margin-[pos]` ([#3974](https://github.com/elastic/eui/pull/3974))
- Fix server-side rendering of `EuiBreadcrumbs` and `EuiCollapsibleNav` ([#3970](https://github.com/elastic/eui/pull/3970))

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

Expand Down
6 changes: 3 additions & 3 deletions src/components/breadcrumbs/breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import React, {
FunctionComponent,
MouseEventHandler,
ReactNode,
useState,
useEffect,
useState,
} from 'react';
import classNames from 'classnames';
import { throttle } from '../color_picker/utils';
Expand All @@ -34,7 +34,7 @@ import { EuiInnerText } from '../inner_text';
import { EuiLink } from '../link';
import { EuiPopover } from '../popover';
import { EuiIcon } from '../icon';
import { getBreakpoint, EuiBreakpointSize } from '../../services/breakpoint';
import { EuiBreakpointSize, getBreakpoint } from '../../services/breakpoint';

export type EuiBreadcrumbResponsiveMaxCount = {
/**
Expand Down Expand Up @@ -187,7 +187,7 @@ export const EuiBreadcrumbs: FunctionComponent<EuiBreadcrumbsProps> = ({
...rest
}) => {
const [currentBreakpoint, setCurrentBreakpoint] = useState(
getBreakpoint(window.innerWidth)
getBreakpoint(typeof window === 'undefined' ? -Infinity : window.innerWidth)
);

const functionToCallOnWindowResize = throttle(() => {
Expand Down
11 changes: 6 additions & 5 deletions src/components/collapsible_nav/collapsible_nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
*/

import React, {
cloneElement,
FunctionComponent,
HTMLAttributes,
ReactElement,
ReactNode,
useEffect,
useState,
HTMLAttributes,
ReactElement,
cloneElement,
} from 'react';
import classNames from 'classnames';
import { throttle } from '../color_picker/utils';
import { EuiWindowEvent, keys, htmlIdGenerator } from '../../services';
import { EuiWindowEvent, htmlIdGenerator, keys } from '../../services';
import { EuiFocusTrap } from '../focus_trap';
import { EuiOverlayMask, EuiOverlayMaskProps } from '../overlay_mask';
import { CommonProps } from '../common';
Expand Down Expand Up @@ -95,7 +95,8 @@ export const EuiCollapsibleNav: FunctionComponent<EuiCollapsibleNavProps> = ({
}) => {
const [flyoutID] = useState(id || htmlIdGenerator()('euiCollapsibleNav'));
const [windowIsLargeEnoughToDock, setWindowIsLargeEnoughToDock] = useState(
window.innerWidth >= dockedBreakpoint
(typeof window === 'undefined' ? Infinity : window.innerWidth) >=
dockedBreakpoint
);
const navIsDocked = isDocked && windowIsLargeEnoughToDock;

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

0 comments on commit fc3a93c

Please sign in to comment.