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

Fixed EuiCode's fullscreen mode #3633

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 `26.0.0`.
**Bug fixes**

- Fixed fullscreen render issue in `EuiCode` ([#3633](https://github.com/elastic/eui/pull/3633))

## [`26.0.0`](https://github.com/elastic/eui/tree/v26.0.0)

Expand Down
21 changes: 13 additions & 8 deletions src/components/code/_code_block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ export const EuiCodeBlockImpl: FunctionComponent<Props> = ({
const [isPortalTargetReady, setIsPortalTargetReady] = useState(false);
const codeTarget = useRef<HTMLDivElement | null>(null);
const code = useRef<HTMLElement | null>(null);
const codeFullScreen = useRef<HTMLElement | null>(null);
const [codeFullScreen, setCodeFullScreen] = useState<HTMLElement | null>(
null
);

useEffect(() => {
codeTarget.current = document.createElement('div');
Expand All @@ -130,20 +132,23 @@ export const EuiCodeBlockImpl: FunctionComponent<Props> = ({
if (code.current) {
code.current.innerHTML = html;
}
if (codeFullScreen.current) {
codeFullScreen.current.innerHTML = html;
}

if (language) {
if (code.current) {
hljs.highlightBlock(code.current);
}
}
});

if (codeFullScreen.current) {
hljs.highlightBlock(codeFullScreen.current);
useEffect(() => {
if (codeFullScreen) {
const html = isPortalTargetReady ? codeTarget.current!.innerHTML : '';
codeFullScreen.innerHTML = html;
if (language) {
hljs.highlightBlock(codeFullScreen);
}
}
});
}, [isPortalTargetReady, codeFullScreen, language]);

const onKeyDown = (event: KeyboardEvent<HTMLElement>) => {
if (event.key === keys.ESCAPE) {
Expand Down Expand Up @@ -289,7 +294,7 @@ export const EuiCodeBlockImpl: FunctionComponent<Props> = ({
<div className={fullScreenClasses}>
<pre className={preClasses}>
<code
ref={codeFullScreen}
ref={setCodeFullScreen}
className={codeClasses}
tabIndex={0}
onKeyDown={onKeyDown}
Expand Down
15 changes: 15 additions & 0 deletions src/components/code/code_block.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,20 @@ describe('EuiCodeBlock', () => {

ReactDOM.render(<App />, appDiv);
});

it('displays content in fullscreen mode', () => {
const component = mount(
<EuiCodeBlock language="javascript" overflowHeight={300}>
const value = &quot;hello&quot;
</EuiCodeBlock>
);

component.find('EuiButtonIcon[iconType="fullScreen"]').simulate('click');
component.update();

expect(component.find('.euiCodeBlock-isFullScreen').text()).toBe(
'const value = "hello"'
);
});
});
});