-
Notifications
You must be signed in to change notification settings - Fork 0
Replace invalid className HTML attributes #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { useState } from 'preact/hooks'; | ||
| import { useLiveRegion } from '../hooks/useLiveRegion'; | ||
| import { useState } from "preact/hooks"; | ||
| import { useLiveRegion } from "../hooks/useLiveRegion"; | ||
| import CircleButton from "../CircleButton"; | ||
|
|
||
| interface CopyCodeButtonProps { | ||
|
|
@@ -9,56 +9,47 @@ interface CopyCodeButtonProps { | |
|
|
||
| export const CopyCodeButton = ({ | ||
| textToCopy, | ||
| announceOnCopy = 'Code copied to clipboard' | ||
| announceOnCopy = "Code copied to clipboard", | ||
| }: CopyCodeButtonProps) => { | ||
| const [isCopied, setIsCopied] = useState(false); | ||
|
|
||
| const { ref: liveRegionRef, announce } = useLiveRegion<HTMLSpanElement>(); | ||
|
|
||
| const copyTextToClipboard = async () => { | ||
| console.log('Copy button clicked'); | ||
| console.log('Text to copy:', textToCopy); | ||
|
|
||
| try { | ||
| console.log('Using Clipboard API'); | ||
| await navigator.clipboard.writeText(textToCopy); | ||
| console.log('Text copied successfully'); | ||
|
|
||
| announce(announceOnCopy); | ||
|
|
||
| setIsCopied(true); | ||
| setTimeout(() => { | ||
| setIsCopied(false); | ||
| console.log('Copy state reset'); | ||
| }, 2000); | ||
| } catch (err) { | ||
| console.error('Clipboard API copy failed:', err); | ||
| console.error("Clipboard API copy failed:", err); | ||
| } | ||
| }; | ||
|
Comment on lines
18
to
31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using A more robust approach is to use the After importing useEffect(() => {
if (!isCopied) return;
const timer = setTimeout(() => {
setIsCopied(false);
}, 2000);
return () => clearTimeout(timer);
}, [isCopied]);And this function should only be responsible for setting |
||
|
|
||
| return ( | ||
| <> | ||
| <CircleButton | ||
| onClick={() => { | ||
| console.log('CircleButton clicked'); | ||
| copyTextToClipboard(); | ||
| }} | ||
| onClick={copyTextToClipboard} | ||
| ariaLabel="Copy code to clipboard" | ||
| className={`bg-white ${isCopied ? 'text-green-600' : 'text-black'} transition-colors duration-200`} | ||
| className={`bg-white ${isCopied ? "text-green-600" : "text-black"} transition-colors duration-200`} | ||
| > | ||
| {isCopied ? ( | ||
| <svg | ||
| width="18" | ||
| height="22" | ||
| viewBox="0 0 24 24" | ||
| fill="none" | ||
| <svg | ||
| width="18" | ||
| height="22" | ||
| viewBox="0 0 24 24" | ||
| fill="none" | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| > | ||
| <path | ||
| d="M20 6L9 17L4 12" | ||
| stroke="currentColor" | ||
| strokeWidth="2" | ||
| strokeLinecap="round" | ||
| <path | ||
| d="M20 6L9 17L4 12" | ||
| stroke="currentColor" | ||
| strokeWidth="2" | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| /> | ||
| </svg> | ||
|
|
@@ -89,4 +80,4 @@ export const CopyCodeButton = ({ | |
| <span ref={liveRegionRef} aria-live="polite" class="sr-only" /> | ||
| </> | ||
| ); | ||
| }; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make the component more robust when handling the timeout for resetting the copy state,
useEffectshould be used. Please import it.