Skip to content

Commit

Permalink
feat: useClipboard hook & coverage badge, closes #7, closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
somoore committed Sep 26, 2022
1 parent a2c611f commit c545f76
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .coveragebadgesrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"label": "code coverage",
"source": "./coverage/coverage-summary.json",
"attribute": "total.statements.pct"
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![JS coverage](./badges/code_coverage.svg)

# Use-Re-Hooks

Useful React hooks :thumbsup:
Expand All @@ -6,6 +8,7 @@ Useful React hooks :thumbsup:
- [useWebWorker](https://github.com/shaneiadt/re-hooks/blob/master/src/useWebWorker/index.tsx)
- [useNotification](https://github.com/shaneiadt/re-hooks/blob/master/src/useNotification/index.tsx)
- [useFile](https://github.com/shaneiadt/re-hooks/blob/master/src/useFile/index.tsx)
- [useClipboard](https://github.com/shaneiadt/re-hooks/blob/master/src/useClipboard/index.tsx)

## Usage

Expand Down
1 change: 1 addition & 0 deletions badges/code_coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 24 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"build": "tsdx build && yarn make-badge",
"test": "tsdx test --env=jsdom --watch",
"test:coverage": "tsdx test --env=jsdom --silent --verbose --coverage",
"lint": "tsdx lint",
"prepare": "tsdx build",
"size": "size-limit",
"analyze": "size-limit --why"
"analyze": "size-limit --why",
"make-badge": "$(yarn bin)/coverage-badges"
},
"peerDependencies": {
"react": ">=16"
Expand Down Expand Up @@ -53,20 +54,36 @@
"limit": "10 KB"
}
],
"keywords": ["react","hooks"],
"keywords": [
"react",
"hooks"
],
"devDependencies": {
"@size-limit/preset-small-lib": "^8.1.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
"@types/jest": "^29.0.1",
"@types/react": "^18.0.19",
"@types/react-dom": "^18.0.6",
"coverage-badges": "^1.0.7",
"husky": "^8.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"size-limit": "^8.1.0",
"tsdx": "^0.14.1",
"tslib": "^2.4.0",
"typescript": "^4.8.3",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^14.4.3",
"@types/jest": "^29.0.1"
"typescript": "^4.8.3"
},
"jest": {
"coverageReporters": [
"text",
"lcov",
"json-summary"
],
"coverageThreshold": {
"global": {
"lines": 80
}
}
}
}
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './useGeolocation';
export * from './useWebWorker';
export * from './useNotification';
export * from './useFile';
export * from './useClipboard';
53 changes: 53 additions & 0 deletions src/useClipboard/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
interface ClipboardItem {
readonly types: string[];
readonly presentationStyle: 'unspecified' | 'inline' | 'attachment';
getType(): Promise<Blob>;
}

interface ClipboardItemData {
[mimeType: string]: Blob | string | Promise<Blob | string>;
}

declare var ClipboardItem: {
prototype: ClipboardItem;
new (itemData: ClipboardItemData): ClipboardItem;
};

let anyNavigator: any;

anyNavigator = window.navigator;

export interface ImageItem {
url: string;
}

export interface TextItem {
text: string;
}

export const useClipboard = () => {
const addToClipboard = async (item: ImageItem | TextItem) => {
try {
let blob;

if ('url' in item) {
const data = await fetch(item.url);
blob = await data.blob();
} else {
blob = new Blob([item.text], { type: 'text/plain' });
}

await anyNavigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob,
}),
]);
} catch (err) {
console.error(err);
}
};

return {
addToClipboard,
};
};
46 changes: 30 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@
integrity sha512-XtIfWmeNY3i4t7t4D2t02q50HvqHybPqW2ki1kosnvWCwuCMeo81Jf0gwr85jy/neUdg5XDdeFE/80DXiO+njw==

"@babel/helper-validator-identifier@^7.18.6":
version "7.18.6"
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.18.6.tgz#9c97e30d31b2b8c72a1d08984f2ca9b574d7a076"
integrity sha512-MmetCkz9ej86nJQV+sFCxoGGrUbU3q02kgLciwkrt9QqEB7cP39oKEY0PakknEO0Gu20SskMRi+AYZ3b1TpN9g==
version "7.19.1"
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.19.1.tgz#7eea834cf32901ffdc1a7ee555e2f9c27e249ca2"
integrity sha512-awrNfaMtnHUr653GgGEs++LlAvW6w+DcPrOliSMXWCKo597CwL5Acf/wWdNkf/tfEQE3mjkeD1YOVZOUV/od1w==

"@babel/helper-validator-option@^7.18.6":
version "7.18.6"
Expand Down Expand Up @@ -1286,9 +1286,9 @@
picomatch "^2.2.2"

