-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from josemarluedke/feat/impl-notificationst
Implement Notifications
- Loading branch information
Showing
42 changed files
with
1,355 additions
and
29 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 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
8 changes: 4 additions & 4 deletions
8
packages/changeset-form/tests/dummy/app/templates/application.hbs
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 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 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 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 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 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,49 @@ | ||
import Notification from './notification'; | ||
import Timer from './timer'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { later } from '@ember/runloop'; | ||
import { NotificationOptions } from './types'; | ||
|
||
export default class NotificationsManager { | ||
@tracked notifications: Notification[] = []; | ||
|
||
add(message: string, options: NotificationOptions = {}): Notification { | ||
const notification = new Notification(message, options); | ||
this.notifications = [...this.notifications, notification]; | ||
|
||
if (options.preserve !== true) { | ||
this.setupAutoRemoval(notification, notification.duration); | ||
} | ||
return notification; | ||
} | ||
|
||
remove(notification?: Notification): void { | ||
if (!notification) { | ||
return; | ||
} | ||
|
||
notification.isRemoving = true; | ||
|
||
later( | ||
this, | ||
() => { | ||
this.notifications = this.notifications.filter(n => { | ||
return n !== notification; | ||
}); | ||
}, | ||
notification.transitionDuration | ||
); | ||
} | ||
|
||
removeAll(): void { | ||
this.notifications.forEach(notification => { | ||
this.remove(notification); | ||
}); | ||
} | ||
|
||
private setupAutoRemoval(notification: Notification, duration: number): void { | ||
notification.timer = new Timer(duration, () => { | ||
this.remove(notification); | ||
}); | ||
} | ||
} |
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,46 @@ | ||
import { tracked } from '@glimmer/tracking'; | ||
import Timer from './timer'; | ||
import { NotificationOptions, CustomAction } from './types'; | ||
import config from 'ember-get-config'; | ||
import { getWithDefault } from '@ember/object'; | ||
|
||
function getConfigOption<T extends keyof NotificationOptions>( | ||
key: T, | ||
defaultValue: NonNullable<NotificationOptions[T]> | ||
): NonNullable<NotificationOptions[T]> { | ||
return getWithDefault( | ||
config['@frontile/notifications'] || ({} as never), | ||
key as never, | ||
defaultValue as never | ||
); | ||
} | ||
|
||
export default class Notification { | ||
readonly message: string; | ||
readonly transitionDuration: number; | ||
readonly appearance: NonNullable<NotificationOptions['appearance']>; | ||
readonly customActions?: CustomAction[]; | ||
readonly allowClosing: boolean; | ||
readonly duration: number; | ||
|
||
@tracked timer?: Timer; | ||
@tracked isRemoving = false; | ||
|
||
constructor(message: string, options: NotificationOptions = {}) { | ||
this.message = message; | ||
this.appearance = | ||
options.appearance || getConfigOption('appearance', 'info'); | ||
this.customActions = options.customActions; | ||
this.duration = options.duration || getConfigOption('duration', 5000); | ||
this.transitionDuration = | ||
typeof options.transitionDuration !== 'undefined' | ||
? options.transitionDuration | ||
: getConfigOption('transitionDuration', 200); | ||
|
||
if (options.allowClosing === false) { | ||
this.allowClosing = false; | ||
} else { | ||
this.allowClosing = true; | ||
} | ||
} | ||
} |
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,51 @@ | ||
import { tracked } from '@glimmer/tracking'; | ||
import { later, cancel } from '@ember/runloop'; | ||
import { EmberRunTimer } from '@ember/runloop/types'; | ||
import { action } from '@ember/object'; | ||
|
||
export default class Timer { | ||
@tracked remaining: number; | ||
@tracked isRunning = true; | ||
|
||
readonly onFinish: () => void; | ||
private timer?: EmberRunTimer; | ||
private start!: number; | ||
|
||
constructor(duration: number, onFinish: () => void) { | ||
this.remaining = duration; | ||
this.onFinish = onFinish; | ||
this.setup(); | ||
} | ||
|
||
@action clear(): void { | ||
this.isRunning = false; | ||
|
||
if (this.timer) { | ||
cancel(this.timer); | ||
} | ||
} | ||
|
||
@action pause(): void { | ||
this.clear(); | ||
this.remaining -= Date.now() - this.start; | ||
} | ||
|
||
@action resume(): void { | ||
this.clear(); | ||
this.setup(); | ||
} | ||
|
||
private setup(): void { | ||
this.start = Date.now(); | ||
this.isRunning = true; | ||
|
||
this.timer = later( | ||
this, | ||
() => { | ||
this.onFinish(); | ||
this.isRunning = false; | ||
}, | ||
this.remaining | ||
); | ||
} | ||
} |
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,38 @@ | ||
export interface CustomAction { | ||
label: string; | ||
onClick: () => void; | ||
} | ||
|
||
export interface NotificationOptions { | ||
/* If set to false, the close button will not be displayed. | ||
* | ||
* @defaultValue true | ||
*/ | ||
allowClosing?: boolean; | ||
|
||
/* | ||
* @defaultValue false | ||
*/ | ||
preserve?: boolean; | ||
|
||
/* | ||
* @defaultValue 5000 | ||
*/ | ||
duration?: number; | ||
|
||
/* | ||
* The duration for the transition on removal | ||
* @defaultValue 200 | ||
*/ | ||
transitionDuration?: number; | ||
|
||
/* | ||
* @defaultValue 'info' | ||
*/ | ||
appearance?: 'info' | 'success' | 'warning' | 'error'; | ||
|
||
/* | ||
* @defaultValue undefined | ||
*/ | ||
customActions?: CustomAction[]; | ||
} |
Empty file.
49 changes: 49 additions & 0 deletions
49
packages/notifications/addon/components/notification-card.hbs
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,49 @@ | ||
{{!-- template-lint-disable no-invalid-interactive --}} | ||
{{!-- template-lint-disable no-unnecessary-concat --}} | ||
{{!-- template-lint-disable no-inline-styles --}} | ||
{{!-- template-lint-disable style-concatenation --}} | ||
{{!- | ||
appearances: | ||
notification-card-info | ||
notification-card-success | ||
notification-card-warning | ||
notification-card-error | ||
-}} | ||
|
||
<div | ||
{{on "mouseenter" this.pause}} | ||
{{on "mouseleave" this.resume}} | ||
class="notification-card | ||
notification-card-{{@notification.appearance}}{{if @notification.isRemoving "is-removing"}}" | ||
style="{{this.styles}}" | ||
...attributes | ||
> | ||
<div class="notification-card-message"> | ||
{{@notification.message}} | ||
</div> | ||
|
||
{{#if @notification.customActions}} | ||
<div class="notification-card-custom-actions"> | ||
{{#each @notification.customActions as |customAction|}} | ||
<button | ||
type="button" | ||
class="notification-card-custom-action-btn" | ||
{{on "click" (fn this.handleClickCustomAction customAction)}} | ||
> | ||
{{customAction.label}} | ||
</button> | ||
{{/each}} | ||
</div> | ||
{{/if}} | ||
|
||
{{#if @notification.allowClosing}} | ||
<button | ||
type="button" | ||
class="notification-card-close-btn" | ||
{{on "click" this.remove}} | ||
> | ||
<div class="notification-card-close-btn-icon"></div> | ||
<VisuallyHidden>Close</VisuallyHidden> | ||
</button> | ||
{{/if}} | ||
</div> |
40 changes: 40 additions & 0 deletions
40
packages/notifications/addon/components/notification-card.ts
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,40 @@ | ||
import Component from '@glimmer/component'; | ||
import { action } from '@ember/object'; | ||
import { inject as service } from '@ember/service'; | ||
import { htmlSafe } from '@ember/string'; | ||
import { NotificationsService, Notification, CustomAction } from '../.'; | ||
|
||
interface NotificationCardArgs { | ||
notification: Notification; | ||
} | ||
|
||
export default class NotificationCard extends Component<NotificationCardArgs> { | ||
@service notifications!: NotificationsService; | ||
|
||
get styles(): unknown { | ||
return htmlSafe( | ||
`transition-duration: ${this.args.notification.transitionDuration}ms` | ||
); | ||
} | ||
|
||
@action remove(): void { | ||
this.notifications.remove(this.args.notification); | ||
} | ||
|
||
@action pause(): void { | ||
if (this.args.notification.timer) { | ||
this.args.notification.timer.pause(); | ||
} | ||
} | ||
|
||
@action resume(): void { | ||
if (this.args.notification.timer) { | ||
this.args.notification.timer.resume(); | ||
} | ||
} | ||
|
||
@action handleClickCustomAction(customAction: CustomAction): void { | ||
customAction.onClick(); | ||
this.remove(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/notifications/addon/components/notifications-container.hbs
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,23 @@ | ||
{{!-- | ||
Placements: | ||
top-left | ||
top-center | ||
top-right | ||
bottom-left | ||
bottom-center | ||
bottom-right | ||
--}} | ||
|
||
{{#if this.notifications.notifications}} | ||
<div | ||
class="notifications-container {{if @placement @placement "bottom-right"}}" | ||
role="alert" | ||
aria-live="assertive" | ||
aria-atomic="true" | ||
...attributes | ||
> | ||
{{#each this.notifications.notifications as |notification|}} | ||
<NotificationCard @notification={{notification}} /> | ||
{{/each}} | ||
</div> | ||
{{/if}} |
Oops, something went wrong.