Skip to content

Commit adbcc01

Browse files
scroll to top when adding comment (codesandbox#3829)
* scroll to top when adding comment * add smooth scroll * fix type check
1 parent 7299c1a commit adbcc01

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Comments/AddComment.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
import { ENTER } from '@codesandbox/common/lib/utils/keycodes';
22
import { Element, FormField, Textarea } from '@codesandbox/components';
33
import { css } from '@styled-system/css';
4-
import { useOvermind } from 'app/overmind';
54
import React, { useState } from 'react';
65

7-
export const AddComment: React.FC = () => {
6+
type Props = {
7+
onSubmit: (value: string) => void;
8+
};
9+
10+
export const AddComment: React.FC<Props> = ({ onSubmit }) => {
811
const [value, setValue] = useState('');
9-
const { actions } = useOvermind();
1012

11-
const onSubmit = e => {
12-
e.preventDefault();
13+
const submit = event => {
14+
event.preventDefault();
1315
if (value) {
14-
actions.comments.addComment({
15-
content: value,
16-
});
16+
onSubmit(value);
1717
setValue('');
1818
}
1919
};
2020

2121
// Form elements submit on Enter, except Textarea :)
2222
const submitOnEnter = event => {
23-
if (event.keyCode === ENTER && !event.shiftKey) onSubmit(event);
23+
if (event.keyCode === ENTER && !event.shiftKey) {
24+
submit(event);
25+
}
2426
};
2527

2628
return (
@@ -34,7 +36,7 @@ export const AddComment: React.FC = () => {
3436
boxShadow: theme => `0px -32px 32px ${theme.colors.dialog.background}`,
3537
})}
3638
>
37-
<form onSubmit={onSubmit}>
39+
<form onSubmit={submit}>
3840
<FormField label="Add a comment" hideLabel>
3941
<Textarea
4042
autosize

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Comments/index.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { css } from '@styled-system/css';
1111
import { useOvermind } from 'app/overmind';
1212
import React from 'react';
13+
1314
import { AddComment } from './AddComment';
1415
import { Comment } from './Comment';
1516

@@ -24,6 +25,7 @@ export const Comments: React.FC = () => {
2425
},
2526
actions: { comments: commentsActions },
2627
} = useOvermind();
28+
const scrollRef = React.useRef(null);
2729
const options = Object.values(CommentsFilterOption);
2830

2931
const getSelectedFilter = () => {
@@ -37,6 +39,13 @@ export const Comments: React.FC = () => {
3739
}
3840
};
3941

42+
const onSubmit = value => {
43+
commentsActions.addComment({
44+
content: value,
45+
});
46+
scrollRef.current.scrollTop = 0;
47+
};
48+
4049
const Empty = () => (
4150
<Stack
4251
direction="vertical"
@@ -101,11 +110,13 @@ export const Comments: React.FC = () => {
101110

102111
{currentComments.length ? (
103112
<List
113+
ref={scrollRef}
104114
marginTop={4}
105115
css={{
106116
// stretch within container, leaving space for comment box
107117
height: 'calc(100% - 32px)',
108118
overflow: 'auto',
119+
scrollBehavior: 'smooth',
109120
}}
110121
>
111122
{currentCommentsByDate.today.length ? (
@@ -132,7 +143,7 @@ export const Comments: React.FC = () => {
132143
) : null}
133144
</div>
134145
{currentComments.length ? null : <Empty />}
135-
<AddComment />
146+
<AddComment onSubmit={onSubmit} />
136147
</Stack>
137148
);
138149
};

0 commit comments

Comments
 (0)