Skip to content

Commit fd1ae3c

Browse files
committed
Enable text selection within response previews while ensuring clicks outside of selections still toggle expansion
1 parent 808e4d0 commit fd1ae3c

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎packages/ui/src/components/editor/panel/ResponsePreview/components/TextItem.module.scss‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
.text {
77
margin: 6px 12px;
88
word-wrap: break-word;
9+
user-select: text;
910

1011
> * {
1112
margin: 0;

‎packages/ui/src/components/editor/panel/ResponsePreview/components/TextItem.tsx‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,35 @@ type Props = {
1111

1212
export const TextItem: FC<Props> = (props) => {
1313
const header_ref = useRef<HTMLDivElement>(null)
14+
const mouse_moved = useRef(false)
15+
const has_selection = useRef(false)
16+
17+
const handle_mouse_down = () => {
18+
mouse_moved.current = false
19+
20+
const selection = window.getSelection()
21+
has_selection.current =
22+
(selection && selection.toString().length > 0) || false
23+
}
24+
25+
const handle_mouse_move = () => {
26+
mouse_moved.current = true
27+
28+
29+
}
30+
31+
const handle_mouse_up = () => {
32+
if (!mouse_moved.current && !has_selection.current) {
33+
props.on_toggle(header_ref.current!)
34+
}
35+
}
1436

1537
return (
1638
<div className={styles.container}>
1739
<div
1840
ref={header_ref}
1941
className={styles.header}
20-
onClick={() =>
21-
header_ref.current && props.on_toggle(header_ref.current)
22-
}
42+
onClick={() => props.on_toggle(header_ref.current!)}
2343
title={props.content}
2444
>
2545
<IconButton
@@ -33,14 +53,19 @@ export const TextItem: FC<Props> = (props) => {
3353
{props.is_expanded && (
3454
<div
3555
className={styles.text}
36-
onClick={() =>
37-
header_ref.current && props.on_toggle(header_ref.current)
38-
}
56+
onMouseDown={handle_mouse_down}
57+
onMouseMove={handle_mouse_move}
58+
onMouseUp={handle_mouse_up}
3959
>
4060
<ReactMarkdown disallowedElements={['a', 'hr']}>
4161
{props.content}
4262
</ReactMarkdown>
43-
<div className={styles.text__collapse}>
63+
<div
64+
className={styles.text__collapse}
65+
onClick={() => {
66+
props.on_toggle(header_ref.current!)
67+
}}
68+
>
4469
<span>Show less</span>
4570
</div>
4671
</div>

‎packages/ui/src/components/editor/panel/Responses/Responses.module.scss‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@
109109
);
110110
padding-left: 10px;
111111
visibility: hidden;
112+
pointer-events: none;
112113

113114
&__button {
114115
display: flex;
116+
pointer-events: all;
115117

116118
> span {
117119
opacity: var(--dimmed-opacity);

0 commit comments

Comments
 (0)