generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from hlxsites/feature/social-share
Updated the social share block
- Loading branch information
Showing
7 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* social share */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
function createShareButton(network, url, title) { | ||
const baseUrl = { | ||
twitter: 'https://twitter.com/intent/tweet?url=', | ||
linkedin: 'https://www.linkedin.com/sharing/share-offsite/?url=', | ||
facebook: 'https://www.facebook.com/sharer/sharer.php?u=', | ||
}; | ||
|
||
const fullUrl = `${baseUrl[network]}${encodeURIComponent(url)}&title=${encodeURIComponent(title)}`; | ||
|
||
const listItem = document.createElement('li'); | ||
listItem.className = 'hs-blog-social-share-item'; | ||
const link = document.createElement('a'); | ||
link.className = `hs-blog-social-share-item-link hs-blog-social-share-item-${network}`; | ||
link.href = fullUrl; | ||
const img = document.createElement('img'); | ||
img.setAttribute('src', `/icons/${network}.svg`); | ||
const label = document.createElement('span'); | ||
label.innerText = network === 'twitter' ? 'Tweet' : 'Share'; | ||
link.appendChild(img); | ||
link.appendChild(label); | ||
listItem.appendChild(link); | ||
|
||
return listItem; | ||
} | ||
|
||
function renderShareButtons(container, url, title) { | ||
const networks = ['twitter', 'linkedin', 'facebook']; | ||
const list = document.createElement('ul'); | ||
list.className = 'hs-blog-social-share-list'; | ||
|
||
networks.forEach((network) => { | ||
const listItem = createShareButton(network, url, title); | ||
list.appendChild(listItem); | ||
}); | ||
|
||
container.appendChild(list); | ||
} | ||
|
||
export default function decorate(block) { | ||
const title = block.querySelector('h1'); | ||
const link = block.querySelector('a'); | ||
const shareContainer = document.createElement('div'); | ||
shareContainer.className = 'hs-blog-social-share'; | ||
renderShareButtons(shareContainer, link.href, title.innerText); | ||
block.innerText = ''; | ||
block.appendChild(shareContainer); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters