Web Share API Usage Guide:Claude Code 实战指南
了解web share api usage guide:Claude Code 实战. 包含实用技巧和代码示例。
Web Share APIでネイティブの共有体験を提供
Web Share APIを使えば、OSネイティブの共有对话框を呼び出せます。モバイルではLINEやX(Twitter)など、安装済みの应用に直接共有でき、用户体験が大幅に向上します。
基本的共有按钮の实现
> Web Share API使用...的共有按钮创建。
> 非支持浏览器にはSNSリンクのフォールバックを显示して。
// components/ShareButton.tsx
import { useState } from 'react';
interface ShareButtonProps {
title: string;
text: string;
url: string;
}
export function ShareButton({ title, text, url }: ShareButtonProps) {
const [showFallback, setShowFallback] = useState(false);
const canShare = typeof navigator !== 'undefined' && !!navigator.share;
const handleShare = async () => {
if (canShare) {
try {
await navigator.share({ title, text, url });
} catch (err) {
if ((err as Error).name !== 'AbortError') {
setShowFallback(true);
}
}
} else {
setShowFallback(true);
}
};
return (
<div className="relative">
<button
onClick={handleShare}
className="flex items-center gap-2 rounded-lg bg-blue-600 px-4 py-2 text-white hover:bg-blue-700"
>
<ShareIcon />
共有する
</button>
{showFallback && (
<ShareFallback title={title} url={url} onClose={() => setShowFallback(false)} />
)}
</div>
);
}
function ShareIcon() {
return (
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
<path d="M4 12v8a2 2 0 002 2h12a2 2 0 002-2v-8" />
<polyline points="16 6 12 2 8 6" />
<line x1="12" y1="2" x2="12" y2="15" />
</svg>
);
}
SNSフォールバック组件
// components/ShareFallback.tsx
interface ShareFallbackProps {
title: string;
url: string;
onClose: () => void;
}
function ShareFallback({ title, url, onClose }: ShareFallbackProps) {
const encodedUrl = encodeURIComponent(url);
const encodedTitle = encodeURIComponent(title);
const platforms = [
{
name: 'X (Twitter)',
href: `https://twitter.com/intent/tweet?text=${encodedTitle}&url=${encodedUrl}`,
color: 'bg-black',
},
{
name: 'Facebook',
href: `https://www.facebook.com/sharer/sharer.php?u=${encodedUrl}`,
color: 'bg-blue-600',
},
{
name: 'LINE',
href: `https://social-plugins.line.me/lineit/share?url=${encodedUrl}`,
color: 'bg-green-500',
},
{
name: 'はてなブックマーク',
href: `https://b.hatena.ne.jp/entry/${url}`,
color: 'bg-blue-400',
},
];
return (
<div className="absolute right-0 top-full mt-2 w-56 rounded-xl border bg-white p-3 shadow-lg dark:bg-gray-800">
<div className="mb-2 flex items-center justify-between">
<span className="text-sm font-medium">共有先を選択</span>
<button onClick={onClose} className="text-gray-400 hover:text-gray-600">×</button>
</div>
<div className="space-y-1">
{platforms.map(p => (
<a
key={p.name}
href={p.href}
target="_blank"
rel="noopener noreferrer"
className={`block rounded-lg ${p.color} px-3 py-2 text-sm text-white transition-opacity hover:opacity-80`}
>
{p.name}
</a>
))}
</div>
<button
onClick={() => { navigator.clipboard.writeText(url); onClose(); }}
className="mt-2 w-full rounded-lg bg-gray-100 px-3 py-2 text-sm hover:bg-gray-200 dark:bg-gray-700"
>
URLをコピー
</button>
</div>
);
}
文件の共有
// 图片や文件の共有
async function shareFile(file: File, title: string): Promise<void> {
if (!navigator.canShare?.({ files: [file] })) {
console.log('ファイル共有は非対応です');
return;
}
await navigator.share({
title,
files: [file],
});
}
// Canvas图片の共有
async function shareCanvasImage(canvas: HTMLCanvasElement): Promise<void> {
const blob = await new Promise<Blob | null>(resolve =>
canvas.toBlob(resolve, 'image/png')
);
if (!blob) return;
const file = new File([blob], 'image.png', { type: 'image/png' });
await shareFile(file, '生成した画像');
}
Web Share Target API
PWA作为共有ターゲットに注册することで、他の应用からの共有を受け取ることも可以。
{
"share_target": {
"action": "/share-receiver",
"method": "POST",
"enctype": "multipart/form-data",
"params": {
"title": "title",
"text": "text",
"url": "url",
"files": [
{
"name": "media",
"accept": ["image/*", "video/*"]
}
]
}
}
}
总结
Web Share APIは、モバイル用户に尤其有效的共有機能を提供します。借助 Claude Code,クリップボードAPIと联动したフォールバック付きの实现を高效地构建可以。PWAと組み合わせれば、共有ターゲット作为の機能も添加是可能的。詳しい仕様はMDN Web Docs - Web Share API。
免费 PDF:5 分钟看懂 Claude Code 速查表
只需留下邮箱,我们就会立即把这份 A4 一页速查表 PDF 发送给你。
我们会严格保护你的个人信息,绝不发送垃圾邮件。
把 Claude Code 变成真正能带来结果的工作流
先领取中文说明的免费 PDF,再进入英文商品页选择合适的教材。如果你需要团队落地、流程设计或内容变现支持,也可以直接咨询。
本文作者
Masa
深度使用 Claude Code 的工程师。运营 claudecode-lab.com——一个涵盖 10 种语言、超过 2,000 页内容的科技媒体。
相关文章
Claude Code 的 7 个 CLAUDE.md 模板 | 可以直接复制到真实项目
面向个人应用、内容站、API、团队仓库和遗留代码库的 7 个实用 CLAUDE.md 模板,附常见失败案例。
Claude Code Approval 与 Sandbox 指南 | 日常安全使用的实战设置
用可直接参考的 settings、hooks、失败案例与流程示例,讲清楚 Claude Code 的 allow / ask / deny / sandbox 应该怎么分。
Claude Code 完全入门指南 2026 | 从零到实战应用的 7 个步骤
专为 Claude Code 新手打造的完整入门指南。从安装到融入真实开发工作流——涵盖 Masa 刚开始使用时踩过的所有坑。