Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1c26283

Browse files
Scroll to top code block on question change
1 parent 1b58d96 commit 1c26283

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

‎src/components/Code.jsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { connect } from 'react-redux';
44
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter';
55
import js from 'react-syntax-highlighter/dist/languages/hljs/javascript';
66
import { getJS, getCurrent } from '../selectors';
7+
import CodePreTag from './CodePreTag';
78

89
SyntaxHighlighter.registerLanguage('javascript', js);
910

@@ -13,13 +14,13 @@ const Code = props => {
1314
return (
1415
<Fragment>
1516
{js === 'es5' && (
16-
<SyntaxHighlighter language="javascript" style={style} className="fixed">
17+
<SyntaxHighlighter language="javascript" style={style} className="fixed"PreTag={CodePreTag}>
1718
{current.codeES5}
1819
</SyntaxHighlighter>
1920
)}
2021

2122
{js === 'es6' && (
22-
<SyntaxHighlighter language="javascript" style={style} className="fixed">
23+
<SyntaxHighlighter language="javascript" style={style} className="fixed"PreTag={CodePreTag}>
2324
{current.codeES6}
2425
</SyntaxHighlighter>
2526
)}

‎src/components/CodePreTag.jsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React, { useRef, useEffect } from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
const CodePreTag = ({ children, ...restProps }) => {
5+
const syntaxHighlighterEl = useRef(null);
6+
7+
useEffect(() => {
8+
if (syntaxHighlighterEl.current && syntaxHighlighterEl.current.scroll) {
9+
syntaxHighlighterEl.current.scroll(0, 0);
10+
}
11+
}, [children]);
12+
13+
return <pre {...restProps} ref={syntaxHighlighterEl}>{children}</pre>;
14+
};
15+
16+
CodePreTag.propTypes = {
17+
children: PropTypes.node
18+
};
19+
20+
export default CodePreTag;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /