Skip to content

Commit

Permalink
feat(share-button): small perf and size refactor for v1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
p-m-p committed Dec 23, 2024
1 parent 2bc1a15 commit 9233b7b
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-teachers-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@parsonic/share-button': major
---

Tidy up and release as v1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
packages/share-button/min.js
21 changes: 11 additions & 10 deletions packages/share-button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ Add the script tag and use the button in your page.
```html
<script
defer
type="module"
src="https://cdn.jsdelivr.net/npm/@parsonic/share-button@latest/dist/index.js"
src="https://cdn.jsdelivr.net/npm/@parsonic/share-button@latest/min.js"
></script>
<share-button data-button-label="Share this page"></share-button>
```
Expand Down Expand Up @@ -68,7 +67,7 @@ supports it. If the share feature is not available the component will not be
defined and will either show the fallback content or no button at all.

For control over the share data you can provide the share button with data
attribute. Below is an example using Nunjucks template syntax.
attributes. Below is an example using Nunjucks template syntax.

```html
<share-button
Expand Down Expand Up @@ -103,7 +102,7 @@ Provide a label for the button with the `data-button-label` attribute.
<share-button data-button-label="Share this page"></share-button>
```

Style the button use the `button` part selector.
Style the button using the `button` part selector.

```css
share-button::part(button) {
Expand All @@ -121,9 +120,9 @@ Provide your own button in the `button` slot.

## Share event

When the share button is clicked a custom event with the name `share` is
dispatched. This event has the share data as the payload, bubbles and is
cancelable.
When the share button is clicked a [custom event][custom-event] with the
name `share` is dispatched. This event has the share data as the payload,
bubbles and is cancelable.

```js
// Example metric capture
Expand All @@ -143,9 +142,10 @@ document.addEventListener('share', (ev) => {

## Fallback content

Fallback content can be provided if for some reason the native share
function isn't available or the scripts aren't loaded. Please see this
[blog post][blog-post] for thorough explanation of using fallback content.
Fallback content can be provided for situations where the native share
function isn't available or the component script isn't loaded. Please see
this [blog post][blog-post] for thorough explanation of using fallback
content.

```html
<share-button>
Expand All @@ -164,5 +164,6 @@ function isn't available or the scripts aren't loaded. Please see this

[share]: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share
[open-graph]: https://ogp.me/
[custom-event]: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
[dist]: https://cdn.jsdelivr.net/npm/@parsonic/[email protected]/dist/
[blog-post]: https://philparsons.co.uk/blog/dont-fouc-up-your-web-components/
77 changes: 33 additions & 44 deletions packages/share-button/ShareButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ if (typeof HTMLElement !== 'undefined') {
BaseElement = class {}
}

function getGraphContent(property, defaultValue) {
return (
document
.querySelector(`meta[property='og:${property}']`)
?.getAttribute('content') ?? defaultValue
)
}

export default class ShareButton extends BaseElement {
static register(tag = 'share-button') {
if ('share' in navigator) {
Expand All @@ -14,60 +22,41 @@ export default class ShareButton extends BaseElement {
}

connectedCallback() {
let { url, text, title, buttonLabel } = this.dataset

if (!url) {
url =
document
.querySelector("meta[property='og:url']")
?.getAttribute('content') ?? location.href
}
const defaultButton = document.createElement('button')
defaultButton.textContent = this.dataset.buttonLabel ?? 'Share'
defaultButton.setAttribute('part', 'button')

if (!text) {
text = document
.querySelector("meta[property='og:description']")
?.getAttribute('content')
}
const slot = document.createElement('slot')
slot.setAttribute('name', 'button')
slot.appendChild(defaultButton)

if (!title) {
title =
document
.querySelector("meta[property='og:title']")
?.getAttribute('content') ?? document.title
}
const shadowRoot = this.attachShadow({ mode: 'open' })
shadowRoot.appendChild(slot)

const data = { url, text, title }
slot.addEventListener('click', async () => {
let {
url = getGraphContent('url', location.href),
title = getGraphContent('title', document.title),
text = getGraphContent('description'),
} = this.dataset

if (navigator.canShare(data)) {
const defaultButton = document.createElement('button')
defaultButton.textContent = buttonLabel ?? 'Share'
defaultButton.setAttribute('part', 'button')
const data = { url, text, title }

const slot = document.createElement('slot')
slot.setAttribute('name', 'button')
slot.appendChild(defaultButton)

const shadowRoot = this.attachShadow({ mode: 'open' })
shadowRoot.appendChild(slot)

slot.addEventListener('click', async (ev) => {
ev.preventDefault()

const canShare = this.dispatchEvent(
if (
navigator.canShare(data) &&
this.dispatchEvent(
new CustomEvent('share', {
cancelable: true,
bubbles: true,
detail: data,
})
)

if (canShare) {
try {
await navigator.share(data)
// eslint-disable-next-line no-empty, no-unused-vars
} catch (err) {}
}
})
}
) {
try {
await navigator.share(data)
// eslint-disable-next-line no-empty, no-unused-vars
} catch (err) {}
}
})
}
}
7 changes: 7 additions & 0 deletions packages/share-button/min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions packages/share-button/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/p-m-p/parsonic/tree/main/packages/share-button"
"url": "https://github.com/p-m-p/parsonic"
},
"scripts": {
"build": "esbuild index.js ShareButton.js --bundle --minify --sourcemap --outdir=dist --format=esm",
"clean": "rm -rf dist",
"prepare": "cp ../../LICENSE ./",
"test": "echo \"Error: no test specified\" && exit 1"
"build": "esbuild index.js --bundle --minify --sourcemap --outfile=min.js --format=iife",
"clean": "rm -f min.*",
"prepare": "cp ../../LICENSE ./"
},
"keywords": [
"button",
Expand Down
10 changes: 10 additions & 0 deletions stories/ShareButton.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import './share-button.css'
ShareButton.register('test-button')

export default {
tags: ['autodocs'],
title: 'ShareButton',
}

Expand All @@ -17,6 +18,15 @@ export const Label = {
render: () => html`<test-button data-button-label="Share it!"></test-button>`,
}

export const Attributes = {
render: () =>
html`<test-button
data-url="https://example.com/"
data-title="Hello world!"
data-text="Test data attributes"
></test-button>`,
}

export const Styled = {
render: () => html`<test-button class="styled"></test-button>`,
}
Expand Down

0 comments on commit 9233b7b

Please sign in to comment.