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 clipboard hooks - first approach #85

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions packages/web-api-hooks/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { EventArgs, EventMap, JSONArray, JSONObject, JSONValue } from './types';

export { default as useClipboard } from './useClipboard';
export { default as useDeviceMotion } from './useDeviceMotion';
export { default as useDeviceOrientation } from './useDeviceOrientation';
export { default as useDocumentReadiness } from './useDocumentReadiness';
Expand Down
77 changes: 77 additions & 0 deletions packages/web-api-hooks/src/useClipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* eslint consistent-return: 0 */
/* eslint no-param-reassign: 0 */
import { checkForPermission } from './utils';

export default function useClipboard() {
const cut = async (
element: HTMLInputElement,
permissionErrorCallback: Function = () => {},
): Promise<void | Error> => {
const permissionGranted = await checkForPermission(
'clipboard-write' as PermissionName,
permissionErrorCallback,
);
try {
if (permissionGranted) {
element.select();
document.execCommand('cut');
element.value = '';
element.blur();
}
} catch (error) {
return error;
}
};

const paste = async (
element: HTMLInputElement,
permissionErrorCallback: Function = () => {},
): Promise<void | Error> => {
const permissionGranted = await checkForPermission(
'clipboard-read' as PermissionName,
permissionErrorCallback,
);
try {
if (permissionGranted) {
element.focus();
document.execCommand('paste');
}
} catch (error) {
return error;
}
};

const copy = async (
text: string,
permissionErrorCallback: Function = () => {},
): Promise<void | Error> => {
const permissionGranted = await checkForPermission(
'clipboard-write' as PermissionName,
permissionErrorCallback,
);
try {
if (permissionGranted) {
if (navigator.clipboard) {
navigator.clipboard.writeText(text);
} else {
const tempInput = document.createElement('input');
document.body.appendChild(tempInput);
tempInput.setAttribute('id', 'temp-input');
(document.getElementById(
'temp-input',
) as HTMLInputElement).value = text;
tempInput.select();
document.execCommand('copy');
document.body.removeChild(tempInput);
}
}
} catch (error) {
return error;
}
};
return {
copy,
cut,
paste,
};
}
26 changes: 26 additions & 0 deletions packages/web-api-hooks/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,29 @@ export function useEventCallback<T extends Function>(callback: T) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return useCallback((...args) => ref.current!(...args) as T, [ref]);
}

export async function checkForPermission(
type: PermissionName,
errorCallback: Function = () => {},
): Promise<boolean> {
let status = false;
if (!navigator.permissions) {
status = true;
} else {
try {
/* Permission API is still a working draft, and Typescript types for it
are still not correct, hence 'any' type as an argument. Similar situation
in lines where checkForPermission function is invoked. */
const permissions = await navigator.permissions.query({ name: type });
if (permissions.state === 'granted') {
status = true;
} else {
errorCallback();
status = false;
}
} catch (error) {
return error;
}
}
return status;
}