UNPKG

@mobiloud/push-banner-widget

Version:

Smart, responsive banner widget for enabling push notifications in MobiLoud apps. Features session limiting, mobile optimization, and zero dependencies.

396 lines (325 loc) โ€ข 10.3 kB
# ๐ŸŽฏ Push Banner Widget A smart, responsive banner widget for enabling push notifications in MobiLoud apps. Features intelligent display logic, session limiting, mobile optimization, and zero dependencies. ## โœจ Features - ๐Ÿง  **Smart Display Logic** - Only shows when user is in app AND notifications are disabled - ๐Ÿ“ฑ **Mobile Responsive** - Optimized for all device sizes - ๐Ÿ”ข **Session Limiting** - Control how many times banner appears - โšก **Real-time Updates** - Instantly reflects notification status changes - ๐ŸŽจ **Fully Customizable** - Colors, text, positioning, behavior - ๐Ÿ“ **Flexible Positioning** - Fixed top/bottom or within containers - ๐Ÿš€ **Zero Dependencies** - Completely standalone - โ™ฟ **Accessible** - WCAG compliant with keyboard navigation - ๐ŸŒ— **Dark Mode Support** - Automatic dark/light mode detection ## ๐Ÿš€ Quick Start ### CDN (Recommended) ```html <script src="https://cdn.jsdelivr.net/npm/@mobiloud/push-banner-widget/script.js"></script> <script> createPushBanner({ position: 'top', sessionLimit: 3 }); </script> ``` ### NPM Installation ```bash npm install push-banner-widget ``` ```javascript import 'push-banner-widget'; createPushBanner({ heading: "๐ŸŽ‰ Special Offer!", text: "Enable notifications for exclusive deals", position: 'top', sessionLimit: 3 }); ``` ### Direct Download 1. Download `script.js` from the [releases page](https://github.com/mobiloud/push-banner-widget/releases) 2. Include in your HTML: ```html <script src="path/to/script.js"></script> <script> createPushBanner({ position: 'top' }); </script> ``` ## ๐Ÿ“– Usage Examples ### Basic Banner ```javascript createPushBanner({ position: 'top', sessionLimit: 3 }); ``` ### Customized Banner ```javascript createPushBanner({ heading: "๐Ÿ›๏ธ Exclusive Deals", text: "Enable notifications for flash sales and limited offers", successMessage: "๐ŸŽ‰ You're all set for exclusive deals!", position: 'bottom', sessionLimit: 5, backgroundColor: '#e3f2fd', headingColor: '#1e293b', textColor: '#475569', autoHideSuccess: false, onAccept: () => { console.log('User enabled notifications'); gtag('event', 'notification_enabled'); } }); ``` ### Container Positioning ```javascript createPushBanner({ heading: "๐Ÿ“ฌ Stay Updated", text: "Get notified about important announcements", position: { element: '#notification-area' }, sessionLimit: 2 }); ``` ## โš™๏ธ Configuration Options | Option | Type | Default | Description | |--------|------|---------|-------------| | `heading` | `string` | `"Get 10% OFF your next order"` | Main banner heading | | `text` | `string` | `"Enable push notifications to receive your unique coupon code"` | Banner description | | `successMessage` | `string` | `"Thank you for subscribing! Use APPLOVER coupon to get your 10% discount"` | Success message | | `position` | `string\|object` | `'top'` | `'top'`, `'bottom'`, or `{ element: '#selector' }` | | `sessionLimit` | `number` | `3` | Maximum times to show per session | | `backgroundColor` | `string` | `'#e3f2fd'` | Banner background color | | `headingColor` | `string` | `'#1e293b'` | Heading text color | | `textColor` | `string` | `'#475569'` | Description text color | | `autoHideSuccess` | `boolean` | `true` | Auto-hide success message after 3s | | `debugMode` | `boolean` | `false` | Show in browsers (for testing) | | `allowedUrls` | `array\|string` | `null` | Restrict to specific URLs | | `onAccept` | `function` | `() => {}` | Callback when banner clicked | | `onShow` | `function` | `() => {}` | Callback when banner shown | | `onHide` | `function` | `() => {}` | Callback when banner hidden | ## ๐ŸŽจ Color Themes ### Light Blue (Default) ```javascript backgroundColor: '#e3f2fd', headingColor: '#1e293b', textColor: '#475569' ``` ### Green Theme ```javascript backgroundColor: '#ecfdf5', headingColor: '#14532d', textColor: '#166534' ``` ### Purple Theme ```javascript backgroundColor: '#f3e8ff', headingColor: '#581c87', textColor: '#7c3aed' ``` ### Yellow Theme ```javascript backgroundColor: '#fef3c7', headingColor: '#92400e', textColor: '#b45309' ``` ## ๐Ÿ“ฑ Mobile Responsive The widget automatically adapts to different screen sizes: - **Desktop (1200px+)**: Full size with optimal spacing - **Tablet (768px-1199px)**: Adjusted padding and icon size - **Mobile (480px-767px)**: Compact layout with smaller text - **Small Mobile (320px-479px)**: Minimal spacing for tiny screens ## ๐Ÿ”ง Advanced Usage ### Session Management ```javascript // Show only once per session createPushBanner({ sessionLimit: 1 }); // Clear session data (for testing) Object.keys(sessionStorage) .filter(key => key.startsWith('pushBanner_')) .forEach(key => sessionStorage.removeItem(key)); ``` ### URL Restrictions ```javascript // Show only on specific pages createPushBanner({ allowedUrls: ['/products', '/offers'] }); // Pattern matching createPushBanner({ allowedUrls: ['/products/*', '/category/*'] }); ``` ### Event Tracking ```javascript createPushBanner({ onShow: () => { gtag('event', 'banner_shown', { banner_type: 'push_notification' }); }, onAccept: () => { gtag('event', 'notification_enabled', { source: 'banner_widget' }); } }); ``` ### Programmatic Control ```javascript const banner = createPushBanner({ position: 'top' }); // Hide manually banner.hide(); // Destroy completely banner.destroy(); ``` ## ๐Ÿ› ๏ธ Framework Integration ### React ```jsx import { useEffect } from 'react'; function App() { useEffect(() => { // Dynamic import to avoid SSR issues import('push-banner-widget').then(() => { window.createPushBanner({ position: 'top', sessionLimit: 3 }); }); }, []); return <div>Your app content</div>; } ``` ### Vue.js ```vue <template> <div>Your app content</div> </template> <script> export default { async mounted() { await import('push-banner-widget'); window.createPushBanner({ position: 'top', sessionLimit: 3 }); } } </script> ``` ### WordPress ```php function add_push_banner() { wp_enqueue_script( 'push-banner-widget', 'https://cdn.jsdelivr.net/npm/push-banner-widget@1.0.0/script.js', [], '1.0.0', true ); wp_add_inline_script('push-banner-widget', ' createPushBanner({ position: "top", sessionLimit: 3, heading: "Stay Connected!", text: "Enable notifications for the latest updates" }); '); } add_action('wp_enqueue_scripts', 'add_push_banner'); ``` ## ๐Ÿงช Testing & Debug Mode Enable debug mode to test in regular browsers: ```javascript createPushBanner({ debugMode: true, sessionLimit: 999, // Don't limit for testing position: 'top' }); ``` Debug mode bypasses: - App context checks - Push notification status checks - Allows testing in any browser ## ๐ŸŒ Browser Support - **Chrome** 60+ - **Firefox** 55+ - **Safari** 12+ - **Edge** 79+ - **iOS Safari** 12+ - **Chrome Android** 60+ ## ๐Ÿ“ฆ What's Included ``` push-banner-widget/ โ”œโ”€โ”€ script.js # Main widget file (standalone) โ”œโ”€โ”€ styles.css # Demo page styles (optional) โ”œโ”€โ”€ examples/ โ”‚ โ”œโ”€โ”€ basic.html # Basic implementation โ”‚ โ”œโ”€โ”€ advanced.html # Advanced customization โ”‚ โ””โ”€โ”€ frameworks/ # Framework examples โ”œโ”€โ”€ types/ โ”‚ โ””โ”€โ”€ index.d.ts # TypeScript definitions โ”œโ”€โ”€ README.md โ”œโ”€โ”€ CHANGELOG.md โ””โ”€โ”€ package.json ``` ## ๐Ÿ”’ Security & Privacy - **No external requests** - Completely self-contained - **No tracking** - Only uses sessionStorage for display limiting - **XSS safe** - All user content is properly escaped - **GDPR compliant** - No personal data collection ## ๐Ÿš€ Performance - **Bundle size**: ~12KB minified - **Zero dependencies**: No external libraries - **Lazy loading ready**: Can be loaded on demand - **Memory efficient**: Automatic cleanup and optimization ## ๐Ÿ“ˆ Real-World Examples ### E-commerce ```javascript createPushBanner({ heading: "๐Ÿ›๏ธ Flash Sale Alert", text: "Be first to know about our 24-hour flash sales", successMessage: "๐ŸŽ‰ You'll never miss a flash sale again!", backgroundColor: '#f1f5f9', headingColor: '#334155', textColor: '#64748b' }); ``` ### News Site ```javascript createPushBanner({ heading: "๐Ÿ“ฐ Breaking News", text: "Get instant alerts for important news updates", successMessage: "โœ… You're now subscribed to breaking news", backgroundColor: '#ecfdf5', headingColor: '#14532d', textColor: '#166534' }); ``` ### SaaS Application ```javascript createPushBanner({ heading: "๐Ÿ”” System Updates", text: "Stay informed about maintenance and new features", position: { element: '.dashboard-header' }, autoHideSuccess: false, backgroundColor: '#fef3c7', headingColor: '#92400e', textColor: '#b45309' }); ``` ## ๐Ÿค Contributing We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details. 1. Fork the repository 2. Create your feature branch (`git checkout -b feature/amazing-feature`) 3. Commit your changes (`git commit -m 'Add amazing feature'`) 4. Push to the branch (`git push origin feature/amazing-feature`) 5. Open a Pull Request ## ๐Ÿ“ License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. ## ๐Ÿ†˜ Support - ๐Ÿ“š [Documentation](https://github.com/mobiloud/push-banner-widget/wiki) - ๐Ÿ› [Issue Tracker](https://github.com/mobiloud/push-banner-widget/issues) - ๐Ÿ’ฌ [Discussions](https://github.com/mobiloud/push-banner-widget/discussions) - ๐Ÿ“ง [Email Support](mailto:support@mobiloud.com) ## ๐Ÿท๏ธ Changelog See [CHANGELOG.md](CHANGELOG.md) for a detailed history of changes. --- Made with โค๏ธ by [MobiLoud](https://www.mobiloud.com)