webapp/src/components/Article/SharePopup.tsx

67 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-11-14 17:41:05 +00:00
import { Icon } from '../_shared/Icon'
2022-10-25 15:36:32 +00:00
import { t } from '../../utils/intl'
import { createSocialShare, TWITTER, VK, FACEBOOK, TELEGRAM } from '@solid-primitives/share'
2022-11-20 21:25:59 +00:00
import styles from '../_shared/Popup/Popup.module.scss'
2022-11-14 10:02:08 +00:00
import type { PopupProps } from '../_shared/Popup'
import { Popup } from '../_shared/Popup'
2022-10-25 15:36:32 +00:00
type SharePopupProps = {
title: string
shareUrl?: string
imageUrl: string
description: string
} & Omit<PopupProps, 'children'>
2022-10-25 15:36:32 +00:00
2023-01-31 13:58:28 +00:00
export const getShareUrl = (params: { pathname?: string } = {}) => {
if (typeof location === 'undefined') return ''
const pathname = params.pathname ?? location.pathname
return location.origin + pathname
}
2022-10-25 15:36:32 +00:00
export const SharePopup = (props: SharePopupProps) => {
const [share] = createSocialShare(() => ({
title: props.title,
url: props.shareUrl,
description: props.description
}))
const copyLink = async () => {
2023-01-31 13:58:28 +00:00
await navigator.clipboard.writeText(props.shareUrl)
}
2022-10-25 15:36:32 +00:00
return (
2022-12-17 03:27:00 +00:00
<Popup {...props} variant="bordered">
2022-10-25 15:36:32 +00:00
<ul class="nodash">
<li>
2023-02-08 20:43:59 +00:00
<button role="button" class={styles.shareControl} onClick={() => share(VK)}>
2022-10-25 15:36:32 +00:00
<Icon name="vk-white" class={styles.icon} />
VK
</button>
2022-10-25 15:36:32 +00:00
</li>
<li>
2023-02-08 20:43:59 +00:00
<button role="button" class={styles.shareControl} onClick={() => share(FACEBOOK)}>
2022-10-25 15:36:32 +00:00
<Icon name="facebook-white" class={styles.icon} />
Facebook
</button>
2022-10-25 15:36:32 +00:00
</li>
<li>
2023-02-08 20:43:59 +00:00
<button role="button" class={styles.shareControl} onClick={() => share(TWITTER)}>
2022-10-25 15:36:32 +00:00
<Icon name="twitter-white" class={styles.icon} />
Twitter
</button>
2022-10-25 15:36:32 +00:00
</li>
<li>
2023-02-08 20:43:59 +00:00
<button role="button" class={styles.shareControl} onClick={() => share(TELEGRAM)}>
2022-10-25 15:36:32 +00:00
<Icon name="telegram-white" class={styles.icon} />
Telegram
</button>
2022-10-25 15:36:32 +00:00
</li>
<li>
2023-02-08 20:43:59 +00:00
<button role="button" class={styles.shareControl} onClick={copyLink}>
2022-10-25 15:36:32 +00:00
<Icon name="link-white" class={styles.icon} />
{t('Copy link')}
</button>
2022-10-25 15:36:32 +00:00
</li>
</ul>
</Popup>
)
}