https://github.com/philnash/web-share-wrapper
A web component that wraps other share elements to replace with a web share button where supported.
https://github.com/philnash/web-share-wrapper
web-component web-components web-share web-share-api
Last synced: 28 days ago
JSON representation
A web component that wraps other share elements to replace with a web share button where supported.
- Host: GitHub
- URL: https://github.com/philnash/web-share-wrapper
- Owner: philnash
- License: mit
- Created: 2018-02-19T23:38:00.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-20T16:11:04.000Z (about 2 years ago)
- Last Synced: 2025-04-16T22:08:39.851Z (about 1 month ago)
- Topics: web-component, web-components, web-share, web-share-api
- Language: JavaScript
- Size: 79.1 KB
- Stars: 24
- Watchers: 2
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# ``
[](https://www.webcomponents.org/element/philnash/web-share-wrapper)
A custom element that can be used to wrap existing share buttons and replace them with a button that invokes the [Web Share API](https://philna.sh/blog/2017/03/14/the-web-share-api/).
For example, in supporting browsers the following HTML will show a plain `` saying "Share". In browsers that don't support the Web Share API, the default link to share on Twitter will be displayed.
```html
```
## How to use
The `` is a web component that you can use to enhance your website with the Web Share API. If the API is available, the component will replace its contents with a single share button or a template that you provide.
The component only activates if the Web Share API is available, otherwise it will just show it's contents.
### Browser support
Currently the only browser that supports the Web Share API (Chrome on Android) also supports custom elements and templates. So this module works where it will work and will fallback to your original HTML where it doesn't.
### Installation
#### Bower
You can install the component with bower.
```bash
bower install --save web-share-wrapper
```#### npm
You can also install with npm.
```bash
npm install web-share-wrapper
```Once installed, you can either use the minified version by referencing it from the `dist` directory.
```html
```
Or if you have a build step, you can require the module.
```javascript
const WebShareWrapper = require('web-share-wrapper');
```#### unpkg
If you want to use the component from the unpkg CDN then you can.
You can get the latest version using the bare URL. See more options on [https://unpkg.com/](https://unpkg.com/).
```html
```
### Example
```html
```
If `navigator.share` is available, the contents of the `` will be replaced with a single `` element, which when clicked will invoke the Web Share API.
Check out [this page with more examples](https://philnash.github.io/web-share-wrapper).
### Attributes
You can set attributes to control the text on the share button and the text and URL that is shared. The available attributes are:
- **text**: Sets the text on the share button.
- **template**: An id for a `` element. The component will find the template, hydrate it and add it as the child of the component. This overrides the `text` attribute.
- **sharetitle**: Sets the title to be shared.
- **sharetext**: Sets the text to be shared. Falls back to the text contents of the `` element.
- **shareurl**: Sets the URL to be shared. Fallsback to the `href` of the canonical link, failing that `window.location.href`.```html
```
Check out [this page with more examples](https://philnash.github.io/web-share-wrapper).
### Events
The `` emits one of two events in response to the result of using `navigator.share`.
If the share is successful then a `share-success` event is fired. The share title, text and URL are emitted in the `event`'s `details` (though the user may have edited them in the share target).
If the share is unsuccessful then a `share-failure` event is fired. The share title, text, URL and the error that was thrown are emitted in the `event`'s `details`.
## Tests
[TODO]
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/philnash/web-share-wrapper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org/) code of conduct.
## License
The code is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the `` project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/philnash/web-share-wrapper/blob/master/CODE_OF_CONDUCT.md).