magic-pill 8.1.0 → 9.0.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +46 -0
- package/MIGRATING.md +224 -0
- package/README.md +312 -257
- package/dist/MagicPill/MagicPill.d.mts +27 -0
- package/dist/MagicPill/MagicPill.d.ts +27 -0
- package/dist/MagicPill/MagicPill.js +185 -0
- package/dist/MagicPill/MagicPill.mjs +185 -0
- package/dist/MagicPill/choreography.js +91 -0
- package/dist/MagicPill/choreography.mjs +85 -0
- package/dist/Pill/Pill.d.mts +7 -0
- package/dist/Pill/Pill.d.ts +7 -0
- package/dist/Pill/Pill.js +106 -0
- package/dist/Pill/Pill.mjs +106 -0
- package/dist/Pill/PillAction.d.mts +7 -0
- package/dist/Pill/PillAction.d.ts +7 -0
- package/dist/Pill/PillAction.js +67 -0
- package/dist/Pill/PillAction.mjs +67 -0
- package/dist/Pill/PillContext.d.mts +3 -0
- package/dist/Pill/PillContext.d.ts +3 -0
- package/dist/Pill/PillContext.js +26 -0
- package/dist/Pill/PillContext.mjs +24 -0
- package/dist/Pill/PillPanel.d.mts +9 -0
- package/dist/Pill/PillPanel.d.ts +9 -0
- package/dist/Pill/PillPanel.js +88 -0
- package/dist/Pill/PillPanel.mjs +87 -0
- package/dist/Pill/parts.d.mts +10 -0
- package/dist/Pill/parts.d.ts +10 -0
- package/dist/Pill/parts.js +46 -0
- package/dist/Pill/parts.mjs +43 -0
- package/dist/Pill/types.d.mts +88 -0
- package/dist/Pill/types.d.ts +88 -0
- package/dist/PillNavbar/PillNavbar.d.mts +31 -0
- package/dist/PillNavbar/PillNavbar.d.ts +31 -0
- package/dist/PillNavbar/PillNavbar.js +44 -0
- package/dist/PillNavbar/PillNavbar.mjs +44 -0
- package/dist/PillNavbar/matchPath.d.mts +2 -0
- package/dist/PillNavbar/matchPath.d.ts +2 -0
- package/dist/PillNavbar/matchPath.js +34 -0
- package/dist/PillNavbar/matchPath.mjs +34 -0
- package/dist/PillNotification/PillNotification.d.mts +51 -0
- package/dist/PillNotification/PillNotification.d.ts +51 -0
- package/dist/PillNotification/PillNotification.js +64 -0
- package/dist/PillNotification/PillNotification.mjs +63 -0
- package/dist/icons/ArrowIcon.d.mts +4 -0
- package/dist/icons/ArrowIcon.d.ts +4 -0
- package/dist/icons/ArrowIcon.js +21 -0
- package/dist/icons/ArrowIcon.mjs +21 -0
- package/dist/icons/BoxIcon.d.mts +4 -0
- package/dist/icons/BoxIcon.d.ts +4 -0
- package/dist/icons/BoxIcon.js +21 -0
- package/dist/icons/BoxIcon.mjs +21 -0
- package/dist/icons/CheckIcon.d.mts +4 -0
- package/dist/icons/CheckIcon.d.ts +4 -0
- package/dist/icons/CheckIcon.js +17 -0
- package/dist/icons/CheckIcon.mjs +17 -0
- package/dist/icons/CloseIcon.d.mts +4 -0
- package/dist/icons/CloseIcon.d.ts +4 -0
- package/dist/icons/CloseIcon.js +17 -0
- package/dist/icons/CloseIcon.mjs +17 -0
- package/dist/icons/CouponIcon.d.mts +4 -0
- package/dist/icons/CouponIcon.d.ts +4 -0
- package/dist/icons/CouponIcon.js +17 -0
- package/dist/icons/CouponIcon.mjs +17 -0
- package/dist/icons/DocumentIcon.d.mts +4 -0
- package/dist/icons/DocumentIcon.d.ts +4 -0
- package/dist/icons/DocumentIcon.js +17 -0
- package/dist/icons/DocumentIcon.mjs +17 -0
- package/dist/icons/HelpIcon.d.mts +4 -0
- package/dist/icons/HelpIcon.d.ts +4 -0
- package/dist/icons/HelpIcon.js +17 -0
- package/dist/icons/HelpIcon.mjs +17 -0
- package/dist/icons/InfoIcon.d.mts +4 -0
- package/dist/icons/InfoIcon.d.ts +4 -0
- package/dist/icons/InfoIcon.js +17 -0
- package/dist/icons/InfoIcon.mjs +17 -0
- package/dist/icons/MailIcon.d.mts +4 -0
- package/dist/icons/MailIcon.d.ts +4 -0
- package/dist/icons/MailIcon.js +17 -0
- package/dist/icons/MailIcon.mjs +17 -0
- package/dist/icons/PhoneIcon.d.mts +4 -0
- package/dist/icons/PhoneIcon.d.ts +4 -0
- package/dist/icons/PhoneIcon.js +21 -0
- package/dist/icons/PhoneIcon.mjs +21 -0
- package/dist/icons/PlusIcon.d.mts +4 -0
- package/dist/icons/PlusIcon.d.ts +4 -0
- package/dist/icons/PlusIcon.js +17 -0
- package/dist/icons/PlusIcon.mjs +17 -0
- package/dist/icons/UndoIcon.d.mts +4 -0
- package/dist/icons/UndoIcon.d.ts +4 -0
- package/dist/icons/UndoIcon.js +21 -0
- package/dist/icons/UndoIcon.mjs +21 -0
- package/dist/icons/types.d.mts +3 -0
- package/dist/icons/types.d.ts +3 -0
- package/dist/index.d.mts +25 -8
- package/dist/index.d.ts +25 -9
- package/dist/index.js +50 -1
- package/dist/index.mjs +24 -1
- package/dist/store/store.d.mts +63 -0
- package/dist/store/store.d.ts +63 -0
- package/dist/store/store.js +104 -0
- package/dist/store/store.mjs +102 -0
- package/dist/store/usePillQueue.d.mts +6 -0
- package/dist/store/usePillQueue.d.ts +6 -0
- package/dist/store/usePillQueue.js +17 -0
- package/dist/store/usePillQueue.mjs +17 -0
- package/dist/styles.css +456 -0
- package/dist/utils/react.js +23 -0
- package/dist/utils/react.mjs +21 -0
- package/dist/utils/url.js +22 -0
- package/dist/utils/url.mjs +22 -0
- package/package.json +7 -4
- package/dist/MagicPill.d.mts +0 -6
- package/dist/MagicPill.d.ts +0 -7
- package/dist/MagicPill.js +0 -2
- package/dist/MagicPill.js.map +0 -1
- package/dist/MagicPill.mjs +0 -2
- package/dist/MagicPill.mjs.map +0 -1
- package/dist/Navbar/Navbar.js +0 -2
- package/dist/Navbar/Navbar.js.map +0 -1
- package/dist/Navbar/Navbar.mjs +0 -2
- package/dist/Navbar/Navbar.mjs.map +0 -1
- package/dist/Notification/Notification.js +0 -2
- package/dist/Notification/Notification.js.map +0 -1
- package/dist/Notification/Notification.mjs +0 -2
- package/dist/Notification/Notification.mjs.map +0 -1
- package/dist/defaultPillData.d.mts +0 -2
- package/dist/defaultPillData.d.ts +0 -3
- package/dist/defaultPillData.js +0 -7
- package/dist/defaultPillData.js.map +0 -1
- package/dist/defaultPillData.mjs +0 -7
- package/dist/defaultPillData.mjs.map +0 -1
- package/dist/icons/arrow_label.js +0 -2
- package/dist/icons/arrow_label.js.map +0 -1
- package/dist/icons/arrow_label.mjs +0 -2
- package/dist/icons/arrow_label.mjs.map +0 -1
- package/dist/icons/box_label.js +0 -2
- package/dist/icons/box_label.js.map +0 -1
- package/dist/icons/box_label.mjs +0 -2
- package/dist/icons/box_label.mjs.map +0 -1
- package/dist/icons/checkmark.js +0 -2
- package/dist/icons/checkmark.js.map +0 -1
- package/dist/icons/checkmark.mjs +0 -2
- package/dist/icons/checkmark.mjs.map +0 -1
- package/dist/icons/checkmark_label.js +0 -2
- package/dist/icons/checkmark_label.js.map +0 -1
- package/dist/icons/checkmark_label.mjs +0 -2
- package/dist/icons/checkmark_label.mjs.map +0 -1
- package/dist/icons/coupon.js +0 -2
- package/dist/icons/coupon.js.map +0 -1
- package/dist/icons/coupon.mjs +0 -2
- package/dist/icons/coupon.mjs.map +0 -1
- package/dist/icons/cross_label.js +0 -2
- package/dist/icons/cross_label.js.map +0 -1
- package/dist/icons/cross_label.mjs +0 -2
- package/dist/icons/cross_label.mjs.map +0 -1
- package/dist/icons/info.js +0 -2
- package/dist/icons/info.js.map +0 -1
- package/dist/icons/info.mjs +0 -2
- package/dist/icons/info.mjs.map +0 -1
- package/dist/icons/info_label.js +0 -2
- package/dist/icons/info_label.js.map +0 -1
- package/dist/icons/info_label.mjs +0 -2
- package/dist/icons/info_label.mjs.map +0 -1
- package/dist/icons/mail_label.js +0 -2
- package/dist/icons/mail_label.js.map +0 -1
- package/dist/icons/mail_label.mjs +0 -2
- package/dist/icons/mail_label.mjs.map +0 -1
- package/dist/icons/plus_label.js +0 -2
- package/dist/icons/plus_label.js.map +0 -1
- package/dist/icons/plus_label.mjs +0 -2
- package/dist/icons/plus_label.mjs.map +0 -1
- package/dist/icons/questionmark.js +0 -2
- package/dist/icons/questionmark.js.map +0 -1
- package/dist/icons/questionmark.mjs +0 -2
- package/dist/icons/questionmark.mjs.map +0 -1
- package/dist/icons/quotation_label.js +0 -2
- package/dist/icons/quotation_label.js.map +0 -1
- package/dist/icons/quotation_label.mjs +0 -2
- package/dist/icons/quotation_label.mjs.map +0 -1
- package/dist/icons/tel_label.js +0 -2
- package/dist/icons/tel_label.js.map +0 -1
- package/dist/icons/tel_label.mjs +0 -2
- package/dist/icons/tel_label.mjs.map +0 -1
- package/dist/icons/undo_label.js +0 -2
- package/dist/icons/undo_label.js.map +0 -1
- package/dist/icons/undo_label.mjs +0 -2
- package/dist/icons/undo_label.mjs.map +0 -1
- package/dist/magicpill.css +0 -315
- package/dist/types/index.d.ts +0 -4
- package/dist/types/magicPill.d.mts +0 -50
- package/dist/types/magicPill.d.ts +0 -50
- package/dist/types/navbar.d.mts +0 -27
- package/dist/types/navbar.d.ts +0 -28
- package/dist/types/notification.d.mts +0 -140
- package/dist/types/notification.d.ts +0 -141
- package/dist/utils/handlers.d.mts +0 -20
- package/dist/utils/handlers.d.ts +0 -21
- package/dist/utils/handlers.js +0 -2
- package/dist/utils/handlers.js.map +0 -1
- package/dist/utils/handlers.mjs +0 -2
- package/dist/utils/handlers.mjs.map +0 -1
- package/dist/utils/icons.d.mts +0 -20
- package/dist/utils/icons.d.ts +0 -20
- package/dist/utils/icons.js +0 -2
- package/dist/utils/icons.js.map +0 -1
- package/dist/utils/icons.mjs +0 -2
- package/dist/utils/icons.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
# Magic Pill
|
|
2
2
|
Let it empower you.
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
Magic Pill 8.x is usable and maintained: it gets bug fixes and security updates.
|
|
6
|
-
Please keep in mind that its API isn't final yet, though. The next major version will rework it (with breaking changes), and the documentation is still being improved.
|
|
4
|
+
> **Status:** Magic Pill 9 is the first version with a **stable, documented API**. While it's in pre-release, install it with `npm install magic-pill@next`. The `latest` tag is still 8.1.0, documented in the [8.1.0 README](https://github.com/CedricTheveneau/Magic-Pill/tree/v8.1.0#readme). Coming from 8.x? Read the [migration guide](./MIGRATING.md).
|
|
7
5
|
|
|
8
6
|
#### Thank you !!
|
|
9
7
|
Thank you for your patience and to all people who already have shown interest in Magic Pill, it's my very first npm package and I couldn't ask for a better reception of my package after only a few days after its initial release.
|
|
@@ -14,296 +12,352 @@ Thank you for your patience and to all people who already have shown interest in
|
|
|
14
12
|
[](https://deepscan.io/dashboard#view=project&tid=23515&pid=28307&bid=911491)
|
|
15
13
|
[](https://snyk.io/test/github/CedricTheveneau/Magic-Pill)
|
|
16
14
|
[](https://packagephobia.com/result?p=magic-pill@latest)
|
|
17
|
-
[<svg width=20 xmlns="http://www.w3.org/2000/svg" viewBox="0 0 98 96"><path d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z"></path></svg>](https://github.com/CedricTheveneau/Magic-Pill)
|
|
18
|
-
[<svg width=20 xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M0 7.334v8h6.666v1.332H12v-1.332h12v-8zm6.666 6.664H5.334v-4H3.999v4H1.335V8.667h5.331zm4 0v1.336H8.001V8.667h5.334v5.332zm12.001 0h-1.33v-4h-1.336v4h-1.335v-4h-1.33v4h-2.671V8.667h8.002z"></path><path d="M10.665 10H12v2.667h-1.335z"></path></svg>](https://www.npmjs.com/package/magic-pill)
|
|
19
15
|
|
|
20
16
|
## Introduction
|
|
21
|
-
Magic Pill is a
|
|
17
|
+
Magic Pill is **a design primitive, not just a notification library.** At its core is *the pill*: a dark, fully rounded, elevated container with a strong design language, and you can put anything inside it.
|
|
18
|
+
|
|
19
|
+
On top of it come ready-made **templates** (a notification, a navbar) and a **controller** you can call from anywhere: `pill().notify(...)`. Use one pill as a discreet assistant at the bottom of the page, or several pills together (a sidebar, a navbar, a modal) to build a whole interface.
|
|
22
20
|
|
|
23
21
|
## Features
|
|
24
|
-
- [X]
|
|
25
|
-
- [X]
|
|
26
|
-
- [X]
|
|
27
|
-
- [X]
|
|
28
|
-
- [X]
|
|
29
|
-
- [X]
|
|
30
|
-
- [X]
|
|
31
|
-
- [X]
|
|
22
|
+
- [X] Two lines to get started: mount `<MagicPill />`, then call `pill().notify(...)` from anywhere, even outside React
|
|
23
|
+
- [X] A notification queue on top of a **base layer** (e.g. a navbar): notifications take the pill over, then hand it back
|
|
24
|
+
- [X] Several named pills on one page, at any edge or corner, inline, or as a modal
|
|
25
|
+
- [X] Anything inside: messages, icons and panels are React content; build your own templates from the same parts
|
|
26
|
+
- [X] Shape-changing animations (circle ↔ pill ↔ panel) that respect reduced motion
|
|
27
|
+
- [X] Accessible: screen reader announcements, keyboard focus pauses the countdown, focus handling for panels and modals
|
|
28
|
+
- [X] Works in any React 18+ app, including React Server Components (Next.js App Router), with any router
|
|
29
|
+
- [X] Styles you can override without `!important`, design tokens, no fonts loaded behind your back
|
|
30
|
+
- [X] Tiny: the quick start adds about **7 kB gzipped** to your app (4.9 kB of JavaScript + 2.2 kB of CSS, minified by your bundler), checked in CI. Unused parts are tree-shaken.
|
|
31
|
+
- [X] Written in TypeScript, zero runtime dependencies
|
|
32
32
|
|
|
33
33
|
## Installation
|
|
34
|
-
### npm
|
|
35
|
-
Magic Pill is available on npm, simply use the following command and you're ready to go!
|
|
36
34
|
```bash
|
|
37
|
-
npm install magic-pill
|
|
35
|
+
npm install magic-pill@next
|
|
38
36
|
```
|
|
39
37
|
|
|
40
38
|
### Requirements
|
|
41
39
|
- **React and React DOM 18 or later** (peer dependencies).
|
|
42
|
-
- **Next.js is optional**: Magic Pill works in any React app.
|
|
40
|
+
- **Next.js is optional**: Magic Pill works in any React app (Vite, React Router, Remix…).
|
|
43
41
|
- **TypeScript is optional**: the type definitions ship with the package.
|
|
42
|
+
- Browsers from 2023 onwards (the stylesheet uses CSS nesting and cascade layers).
|
|
43
|
+
|
|
44
|
+
> **No CDN build:** Magic Pill is published for bundlers (Next.js, Vite, webpack…) as ES modules and CommonJS. It has no UMD build, so it can't be loaded with a plain `<script>` tag yet.
|
|
45
|
+
|
|
46
|
+
## Quick start
|
|
47
|
+
**1. Import the stylesheet and mount the pill once**, e.g. in your root layout:
|
|
48
|
+
```tsx
|
|
49
|
+
// app/layout.tsx (Next.js App Router: this can stay a Server Component)
|
|
50
|
+
import "magic-pill/styles.css";
|
|
51
|
+
import { MagicPill } from "magic-pill";
|
|
52
|
+
|
|
53
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
54
|
+
return (
|
|
55
|
+
<html lang="en">
|
|
56
|
+
<body>
|
|
57
|
+
{children}
|
|
58
|
+
<MagicPill />
|
|
59
|
+
</body>
|
|
60
|
+
</html>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**2. Notify from anywhere** in your client code: an event handler, a store, a fetch callback…
|
|
66
|
+
```tsx
|
|
67
|
+
import { pill, CheckIcon } from "magic-pill";
|
|
44
68
|
|
|
45
|
-
|
|
69
|
+
pill().notify({ icon: <CheckIcon />, message: "Saved!", variant: "success" });
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
That's it. The pill pops in at the bottom of the page, stays for 5 seconds, then goes away.
|
|
73
|
+
|
|
74
|
+
### Fonts
|
|
75
|
+
Magic Pill loads no fonts: it uses your page's fonts. For the signature look, an italic serif for messages (**Overlock**) and a bold sans for action labels (**Lexend**), point the two font tokens at them.
|
|
76
|
+
|
|
77
|
+
With `next/font`, the `variable` option sets the tokens directly:
|
|
78
|
+
```tsx
|
|
79
|
+
// app/layout.tsx
|
|
80
|
+
import { Lexend, Overlock } from "next/font/google";
|
|
81
|
+
|
|
82
|
+
const overlock = Overlock({ subsets: ["latin"], style: "italic", weight: ["400", "700", "900"], variable: "--mp-font-text" });
|
|
83
|
+
const lexend = Lexend({ subsets: ["latin"], weight: "700", variable: "--mp-font-accent" });
|
|
46
84
|
|
|
47
|
-
|
|
48
|
-
### Import styles
|
|
49
|
-
For it to work properly, you need to import styles in your app (we recommend importing it into your root layout or `App` file).
|
|
50
|
-
```typescript
|
|
51
|
-
// Import in your Next.js or React project
|
|
52
|
-
import "magic-pill/dist/magicpill.css";
|
|
53
|
-
// or, equivalently
|
|
54
|
-
import "magic-pill/magicpill.css";
|
|
85
|
+
// <html lang="en" className={`${overlock.variable} ${lexend.variable}`}>
|
|
55
86
|
```
|
|
56
87
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
> `MagicPill` is a Client Component, so you can also render it directly from a Server Component. The Context below is what lets any part of your app change its content.
|
|
63
|
-
|
|
64
|
-
1. First, create a context for Magic Pill (e.g., in `app/contexts/magicPillContext.tsx`):
|
|
65
|
-
```typescript
|
|
66
|
-
"use client"
|
|
67
|
-
import React, { createContext, useContext, useState } from "react";
|
|
68
|
-
import { MagicPillType, Mode, NotificationIcon, CTAIcon, MagicPillNavbarItem } from "magic-pill";
|
|
69
|
-
import { updateMode, updateNotificationIcon, updateNotificationMessage, updateCTAIcon, updateCTALabel, updateCTALink, updateInfoTitle, updateInfoContent, updateInfoCloseLabel, updateNavItemLabel, updateNavItemLink, updateNavItemIcon, addNavItem, deleteNavItem } from "magic-pill";
|
|
70
|
-
|
|
71
|
-
const defaultPillData: MagicPillType = {
|
|
72
|
-
mode: "navbar",
|
|
73
|
-
navbar: {
|
|
74
|
-
navItems: [
|
|
75
|
-
{
|
|
76
|
-
icon: "arrow",
|
|
77
|
-
label: "mp-test",
|
|
78
|
-
link: "/mp-test"
|
|
79
|
-
},
|
|
80
|
-
{
|
|
81
|
-
icon: "cross",
|
|
82
|
-
label: "demo",
|
|
83
|
-
link: "/demo"
|
|
84
|
-
}
|
|
85
|
-
]
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
interface MagicPillContextType {
|
|
90
|
-
pillData: MagicPillType;
|
|
91
|
-
setPillData: (data: MagicPillType) => void;
|
|
92
|
-
// Direct data manipulation methods
|
|
93
|
-
updateMode: (mode: Mode) => void;
|
|
94
|
-
updateNotificationIcon: (icon: NotificationIcon) => void;
|
|
95
|
-
updateNotificationMessage: (message: string) => void;
|
|
96
|
-
updateCTAIcon: (icon: CTAIcon) => void;
|
|
97
|
-
updateCTALabel: (label: string) => void;
|
|
98
|
-
updateCTALink: (link: string) => void;
|
|
99
|
-
updateInfoTitle: (title: string) => void;
|
|
100
|
-
updateInfoContent: (content: string) => void;
|
|
101
|
-
updateInfoCloseLabel: (closeLabel: string) => void;
|
|
102
|
-
updateNavItemLabel: (label: string, index: number) => void;
|
|
103
|
-
updateNavItemLink: (link: string, index: number) => void;
|
|
104
|
-
updateNavItemIcon: (icon: CTAIcon, index: number) => void;
|
|
105
|
-
addNavItem: (navItem: MagicPillNavbarItem) => void;
|
|
106
|
-
deleteNavItem: (index: number) => void;
|
|
88
|
+
Without Next.js, load the fonts however you like (self-hosted is best for privacy), then set the tokens:
|
|
89
|
+
```css
|
|
90
|
+
:root {
|
|
91
|
+
--mp-font-text: "Overlock", serif;
|
|
92
|
+
--mp-font-accent: "Lexend", sans-serif;
|
|
107
93
|
}
|
|
94
|
+
```
|
|
108
95
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
updateInfoContent: () => {},
|
|
120
|
-
updateInfoCloseLabel: () => {},
|
|
121
|
-
updateNavItemLabel: () => {},
|
|
122
|
-
updateNavItemLink: () => {},
|
|
123
|
-
updateNavItemIcon: () => {},
|
|
124
|
-
addNavItem: () => {},
|
|
125
|
-
deleteNavItem: () => {},
|
|
96
|
+
## Notifications
|
|
97
|
+
```tsx
|
|
98
|
+
import { pill, CheckIcon, UndoIcon } from "magic-pill";
|
|
99
|
+
|
|
100
|
+
const id = pill().notify({
|
|
101
|
+
variant: "success",
|
|
102
|
+
icon: <CheckIcon />,
|
|
103
|
+
message: "Item successfully removed from cart!",
|
|
104
|
+
action: { icon: <UndoIcon />, label: "Undo", onClick: restoreItem },
|
|
105
|
+
duration: 8000,
|
|
126
106
|
});
|
|
107
|
+
```
|
|
127
108
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
deleteNavItem: (index: number) => deleteNavItem(index, setPillData),
|
|
155
|
-
};
|
|
109
|
+
| Option | Type | Notes |
|
|
110
|
+
|---|---|---|
|
|
111
|
+
| `message` | `ReactNode` | Required (unless you use `render`). Any content, e.g. `<>Total: <strong>768.99$</strong></>`. Never parsed as HTML. |
|
|
112
|
+
| `icon` | `ReactNode` | The leading icon, followed by the divider. |
|
|
113
|
+
| `action` | `{ icon?, label, onClick }` or `{ icon?, label, href }` | The round button. Its label shows on hover and focus. Clicking it dismisses the notification; call `event.preventDefault()` in `onClick` to keep it open. |
|
|
114
|
+
| `panel` | `{ title?, content, trigger?, closeLabel? }` | A "Learn more" action that grows the pill into a panel. `trigger` defaults to `{ icon: <PlusIcon />, label: "Learn more" }`, `closeLabel` to `"Close"`. |
|
|
115
|
+
| `variant` | `"neutral"` · `"success"` · `"danger"` · any string | Defaults to the host's variant. |
|
|
116
|
+
| `duration` | `number` (ms) | Defaults to the host's (5000). `Infinity` keeps it until dismissed. |
|
|
117
|
+
| `id` | `string` | Notifying again with the same id updates the notification in place. |
|
|
118
|
+
| `announce` | `string` · `false` | What screen readers hear. Defaults to the message's text. |
|
|
119
|
+
| `onDismiss` | `(id, reason) => void` | `reason` is `"timeout"`, `"action"`, `"dismissed"` or `"cleared"`. |
|
|
120
|
+
| `render` | `({ id, dismiss }) => ReactNode` | Your own content instead of the template, with the same queue, timing and animation. See [Your own templates](#your-own-templates). |
|
|
121
|
+
|
|
122
|
+
**How it behaves:**
|
|
123
|
+
- Notifications queue up and show one at a time, first in, first out.
|
|
124
|
+
- The countdown starts once the pill is fully shown. It pauses while the pill is hovered, holds keyboard focus or has its panel open, or while the browser tab is hidden, and it starts again from the full duration afterwards.
|
|
125
|
+
- Calls made before `<MagicPill>` mounts are kept, and shown once it mounts.
|
|
126
|
+
- `pill()` is for client code. Called on the server, it does nothing (it warns in development), so visitors never see each other's notifications.
|
|
127
|
+
|
|
128
|
+
**The controller:**
|
|
129
|
+
```ts
|
|
130
|
+
const id = pill().notify({ message: "Saving…", duration: Infinity });
|
|
131
|
+
pill().update(id, { message: "Saved", variant: "success", duration: 3000 }); // in place, countdown restarts
|
|
132
|
+
pill().dismiss(id); // or pill().dismiss() for the visible one
|
|
133
|
+
pill().clear(); // empty the queue
|
|
134
|
+
```
|
|
156
135
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
136
|
+
**More examples**, the templates from the design:
|
|
137
|
+
```tsx
|
|
138
|
+
// A question: the action is an answer
|
|
139
|
+
pill().notify({ icon: <HelpIcon />, message: "Do you need some help?", action: { icon: <CheckIcon />, label: "Yes", onClick: openHelp } });
|
|
140
|
+
|
|
141
|
+
// Live data, and a link (rendered with your router's link component, see below)
|
|
142
|
+
pill().notify({
|
|
143
|
+
icon: <InfoIcon />,
|
|
144
|
+
message: <>Your current cart total is: <strong>{total}$</strong></>,
|
|
145
|
+
action: { icon: <CheckIcon />, label: "Checkout", href: "/checkout" },
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// A promotion that stays open while it works
|
|
149
|
+
pill().notify({
|
|
150
|
+
icon: <CouponIcon />,
|
|
151
|
+
message: "Interested in 10% off your next purchase?",
|
|
152
|
+
action: {
|
|
153
|
+
icon: <ArrowIcon />,
|
|
154
|
+
label: "Activate the promo code",
|
|
155
|
+
onClick: (event) => {
|
|
156
|
+
event.preventDefault();
|
|
157
|
+
activatePromo().then(() => pill().dismiss());
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// An expandable panel
|
|
163
|
+
pill().notify({
|
|
164
|
+
icon: <InfoIcon />,
|
|
165
|
+
message: "Discover the powers Magic Pill gives you!",
|
|
166
|
+
panel: { title: "Discover the powers Magic Pill gives you!", content: <p>Anything can go in a panel.</p> },
|
|
167
|
+
duration: Infinity,
|
|
168
|
+
});
|
|
163
169
|
```
|
|
164
170
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
import "magic-pill/dist/magicpill.css";
|
|
168
|
-
import { MagicPillProvider } from "./contexts/magicPillContext";
|
|
169
|
-
import { MagicPillInitializer } from "./contexts/magicPillInitializer";
|
|
171
|
+
## The host: `<MagicPill>`
|
|
172
|
+
`<MagicPill>` is a pill plus its queue. It takes every [`<Pill>` prop](#the-primitive) except `presence`, and adds:
|
|
170
173
|
|
|
171
|
-
|
|
174
|
+
| Prop | Default | Notes |
|
|
175
|
+
|---|---|---|
|
|
176
|
+
| `name` | `"main"` | Which `pill(name)` controls it. Mount one host per name. |
|
|
177
|
+
| `children` | none | The **base layer**: shown whenever no notification is. Without it, the pill hides between notifications. |
|
|
178
|
+
| `duration` | `5000` | Default display time for notifications. |
|
|
179
|
+
| `idle` | `"hidden"` | With no base layer and nothing queued: `"hidden"`, or `"rest"` for a small resting circle showing `idleIcon`. |
|
|
180
|
+
| `idleIcon` | none | The icon of the resting circle. |
|
|
181
|
+
| `linkComponent` | `<a>` | Renders notification actions that have an `href`, e.g. Next's `Link`. |
|
|
182
|
+
| `classNames` | none | Classes for the notification's parts: `icon`, `divider`, `message`, `actions`, `action`, `panel`. |
|
|
183
|
+
| `placement` | `"bottom"` | See [placements](#the-primitive). |
|
|
172
184
|
|
|
185
|
+
### A navbar as the base layer
|
|
186
|
+
Notifications take the pill over, then it goes back to the navbar. The navbar works with any router: give it the current `pathname` and your link component.
|
|
187
|
+
|
|
188
|
+
```tsx
|
|
189
|
+
// app/app-pill.tsx
|
|
190
|
+
"use client";
|
|
191
|
+
import Link from "next/link";
|
|
192
|
+
import { usePathname } from "next/navigation";
|
|
193
|
+
import { MagicPill, PillNavbar, BoxIcon, DocumentIcon, MailIcon } from "magic-pill";
|
|
194
|
+
|
|
195
|
+
const items = [
|
|
196
|
+
{ href: "/", label: "Home", icon: <BoxIcon /> },
|
|
197
|
+
{ href: "/blog", label: "Blog", icon: <DocumentIcon /> },
|
|
198
|
+
{ href: "/contact", label: "Contact", icon: <MailIcon /> },
|
|
199
|
+
];
|
|
200
|
+
|
|
201
|
+
export function AppPill() {
|
|
173
202
|
return (
|
|
174
|
-
<
|
|
175
|
-
<
|
|
176
|
-
|
|
177
|
-
{children}
|
|
178
|
-
<MagicPillInitializer />
|
|
179
|
-
</MagicPillProvider>
|
|
180
|
-
</body>
|
|
181
|
-
</html>
|
|
203
|
+
<MagicPill linkComponent={Link}>
|
|
204
|
+
<PillNavbar items={items} pathname={usePathname()} linkComponent={Link} />
|
|
205
|
+
</MagicPill>
|
|
182
206
|
);
|
|
183
207
|
}
|
|
184
208
|
```
|
|
185
209
|
|
|
186
|
-
|
|
210
|
+
- **Matching:** an item is active on its own path and below it, on whole segments: `/blog` is active on `/blog/my-post`, not on `/blogroll`. `/` is only active on `/`. When several items match, the longest `href` wins. Override with `match: "exact"` or `match: (pathname) => boolean`.
|
|
211
|
+
- **The active link** shows its label and gets `aria-current="page"`.
|
|
212
|
+
- **Without `pathname`**, the navbar reads `window.location.pathname` after mounting and on back/forward navigation. That misses client-side navigations, so pass `pathname` when you use a client-side router.
|
|
213
|
+
- **`aria-label`** defaults to `"Main"`.
|
|
214
|
+
|
|
215
|
+
**React Router:** its `Link` takes `to` instead of `href`. Wrap it once:
|
|
187
216
|
```tsx
|
|
188
|
-
|
|
189
|
-
import {
|
|
190
|
-
import {
|
|
217
|
+
import { forwardRef, type AnchorHTMLAttributes } from "react";
|
|
218
|
+
import { Link, useLocation } from "react-router";
|
|
219
|
+
import type { LinkComponent } from "magic-pill";
|
|
191
220
|
|
|
192
|
-
export const
|
|
193
|
-
|
|
221
|
+
export const RouterLink: LinkComponent = forwardRef<HTMLAnchorElement, AnchorHTMLAttributes<HTMLAnchorElement> & { href: string }>(
|
|
222
|
+
function RouterLink({ href, ...props }, ref) {
|
|
223
|
+
return <Link to={href} {...props} ref={ref} />;
|
|
224
|
+
},
|
|
225
|
+
);
|
|
194
226
|
|
|
195
|
-
|
|
196
|
-
<MagicPill pillData={pillData}/>
|
|
197
|
-
)
|
|
198
|
-
};
|
|
227
|
+
// <PillNavbar items={items} pathname={useLocation().pathname} linkComponent={RouterLink} />
|
|
199
228
|
```
|
|
200
|
-
> **🎉 | At this point, if you launch you app, Magic Pill should appear in your view**
|
|
201
229
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
230
|
+
### Several pills
|
|
231
|
+
Give each host a name, and target it with `pill(name)`:
|
|
232
|
+
```tsx
|
|
233
|
+
<MagicPill name="sidebar" placement="left">
|
|
234
|
+
<PillNavbar items={items} pathname={pathname} linkComponent={Link} />
|
|
235
|
+
</MagicPill>
|
|
236
|
+
<MagicPill name="assistant" placement="bottom-end" idle="rest" idleIcon={<HelpIcon />} />
|
|
237
|
+
```
|
|
238
|
+
```ts
|
|
239
|
+
pill("sidebar").notify({ message: "3 new posts" });
|
|
240
|
+
pill("assistant").notify({ icon: <HelpIcon />, message: "Need a hand?", duration: Infinity });
|
|
241
|
+
```
|
|
207
242
|
|
|
208
|
-
|
|
209
|
-
|
|
243
|
+
## The primitive
|
|
244
|
+
Everything above is built from `<Pill>` and its parts, and you can use them directly. A `<Pill>` you write yourself isn't connected to `pill()`: you control it with props.
|
|
210
245
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
246
|
+
```tsx
|
|
247
|
+
import { Pill, PillActions, PillAction, PhoneIcon, MailIcon, DocumentIcon } from "magic-pill";
|
|
248
|
+
|
|
249
|
+
// The toolbox from the design: only actions
|
|
250
|
+
<Pill placement="bottom" aria-label="Contact us">
|
|
251
|
+
<PillActions>
|
|
252
|
+
<PillAction icon={<PhoneIcon />} label="Call us" href="tel:+33100000000" />
|
|
253
|
+
<PillAction icon={<MailIcon />} label="Send an email" href="mailto:hello@example.com" />
|
|
254
|
+
<PillAction icon={<DocumentIcon />} label="Ask for a quotation" href="/quote" linkComponent={Link} />
|
|
255
|
+
</PillActions>
|
|
256
|
+
</Pill>
|
|
257
|
+
```
|
|
214
258
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
259
|
+
| `<Pill>` prop | Values | Default |
|
|
260
|
+
|---|---|---|
|
|
261
|
+
| `placement` | `"inline"` (in the page flow), `"top"`, `"bottom"`, `"left"`, `"right"`, `"top-start"`, `"top-end"`, `"bottom-start"`, `"bottom-end"` (fixed; `start`/`end` follow the text direction), `"overlay"` (a modal `<dialog>`) | `"inline"` |
|
|
262
|
+
| `presence` / `defaultPresence` / `onPresenceChange` | `"hidden"`, `"rest"` (a circle with the icon only), `"compact"`, `"expanded"` (every action label shown), `"panel"` | `"compact"` |
|
|
263
|
+
| `orientation` | `"horizontal"`, `"vertical"` | `"vertical"` for `left`/`right` |
|
|
264
|
+
| `variant` | `"neutral"`, `"success"`, `"danger"`, or any string (see [Styling](#styling)) | `"neutral"` |
|
|
265
|
+
| `unstyled` | `boolean`: keep the behaviour, drop the look | `false` |
|
|
266
|
+
|
|
267
|
+
Any other prop (`className`, `aria-label`, `ref`…) goes to the element.
|
|
268
|
+
|
|
269
|
+
**The parts:**
|
|
270
|
+
- `PillIcon`: the leading icon. Decorative unless you give it a `label`.
|
|
271
|
+
- `PillDivider`: the thin line after the icon.
|
|
272
|
+
- `PillMessage`: the message, in the italic serif.
|
|
273
|
+
- `PillActions` and `PillAction`: round buttons. `label` is required: it's the accessible name, and it shows on hover, on focus, with `expanded`, or at the `expanded` level. A `PillAction` is a `<button>` with `onClick`, or a link with `href` (through `linkComponent` if you pass one).
|
|
274
|
+
- `PillPanel` and `PillPanelTrigger`: content shown at the `panel` level, and the action that opens it. The panel takes the focus when it opens, closes on Escape or with its close button, and gives the focus back to the trigger. One panel per pill.
|
|
275
|
+
|
|
276
|
+
**A modal:** `placement="overlay"` renders a native modal `<dialog>`, which brings a focus trap, Escape and an inert page with it.
|
|
277
|
+
```tsx
|
|
278
|
+
<Pill placement="overlay" variant="danger" presence={open ? "compact" : "hidden"} onPresenceChange={(p) => setOpen(p !== "hidden")}>
|
|
279
|
+
<PillMessage>Delete this item?</PillMessage>
|
|
280
|
+
<PillActions>
|
|
281
|
+
<PillAction icon={<CheckIcon />} label="Delete" expanded onClick={confirmDelete} />
|
|
282
|
+
<PillAction icon={<CloseIcon />} label="Cancel" onClick={() => setOpen(false)} />
|
|
283
|
+
</PillActions>
|
|
284
|
+
</Pill>
|
|
219
285
|
```
|
|
220
|
-
> **It really is as simple as that, to use Magic Pill, once the setup is done**
|
|
221
|
-
|
|
222
|
-
A notification appears as a small pill, expands after 2.5 s, and hides itself 5 s later (it stays open while hovered or focused). Setting a notification with different content shows the pill again.
|
|
223
|
-
|
|
224
|
-
### Update Magic Pill's content granularly
|
|
225
|
-
Magic Pill, right from the start, has been thought out to be as highly customizable as possible.
|
|
226
|
-
To help you use it in the best way possible, a collection of handlers is part of the package.
|
|
227
|
-
|
|
228
|
-
- `updateMode` - Change Magic Pill's mode between "notification" and "navbar"
|
|
229
|
-
- `updateNotificationIcon` - Change the main notification icon
|
|
230
|
-
- `updateNotificationMessage` - Change the main notification message
|
|
231
|
-
- `updateCTAIcon` - Change the notification's CTA (Call To Action) icon
|
|
232
|
-
- `updateCTALabel` - Change the notification's CTA (Call To Action) label
|
|
233
|
-
- `updateCTALink` - Change the notification's CTA (Call To Action) redirection link
|
|
234
|
-
- `updateInfoTitle` - Change the notification's expanded info title
|
|
235
|
-
- `updateInfoContent` - Change the notification's expanded info main content
|
|
236
|
-
- `updateInfoCloseLabel` - Change the notification's expanded info close button's label
|
|
237
|
-
- `updateNavItemLabel` - Change a navbar item's label
|
|
238
|
-
- `updateNavItemLink` - Change a navbar item's link
|
|
239
|
-
- `updateNavItemIcon` - Change a navbar item's icon
|
|
240
|
-
- `handleActiveNavItem` - Automatically working handler directly used by the navbar to update the active link
|
|
241
|
-
- `addNavItem` - Add an item to the navbar
|
|
242
|
-
- `deleteNavItem` - Delete an item from the navbar
|
|
243
|
-
|
|
244
|
-
### ⚠️ Security: `info.content` is raw HTML
|
|
245
|
-
The notification's expanded info content (`notification.info.content`, or `updateInfoContent`) is rendered as **raw HTML**, without any sanitising, so that you can format it.
|
|
246
|
-
|
|
247
|
-
**Never put user input or any other untrusted text in it** (form values, URL parameters, data from an API or a database that users can write to…). Doing so lets anyone inject scripts into your page (XSS).
|
|
248
|
-
- Only pass HTML that you wrote yourself.
|
|
249
|
-
- If you really need to display external text there, sanitise it first with a dedicated library such as [DOMPurify](https://github.com/cure53/DOMPurify).
|
|
250
|
-
|
|
251
|
-
The message, the info title and the labels are rendered as plain text, so they're safe. Links (`cta.link`, navbar `link`) go into an `href`: if they can come from users, only allow `http(s)` or relative URLs, because React 18 doesn't block `javascript:` URLs (React 19 does).
|
|
252
|
-
|
|
253
|
-
A future major version will replace this HTML string with regular React content.
|
|
254
|
-
|
|
255
|
-
### Available Icons
|
|
256
|
-
#### Notification Icons
|
|
257
|
-
- `"info"` - Information icon
|
|
258
|
-
- `"checkmark"` - Success/confirmation icon
|
|
259
|
-
- `"questionmark"` - Help/question icon
|
|
260
|
-
- `"coupon"` - Coupon/offer icon
|
|
261
|
-
- `""` - No icon
|
|
262
|
-
|
|
263
|
-
#### CTA (Call to Action) Icons
|
|
264
|
-
- `"arrow"` - Arrow icon
|
|
265
|
-
- `"box"` - Box icon
|
|
266
|
-
- `"checkmark"` - Checkmark icon
|
|
267
|
-
- `"cross"` - Cross/close icon
|
|
268
|
-
- `"info"` - Information icon
|
|
269
|
-
- `"mail"` - Mail/email icon
|
|
270
|
-
- `"plus"` - Plus/add icon
|
|
271
|
-
- `"quotation"` - Quote/speech icon
|
|
272
|
-
- `"tel"` - Phone/telephone icon
|
|
273
|
-
- `"undo"` - Undo/back icon
|
|
274
|
-
- `""` - No icon
|
|
275
|
-
|
|
276
|
-
### Accessibility
|
|
277
|
-
- Notifications are announced by screen readers (the pill is a polite live region).
|
|
278
|
-
- The auto-hide pauses while the pill is hovered or holds keyboard focus.
|
|
279
|
-
- With `prefers-reduced-motion: reduce`, the animations become short fades.
|
|
280
|
-
- The info toggle exposes `aria-expanded` and `aria-controls`.
|
|
281
|
-
|
|
282
|
-
### Customization
|
|
283
|
-
Magic Pill is easily customizable through CSS variables. Simply replace the theme colors or fonts by changing the values in the variables set in ":root".
|
|
284
286
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
@import url('https://fonts.googleapis.com/css2?family=Lexend:wght@100..900&family=Overlock:ital,wght@0,400;0,700;0,900;1,400;1,700;1,900&display=swap');
|
|
287
|
+
### Your own templates
|
|
288
|
+
Templates are plain arrangements of the parts, so yours work the same way. `usePill()` gives a part access to its pill (`presence`, `setPresence`, `placement`, `orientation`, `variant`). Inside a `<MagicPill>`, the `render` option puts your arrangement in the queue:
|
|
288
289
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
290
|
+
```tsx
|
|
291
|
+
pill().notify({
|
|
292
|
+
announce: "Incoming call from Alex",
|
|
293
|
+
duration: Infinity,
|
|
294
|
+
render: ({ dismiss }) => (
|
|
295
|
+
<>
|
|
296
|
+
<PillIcon><PhoneIcon /></PillIcon>
|
|
297
|
+
<PillDivider />
|
|
298
|
+
<PillMessage>Alex is calling</PillMessage>
|
|
299
|
+
<PillActions>
|
|
300
|
+
<PillAction icon={<CheckIcon />} label="Answer" onClick={() => { answer(); dismiss(); }} />
|
|
301
|
+
<PillAction icon={<CloseIcon />} label="Decline" onClick={dismiss} />
|
|
302
|
+
</PillActions>
|
|
303
|
+
</>
|
|
304
|
+
),
|
|
305
|
+
});
|
|
305
306
|
```
|
|
306
307
|
|
|
308
|
+
### React Server Components
|
|
309
|
+
- The parts, `PillAction`, `PillNotification`, the icons and `pill()` have no `"use client"`. A Server Component can render them (and pass `linkComponent={Link}`) without shipping them to the browser.
|
|
310
|
+
- `Pill`, `MagicPill`, `PillPanel`, `PillPanelTrigger`, `PillNavbar`, `usePill` and `usePillQueue` are Client Components. You can still render them from a Server Component.
|
|
311
|
+
- Call `pill()` from client code: event handlers, effects, client-side stores.
|
|
312
|
+
|
|
313
|
+
### Headless: `usePillQueue`
|
|
314
|
+
To build your own display on the same store: `const { current, queued } = usePillQueue("main");`.
|
|
315
|
+
|
|
316
|
+
## Icons
|
|
317
|
+
Twelve built-in icons, drawn in `currentColor` and decorative by default (`aria-hidden`). Any other icon or SVG works too: icons are just React content.
|
|
318
|
+
|
|
319
|
+
`ArrowIcon` · `BoxIcon` · `CheckIcon` · `CloseIcon` · `CouponIcon` · `DocumentIcon` · `HelpIcon` · `InfoIcon` · `MailIcon` · `PhoneIcon` · `PlusIcon` · `UndoIcon`
|
|
320
|
+
|
|
321
|
+
## Styling
|
|
322
|
+
- **Easy to override:** every rule sits in a `magic-pill` cascade layer. Any CSS of yours that isn't in a layer wins, whatever its specificity, with no `!important`.
|
|
323
|
+
- **No collisions:** parts are selected by `data-mp` attributes (`[data-mp="pill"]`, `"icon"`, `"divider"`, `"message"`, `"actions"`, `"action"`, `"action-body"`, `"action-icon"`, `"label"`, `"panel"`, `"panel-title"`, `"panel-body"`, `"panel-footer"`, `"navbar"`), and states by `data-presence`, `data-placement`, `data-orientation` and `data-variant`. There are no class names of ours to clash with yours.
|
|
324
|
+
- **Classes:** every part takes a `className`; templates and `<MagicPill>` take `classNames`.
|
|
325
|
+
- **Global resets are safe:** an action's look lives on its inner `[data-mp="action-body"]`, so app-wide rules such as `a { color: inherit }` or `button { background: none }` don't break it. To restyle an action, target `[data-mp="action-body"]`.
|
|
326
|
+
- **Tailwind CSS v4:** declare the layer order once, before your imports, so utilities win: `@layer theme, base, magic-pill, components, utilities;`.
|
|
327
|
+
- **Your own variants:** `variant="brand"` gives `data-variant="brand"`, so you can write `[data-variant="brand"] { --mp-surface: #4b2bd6; }`.
|
|
328
|
+
|
|
329
|
+
**Tokens:** set them on `:root`, on one pill, or anywhere in between.
|
|
330
|
+
|
|
331
|
+
| Token | Default |
|
|
332
|
+
|---|---|
|
|
333
|
+
| `--mp-surface` / `--mp-on-surface` | `#141414` / `#f9f9f9` |
|
|
334
|
+
| `--mp-action` / `--mp-on-action` | `#f9f9f9` / `#141414` |
|
|
335
|
+
| `--mp-success` / `--mp-danger` | `#3f7253` / `#c2303e` |
|
|
336
|
+
| `--mp-font-text` / `--mp-font-accent` | `inherit` (see [Fonts](#fonts)) |
|
|
337
|
+
| `--mp-text-size` / `--mp-title-size` | fluid, about 1rem / 1.5–2rem |
|
|
338
|
+
| `--mp-padding` / `--mp-gap` / `--mp-action-size` | `0.875rem` / `1rem` / `2.75rem` |
|
|
339
|
+
| `--mp-max-width` / `--mp-message-width` / `--mp-panel-width` | `48rem` / `24rem` / `32rem` |
|
|
340
|
+
| `--mp-radius-panel` / `--mp-shadow` | `2.5rem` / a soft drop shadow |
|
|
341
|
+
| `--mp-offset` / `--mp-z-index` | `1.5rem` from the viewport edges (plus safe areas) / `50` |
|
|
342
|
+
| `--mp-backdrop` | the overlay's backdrop |
|
|
343
|
+
| `--mp-duration-enter` / `--mp-duration-change` / `--mp-duration-exit` | `500ms` / `250ms` / `300ms` |
|
|
344
|
+
|
|
345
|
+
The pill fits small screens: sizes are in `rem`, fixed pills stay inside the viewport and its safe areas, and long panels scroll.
|
|
346
|
+
|
|
347
|
+
## Accessibility
|
|
348
|
+
- **Announcements:** each `<MagicPill>` has a polite live region that reads notifications out (or your `announce` text). A hidden pill leaves the accessibility tree.
|
|
349
|
+
- **Time to act:** the countdown pauses on hover and keyboard focus, and while a panel is open.
|
|
350
|
+
- **Panels and modals:** they take and give back the focus, and close on Escape. The overlay is a native modal `<dialog>`.
|
|
351
|
+
- **Accessible names:** every action has one (`label` is required), and icons are hidden from assistive technology.
|
|
352
|
+
- **Motion and contrast:** with `prefers-reduced-motion`, shape changes become short fades. In forced-colours mode, the shapes keep visible outlines.
|
|
353
|
+
|
|
354
|
+
## Security
|
|
355
|
+
- **No HTML strings:** contents are React nodes and never parsed as HTML, so text from your users can't inject markup.
|
|
356
|
+
- **Safe links:** links only render relative, `http(s):`, `mailto:` and `tel:` URLs. Anything else, such as `javascript:`, renders without a link (with a warning in development). React 18 doesn't block those URLs by itself.
|
|
357
|
+
|
|
358
|
+
## TypeScript
|
|
359
|
+
Everything is typed. The main types are `NotifyOptions`, `NotifyInput`, `PillController`, `MagicPillProps`, `PillProps`, `PillPresence`, `PillPlacement`, `PillVariant`, `NavItem` and `LinkComponent`.
|
|
360
|
+
|
|
307
361
|
## Build your own
|
|
308
362
|
Maybe what I brought to the table wasn't enough but you like the idea behind it, that's why Magic Pill is under [MIT License](https://www.tldrlegal.com/license/mit-license), which means you can pretty much do whatever you want and build upon my own version of Magic Pill to bring it to new heights or just make it your own.
|
|
309
363
|
|
|
@@ -324,6 +378,7 @@ npm install
|
|
|
324
378
|
npm run typecheck # TypeScript
|
|
325
379
|
npm test # Vitest test suite
|
|
326
380
|
npm run build # Builds dist/ with tsdown
|
|
381
|
+
npm run size # What an app ships, against the size budget (after a build)
|
|
327
382
|
```
|
|
328
383
|
|
|
329
384
|
4. You're good to go! For easier development, we recommend creating a Next.js app and creating a symbolic link between the app and the package. Now, when you're making changes to the code in src/, the changes should be reflected in your Next.js app.
|
|
@@ -339,16 +394,16 @@ npm link magic-pill
|
|
|
339
394
|
```
|
|
340
395
|
|
|
341
396
|
## Troubleshooting / Contact
|
|
342
|
-
|
|
343
|
-
You can send me emails anytime at [theveneaucedricpro@gmail.com](mailto:theveneaucedricpro@gmail.com), I'll try my best to help you out.
|
|
397
|
+
Found a bug or something unclear? Please [open an issue](https://github.com/CedricTheveneau/Magic-Pill/issues).
|
|
398
|
+
You can also send me emails anytime at [theveneaucedricpro@gmail.com](mailto:theveneaucedricpro@gmail.com), I'll try my best to help you out.
|
|
344
399
|
|
|
345
400
|
## License
|
|
346
401
|
Magic Pill is distributed under the [MIT License](https://www.tldrlegal.com/license/mit-license).
|
|
347
402
|
|
|
348
403
|
## What's next?
|
|
349
|
-
|
|
|
404
|
+
| Coming next | Done in 9.0 |
|
|
350
405
|
|-----------|-----------|
|
|
351
|
-
|
|
|
352
|
-
|
|
|
353
|
-
|
|
|
354
|
-
| |
|
|
406
|
+
| Toolbox and carousel indicator templates | Custom icons (any React content) |
|
|
407
|
+
| Animated morphing between contents | Transitions between the navbar and notifications |
|
|
408
|
+
| More built-in icons | Function triggers on actions (`onClick`) |
|
|
409
|
+
| A documentation site with live examples | Responsive styles, full documentation, tests |
|