Skip to content

Instantly share code, notes, and snippets.

@christianjuth
Last active February 5, 2022 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianjuth/8896966b6ecd0c81a693dcc61d475510 to your computer and use it in GitHub Desktop.
Save christianjuth/8896966b6ecd0c81a693dcc61d475510 to your computer and use it in GitHub Desktop.
Share logic
function openPopup(url: string, height: number, width: number) {
if (typeof window !== "undefined") {
const newwindow = window.open(
url,
"Share Window",
`height=${height}, width=${width}, top=` +
(window.innerHeight / 2 - height) +
", left=" +
(window.innerWidth / 2 - width) +
", toolbar=0, location=0, menubar=0, directories=0, scrollbars=0"
)
newwindow?.focus()
return newwindow
}
return null
}
type Type = "facebook" | "twitter" | "linkedin"
function shareLinkProps({
type,
/**
* Url should not start with //,
* either use http(s):// or exclude protocol
*/
url,
contentTitle,
contentSummary,
}: {
type: Type
url: string
contentTitle?: string
contentSummary?: string
}) {
let href = ""
let height = 550
let width = 400
switch (type) {
case "facebook":
href = `https://www.facebook.com/sharer/sharer.php?${queryString.stringify(
{
u: url,
quote: contentSummary,
}
)}`
height = 550
width = 400
break
case "linkedin":
href = `https://linkedin.com/shareArticle?${queryString.stringify({
url,
mini: "true",
title: contentTitle,
summary: contentSummary,
})}`
height = 600
width = 750
break
case "twitter":
href = `https://twitter.com/share?${queryString.stringify({
url,
text: contentSummary,
source: site.name,
})}`
height = 550
width = 400
break
}
return {
href,
rel: "noopener nofollow",
target: "_blank",
onClick: () => {
const newWindow = openPopup(href, height, width)
if (
!newWindow ||
newWindow.closed ||
typeof newWindow.closed == "undefined"
) {
// Popup blocked
return true
} else {
return false
}
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment