-
Notifications
You must be signed in to change notification settings - Fork 53
Add ShareButton component #5406
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,39 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { Button, Flex, Icon } from '@webkom/lego-bricks'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { Share2, ShareIcon } from 'lucide-react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { useAppDispatch } from 'lego-webapp/redux/hooks'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { addToast } from '~/redux/slices/toasts'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type Props = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
title: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
url: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const ShareButton: React.FC<Props> = ({ title, url }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const dispatch = useAppDispatch(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const handleShare = async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (navigator?.canShare?.({ title, url })) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
navigator.share({ title, url }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
navigator.clipboard.writeText(url); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dispatch( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
addToast({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message: 'Link er kopiert til utklippstavlen', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type: 'success', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+13
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Fix unnecessary block statement and add error handling. The share functionality works as expected, but there's an unnecessary block statement and error handling should be added for clipboard and share operations. const handleShare = async () => {
if (navigator?.canShare?.({ title, url })) {
- navigator.share({ title, url });
+ try {
+ await navigator.share({ title, url });
+ } catch (error) {
+ console.error('Error sharing:', error);
+ await copyToClipboard();
+ }
} else {
- {
- navigator.clipboard.writeText(url);
- }
- dispatch(
- addToast({
- message: 'Link er kopiert til utklippstavlen',
- type: 'success',
- }),
- );
+ await copyToClipboard();
+ }
+};
+
+const copyToClipboard = async () => {
+ try {
+ await navigator.clipboard.writeText(url);
+ dispatch(
+ addToast({
+ message: 'Link er kopiert til utklippstavlen',
+ type: 'success',
+ }),
+ );
+ } catch (error) {
+ console.error('Error copying to clipboard:', error);
+ dispatch(
+ addToast({
+ message: 'Kunne ikke kopiere link',
+ type: 'error',
+ }),
+ );
}
}; 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Biome (1.9.4)[error] 17-19: This block statement doesn't serve any purpose and can be safely removed. Standalone block statements without any block-level declarations are redundant in JavaScript and can be removed to simplify the code. (lint/complexity/noUselessLoneBlockStatements) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Button onPress={handleShare}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Flex justifyContent="center" gap="var(--spacing-sm)"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Icon iconNode={<ShareIcon />} size={20} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{title} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</Flex> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</Button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export default ShareButton; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Consider browser compatibility for Web Share API.
While you're correctly checking for
navigator.canShare
support, some browsers might partially implement the API but have limitations. Consider adding a fallback for unsupported share data types.🌐 Web query:
💡 Result:
The Web Share API is supported across most modern browsers, but with varying levels of compatibility and limitations. Here's a detailed breakdown:
Browser Support
Chrome:
Edge:
Safari:
Opera:
Mobile Browsers:
Key Limitations
Secure Context Requirement:
Desktop Browser Gaps:
User Interaction Dependency:
Variable Share Targets:
Third-Party Iframe Restrictions:
<iframe allow="web-share">
. Otherwise,NotAllowedError
occurs [5][6].File Sharing Constraints:
navigator.canShare({ files })
first. Large files or unsupported types may fail silently [7][12].For unsupported browsers (e.g., Firefox desktop), developers must implement fallback sharing UIs. Compatibility checks with
navigator.canShare()
are recommended before invoking the API [7][10].Citations:
Web Share API: Enhance Fallbacks for Partial & Unsupported Environments
In
lego-webapp/components/ShareButton/index.tsx
(lines 14–15), you’re correctly checking for the existence ofnavigator?.canShare
before callingnavigator.share
. However, as our latest research indicates, browser support for the Web Share API is nuanced:Variability Across Browsers:
Limitations & Fallback Needs:
navigator.canShare
is available, certain share data types (especially files) may not be fully supported.Please consider implementing a graceful fallback for cases where the API’s capabilities are limited or absent. This will ensure a more consistent sharing experience across all browsers.