Skip to content

Commit 0904d09

Browse files
committed
done with the copy button. Need to fix the positioning tho, when scrolling it moves instead of being fixed to the top right
1 parent 91d19e9 commit 0904d09

5 files changed

Lines changed: 83 additions & 28 deletions

File tree

package-lock.json

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"compression": "^1.7.4",
5555
"connect": "^3.7.0",
5656
"debug": "^4.3.4",
57-
"sirv": "^2.0.2"
57+
"sirv": "^2.0.2",
58+
"solid-icons": "^1.0.4"
5859
},
5960
"engines": {
6061
"node": ">=16"

src/components/CopyButton.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { BiSolidCopy } from "solid-icons/bi";
2+
import { createSignal, Show } from "solid-js";
3+
4+
interface Props {
5+
parentRef: HTMLPreElement;
6+
}
7+
8+
function CopyButton(props: Props) {
9+
const [copied, setCopied] = createSignal(false);
10+
11+
function copyToClipboard() {
12+
navigator.clipboard.writeText(props.parentRef.innerText);
13+
setCopied(true);
14+
setInterval(() => setCopied(false), 2000);
15+
}
16+
17+
return (
18+
<button onclick={copyToClipboard} class="absolute top-4 right-4">
19+
<Show when={copied()} fallback={<BiSolidCopy size={24} />}>
20+
Copied!
21+
</Show>
22+
</button>
23+
);
24+
}
25+
26+
export default CopyButton;

src/components/Snippet.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { PropsWithChildren, useContext } from "solid-js";
2-
import { CodeFormat, ConfigContext } from "./context/ConfigContext";
1+
import { JSXElement, useContext } from "solid-js";
2+
import { ConfigContext } from "./context/ConfigContext";
33

4-
const Container = (props: PropsWithChildren) => {
4+
const Container = (props: {children:JSXElement}) => {
55
const [config, setConfig] = useContext(ConfigContext);
66

77
return (
@@ -37,7 +37,7 @@ const Container = (props: PropsWithChildren) => {
3737
);
3838
};
3939

40-
const Code = (props: PropsWithChildren<{ language: CodeFormat }>) => {
40+
const Code = (props: { language: string, children: JSXElement }) => {
4141
const [config] = useContext(ConfigContext);
4242
return (
4343
<div

src/md.tsx

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import "tippy.js/dist/tippy.css";
66
import { Title } from "./components/Main";
77
import { Title as MetaTitle } from "@solidjs/meta";
88
import { usePageState } from "~/components/context/PageStateContext";
9+
import { BiSolidCopy } from "solid-icons/bi";
10+
import CopyButton from "./components/CopyButton";
911

1012
function Anchor(props: ParentProps<{ id: string }>) {
1113
return (
@@ -127,31 +129,37 @@ export default {
127129
</code>
128130
);
129131
},
130-
pre: (props) => (
131-
<>
132-
{/* <Show when={props.filename?.length > 5}>
132+
pre: (props) => {
133+
let ref: HTMLPreElement;
134+
135+
return (
136+
<>
137+
{/* <Show when={props.filename?.length > 5}>
133138
<span {...props} class="h-4 p-1">
134139
{props.filename}
135140
</span>
136141
</Show> */}
137-
<pre
138-
{...mergeProps(props, {
139-
get class() {
140-
return (
141-
props.className +
142-
" " +
143-
(props.bad ? "border-red-400 border-1" : "")
144-
);
145-
},
146-
get className() {
147-
return undefined;
148-
},
149-
})}
150-
>
151-
{props.children}
152-
</pre>
153-
</>
154-
),
142+
<pre
143+
{...mergeProps(props, {
144+
get class() {
145+
return (
146+
props.className +
147+
" " +
148+
(props.bad ? "border-red-400 border-1" : "")
149+
);
150+
},
151+
get className() {
152+
return undefined;
153+
},
154+
})}
155+
ref={ref}
156+
>
157+
<CopyButton parentRef={ref}/>
158+
{props.children}
159+
</pre>
160+
</>
161+
);
162+
},
155163
table: (props) => (
156164
<table class="w-full max-w-full <sm:portrait:text-xs my-6 rounded-1xl dark:bg-[rgba(17,24,39,1)] shadow-lg text-left overflow-hidden">
157165
{props.children}
@@ -163,7 +171,9 @@ export default {
163171
),
164172
td: (props) => <td class="p-4 <sm:p-2">{props.children}</td>,
165173
tr: (props) => (
166-
<tr class="dark:even-of-type:bg-[#23406e] light:even-of-type:bg-[#90C2E7]">{props.children}</tr>
174+
<tr class="dark:even-of-type:bg-[#23406e] light:even-of-type:bg-[#90C2E7]">
175+
{props.children}
176+
</tr>
167177
),
168178
"data-lsp": (props) => {
169179
const id = createUniqueId();

0 commit comments

Comments
 (0)