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

feat: implement ShareDropdownMenu #39

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { TranslationFn } from '@/features/i18n'
import {
DropdownMenuItem,
TooltipContent,
TooltipPortal,
TooltipProvider,
TooltipRoot,
TooltipTrigger,
} from '@packages/ui'
import { CopyIcon } from 'lucide-react'
import { type FC, useCallback, useEffect, useState } from 'react'
import styles from './ShareDropdownMenu.module.css'

type Props = {
t: TranslationFn
onOpenChange: (open: boolean) => void
}

export const CopyLinkItem: FC<Props> = ({ t, onOpenChange }) => {
const [show, setShow] = useState(false)

useEffect(() => {
if (!show) return

const timer = setTimeout(() => {
setShow(false)
onOpenChange(false)
}, 1000)
MH4GF marked this conversation as resolved.
Show resolved Hide resolved
return () => clearTimeout(timer)
}, [show, onOpenChange])

const handleCopyLink = useCallback(async (event: Event) => {
event.preventDefault()
navigator.clipboard.writeText(window.location.href).then(() => {
setShow(true)
})
}, [])

return (
<TooltipProvider>
<TooltipRoot open={show}>
<TooltipTrigger asChild>
<DropdownMenuItem
leftIcon={<CopyIcon className={styles.icon} />}
onSelect={handleCopyLink}
>
<span>{t('posts.share.copyLink')}</span>
</DropdownMenuItem>
</TooltipTrigger>
<TooltipPortal>
<TooltipContent sideOffset={5}>Link Copied</TooltipContent>
</TooltipPortal>
</TooltipRoot>
</TooltipProvider>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
height: 10px;
color: var(--overlay-70);
}

.link {
width: 100%;
height: 100%;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { Meta, StoryObj } from '@storybook/react'

import { getTranslation } from '@/features/i18n'
import { ShareDropdownMenu } from '.'

const { t } = getTranslation('en')

const meta = {
component: ShareDropdownMenu,
args: {
t,
lang: 'en',
children: <button type="button">Share</button>,
},
} satisfies Meta<typeof ShareDropdownMenu>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TranslationFn } from '@/features/i18n'
import { type Lang, getTranslation } from '@/features/i18n'
import {
DropdownMenuContent,
DropdownMenuItem,
Expand All @@ -9,42 +9,52 @@ import {
LinkedInIcon,
XIcon,
} from '@packages/ui'
import { CopyIcon } from 'lucide-react'
import type { FC, PropsWithChildren } from 'react'
import { type FC, type PropsWithChildren, useState } from 'react'
import { CopyLinkItem } from './CopyLinkItem'
import styles from './ShareDropdownMenu.module.css'

type Props = PropsWithChildren<{
t: TranslationFn
lang: Lang
}>

export const ShareDropdownMenu: FC<Props> = ({ children, t }) => {
const handleSelect = (url: string) => () => {
window.open(url, '_blank', 'noreferrer')
}

export const ShareDropdownMenu: FC<Props> = ({ children, lang }) => {
const [open, setOpen] = useState(false)
const { t } = getTranslation(lang)
const url = window.location.href
const title = document.title
MH4GF marked this conversation as resolved.
Show resolved Hide resolved

return (
<DropdownMenuRoot>
<DropdownMenuRoot open={open} onOpenChange={setOpen}>
<DropdownMenuTrigger asChild>{children}</DropdownMenuTrigger>

<DropdownMenuPortal>
<DropdownMenuContent sideOffset={5} align="start">
<DropdownMenuItem
leftIcon={<CopyIcon className={styles.icon} />}
onSelect={() => alert('Item 1 clicked')}
>
<span>{t('posts.share.copyLink')}</span>
</DropdownMenuItem>
<CopyLinkItem t={t} onOpenChange={setOpen} />
<DropdownMenuItem
leftIcon={<XIcon className={styles.icon} />}
onSelect={() => alert('Item 2 clicked')}
onSelect={handleSelect(
`http://twitter.com/share?url=${url}&text=${title}`,
)}
>
<span>{t('posts.share.x')}</span>
</DropdownMenuItem>
<DropdownMenuItem
leftIcon={<FacebookIcon className={styles.icon} />}
onSelect={() => alert('Item 3 clicked')}
onSelect={handleSelect(
`http://www.facebook.com/share.php?u=${url}`,
)}
>
<span>{t('posts.share.facebook')}</span>
</DropdownMenuItem>
<DropdownMenuItem
leftIcon={<LinkedInIcon className={styles.icon} />}
onSelect={() => alert('Item 3 clicked')}
onSelect={handleSelect(
`https://www.linkedin.com/sharing/share-offsite/?url=${url}`,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that if the page returns 200 statuses, as in this URL, it can be shared without any problem.

https://www.linkedin.com/sharing/share-offsite/?url=https://route06.co.jp

)}
>
<span>{t('posts.share.linkedin')}</span>
</DropdownMenuItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const DropdownMenuItem = forwardRef<
{leftIcon && (
<span className={clsx(styles.icon, styles.leftIcon)}>{leftIcon}</span>
)}
<div>{children}</div>
{children}
</Item>
)
},
Expand Down