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

Make tabbable codeblocks if content overflows #4463

Merged
merged 4 commits into from
Feb 9, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 45 additions & 37 deletions src/components/code/_code_block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,27 @@
* under the License.
*/

import classNames from 'classnames';
import hljs from 'highlight.js';
import React, {
CSSProperties,
FunctionComponent,
KeyboardEvent,
CSSProperties,
useEffect,
useRef,
useState,
} from 'react';
import { createPortal } from 'react-dom';
import classNames from 'classnames';
import hljs from 'highlight.js';

import { EuiCopy } from '../copy';

import { keys, useCombinedRefs } from '../../services';
import { EuiButtonIcon } from '../button';

import { EuiOverlayMask } from '../overlay_mask';

import { keysOf } from '../common';
import { EuiCopy } from '../copy';
import { EuiFocusTrap } from '../focus_trap';

import { keys } from '../../services';
import { EuiI18n } from '../i18n';
import { EuiInnerText } from '../inner_text';
import { keysOf } from '../common';
import { useInnerText } from '../inner_text';
import { useMutationObserver } from '../observer/mutation_observer';
import { useResizeObserver } from '../observer/resize_observer';
import { EuiOverlayMask } from '../overlay_mask';
import { FontSize, PaddingSize } from './code_block';

const fontSizeToClassNameMap = {
Expand Down Expand Up @@ -110,10 +107,31 @@ export const EuiCodeBlockImpl: FunctionComponent<Props> = ({
const [isPortalTargetReady, setIsPortalTargetReady] = useState(false);
const codeTarget = useRef<HTMLDivElement | null>(null);
const code = useRef<HTMLElement | null>(null);
const [wrapperRef, setWrapperRef] = useState<Element | null>(null);
const [innerTextRef, innerText] = useInnerText('');
const combinedRef = useCombinedRefs([innerTextRef, setWrapperRef]);
const { width, height } = useResizeObserver(wrapperRef);
const [codeFullScreen, setCodeFullScreen] = useState<HTMLElement | null>(
null
);

const doesOverflow = () => {
if (!wrapperRef) return;

const { clientWidth, clientHeight, scrollWidth, scrollHeight } = wrapperRef;
const doesOverflow =
scrollHeight > clientHeight || scrollWidth > clientWidth;

wrapperRef.setAttribute('tabindex', doesOverflow ? '0' : '-1');
myasonik marked this conversation as resolved.
Show resolved Hide resolved
};

useMutationObserver(wrapperRef, doesOverflow, {
subtree: true,
childList: true,
});

useEffect(doesOverflow, [width, height, wrapperRef]);

useEffect(() => {
codeTarget.current = document.createElement('div');
setIsPortalTargetReady(true);
Expand Down Expand Up @@ -292,11 +310,10 @@ export const EuiCodeBlockImpl: FunctionComponent<Props> = ({
<EuiOverlayMask>
<EuiFocusTrap clickOutsideDisables={true}>
<div className={fullScreenClasses}>
<pre className={preClasses}>
<pre className={preClasses} tabIndex={0}>
<code
ref={setCodeFullScreen}
className={codeClasses}
tabIndex={0}
onKeyDown={onKeyDown}
/>
</pre>
Expand All @@ -311,31 +328,22 @@ export const EuiCodeBlockImpl: FunctionComponent<Props> = ({
return fullScreenDisplay;
};

const codeBlockControls = getCodeBlockControls(innerText);
return isPortalTargetReady ? (
<>
{createPortal(children, codeTarget.current!)}
<EuiInnerText fallback="">
{(innerTextRef, innerText) => {
const codeBlockControls = getCodeBlockControls(innerText);
return (
<div {...wrapperProps}>
<pre
ref={innerTextRef}
style={optionalStyles}
className={preClasses}>
{codeSnippet}
</pre>

{/*
If the below fullScreen code renders, it actually attaches to the body because of
EuiOverlayMask's React portal usage.
*/}
{codeBlockControls}
{getFullScreenDisplay(codeBlockControls)}
</div>
);
}}
</EuiInnerText>
<div {...wrapperProps}>
<pre ref={combinedRef} style={optionalStyles} className={preClasses}>
myasonik marked this conversation as resolved.
Show resolved Hide resolved
{codeSnippet}
</pre>

{/*
If the below fullScreen code renders, it actually attaches to the body because of
EuiOverlayMask's React portal usage.
*/}
{codeBlockControls}
{getFullScreenDisplay(codeBlockControls)}
</div>
</>
) : null;
};