Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
svelte-notifications
Advanced tools
Simple and flexible notifications system for Svelte 3
https://svelte-notifications.netlify.com
npm install --save svelte-notifications
// MainComponent.svelte
<script>
import Notifications from 'svelte-notifications';
import App from './App.svelte';
</script>
<Notifications>
<App />
</Notifications>
// ChildrenComponent.svelte
<script>
import { getNotificationsContext } from 'svelte-notifications';
const { addNotification } = getNotificationsContext();
</script>
<button
on:click={() => addNotification({
text: 'Notification',
position: 'bottom-center',
})}
>
Add notification
</button>
// MainComponent.svelte
<script>
import Notifications from 'svelte-notifications';
import CustomNotification from './CustomNotification.svelte';
import App from './App.svelte';
</script>
<Notifications item={CustomNotification}>
<App />
</Notifications>
// CustomNotification.svelte
<script>
export let notification = {};
// `onRemove` function will be passed into your component.
export let onRemove = null;
const handleButtonClick = () => {
onRemove();
}
</script>
<div class={notification.type === 'error' ? 'error' : ''}>
<h4>{notification.heading}</h4>
<p>{notification.description}</p>
<button on:click={handleButtonClick}>Close me</button>
</div>
// AnotherComponent.svelte
<script>
import { getNotificationsContext } from 'svelte-notifications';
const { addNotification } = getNotificationsContext();
const handleButtonClick = () => {
addNotification({
position: 'bottom-right',
heading: 'hi i am custom notification',
type: 'error',
description: 'lorem ipsum',
});
}
</script>
<div>
<button on:click={handleButtonClick}>Show notification</button>
</div>
Notifications
The Notifications
component supplies descendant components with notifications store through context.
[item=null]
- Custom notification component that receives the notification object[withoutStyles=false]
- If you don't want to use the default styles, this flag will remove the classes to which the styles are attached[zIndex]
- Adds a style with z-index for the notification container// MainComponent.svelte
<script>
import Notifications from 'svelte-notifications';
import App from './App.svelte';
</script>
<Notifications>
<App />
</Notifications>
getNotificationsContext
A function that allows you to access the store and the functions that control the store.
// ChildrenComponent.svelte
<script>
import { getNotificationsContext } from 'svelte-notifications';
const notificationsContext = getNotificationsContext();
const {
addNotification,
removeNotification,
clearNotifications,
subscribe,
} = notificationsContext;
</script>
getNotificationsContext:addNotification
You can provide any object that the notification component will receive. The default object looks like this:
notification
- The object that will receive the notification component[id=timestamp-rand]
- Unique notification identificatortext
– Notification text[position=bottom-center]
– One of these values: top-left
, top-center
, top-right
, bottom-left
, bottom-center
, bottom-right
type
– One of these values: success
, warning
, error
[removeAfter]
– After how much the notification will disappear (in milliseconds)// ChildrenComponent.svelte
<script>
import { getNotificationsContext } from 'svelte-notifications';
const { addNotification } = getNotificationsContext();
addNotification({
id: 'uniqNotificationId',
text: 'Notification',
position: 'top-center',
type: 'success',
removeAfter: 4000,
...rest,
});
</script>
getNotificationsContext:removeNotification
id
- Unique notification identificator// ChildrenComponent.svelte
<script>
import { getNotificationsContext } from 'svelte-notifications';
const { removeNotification } = getNotificationsContext();
removeNotification('uniqNotificationId');
</script>
getNotificationsContext:clearNotifications
// ChildrenComponent.svelte
<script>
import { getNotificationsContext } from 'svelte-notifications';
const { clearNotifications } = getNotificationsContext();
clearNotifications();
</script>
getNotificationsContext:subscribe
Default Svelte subscribe method that allows interested parties to be notified whenever the store value changes
Read more about contributing here
FAQs
Extremely simple and flexible notifications for Svelte
The npm package svelte-notifications receives a total of 1,318 weekly downloads. As such, svelte-notifications popularity was classified as popular.
We found that svelte-notifications demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.