Skip to content

Commit

Permalink
feat: prevent copy when the answer is hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongao97 committed Sep 24, 2024
1 parent 52a44c0 commit d0e1bbd
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/components/Codesplit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ const Codesplit = (props) => {
const [isAnswerVisible, setIsAnswerVisible] = React.useState(false);
const [isCopied, setIsCopied] = React.useState(false);

const containsBlank = !!props['data-contains-blank'];

// Prevent copy feature when:
// the snippet contains blanks and the answer is not visible
const preventCopy = containsBlank && !isAnswerVisible;

const toggleAnswerHiddenStatus = () => {
setIsAnswerVisible((lastState) => !lastState);
};

const copyRaw = () => {
if (preventCopy) return;
if (isCopied) return;
if (!navigator.clipboard || !navigator.clipboard.writeText) return;

Expand All @@ -38,8 +45,6 @@ const Codesplit = (props) => {
}, 1000);
};

const containsBlank = !!props['data-contains-blank'];

return (
<div
className={`pt-0 ${props.className} ${isAnswerVisible ? 'is-answer-visible' : ''}`}
Expand Down Expand Up @@ -68,7 +73,8 @@ const Codesplit = (props) => {
)}

<button
className="relative flex items-center rounded px-2.5 py-1.5 text-xs font-semibold hover:bg-gray-300"
className={`first-letter relative flex items-center rounded px-2.5 py-1.5 text-xs font-semibold
${preventCopy ? 'cursor-not-allowed text-gray-400' : 'hover:bg-gray-300'}`}
onClick={copyRaw}
>
<div
Expand Down

0 comments on commit d0e1bbd

Please sign in to comment.