From 95f661eae9a02ea4413a9635d533177f5a77dab7 Mon Sep 17 00:00:00 2001 From: olayway Date: Thu, 26 May 2022 20:17:42 +0200 Subject: [PATCH] [components/toc][f]: copy url on heading click --- site/components/Heading.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/site/components/Heading.js b/site/components/Heading.js index 1893ace..cd741db 100644 --- a/site/components/Heading.js +++ b/site/components/Heading.js @@ -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 }) }