A new way to guide your audience and bring them contextual information through your app
npm install magic-pill#### Thank you !!
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.
html
`
#### unpkg
`html
`$3
Magic Pill is available on npm, simply use the following command and you're ready to go!
`bash
npm install magic-pill
`How to use it?
$3
For it to work properly, you need to import styles in your app (we recommend importing it into your app.tsx file).
`typescript
// Import in your Next.js or React project
import "magic-pill/dist/magicpill.css";
`$3
Magic Pill is designed to be used with a Context to manage its state globally. Here's how to set it up:1. First, create a context for Magic Pill (e.g., in
app/contexts/magicPillContext.tsx):
`typescript
"use client"
import React, { createContext, useContext, useState } from "react";
import { MagicPillType, Mode, NotificationIcon, CTAIcon, MagicPillNavbarItem } from "magic-pill";
import { updateMode, updateNotificationIcon, updateNotificationMessage, updateCTAIcon, updateCTALabel, updateCTALink, updateInfoTitle, updateInfoContent, updateInfoCloseLabel, updateNavItemLabel, updateNavItemLink, updateNavItemIcon, addNavItem, deleteNavItem } from "magic-pill";const defaultPillData: MagicPillType = {
mode: "navbar",
navbar: {
navItems: [
{
icon: "arrow",
label: "mp-test",
link: "/mp-test"
},
{
icon: "cross",
label: "demo",
link: "/demo"
}
]
}
};
interface MagicPillContextType {
pillData: MagicPillType;
setPillData: (data: MagicPillType) => void;
// Direct data manipulation methods
updateMode: (mode: Mode) => void;
updateNotificationIcon: (icon: NotificationIcon) => void;
updateNotificationMessage: (message: string) => void;
updateCTAIcon: (icon: CTAIcon) => void;
updateCTALabel: (label: string) => void;
updateCTALink: (link: string) => void;
updateInfoTitle: (title: string) => void;
updateInfoContent: (content: string) => void;
updateInfoCloseLabel: (closeLabel: string) => void;
updateNavItemLabel: (label: string, index: number) => void;
updateNavItemLink: (link: string, index: number) => void;
updateNavItemIcon: (icon: CTAIcon, index: number) => void;
addNavItem: (navItem: MagicPillNavbarItem) => void;
deleteNavItem: (index: number) => void;
}
const MagicPillContext = createContext({
pillData: defaultPillData,
setPillData: () => {},
updateMode: () => {},
updateNotificationIcon: () => {},
updateNotificationMessage: () => {},
updateCTAIcon: () => {},
updateCTALabel: () => {},
updateCTALink: () => {},
updateInfoTitle: () => {},
updateInfoContent: () => {},
updateInfoCloseLabel: () => {},
updateNavItemLabel: () => {},
updateNavItemLink: () => {},
updateNavItemIcon: () => {},
addNavItem: () => {},
deleteNavItem: () => {},
});
export const useMagicPill = () => {
const context = useContext(MagicPillContext);
if (!context) {
throw new Error("useMagicPill must be used within a MagicPillProvider");
}
return context;
};
export const MagicPillProvider = ({
children
}: {
children: React.ReactNode
}) => {
const [pillData, setPillData] = useState(defaultPillData);
// Create wrapper functions that use the handlers with the local setPillData
const contextValue: MagicPillContextType = {
pillData,
setPillData,
updateMode: (mode: Mode) => updateMode(mode, setPillData),
updateNotificationIcon: (icon: NotificationIcon) => updateNotificationIcon(icon, setPillData),
updateNotificationMessage: (message: string) => updateNotificationMessage(message, setPillData),
updateCTAIcon: (icon: CTAIcon) => updateCTAIcon(icon, setPillData),
updateCTALabel: (label: string) => updateCTALabel(label, setPillData),
updateCTALink: (link: string) => updateCTALink(link, setPillData),
updateInfoTitle: (title: string) => updateInfoTitle(title, setPillData),
updateInfoContent: (content: string) => updateInfoContent(content, setPillData),
updateInfoCloseLabel: (closeLabel: string) => updateInfoCloseLabel(closeLabel, setPillData),
updateNavItemLabel: (label: string, index: number) => updateNavItemLabel(label, index, setPillData),
updateNavItemLink: (link: string, index: number) => updateNavItemLink(link, index, setPillData),
updateNavItemIcon: (icon: CTAIcon, index: number) => updateNavItemIcon(icon, index, setPillData),
addNavItem: (navItem: MagicPillNavbarItem) => addNavItem(navItem, setPillData),
deleteNavItem: (index: number) => deleteNavItem(index, setPillData),
};
return (
{children}
{pillData && }
);
};
`2. Wrap your app with the provider (in
app/layout.tsx):
`typescript
import "magic-pill/dist/magicpill.css";
import { MagicPillProvider } from "./contexts/magicPillContext";
import { MagicPillInitializer } from "./contexts/magicPillInitializer";export default function RootLayout({ children }) {
return (
{children}
);
}
`3. Create an initializer file for Magic Pill (e.g., in
app/contexts/magicPillInitializer.tsx):
`tsx
"use client"
import { MagicPill } from "magic-pill";
import { useMagicPill } from "./magicPill";export const MagicPillInitializer = () => {
const { pillData } = useMagicPill();
return (
)
};
`
> 🎉 | At this point, if you launch you app, Magic Pill should appear in your view4. Use Magic Pill anywhere in your app (e.g., in
app/demo/page.tsx):
`typescript
"use client"
import { useEffect } from "react";
import { useMagicPill } from "../contexts/magicPillContext";export default function MPTest() {
const { setPillData } = useMagicPill();
useEffect(() => {
setPillData({ mode: "notification", notification: { icon: "info", message: "Hello, world!" } });
}, []);
return (
Magic Pill Demo Page
);
}
`
> It really is as simple as that, to use Magic Pill, once the setup is done$3
Magic Pill, right from the start, has been thought out to be as highly customizable as possible.
To help you use it in the best way possible, a collection of handlers is part of the package.-
updateMode - Change Magic Pill's mode between "notification" and "navbar"
- updateNotificationIcon - Change the main notification icon
- updateNotificationMessage - Change the main notification message
- updateCTAIcon - Change the notification's CTA (Call To Action) icon
- updateCTALabel - Change the notification's CTA (Call To Action) label
- updateCTALink - Change the notification's CTA (Call To Action) redirection link
- updateInfoTitle - Change the notification's expanded info title
- updateInfoContent - Change the notification's expanded info main content
- updateInfoCloseLabel - Change the notification's expanded info close button's label
- updateNavItemLabel - Change a navbar item's label
- updateNavItemLink - Change a navbar item's link
- updateNavItemIcon - Change a navbar item's icon
- handleActiveNavItem - Automatically working handler directly used by the navbar to update the active link
- addNavItem - Add an item to the navbar
- deleteNavItem - Delete an item to the navbar$3
#### Notification Icons
- "info" - Information icon
- "checkmark" - Success/confirmation icon
- "questionmark" - Help/question icon
- "coupon" - Coupon/offer icon
- "" - No icon#### CTA (Call to Action) Icons
-
"arrow" - Arrow icon
- "box" - Box icon
- "checkmark" - Checkmark icon
- "cross" - Cross/close icon
- "info" - Information icon
- "mail" - Mail/email icon
- "plus" - Plus/add icon
- "quotation" - Quote/speech icon
- "tel" - Phone/telephone icon
- "undo" - Undo/back icon
- "" - No icon$3
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".`css
/ Font import /
@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');/ CSS variables /
:root {
--mp__themeDark: #141414;
--mp__themeLight: #F9F9F9;
--mp__font-text: "Overlock", sans-serif;
--mp__font-accent: "Lexend", sans-serif;
--mp__normal: normal;
--mp__italic: italic;
--mp__medium: 400;
--mp__bold: 700;
--mp__black: 900;
--mp__success: #3F7253;
--mp__danger: #FC5D6A;
--mp__textSize: 19.4px;
--mp__titleSize: 31.42px;
}
`Build your own
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, 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.Here's how to proceed:
1. Clone the project:
`bash
git clone https://github.com/CedricTheveneau/Magic-Pill.git
`2. Install all dependencies:
`bash
npm install
`3. 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.
`bash
Create a new Next.js app
npx create-next-app@latest my-app --typescriptLink the package (from the Magic-Pill directory)
npm linkLink to your app (from your app directory)
npm link magic-pill
``