[components/toc][f]: copy url on heading click

This commit is contained in:
olayway 2022-05-26 20:17:42 +02:00
parent 0d5f8f5540
commit 95f661eae9
1 changed files with 15 additions and 3 deletions

View File

@ -1,16 +1,28 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useRef } from 'react';
export const Heading = ({ level, observer }) => (props) => {
const ref = useRef(null);
const handleClick = async () => {
const url = ref.current.querySelector("a").href;
try {
await navigator.clipboard.writeText(url);
} catch {
return
}
}
useEffect(() => {
if (observer) {
observer.observe(document.getElementById(props.id));
}
});
return React.createElement(`h${level}`, {
...props,
className: "scroll-mt-16"
className: "scroll-mt-16 cursor-copy",
ref,
onClick: handleClick
})
}