Skip to content

Commit

Permalink
Add: Click to copy button
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayao0819 committed Oct 27, 2023
1 parent 12dd0c1 commit 92c1c04
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
44 changes: 44 additions & 0 deletions src/components/CopyBtn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ReactNode, useState } from "react";
import { Tooltip } from "react-daisyui";

export default function CopyTootrip({
children,
copyText,
className,
}: {
children: ReactNode;
copyText: string;
className?: string;
}) {
const defaultMsg = "クリックしてコピー";
const [tooltipMsg, setTooltipMsg] = useState(defaultMsg);

const handleClick = (/*e: MouseEvent<HTMLDivElement, globalThis.MouseEvent>*/) => {
navigator.clipboard.writeText(copyText);
setTooltipMsg("コピーしました");
};

const handleMouseOut = () => {
setTooltipMsg(defaultMsg);
};

const handleMouseOver = (/*e: React.MouseEvent<HTMLDivElement, MouseEvent>*/) => {
setTimeout(() => {
setTooltipMsg(defaultMsg);
}, 2000);
};

return (
<>
<Tooltip
message={tooltipMsg}
onClick={handleClick}
onMouseOut={handleMouseOut}
onMouseOver={handleMouseOver}
className={className}
>
{children}
</Tooltip>
</>
);
}
7 changes: 4 additions & 3 deletions src/options/pages/Top.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import classNames from "classnames";
import { useEffect, useState } from "react";

import { StorageTool } from "../../class/StorageTool";
import CopyTootrip from "../../components/CopyBtn";
import Heading from "../../components/Heading";
import { Switches } from "../../components/Switches";
import IsTrue from "../../utils/isTrue";
Expand All @@ -27,9 +28,9 @@ export default function Top() {

<ConfigSection name="群馬大学学務課・メディアセンターの方へ">
<p>このソフトウェアについて問題がある場合は以下のメールアドレスまでご連絡ください。</p>
<a href="mailto:[email protected]" className="btn btn-neutral my-5">
[email protected]
</a>
<CopyTootrip copyText="[email protected]" className="my-5">
<button className="btn btn-neutral">[email protected]</button>
</CopyTootrip>
</ConfigSection>
</>
);
Expand Down
6 changes: 3 additions & 3 deletions src/utils/addClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export function addClass(elements: Elements[], classNames: string[]) {
elements.forEach((element) => {
if (typeof element === "string") {
const elements = document.querySelectorAll(element);
elements.forEach((element) => {
//console.log("adding classes to", element);
element.classList.add(...classNames);
elements.forEach((e) => {
console.log(`adding classes (${classNames}) to`, e);
e.classList.add(...classNames);
});
} else {
let list: HTMLElement[];
Expand Down

0 comments on commit 92c1c04

Please sign in to comment.