reactuals
Version:
A useful package providing a collection of 50+ React hooks and utilities to simplify React development.
23 lines (22 loc) • 748 B
JavaScript
import { useCallback, useState } from "react";
export function useWebShare() {
const [error, setError] = useState(null);
const isSupported = typeof navigator !== "undefined" && "share" in navigator;
const share = useCallback(async (data) => {
if (!isSupported) {
setError(new Error("Web Share API not supported"));
return false;
}
try {
await navigator.share(data);
setError(null);
return true;
}
catch (err) {
const error = err instanceof Error ? err : new Error("Failed to share content");
setError(error);
return false;
}
}, [isSupported]);
return { share, isSupported, error };
}