Skip to content

Commit

Permalink
feat: add TO support for email sharer
Browse files Browse the repository at this point in the history
  • Loading branch information
avdeev committed Sep 23, 2019
1 parent 1042e92 commit ae3a461
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ email({
})
```

You can use `getEmailUrl(options)` for getting URL.

### `linkedin(options)`

Share on LinkedIn
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fbShare, getFbShareUrl } from './sharers/fbShare';
import { fbButton, getFbButtonUrl } from './sharers/fbButton';
import gp from './sharers/gp';
import mail from './sharers/mail';
import email from './sharers/email';
import { email, getEmailUrl } from './sharers/email';
import { ok, getOkUrl } from './sharers/ok';
import { telegram, getTelegramUrl } from './sharers/telegram';
import { tw, getTwUrl } from './sharers/tw';
Expand Down Expand Up @@ -31,7 +31,7 @@ export {
vk, getVkUrl,
ok, getOkUrl,
mail,
email,
email, getEmailUrl,
linkedin,

whatsapp, getWhatsappUrl,
Expand Down
12 changes: 9 additions & 3 deletions src/sharers/email.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import encodeParams from '../utils/encodeParams';

export default function email(options = {}) {
export function getEmailUrl(options = {}) {
const {
url, title, description, subject,
to, url, title, description, subject,
} = options;

const params = encodeParams({
subject,
body: `${title || ''}\r\n${description || ''}\r\n${url || ''}`,
});
return window.location.assign(`mailto:?${params}`);

return `mailto:${to || ''}?${params}`;
}

export function email(options = {}) {
return window.location.assign(getEmailUrl(options));
}
8 changes: 7 additions & 1 deletion src/sharers/tests/email.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import faker from 'faker';

import email from '../email';
import { email } from '../email';

describe('email', () => {
beforeEach(() => {
Expand All @@ -13,6 +13,12 @@ describe('email', () => {
expect(window.location.assign).toBeCalledWith('mailto:?body=%0D%0A%0D%0A');
});

it('should call with to', () => {
const fixture = faker.internet.email();
email({ to: fixture });
expect(window.location.assign).toBeCalledWith(`mailto:${fixture}?body=%0D%0A%0D%0A`);
});

it('should call with url', () => {
const fixture = faker.internet.url();
email({ url: fixture });
Expand Down
3 changes: 3 additions & 0 deletions vanilla-sharing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export function ok(...args: any[]): any;
export function getOkUrl(...args: any[]): string;

export function mail(...args: any[]): any;

export function email(...args: any[]): any;
export function getEmailUrl(...args: any[]): any;

export function linkedin(...args: any[]): any;

export function whatsapp(...args: any[]): any;
Expand Down

0 comments on commit ae3a461

Please sign in to comment.