Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/justintaddei/v-wave
The material-ripple directive for Vue that actually works
https://github.com/justintaddei/v-wave
material-design micro-interactions microinteractions nuxtjs vue vuejs
Last synced: about 1 month ago
JSON representation
The material-ripple directive for Vue that actually works
- Host: GitHub
- URL: https://github.com/justintaddei/v-wave
- Owner: justintaddei
- License: mit
- Created: 2020-07-20T02:16:56.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-04-11T00:10:47.000Z (7 months ago)
- Last Synced: 2024-04-11T03:13:29.068Z (7 months ago)
- Topics: material-design, micro-interactions, microinteractions, nuxtjs, vue, vuejs
- Language: TypeScript
- Homepage: https://justintaddei.github.io/v-wave/
- Size: 5.67 MB
- Stars: 355
- Watchers: 2
- Forks: 12
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
v-wave
The material-ripple directive for Vue that actually works![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/justintaddei/v-wave/main.yml?logo=github&label=checks)
![Vue Support](https://img.shields.io/badge/vue-2%20&%203-1cb884.svg?style=flat)
[![Issues](https://img.shields.io/github/issues-raw/justintaddei/v-wave.svg?style=flat)](https://github.com/justintaddei/v-wave/issues)
![NPM version](https://img.shields.io/npm/v/v-wave.svg?style=flat)
![Downloads per month](https://img.shields.io/npm/dm/v-wave.svg?style=flat)
![Total downloads](https://img.shields.io/npm/dt/v-wave.svg?style=flat&label=total+downloads)
![Language](https://img.shields.io/badge/types-included-blue.svg?style=flat)
![License](https://img.shields.io/npm/l/v-wave.svg?style=flat)Also available for React: [use-wave](https://github.com/justintaddei/use-wave)
Why did I make this?
Because every ripple plugin I've tried to use in the past either didn't work or was missing basic features.**Here's what you can expect from this plugin:**
- It works ([see for yourself](https://justintaddei.github.io/v-wave)).
- The wave appears on `pointerdown` instead of `pointerup`
_(you might think that's an obvious choice... but you'd be wrong)._
- The wave responds to keyboard events on keyboard-accessible elements (i.e. Enter and Space on ``).
- There is a small delay before the ripple appears, during which the animation will be canceled if the user moves the pointer (e.g. scrolling on a mobile phone). This is similar to how native Android ripples work.
- Uses CSS transforms instead of `width` and `height`.
- Doesn't affect the appearance of the element you apply it to (won't explode when used on an element with `display: flex`).
- Guesses the color of the wave automatically by default (using `currentColor`).
- Works with fixed, absolute, relative, and statically positioned elements.
- Will handle independent border-radii (e.g. `border-radius: 5px 20px 15px 30px`) perfectly fine.## [[Live Demo]](https://justintaddei.github.io/v-wave)
## Quick start
After installing and registering the plugin, this is all you need to get started:
```html
Click here
```![Example of default options](https://raw.githubusercontent.com/justintaddei/v-wave/assets/default-example.gif)
> Out of the box, this will provide you with a ripple that matches the text color of the element it has been applied to, with reasonable defaults for a responsive feeling ripple.
> You can change the look and feel of the ripple on a [per-element basis](#configuring-locally), or by modifying the [global defaults](#configuring-globally).## Contents
- [Installation](#installation)
- [Via NPM](#via-npm)
- [Via CDN](#via-cdn)
- [Configuration](#configuration)
- [Configuring globally](#configuring-globally)
- [Configuring locally](#configuring-locally)
- [Options](#options)
- [Summary](#summary)
- [Details](#details)
- [color](#color)
- [initialOpacity](#initialopacity)
- [finalOpacity](#finalopacity)
- [duration](#duration)
- [dissolveDuration](#dissolveduration)
- [waitForRelease](#waitforrelease)
- [easing](#easing)
- [cancellationPeriod](#cancellationperiod)
- [trigger](#trigger)
- [disabled](#disabled)
- [respectDisabledAttribute](#respectdisabledattribute)
- [respectPrefersReducedMotion](#respectprefersreducedmotion)
- [stopPropagation](#stoppropagation)
- [tagName](#tagname)
- [Using triggers](#using-triggers)
- [Advanced](#advanced)
- [Registering the directive locally](#registering-the-directive-locally)
- [Local registration with Composition API:](#local-registration-with-composition-api)
- [Local registration with Options API:](#local-registration-with-options-api)
- [Changing the directive's name](#changing-the-directives-name)
- [Contributing](#contributing)
- [License](#license)## Installation
See: [npm](#via-npm), [cdn](#via-cdn)
### Via NPM
See: [Vue](#vue-), [Nuxt](#nuxt-)
```sh
$ npm i v-wave
```#### Vue
```js
import { createApp } from 'vue'
import VWave from 'v-wave'
import App from './App.vue'const app = createApp(App)
app.use(VWave)
```Vue 2
```js
// Vue 2import Vue from 'vue'
import VWave from 'v-wave'Vue.use(VWave)
```or
#### Nuxt
```js
// nuxt.config.js// Nuxt 3
export default {
modules: ['v-wave/nuxt']
}// Nuxt 2
export default {
modules: ['v-wave/nuxt/v2']
}
```### Via CDN
Expand examples
```html
```
```js
// With a CDN, `VWave` is made available as a global
Vue.use(VWave)
```---
## Configuration
See: [configuring globally](#configuring-globally), [configuring locally](#configuring-locally)
### Configuring globally
See: [Vue](#vue--1), [Nuxt](#nuxt--1)
#### Vue
```js
import { createApp } from 'vue'
import VWave from 'v-wave'
import App from './App.vue'const app = createApp(App)
app.use(VWave, {
color: 'red',
initialOpacity: 0.5,
easing: 'ease-in'
})
```Vue 2
```js
// Vue 2import Vue from 'vue'
import VWave from 'v-wave'Vue.use(VWave, {
color: 'red',
initialOpacity: 0.5,
easing: 'ease-in'
})
```or
#### Nuxt
```js
// nuxt.config.jsexport default {
modules: ['v-wave/nuxt'],
vWave: {
color: 'red',
initialOpacity: 0.5,
easing: 'ease-in'
}
}
```### Configuring locally
```html
Click here
```
---
## Options
See: [summary](#summary), [details](#details)
### Summary
| Name | Default | Type |
| ------------------------------------------------------ | :--------------: | ----------------------------- |
| [color](#color) | `"currentColor"` | `string` |
| [initialOpacity](#initialopacity) | `0.2` | `number` |
| [finalOpacity](#finalopacity) | `0.1` | `number` |
| [duration](#duration) | `0.4` | `number` |
| [dissolveDuration](#dissolveduration) | `0.15` | `number` |
| [waitForRelease](#waitforrelease) | `true` | `boolean` |
| [easing](#easing) | `ease-out` | `string` |
| [cancellationPeriod](#cancellationperiod) | `75` | `number` |
| [trigger](#trigger) | `"auto"` | `string \| boolean \| "auto"` |
| [disabled](#disabled) | `false` | `boolean` |
| [respectDisabledAttribute](#respectdisabledattribute) | `true` | `boolean` |
| [respectPrefersReducedMotion](#respectpreferredmotion) | `true` | `boolean` |
| [stopPropagation](#stoppropagation) | `false` | `boolean` |
| [tagName](#tagname) | `"div"` | `string` |### Details
#### color
- **type:** `string` ([`background` shorthand syntax](https://developer.mozilla.org/en-US/docs/Web/CSS/background#formal_syntax))
- _default:_ `"currentColor"`> Sets the background of the ripple.
> Supports any value that the CSS `background` property does.Expand examples
See: [color](#simple-color-), [gradient](#gradient-), [image](#image-)
#### Simple color
```html
Click here
```
![](https://raw.githubusercontent.com/justintaddei/v-wave/assets/color-red.gif)
#### Gradient
```html
Click here
```
![](https://raw.githubusercontent.com/justintaddei/v-wave/assets/color-gradient.gif)
#### Image
```html
Click here
```
![](https://raw.githubusercontent.com/justintaddei/v-wave/assets/color-image.gif)
#### initialOpacity
- **type:** `number`
- _default:_ `0.2`> The opacity of the ripple when it first appears.
Expand examples
See: [initialOpacity: 1](#initialopacity-of-1-), [initialOpacity: 0](#initialopacity-of-0-)
#### initialOpacity of 1
```html
Click here
```
![](https://raw.githubusercontent.com/justintaddei/v-wave/assets/color-initial-opacity-1.gif)
#### initialOpacity of 0
```html
Click here
```
![](https://raw.githubusercontent.com/justintaddei/v-wave/assets/color-initial-opacity-0.gif)
#### finalOpacity
- **type:** `number`
- _default:_ `0.1`> The opacity the ripple should be when it has stopped moving.
Expand examples
See: [finalOpacity: 1](#finalopacity-of-1-), [finalOpacity: 0](#finalopacity-of-0-)
#### finalOpacity of 1
```html
Click here
```
![](https://raw.githubusercontent.com/justintaddei/v-wave/assets/color-final-opacity-1.gif)
#### finalOpacity of 0
```html
Click here
```
![](https://raw.githubusercontent.com/justintaddei/v-wave/assets/color-final-opacity-0.gif)
#### duration
- **type:** `number` (seconds)
- _default:_ `0.4`> The duration of the ripple in seconds.
> The total duration is `duration + dissolveDuration`Expand example
#### duration of 3 seconds
```html
Click here
```
![](https://raw.githubusercontent.com/justintaddei/v-wave/assets/duration-3s.gif)
#### dissolveDuration
- **type:** `number` (seconds)
- _default:_ `0.15`> The duration of the "dissolve animation" in seconds.
> This is the fade-out animation that plays once the wave has reached its maximum size.
> The total duration is `duration + dissolveDuration`Expand example
#### dissolve duration of 3 seconds
```html
Click here
```
![](https://raw.githubusercontent.com/justintaddei/v-wave/assets/dissolve-duration-3s.gif)
#### waitForRelease
- **type:** `boolean`
- _default:_ `true`> When `true`, the wave will not dissolve until the user releases the pointer.
#### easing
- **type:** `string` ([``](https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function))
- _default:_ `"ease-out"`> Any valid CSS `` ([see more](https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function))
Expand example
#### cubic-bezier
```html
Click here
```
![](https://raw.githubusercontent.com/justintaddei/v-wave/assets/easing.gif)
#### cancellationPeriod
- **type:** `number` (milliseconds)
- _default:_ `75`> The delay, in milliseconds, during which the animation will be canceled if the user moves their figure/pointer (e.g. while scrolling on a mobile device).
> **Note:** The ripple will not appear until after the delay. This means a delay greater than 100ms can feel sluggish.
#### trigger
- **type:** `"auto" | string | boolean`
- _default:_ `"auto"`> Sets the behavior of the wave when used with triggers. (See the [dedication section](#using-triggers) on triggers for more details).
- `false`
Disables the use of triggers. If a `v-wave-trigger` (without an ID) is present in the dom tree of this element, it will be ignored (i.e. `v-wave` always behaves as if there's no trigger).
- `true`
Requires a trigger to activate the ripple. `v-wave` assumes the presence of a `v-wave-trigger` (without an ID) in its dom tree. The ripple will only activate for `pointerdown` events on the trigger element.
- `"auto"`
If a `v-wave-trigger` (without an ID) is present in the dom-tree of the v-wave element, it behaves as `trigger: true`, otherwise it behaves as `trigger: false`.
- `string`
Any string other than `"auto"` will be treated as an ID. `v-wave` will only activate when a `v-wave-trigger` with a matching ID receives a `pointerdown` event.> This is different from the other values as it allows you to place the trigger element anywhere in the dom, while the others require the trigger to be a descendant.
Expand example
#### basic trigger
```html
```
![](https://raw.githubusercontent.com/justintaddei/v-wave/assets/tigger.gif)
#### disabled
- **type:** `boolean`
- _default:_ `false`> Disables the wave effect on the element regardless of [`respectDisabledAttribute`](#respectdisabledattribute).
#### respectDisabledAttribute
- **type:** `boolean`
- _default:_ `true`> When `true`, the wave effect will be disabled if the html `disabled` attribute is present on the element.
```html
Click me!
Click me!
```#### respectPrefersReducedMotion
- **type:** `boolean`
- _default:_ `true`> If `true`, the wave effect will be disabled if the user's [`prefers-reduced-motion`](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion) preference is set to `reduce`.
#### stopPropagation
- **type:** `boolean`
- _default:_ `false`> Prevents the `pointerdown` event from propagating to parent elements.
#### tagName
- **type:** `string`
- _default:_ `"div"`> Sets the tag name of the element used as the wave container. This is is useful in scenarios where the default `div` may interfere with `:last-of-type` or similar selectors.
---
### Using triggers
Triggers allow you to activate a wave on an element when, and only when, a different element receives input.
In the following example, the wave will only activate for the label element when the user clicks or taps on the ``.
```html
Password
showPassword = !showPassword" />```
In this next example, clicking one of the buttons will activate the wave on the other button.
```html
Button 1Button 2
```> Triggers that use an ID support many-to-many relationships. See the grid example on the [example page](https://justintaddei.github.io/v-wave).
## Advanced
### Registering the directive locally
#### Local registration with Composition API:
```html
import VWave from 'v-wave'
const { vWave, vWaveTrigger } = VWave.createLocalWaveDirective({
/* global options */
})Click me!
```
#### Local registration with Options API:
```html
import VWave from 'v-wave'
const { wave, waveTrigger } = VWave.createLocalWaveDirective({
/* global options */
})export default {
directives: {
wave,
waveTrigger
}
}Click me!
```
Vue 2
If you are using Vue 2, you need to pass `"vue2"` as the second argument to `createLocalWaveDirective`
```html
import VWave from 'v-wave'
const { wave, waveTrigger } = VWave.createLocalWaveDirective(
{
/* global options */
},
'vue2'
) // this is the differenceexport default {
directives: {
wave,
waveTrigger
}
}Click me!
```
### Changing the directive's name
If you are migrating from another ripple directive you can change the name of the directive v-wave uses if you want to avoid changing it in your source code.
Simply pass a new name for the directive using the `directive` option:```js
//main.jsimport Vue from 'vue'
import VWave from 'v-wave'Vue.use(VWave, {
directive: 'ripple'
})
```Now you can use the plugin like so:
```html
Click me!
```> Keep in mind that this option can only be set globally (i.e. it cannot be set on individual directives).
## Contributing
Contributions are welcome! Please see [CONTRIBUTING.md](https://github.com/justintaddei/v-wave/blob/master/CONTRIBUTING.md) for more details.
## License
This project is distributed under the [MIT License](https://github.com/justintaddei/v-wave/blob/master/LICENSE.md).
### The MIT License (MIT)
Copyright (c) 2021 Justin Taddei
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.