npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

qwik-sonner

v1.0.3

Published

An opinionated toast component for Qwik.

Downloads

650

Readme

https://github.com/diecodev/qwik-sonner/assets/51871681/e7a7e4a6-c422-44b2-af7e-1dcb66e1dc19

qwik-sonner

An opinionated toast component for qwik. Based on emilkowalski's React implementation.

[!NOTE] This readme was created using the svelte-sonner readme as template

Quick start

Install it:

npm i qwik-sonner
# or
yarn add qwik-sonner
# or
pnpm add qwik-sonner
# or
bun add qwik-sonner

Add <Toaster /> to your app, it will be the place where all your toasts will be rendered. After that, you can use toast() from anywhere in your app.

import { Toaster, toast } from "qwik-sonner";

export const QwikComponent = component$(() => {
  return (
    <div>
      <Toaster />
      <button onClick$={() => toast("My first toast")}>Give me a toast</button>
    </div>
  );
});

Types

Default

Most basic toast. You can customize it (and any other type) by passing an options object as the second argument.

toast("Event has been created");

With custom icon and description:

import Icon from "./Icon.tsx";

toast("Event has been created", {
  description: "Monday, January 3rd at 6:00pm",
  icon: Icon,
});

Success

Renders a checkmark icon in front of the message.

toast.success("Event has been created");

Info

Renders a question mark icon in front of the message.

toast.info("Event has new information");

Warning

Renders a warning icon in front of the message.

toast.warning("Event has warning");

Error

Renders an error icon in front of the message.

toast.error("Event has not been created");

Action

Renders a button.

toast("Event has been created", {
  action: {
    label: "Undo",
    onClick: $(() => console.log("Undo")),
  },
});

[!CAUTION] You must import the dollar sign when passing a onClick function.

Promise

Starts in a loading state and will update automatically after the promise resolves or fails.

toast.promise(() => new Promise((resolve) => setTimeout(resolve, 2000)), {
  loading: "Loading",
  success: "Success",
  error: "Error",
});

You can pass a function to the success/error messages to incorporate the result/error of the promise.

toast.promise(promise, {
  loading: "Loading...",
  success: (data) => {
    return `${data.name} has been added!`;
  },
  error: "Error",
});

Custom Component

You can pass a component as the first argument instead of a string to render custom component while maintaining default styling. You can use the headless version below for a custom, unstyled toast.

toast(<div>My custom toast</div>);

Updating a toast

You can update a toast by using the toast function and passing it the id of the toast you want to update, the rest stays the same.

const toastId = toast("Sonner");

toast.success("Toast has been updated", {
  id: toastId,
});

Customization

Headless

You can use toast.custom to render an unstyled toast with custom component while maintaining the functionality.

toast.custom((t) => (
  <div>
    <h1>Custom toast</h1>
     <button onClick={$(() => toast.dismiss(t))}>Dismiss</button>
  </div>

Theme

You can change the theme using the theme prop. Default theme is light.

<Toaster theme="dark" />

Position

You can change the position through the position prop on the <Toaster /> component. Default is bottom-right.

// Available positions
// top-left, top-center, top-right, bottom-left, bottom-center, bottom-right

<Toaster position="top-center" />

Expanded

Toasts can also be expanded by default through the expand prop. You can also change the amount of visible toasts which is 3 by default.

<Toaster expand visibleToasts={9} />

Styling for all toasts

You can style your toasts globally with the toastOptions prop in the Toaster component.

<Toaster
  toastOptions={{
    style: "background: red;",
    class: "my-toast",
    descriptionClass: "my-toast-description",
  }}
/>

Styling for individual toast

toast("Event has been created", {
  style: "background: red;",
  class: "my-toast",
  descriptionClass: "my-toast-description",
});

Close button

Add a close button to all toasts that shows on hover by adding the closeButton prop.

<Toaster closeButton />

Rich colors

You can make error and success state more colorful by adding the richColors prop.

<Toaster richColors />

Custom offset

Offset from the edges of the screen.

<Toaster offset="80px" />

Programmatically remove toast

To remove a toast programmatically use toast.dismiss(id).

const toastId = toast("Event has been created");

toast.dismiss(toastId);

You can also dismiss all toasts at once by calling toast.dismiss() without an id.

toast.dismiss();

Duration

You can change the duration of each toast by using the duration property, or change the duration of all toasts like this:

<Toaster duration={10000} />
toast("Event has been created", {
  duration: 10000,
});

// Persisent toast
toast("Event has been created", {
  duration: Number.POSITIVE_INFINITY,
});

On Close Callback

You can pass onDismiss and onAutoClose callbacks. onDismiss gets fired when either the close button gets clicked or the toast is swiped. onAutoClose fires when the toast disappears automatically after it's timeout (duration prop).

toast("Event has been created", {
  onDismiss: $((t) => console.log(`Toast with id ${t.id} has been dismissed`)),
  onAutoClose: $((t) =>
    console.log(`Toast with id ${t.id} has been closed automatically`)
  ),
});

Keyboard focus

You can focus on the toast area by pressing ⌥/alt + T. You can override it by providing an array of event.code values for each key.

<Toaster hotkey={["KeyC"]} />

License

MIT