Skip to content
This repository has been archived by the owner on Nov 30, 2020. It is now read-only.

feat(elevation): add custom directive v-elevation #416

Merged
merged 4 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions components/elevation/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
## Elevation

Material Components Vue provides a customized directive `v-elevation` to provide any components with elevation or shadow.

### Minimal Usage

```html
<my-button :v-elevation="1">Button</my-button>
```

### Add transition between different elevations

```html
<my-button v-elevation.transition="elevation">Button</my-button>
```

```js
data () {
return {
elevation: 1 // change this value to see the transition
}
}
```

Content below is deprecated, which means they may be removed in the future versions.

### Markup

```html
Expand Down
38 changes: 38 additions & 0 deletions components/elevation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@ import { initPlugin } from '../'
const plugin = {
install (vm) {
vm.component('m-elevation', Elevation)
vm.directive('elevation', {
bind: function (el, binding) {
if (binding.value != null) {
const n = Number(binding.value)
el.classList.add(`mdc-elevation--z${n}`)
}
if (binding.modifiers.transition) {
el.classList.add('mdc-elevation-transition')
}
},
componentUpdated: function (el, binding) {
if (Number(binding.oldValue) !== Number(binding.value)) {
if (binding.oldValue != null) {
const n = Number(binding.oldValue)
el.classList.remove(`mdc-elevation--z${n}`)
}
if (binding.value != null) {
const n = Number(binding.value)
el.classList.add(`mdc-elevation--z${n}`)
}
}
if (!binding.modifiers.transition && el.classList.contains('mdc-elevation-transition')) {
el.classList.remove('mdc-elevation-transition')
}
if (binding.modifiers.transition && !el.classList.contains('mdc-elevation-transition')) {
el.classList.add('mdc-elevation-transition')
}
},
unbind: function (el, binding) {
if (binding.value != null) {
const n = Number(binding.value)
el.classList.remove(`mdc-elevation--z${n}`)
}
if (binding.modifiers.transition) {
el.classList.remove('mdc-elevation-transition')
}
}
})
}
}
export default plugin
Expand Down
53 changes: 53 additions & 0 deletions docs/.vuepress/components/ElevationDemo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<template>
<div>
<ComponentSection>
<div class="elevation-demo-surface" v-elevation.transition="elevation" v-if="checkboxProps[0].value"></div>
<div class="elevation-demo-surface" v-elevation="elevation" v-else></div>
</ComponentSection>
<ComponentProps
:radioProps="radioProps"
:checkboxProps="checkboxProps"/>
</div>
</template>

<script>
export default {
name: 'ElevationDemo',
data () {
return {
radioProps: [],
checkboxProps: [
{ prop: 'transition', value: true },
{ prop: 1, value: false },
{ prop: 2, value: false },
{ prop: 4, value: false },
{ prop: 8, value: false },
{ prop: 16, value: false },
]
}
},
computed: {
elevation () {
let res = 0
for (let i = 1; i < this.checkboxProps.length; i++) {
res += this.checkboxProps[i].prop * this.checkboxProps[i].value
}
return res > 24 ? 24 : res
}
}
}
</script>

<style scoped>
.elevation-demo-surface {
display: -ms-inline-flexbox;
display: inline-flex;
justify-content: space-around;
min-height: 100px;
min-width: 200px;
margin: 15px;
-ms-flex-align: center;
align-items: center;
background-color: white;
}
</style>
2 changes: 2 additions & 0 deletions docs/.vuepress/enhanceApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import LinearProgress from '../../components/linear-progress'
import Snackbar from '../../components/snackbar'
import Tabs from '../../components/tabs'
import Ripple from '../../components/ripple'
import Elevation from '../../components/elevation'

export default ({
Vue, // the version of Vue being used in the VuePress app
Expand Down Expand Up @@ -63,4 +64,5 @@ export default ({
Vue.use(Snackbar)
Vue.use(Tabs)
Vue.use(Ripple)
Vue.use(Elevation)
}
4 changes: 4 additions & 0 deletions docs/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ The demonstration in this page is a work in progress..

<ChipsInputDemo/>

## Elevation

<ElevationDemo/>

## Fab

<FabDemo/>
Expand Down