diff --git a/README.md b/README.md index 1d793359..db9166cf 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,17 @@ mail({ }) ``` +### `email(options)` + +Share via user's email + +```js +mail({ + url: string, + title: string, + description: string +}) + ### `linkedin(options)` Share on LinkedIn diff --git a/demo/index.html b/demo/index.html index 29ccdda4..7bffaf86 100644 --- a/demo/index.html +++ b/demo/index.html @@ -37,6 +37,7 @@

vanilla-sharing dem + @@ -130,6 +131,15 @@

vanilla-sharing dem }); }); + document.getElementById('email').addEventListener('click', function() { + VanillaSharing.email({ + url: META.URL, + title: META.TITLE, + description: META.DESCRIPTION, + image: META.IMAGE, + }); + }); + document.getElementById('linkedin').addEventListener('click', function() { VanillaSharing.linkedin({ url: META.URL, diff --git a/package.json b/package.json index 649b7686..8428daec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vanilla-sharing", - "description": "Small simple tool for sharing url, title, description and image to VK, Facebook, OK, G+, Twitter and Mail", + "description": "Small simple tool for sharing url, title, description and image to VK, Facebook, OK, G+, Twitter, email, and Mail", "keywords": [ "sharing", "vk", @@ -35,11 +35,11 @@ "size-limit": [ { "path": "dist/vanilla-sharing.umd.js", - "limit": "1.2 KB" + "limit": "1.3 KB" }, { "path": "dist/vanilla-sharing.esm.js", - "limit": "1.2 KB" + "limit": "1.3 KB" } ], "lint-staged": { diff --git a/src/main.js b/src/main.js index 825d5178..ee053998 100644 --- a/src/main.js +++ b/src/main.js @@ -3,6 +3,7 @@ import fbShare from './sharers/fbShare'; import fbButton from './sharers/fbButton'; import gp from './sharers/gp'; import mail from './sharers/mail'; +import email from './sharers/email'; import ok from './sharers/ok'; import telegram from './sharers/telegram'; import tw from './sharers/tw'; @@ -23,6 +24,7 @@ export { fbButton, gp, mail, + email, ok, telegram, tw, diff --git a/src/sharers/email.js b/src/sharers/email.js new file mode 100644 index 00000000..7d218424 --- /dev/null +++ b/src/sharers/email.js @@ -0,0 +1,9 @@ +export default function email(options = {}) { + const { + url, title, description, + } = options; + + const body = `${title || ''}\r\n${description || ''}\r\n${url || ''}`; + const uri = `mailto:?subject=&body=${encodeURIComponent(body)}`; + return window.location.assign(uri); +} diff --git a/src/sharers/tests/email.test.js b/src/sharers/tests/email.test.js new file mode 100644 index 00000000..b6c1e7ae --- /dev/null +++ b/src/sharers/tests/email.test.js @@ -0,0 +1,37 @@ +import faker from 'faker'; + +import email from '../email'; + +describe('email', () => { + beforeEach(() => { + window.open = jest.fn(); + window.location.assign = jest.fn(); + }); + + it('should call without params', () => { + email(); + expect(window.location.assign).toBeCalledWith('mailto:?subject=&body=%0D%0A%0D%0A'); + }); + + it('should call with url', () => { + const fixture = faker.internet.url(); + email({ url: fixture }); + expect(window.location.assign).toBeCalledWith(`mailto:?subject=&body=%0D%0A%0D%0A${encodeURIComponent(fixture)}`); + }); + + it('should call with title', () => { + const fixture = faker.lorem.sentence(); + + email({ title: fixture }); + + expect(window.location.assign).toBeCalledWith(`mailto:?subject=&body=${encodeURIComponent(fixture)}%0D%0A%0D%0A`); + }); + + it('should call with description', () => { + const fixture = faker.lorem.sentences(); + + email({ description: fixture }); + + expect(window.location.assign).toBeCalledWith(`mailto:?subject=&body=%0D%0A${encodeURIComponent(fixture)}%0D%0A`); + }); +});