"@sinclair/typebox@^0.24.1":
version "0.24.41"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.41.tgz#45470b8bae32a28f1e0501066d0bacbd8b772804"
integrity sha512-TJCgQurls4FipFvHeC+gfAzb+GGstL0TDwYJKQVtTeSvJIznWzP7g3bAd5gEBlr8+bIxqnWS9VGVWREDhmE8jA==
version "0.24.43"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.43.tgz#2e2bce0e5e493aaf639beed0cd6c88cfde7dd3d7"
integrity sha512-1orQTvtazZmsPeBroJjysvsOQCYV2yjWlebkSY38pl5vr2tdLjEJ+LoxITlGNZaH2RE19WlAwQMkH/7C14wLfw==

"@sinonjs/commons@^1.7.0":
version "1.8.3"
Expand Down Expand Up @@ -1444,9 +1444,9 @@
pretty-format "^25.2.1"

"@types/jest@^29.0.1":
version "29.0.1"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.0.1.tgz#b5610ae1a1c5f6e0f6beb132941722da7c493c28"
integrity sha512-CAZrjLRZs4xEdIrfrdV74xK1Vo/BKQZwUcjJv3gp6gMeV3BsVxMnXTcgtYOKyphT4DPPo7jxVEVhuwJTQn3oPQ==
version "29.0.3"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.0.3.tgz#b61a5ed100850686b8d3c5e28e3a1926b2001b59"
integrity sha512-F6ukyCTwbfsEX5F2YmVYmM5TcTHy1q9P5rWlRbrk56KyMh3v9xRGUO3aa8+SkvMi0SHXtASJv1283enXimC0Og==
dependencies:
expect "^29.0.0"
pretty-format "^29.0.0"
Expand All @@ -1462,9 +1462,9 @@
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==

"@types/node@*":
version "18.7.17"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.7.17.tgz#52438111ea98f77475470fc62d79b9eb96bb2c92"
integrity sha512-0UyfUnt02zIuqp7yC8RYtDkp/vo8bFaQ13KkSEvUAohPOAlnVNbj5Fi3fgPSuwzakS+EvvnnZ4x9y7i6ASaSPQ==
version "18.7.20"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.7.20.tgz#d9442de7b5cb166476340b4271b15300fe058a39"
integrity sha512-adzY4vLLr5Uivmx8+zfSJ5fbdgKxX8UMtjtl+17n0B1q1Nz8JEmE151vefMdpD+1gyh+77weN4qEhej/O7budQ==

"@types/normalize-package-data@^2.4.0":
version "2.4.1"
Expand Down Expand Up @@ -1493,7 +1493,7 @@
dependencies:
"@types/react" "*"

"@types/react@*", "@types/react@^18.0.19":
"@types/react@*":
version "18.0.19"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.0.19.tgz#269a5f35b9a73c69dfb0c7189017013ab02acbaa"
integrity sha512-BDc3Q+4Q3zsn7k9xZrKfjWyJsSlEDMs38gD1qp2eDazLCdcPqAT+vq1ND+Z8AGel/UiwzNUk8ptpywgNQcJ1MQ==
Expand All @@ -1502,6 +1502,15 @@
"@types/scheduler" "*"
csstype "^3.0.2"

"@types/react@^18.0.19":
version "18.0.21"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.0.21.tgz#b8209e9626bb00a34c76f55482697edd2b43cc67"
integrity sha512-7QUCOxvFgnD5Jk8ZKlUAhVcRj7GuJRjnjjiY/IUBWKgOlnvDvTMLD4RTF7NPyVmbRhNrbomZiOepg7M/2Kj1mA==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "*"
csstype "^3.0.2"

"@types/[email protected]":
version "1.17.1"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.17.1.tgz#3afd6ad8967c77e4376c598a82ddd58f46ec45d6"
Expand Down Expand Up @@ -1537,9 +1546,9 @@
"@types/yargs-parser" "*"

"@types/yargs@^17.0.8":
version "17.0.12"
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-17.0.12.tgz#0745ff3e4872b4ace98616d4b7e37ccbd75f9526"
integrity sha512-Nz4MPhecOFArtm81gFQvQqdV7XYCrWKx5uUt6GNHredFHn1i2mtWqXTON7EPXMtNi1qjtjEM/VCHDhcHsAMLXQ==
version "17.0.13"
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-17.0.13.tgz#34cced675ca1b1d51fcf4d34c3c6f0fa142a5c76"
integrity sha512-9sWaruZk2JGxIQU+IhI1fhPYRcQ0UuTNuKuCW9bR5fp7qi2Llf7WDzNa17Cy7TKnh3cdxDOiyTu6gaLS0eDatg==
dependencies:
"@types/yargs-parser" "*"

Expand Down Expand Up @@ -2371,6 +2380,11 @@ cosmiconfig@^6.0.0:
path-type "^4.0.0"
yaml "^1.7.2"

coverage-badges@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/coverage-badges/-/coverage-badges-1.0.7.tgz#ec5109f2c7c61fe9f7230685e4b40c868c2b12f2"
integrity sha512-slYFleOr+QreEVAAocjfN8oUmlCKQi/Zol08/IwVQooJU7C8LLqlz+KNf5hgz13TBHNB7fZdJAPKheTpVq8OzQ==

cross-spawn@^6.0.0, cross-spawn@^6.0.5:
version "6.0.5"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
Expand Down

0 comments on commit c545f76

Please sign in to comment.