-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
80 additions
and
32 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
This file was deleted.
Oops, something went wrong.
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,75 @@ | ||
/** | ||
* ShareArticle Hook | ||
* | ||
* This hook provides functionality for sharing data using either the | ||
* Web Share API or a fallback sharing mechanism. | ||
* | ||
* Usage: | ||
* Add the `phx-hook="ShareArticle"` attribute to an element, and include | ||
* `data-title` and `data-url` attributes with the corresponding title and URL. | ||
* | ||
* Example: | ||
* <div phx-hook="ShareArticle" data-title="Article Title" data-url="https://example.com/article"> | ||
* <button data-share-web-share class="hidden">Share</button> | ||
* <div data-share-fallback class="hidden"> | ||
* <!-- Fallback sharing options --> | ||
* </div> | ||
* </div> | ||
*/ | ||
const WebShareApi = { | ||
/** | ||
* Initializes the sharing functionality when the element is mounted. | ||
*/ | ||
mounted() { | ||
const { title, url } = this.el.dataset; | ||
const shareData = { title, url }; | ||
|
||
this.initializeSharing(shareData); | ||
}, | ||
/** | ||
* Sets up the appropriate sharing method based on browser support. | ||
* @param {Object} shareData - The data to be shared (title and URL). | ||
*/ | ||
initializeSharing(shareData) { | ||
const webShareButton = this.el.querySelector("[data-share-web-share]"); | ||
const fallbackShareElement = this.el.querySelector("[data-share-fallback]"); | ||
|
||
if (navigator.share && navigator.canShare(shareData)) { | ||
this.setupWebSharing(webShareButton, shareData); | ||
} else { | ||
this.setupFallbackSharing(webShareButton, fallbackShareElement); | ||
} | ||
}, | ||
/** | ||
* Configures the Web Share API sharing functionality. | ||
* @param {HTMLElement} webShareButton - The button element for sharing. | ||
* @param {Object} shareData - The data to be shared. | ||
*/ | ||
setupWebSharing(webShareButton, shareData) { | ||
if (webShareButton) { | ||
webShareButton.classList.remove("hidden"); | ||
webShareButton.addEventListener("click", async () => { | ||
try { | ||
await navigator.share(shareData); | ||
} catch (err) { | ||
console.error("Error sharing:", err); | ||
} | ||
}); | ||
} | ||
}, | ||
/** | ||
* Sets up the fallback sharing mechanism when native sharing is not supported. | ||
* @param {HTMLElement} webShareButton - The Web Share API button to be hidden. | ||
* @param {HTMLElement} fallbackElement - The fallback sharing element to be displayed. | ||
*/ | ||
setupFallbackSharing(webShareButton, fallbackElement) { | ||
if (webShareButton) { | ||
webShareButton.classList.add("hidden"); | ||
} | ||
if (fallbackElement) { | ||
fallbackElement.classList.remove("hidden"); | ||
} | ||
} | ||
}; | ||
|
||
export default WebShareApi; |
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