Sonner
An opinionated toast component for Ember.
import { on } from '@ember/modifier';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { Button } from '@/components/ui/button';
import type ToastService from '@/services/toast';
export default class SonnerDemo extends Component {
@service declare toast: ToastService;
showToast = () => {
this.toast.add({
message: 'Event has been created',
description: 'Sunday, December 03, 2023 at 9:00 AM',
action: {
label: 'Undo',
onClick: () => console.log('Undo'),
},
});
};
<template>
<Button @variant="outline" {{on "click" this.showToast}}>
Show Toast
</Button>
</template>
}About
The Sonner component uses ember-cli-flash to manage flash messages and renders them as styled toasts.
Installation
Usage
import { Toaster } from '@/components/ui/sonner';<Toaster />
To show a toast, inject the
toast
service and call one of its methods:
import { service } from '@ember/service';
import type ToastService from '@/services/toast';
class MyComponent extends Component {
@service declare toast: ToastService;
showToast = () => {
this.toast.add({
message: 'Event has been created.',
});
};
}Examples
Types
import { on } from '@ember/modifier';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { Button } from '@/components/ui/button';
import type ToastService from '@/services/toast';
export default class SonnerTypes extends Component {
@service declare toast: ToastService;
showDefault = () => {
this.toast.add({
message: 'Event has been created',
});
};
showSuccess = () => {
this.toast.success('Event has been created');
};
showInfo = () => {
this.toast.info('Be at the area 10 minutes before the event time');
};
showWarning = () => {
this.toast.warning('Event start time cannot be earlier than 8am');
};
showError = () => {
this.toast.error('Event has not been created');
};
showPromise = () => {
void this.toast.promise<{ name: string }>(
() =>
new Promise((resolve) =>
setTimeout(() => resolve({ name: 'Event' }), 2000)
),
{
loading: 'Loading...',
success: (data) => `${data.name} has been created`,
error: 'Error',
}
);
};
<template>
<div class="flex flex-wrap gap-2">
<Button @variant="outline" {{on "click" this.showDefault}}>
Default
</Button>
<Button @variant="outline" {{on "click" this.showSuccess}}>
Success
</Button>
<Button @variant="outline" {{on "click" this.showInfo}}>
Info
</Button>
<Button @variant="outline" {{on "click" this.showWarning}}>
Warning
</Button>
<Button @variant="outline" {{on "click" this.showError}}>
Error
</Button>
<Button @variant="outline" {{on "click" this.showPromise}}>
Promise
</Button>
</div>
</template>
}Description
import { on } from '@ember/modifier';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { Button } from '@/components/ui/button';
import type ToastService from '@/services/toast';
export default class SonnerDescription extends Component {
@service declare toast: ToastService;
showToast = () => {
this.toast.add({
message: 'Event has been created',
description: 'Monday, January 3rd at 6:00pm',
});
};
<template>
<Button @class="w-fit" @variant="outline" {{on "click" this.showToast}}>
Show Toast
</Button>
</template>
}Position
Use the
@position
arg to change the position of the toaster.
import { on } from '@ember/modifier';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { Button } from '@/components/ui/button';
import { Toaster } from '@/components/ui/sonner';
import type ToastService from '@/services/toast';
type Position =
| 'top-left'
| 'top-center'
| 'top-right'
| 'bottom-left'
| 'bottom-center'
| 'bottom-right';
export default class SonnerPosition extends Component {
@service declare toast: ToastService;
@tracked position: Position = 'top-center';
showToast = (position: Position) => {
this.position = position;
this.toast.add({
message: 'Event has been created',
});
};
showTopLeft = () => this.showToast('top-left');
showTopCenter = () => this.showToast('top-center');
showTopRight = () => this.showToast('top-right');
showBottomLeft = () => this.showToast('bottom-left');
showBottomCenter = () => this.showToast('bottom-center');
showBottomRight = () => this.showToast('bottom-right');
<template>
<div class="flex flex-wrap justify-center gap-2">
<Button @variant="outline" {{on "click" this.showTopLeft}}>
Top Left
</Button>
<Button @variant="outline" {{on "click" this.showTopCenter}}>
Top Center
</Button>
<Button @variant="outline" {{on "click" this.showTopRight}}>
Top Right
</Button>
<Button @variant="outline" {{on "click" this.showBottomLeft}}>
Bottom Left
</Button>
<Button @variant="outline" {{on "click" this.showBottomCenter}}>
Bottom Center
</Button>
<Button @variant="outline" {{on "click" this.showBottomRight}}>
Bottom Right
</Button>
</div>
<Toaster @position={{this.position}} />
</template>
}