Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notyf events #74

Merged
merged 4 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ notyf.error('Please fill out the form');
```

## API

You can set some options when creating a Notyf instance.

### `new Notyf(options: INotyfOptions)`
Expand Down Expand Up @@ -115,6 +116,38 @@ notyf.error('Please fill out the form');
notyf.dismissAll();
```

## Events

Every individual notification emits events. You can register listeners using the `on` method.

### `'click'`

Triggers when the notification is clicked

```javascript
const notyf = new Notyf();
const notification = notyf.success('Address updated. Click here to continue');
notification.on('click', ({target, event}) => {
// target: the notification being clicked
// event: the mouseevent
window.location.href = '/';
});
```

### `'dismiss'`

Triggers when the notification is **manually** (not programatically) dismissed.

```javascript
const notyf = new Notyf();
notyf
.error({
message: 'There has been an error. Dismiss to retry.',
dismissible: true
})
.on('dismiss', ({target, event}) => foobar.retry());
```

## Interfaces

### INotyfPosition
Expand Down
23 changes: 22 additions & 1 deletion cypress/integration/notyf_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ context('Notyf', () => {
}

function typeCode(obj) {
cy.get('#code').type(JSON.stringify(obj).replace(new RegExp('{', 'g'), '{{}'), { delay: 0 });
return cy.get('#code').type(JSON.stringify(obj).replace(new RegExp('{', 'g'), '{{}'), { delay: 0 });
}

function checkPrintOutput(message) {
return cy.get('#print-output').should('have.text', message);
}

const VIEWPORT_WIDTH = 800;
Expand Down Expand Up @@ -387,5 +391,22 @@ context('Notyf', () => {
cy.get('#dismiss-all-btn').click();
cy.get('.notyf__toast').should('have.length', 0);
});

describe('Notification events', () => {
it('should emit the event: click', () => {
init();
cy.get('#click-listener-btn').click();
cy.get('.notyf__toast').click();
checkPrintOutput('clicked');
});

it('should emit the event: dismiss', () => {
const config = { dismissible: true };
init(config);
cy.get('#dismiss-listener-btn').click();
cy.get('.notyf__dismiss-btn').click();
checkPrintOutput('dismissed');
});
});
});
});
7 changes: 7 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
<button id="dismiss-btn" type="button">Dismiss</button>
</div>

<div style="border: 1px solid black;">
<p>Events</p>
<button id="click-listener-btn" type="button">Click</button>
<button id="dismiss-listener-btn" type="button">Dismiss</button>
<p id="print-output"></p>
</div>

<br>
<label>Message</label>
<input type="text" id="message">
Expand Down
23 changes: 10 additions & 13 deletions demo/scripts/script-demo.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
if (false) { // activate only when testing the demo
if (false) {
// activate only when testing the demo
var notyfDemo = new Notyf();

document.getElementById('success-btn-demo')
.addEventListener('click', function(){
show('success');
});

document.getElementById('error-btn-demo')
.addEventListener('click', function(){
show('error');
});
document.getElementById('success-btn-demo').addEventListener('click', function () {
show('success');
});

document.getElementById('error-btn-demo').addEventListener('click', function () {
show('error');
});

function show(type) {
const message = document.getElementById('message').value;
Expand All @@ -19,8 +17,7 @@ if (false) { // activate only when testing the demo
} else if (type === 'error') {
notyfDemo.error(message || 'You have been disconnected!');
} else {
notyfDemo.open({type, message: message || 'This is a custom notification'})
notyfDemo.open({ type, message: message || 'This is a custom notification' });
}
}

}
}
16 changes: 16 additions & 0 deletions demo/scripts/script-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ document.getElementById('dismiss-btn').addEventListener('click', function () {
dismiss(idx);
});

// events

document.getElementById('click-listener-btn').addEventListener('click', function () {
const notif = notyf.success('Click me');
notif.on('click', () => print('clicked'));
});

document.getElementById('dismiss-listener-btn').addEventListener('click', function () {
const notif = notyf.success({ message: 'Dismiss me', dismissible: true });
notif.on('dismiss', () => print('dismissed'));
});

function init() {
if (notyf) {
try {
Expand All @@ -48,6 +60,10 @@ function dismissAll() {
notyfNotifications.length = 0;
}

function print(msg) {
document.getElementById('print-output').textContent = msg;
}

function show(type) {
const message = document.getElementById('message').value;
const options = JSON.parse(document.getElementById('code').value || '{}');
Expand Down
Loading