@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
Markdown
# 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! ๐