Skip to content

Commit

Permalink
uses useRef instead of useState for simple ref usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitropoulos committed Apr 10, 2020
1 parent 6d5ad6b commit d642d97
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/components/nav_drawer/nav_drawer_flyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {
FunctionComponent,
KeyboardEventHandler,
HTMLAttributes,
useRef,
} from 'react';
import classNames from 'classnames';
import tabbable from 'tabbable';
Expand Down Expand Up @@ -48,7 +49,7 @@ export const EuiNavDrawerFlyout: FunctionComponent<EuiNavDrawerFlyoutProps> = ({
wrapText = false,
...rest
}) => {
const [menuEl, setMenuEl] = useState<HTMLDivElement | null>();
const menuElementRef = useRef<HTMLDivElement>(null);
const [
tabbables,
setTabbables,
Expand All @@ -68,8 +69,10 @@ export const EuiNavDrawerFlyout: FunctionComponent<EuiNavDrawerFlyoutProps> = ({
handleClose();
} else if (event.keyCode === keyCodes.TAB) {
let tabs = tabbables;
if (!tabs && menuEl) {
tabs = tabbable(menuEl).filter(element => element.tagName !== 'DIV');
if (!tabs && menuElementRef.current) {
tabs = tabbable(menuElementRef.current).filter(
element => element.tagName !== 'DIV'
);
setTabbables(tabs);
}
if (!tabs) {
Expand All @@ -96,7 +99,7 @@ export const EuiNavDrawerFlyout: FunctionComponent<EuiNavDrawerFlyoutProps> = ({
className={classes}
aria-labelledby={LABEL}
onKeyDown={handleKeyDown}
ref={setMenuEl}
ref={menuElementRef}
{...rest}>
<EuiTitle className="euiNavDrawerFlyout__title" size="xxs">
<div id={LABEL} tabIndex={-1}>
Expand Down

0 comments on commit d642d97

Please sign in to comment.