UNPKG

@cesarfernandoig/alpine-responsive-plugin

Version:

A lightweight Alpine.js plugin that simplifies managing conditional attributes in responsive designs.

122 lines (96 loc) โ€ข 5.15 kB
# Alpine.js Responsive Plugin **Supercharge your Alpine.js apps with responsive superpowers! ๐Ÿš€** A lightweight [Alpine.js](https://alpinejs.dev/) plugin that simplifies managing responsive behaviors and conditional attributes. This plugin provides utilities to manage media query states and apply conditional attributes or values based on screen size. ## ๐ŸŒŸ Features - **๐Ÿ“ฑ Media Query State Management:** Instantly know if your user is on mobile or desktop. - **โœจ Magic Helpers:** Use `media` and `getMobileValue` to make your components smarter. - **๐ŸŽจ Custom Directive:** Dynamically add or remove attributes with `x-mobileattributes`. ## ๐Ÿ› ๏ธ Installation Getting started with Responsive is a breeze! With just a few lines of code, you'll unlock the power of responsive design in your [Alpine.js](https://alpinejs.dev/) projects. ### Via NPM 1. Install the plugin with npm: ```console npm install @cesarfernandoig/alpine-responsive-plugin ``` 2. Import and initialize the plugin in your project: ```javascript import Alpine from 'alpinejs'; import responsive from '@cesarfernandoig/alpine-responsive-plugin'; Alpine.plugin(responsive); Alpine.start(); ``` ### Via CDN If you prefer using a CDN, simply include the following script tag in your HTML file, make sure to include it BEFORE Alpine's core JS file: ```html <!-- Alpine Plugins --> <script defer src="https://cdn.jsdelivr.net/npm/@cesarfernandoig/alpine-responsive-plugin@1.x.x/dist/responsive.min.js"></script> <!-- Alpine Core --> <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script> ``` ## ๐ŸŽฎ Usage Now that the plugin is set up, letโ€™s explore the magic! You can use the features provided by **`Responsive`** to make your site smarter and more responsive. ### ๐Ÿช„ Magic: $media The media magic helper gives you access to `isMobile` and `isDesktop` properties. You can use these properties anywhere in your Alpine.js components to apply logic based on screen size. #### Example: ```html <div x-data> <span x-text="$media.isMobile ? '๐Ÿ“ฑ On Mobile!' : '๐Ÿ–ฅ๏ธ On Desktop!'"></span> </div> ``` - `$media.isMobile` is `true` if the screen width is less than 768px. - `$media.isDesktop` is `true` if the screen width is 768px or greater. ### ๐Ÿช„ Magic: $getMobileValue The `$getMobileValue` magic helper is your go-to for conditional values. Itโ€™s like having a chameleon that adapts to the device type! ๐ŸฆŽ #### Example: ```html <div x-data> <span x-text="$getMobileValue('This is for mobile ๐Ÿณ', 'This is for desktop ๐Ÿฆ–')"></span> </div> ``` - If the device is mobile, it returns `'This is for mobile ๐Ÿณ'`. - If the device is desktop, it returns `'This is for desktop ๐Ÿฆ–'`. ### ๐Ÿ–Œ๏ธ Directive: x-mobileattributes Want to conditionally add/remove attributes based on screen size? Look no further than the `x-mobileattributes` directive! You can pass in a set of attributes and their values for mobile and desktop resolutions, and `Responsive` will handle the rest. #### Example: ```html <button type="button" x-mobileattributes="{ title: ['Snake', 'Turtle'], 'x-text': ['`๐Ÿ`', '`๐Ÿข`'], class: 'snake-class' }" > </button> ``` - **Mobile:** Sets the `title` to `"Snake"`, `x-text` to `"๐Ÿ"`, and applies the `snake-class`. - **Desktop:** Sets the `title` to `"Turtle"`, `x-text` to `"๐Ÿข"`, and removes the `class` attribute (since it doesn't have a desktop value โ€” similar to writing `class: ['snake-class', null]`). ### ๐Ÿ’ก Pro Tip: By combining `Responsive` with Alpineโ€™s built-in directives like `x-show`, `x-if`, or `x-transition`, you can create rich and highly dynamic UI elements tailored for different screen sizes. ## ๐Ÿง‘โ€๐Ÿ’ป API Reference ### ๐Ÿช„ Magics - **`$media`:** Returns a reactive object with `isMobile` and `isDesktop` properties. ```javascript $media.isMobile; // true if mobile $media.isDesktop; // true if desktop ``` - **`$getMobileValue(mobileValue, desktopValue)`:** Returns `mobileValue` if the device is mobile, otherwise returns `desktopValue`. ```javascript getMobileValue('๐Ÿ“ฑ Mobile', '๐Ÿ–ฅ๏ธ Desktop'); // '๐Ÿ“ฑ Mobile' or '๐Ÿ–ฅ๏ธ Desktop' ``` ### ๐Ÿ–Œ๏ธ Directives - **`x-mobileattributes`:** Applies or removes attributes conditionally. ```html <div x-mobileattributes="{ 'attribute': [mobileValue, desktopValue] }"></div> ``` ## ๐Ÿค Contributing We ๐Ÿ’– contributions! If you have an idea, found a bug, or just want to improve the plugin, feel free to jump in! 1. Fork the repository. 2. Create a new branch for your feature or bugfix. 3. Submit a pull request. ## ๐Ÿ“œ License This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. ## ๐Ÿ™ Acknowledgments A huge shoutout to [Alpine.js](https://alpinejs.dev/) for making JavaScript fun and simple! ## ๐Ÿš€ Ready to Go Responsive? With this plugin, handling responsiveness in your Alpine.js apps has never been easier. Install it today and start building smarter, more dynamic interfaces! ๐ŸŽ‰