Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 3 additions & 15 deletions components/ShareBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ReactElement, useState } from 'react';
import React, { ReactElement } from 'react';
import styled from 'styled-components';
import { laptop } from '../styles/media';
import { postPageMaxWidth } from '../styles/utilities';
Expand All @@ -11,6 +11,7 @@ import FacebookIcon from '../icons/facebook.svg';
import ReactGA from 'react-ga';
import { Post } from '../graphql/posts';
import { typoNuggets } from '../styles/typography';
import { useCopyPostLink } from '../lib/useCopyPostLink';

const barWidth = size7;

Expand Down Expand Up @@ -59,20 +60,7 @@ const ColorfulShareButton = styled(ShareButton)`

export default function ShareBar({ post }: { post: Post }): ReactElement {
const href = encodeURIComponent(window.location.href);
const [copying, setCopying] = useState<boolean>(false);

const copyLink = async () => {
await navigator.clipboard.writeText(window.location.href);
setCopying(true);
ReactGA.event({
category: 'Post',
action: 'Share',
label: 'Copy',
});
setTimeout(() => {
setCopying(false);
}, 1000);
};
const [copying, copyLink] = useCopyPostLink();

return (
<Container>
Expand Down
52 changes: 52 additions & 0 deletions components/ShareMobile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { ReactElement } from 'react';
import styled from 'styled-components';
import { laptop } from '../styles/media';
import CopyIcon from '../icons/copy.svg';
import ShareIcon from '../icons/share.svg';
import { FloatButton } from './Buttons';
import { size1, size10 } from '../styles/sizes';
import { useCopyPostLink } from '../lib/useCopyPostLink';

const Container = styled.div`
display: flex;
flex-direction: column;
align-items: flex-start;
margin-bottom: ${size10};

${laptop} {
display: none;
}

${FloatButton} {
margin: ${size1} 0;

&:first-child {
margin-top: 0;
}

&:last-child {
margin-bottom: 0;
}
}
`;

export interface Props {
share: () => Promise<void>;
}

export function ShareMobile({ share }: Props): ReactElement {
const [copying, copyLink] = useCopyPostLink();

return (
<Container>
<FloatButton onClick={copyLink} done={copying}>
<CopyIcon />
{copying ? 'Copied!' : 'Copy link'}
</FloatButton>
<FloatButton onClick={share}>
<ShareIcon />
Share with your friends
</FloatButton>
</Container>
);
}
21 changes: 21 additions & 0 deletions lib/useCopyPostLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useState } from 'react';
import ReactGA from 'react-ga';

export function useCopyPostLink(): [boolean, () => Promise<void>] {
const [copying, setCopying] = useState(false);

const copy = async () => {
await navigator.clipboard.writeText(window.location.href);
setCopying(true);
ReactGA.event({
category: 'Post',
action: 'Share',
label: 'Copy',
});
setTimeout(() => {
setCopying(false);
}, 1000);
};

return [copying, copy];
}
2 changes: 2 additions & 0 deletions pages/posts/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { laptop, mobileL, mobileM, tablet } from '../../styles/media';
import { colorPepper90 } from '../../styles/colors';
import { focusOutline, postPageMaxWidth } from '../../styles/utilities';
import { NextSeoProps } from 'next-seo/lib/types';
import { ShareMobile } from '../../components/ShareMobile';

const NewCommentModal = dynamic(() =>
import('../../components/NewCommentModal'),
Expand Down Expand Up @@ -491,6 +492,7 @@ export default function PostPage({ id }: Props): ReactElement {
💡 Hint: The comment with most upvotes will be featured on the main
feed of daily.dev browser extension.
</Hint>
<ShareMobile share={sharePost} />
<NewCommentContainer>
<NewCommentButton onClick={openNewComment}>
{user && (
Expand Down