InfoPanel: make snippets keyboard-navigable and autoselected

Code snippets can now be navigated to be keyboard. Additionally,
focusing this way or by single-clicking will select the entire snippet
contents by default, then after a 50ms delay, allow for normal text
selection. This is a better experience for copying, especially on
mobile.
This commit is contained in:
rektdeckard
2020-08-17 13:06:05 -04:00
parent 16bfd4cdf8
commit 1d2bacd899
2 changed files with 21 additions and 2 deletions

View File

@@ -87,10 +87,27 @@
.snippet { .snippet {
margin-bottom: 24px; margin-bottom: 24px;
} }
.snippet pre { .snippet pre {
text-overflow: ellipsis; text-overflow: ellipsis;
color: black; color: black;
user-select: all;
-moz-user-select: all;
-webkit-user-select: all;
} }
.snippet pre:focus {
animation: select 50ms step-end forwards;
}
@keyframes select {
to {
user-select: text;
-moz-user-select: text;
-webkit-user-select: text;
}
}
.snippet button { .snippet button {
background-color: transparent; background-color: transparent;
margin: 0; margin: 0;

View File

@@ -101,7 +101,7 @@ const InfoPanel: React.FC<InfoPanelProps> = (props) => {
<div className="icon-usage"> <div className="icon-usage">
<div className="snippet"> <div className="snippet">
HTML/CSS HTML/CSS
<pre> <pre tabIndex={0}>
{snippets.html} {snippets.html}
<button <button
title="Copy snippet" title="Copy snippet"
@@ -117,7 +117,7 @@ const InfoPanel: React.FC<InfoPanelProps> = (props) => {
</div> </div>
<div className="snippet"> <div className="snippet">
React React
<pre> <pre tabIndex={0}>
{snippets.react} {snippets.react}
<button <button
title="Copy snippet" title="Copy snippet"
@@ -154,10 +154,12 @@ const InfoPanel: React.FC<InfoPanelProps> = (props) => {
</div> </div>
<X <X
className="close-icon" className="close-icon"
tabIndex={0}
color="currentColor" color="currentColor"
size={32} size={32}
weight="fill" weight="fill"
onClick={() => setOpen(false)} onClick={() => setOpen(false)}
onKeyDown={(e) => {e.key === "Enter" && setOpen(false)}}
/> />
</motion.section> </motion.section>
); );