Skip to content

Commit 30504c8

Browse files
committed
Improve focus behavior for code blocks
1 parent cc9ef6d commit 30504c8

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

src/components/InteractiveCodeBlock/InteractiveCodeBlock.tsx

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Tokenizer, CacheableLineTokens } from './tokenizers/types';
88
import { Token } from './tokenizers/token';
99
import 'requestidlecallback';
1010
import { rhythm } from '../../utils/typography';
11-
import { padding, Side, resets, darkMode, animations, minWidth, variables, masked } from '../../styles/utils';
11+
import { padding, Side, resets, darkMode, animations, minWidth, variables, masked, margin } from '../../styles/utils';
1212
import { Icon } from '../Icon';
1313

1414
function createValueFromString(text: string): Value {
@@ -139,16 +139,19 @@ const loadingIcon = (
139139
const editIcon = <Icon css={iconStyles} src={require('../icons/pencil.svg')} alt="Edit" />;
140140
const resetIcon = <Icon css={iconStyles} src={require('../icons/reset.svg')} alt="Reset" />;
141141
const containerStyles = css([
142-
padding(0.5, Side.Vertical),
142+
padding(0.5),
143+
margin(-0.5, Side.Horizontal),
143144
{
144145
position: 'relative',
145146
fontSize: '1rem',
146-
':hover > button': {
147-
opacity: 1,
147+
':focus-within': {
148+
background: 'var(--background-alt)',
148149
},
149150
...minWidth(variables.sizes.bigEnough, [
151+
margin(0, Side.Horizontal),
150152
padding(1, Side.Left),
151153
padding(2, Side.Right),
154+
{ borderRadius: 8 },
152155
]),
153156
},
154157
]);
@@ -200,31 +203,33 @@ export function InteractiveCodeBlock<
200203
}, [props.isLoading]);
201204

202205
return (
203-
<pre css={containerStyles}>
204-
<Editor
205-
ref={editorRef}
206-
value={state}
207-
onChange={({ value, operations }) => {
208-
if (props.onChange) {
209-
props.onChange(getFullText(value), operations);
210-
}
206+
<div css={{ position: 'relative', ':hover > button': { opacity: 1 } }}>
207+
<pre css={containerStyles}>
208+
<Editor
209+
ref={editorRef}
210+
value={state}
211+
onChange={({ value, operations }) => {
212+
if (props.onChange) {
213+
props.onChange(getFullText(value), operations);
214+
}
211215

212-
setState(value);
213-
cancelIdleCallback(callbackId);
214-
callbackId = requestIdleCallback(() => {
215-
const x = value.set('decorations', decorateDocument(value.document)) as Value;
216-
setState(x);
217-
});
218-
}}
219-
plugins={plugins}
220-
decorateNode={decorateLineSync}
221-
className={props.className}
222-
css={editorStyles}
223-
spellCheck={false}
224-
autoCorrect={false}
225-
readOnly={props.readOnly}
226-
style={{ wordWrap: 'normal', whiteSpace: 'pre' }}
227-
/>
216+
setState(value);
217+
cancelIdleCallback(callbackId);
218+
callbackId = requestIdleCallback(() => {
219+
const x = value.set('decorations', decorateDocument(value.document)) as Value;
220+
setState(x);
221+
});
222+
}}
223+
plugins={plugins}
224+
decorateNode={decorateLineSync}
225+
className={props.className}
226+
css={editorStyles}
227+
spellCheck={false}
228+
autoCorrect={false}
229+
readOnly={props.readOnly}
230+
style={{ wordWrap: 'normal', whiteSpace: 'pre' }}
231+
/>
232+
</pre>
228233
{buttonIcon ? (
229234
<Button
230235
css={{ opacity: buttonIcon === resetIcon ? 1 : 0}}
@@ -243,7 +248,7 @@ export function InteractiveCodeBlock<
243248
{buttonIcon}
244249
</Button>
245250
) : null}
246-
</pre>
251+
</div>
247252
);
248253
}
249254

0 commit comments

Comments
 (0)