Skip to content

Commit

Permalink
use data-highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-dot-js committed Oct 17, 2023
1 parent dc44ca3 commit 94e15b8
Showing 1 changed file with 29 additions and 35 deletions.
64 changes: 29 additions & 35 deletions src/components/code-block.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
PropsWithChildren,
createContext,
forwardRef,
useContext,
useEffect,
useId,
useMemo,
useState,
} from "react";
import { PropsWithChildren, createContext, forwardRef, useContext, useEffect, useId, useMemo, useState } from "react";
import type { WithStyleProps } from "../types/with-style-props";
import { cx } from "../lib/cx";

Expand Down Expand Up @@ -114,6 +105,7 @@ const CodeBlockLine = forwardRef<HTMLSpanElement, CodeBlockLineProps>(
return (
<span
data-line-number={lineNumber}
data-highlight={highlight}
className={cx("block before:mr-1 before:content-[attr(data-line-number)]", className)}
ref={ref}
style={style}
Expand All @@ -129,32 +121,34 @@ type CodeBlockCopyButtonProps = WithStyleProps & {
onCopyError?: (error: unknown) => void;
};

const CodeBlockCopyButton = forwardRef<HTMLButtonElement, CodeBlockCopyButtonProps>(({ className, onCopy, onCopyError, style }, ref) => {
const ctx = useContext(CodeBlockCopyContext);
const [copied, setCopied] = useState(false);
const CodeBlockCopyButton = forwardRef<HTMLButtonElement, CodeBlockCopyButtonProps>(

Check failure on line 124 in src/components/code-block.tsx

View workflow job for this annotation

GitHub Actions / ci-checks

Component definition is missing display name
({ className, onCopy, onCopyError, style }, ref) => {
const ctx = useContext(CodeBlockCopyContext);
const [copied, setCopied] = useState(false);

Check failure on line 127 in src/components/code-block.tsx

View workflow job for this annotation

GitHub Actions / ci-checks

'copied' is assigned a value but never used

return (
<button
className={cx(className)}
ref={ref}
style={style}
onClick={() => {
window.navigator.clipboard
.writeText(ctx)
.then(() => {
setCopied(true);
onCopy?.(ctx);
setTimeout(() => setCopied(false), 1000);
})
.catch((error) => {
onCopyError?.(error);
});
}}
>
<CopyIcon />
</button>
);
});
return (
<button
className={cx(className)}
ref={ref}
style={style}
onClick={() => {
window.navigator.clipboard
.writeText(ctx)
.then(() => {
setCopied(true);
onCopy?.(ctx);
setTimeout(() => setCopied(false), 1000);

Check failure on line 140 in src/components/code-block.tsx

View workflow job for this annotation

GitHub Actions / ci-checks

Returning a void expression from an arrow function shorthand is forbidden. Please add braces to the arrow function
})
.catch((error) => {
onCopyError?.(error);
});
}}
>
<CopyIcon />
</button>
);
},
);

export { CodeBlock, CodeBlockContent, CodeBlockCopyButton, CodeBlockLine };

Expand Down

0 comments on commit 94e15b8

Please sign in to comment.