Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add params for copying func #39

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-garlics-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-use-clipboard": patch
---

Add params for copying func
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ Here's how to use `react-use-clipboard`:
import useClipboard from "react-use-clipboard";

function App() {
const [isCopied, setCopied] = useClipboard("Text to copy");
const [isCopied, setCopied] = useClipboard();

return (
<button onClick={setCopied}>
<button onClick={() => setCopied("Text to copy")}>
Was it copied? {isCopied ? "Yes! 👍" : "Nope! 👎"}
</button>
);
Expand All @@ -42,13 +42,13 @@ You can reset the `isCopied` value after a certain amount of time with the `succ
import useClipboard from "react-use-clipboard";

function App() {
const [isCopied, setCopied] = useClipboard("Text to copy", {
const [isCopied, setCopied] = useClipboard({
// `isCopied` will go back to `false` after 1000ms.
successDuration: 1000,
});

return (
<button onClick={setCopied}>
<button onClick={() => setCopied("Text to copy")}>
Was it copied? {isCopied ? "Yes! 👍" : "Nope! 👎"}
</button>
);
Expand Down
4 changes: 2 additions & 2 deletions example/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import useClipboard from "react-use-clipboard";

export default function App() {
const [isCopied, setCopied] = useClipboard("Text to copy", {
const [isCopied, setCopied] = useClipboard({
// `isCopied` will go back to `false` after 1000ms.
successDuration: 1000,
});

return (
<button onClick={setCopied}>
<button onClick={() => setCopied("Text to copy")}>
Was it copied? {isCopied ? "Yes! 👍" : "Nope! 👎"}
</button>
);
Expand Down
12 changes: 6 additions & 6 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ afterEach(cleanup);

test("display sucess message if the copy worked", () => {
const Component = () => {
const [isCopied, setCopied] = useClipboard("Text to copy");
const [isCopied, setCopied] = useClipboard();

return (
<button onClick={setCopied} data-testid="btn-example">
<button onClick={() => setCopied("Text to copy")} data-testid="btn-example">
{isCopied ? "Yes" : "Nope"}
</button>
);
Expand All @@ -31,12 +31,12 @@ describe("successDuration", () => {
const successDuration = 1000;

const Component = () => {
const [isCopied, setCopied] = useClipboard("Text to copy", {
const [isCopied, setCopied] = useClipboard({
successDuration,
});

return (
<button onClick={setCopied} data-testid="btn-example">
<button onClick={()=> setCopied("Text to copy")} data-testid="btn-example">
{isCopied ? "Yes" : "Nope"}
</button>
);
Expand Down Expand Up @@ -68,10 +68,10 @@ describe("successDuration", () => {
jest.useFakeTimers();

const Component = () => {
const [isCopied, setCopied] = useClipboard("Text to copy", {});
const [isCopied, setCopied] = useClipboard({});

return (
<button onClick={setCopied} data-testid="btn-example">
<button onClick={() => setCopied("Anything")} data-testid="btn-example">
{isCopied ? "Yes" : "Nope"}
</button>
);
Expand Down
5 changes: 2 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ interface IOptions {
}

export default function useCopyClipboard(
text: string,
options?: IOptions
): [boolean, () => void] {
): [boolean, (text: string) => void] {
const [isCopied, setIsCopied] = useState(false);
const successDuration = options && options.successDuration;

Expand All @@ -30,7 +29,7 @@ export default function useCopyClipboard(

return [
isCopied,
() => {
(text: string) => {
const didCopy = copy(text);
setIsCopied(didCopy);
},
Expand Down