Skip to content

Commit

Permalink
feat(button): add support for vue-router
Browse files Browse the repository at this point in the history
  • Loading branch information
stasson committed Jan 7, 2018
1 parent 22f720a commit 43a9d3f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 60 deletions.
55 changes: 55 additions & 0 deletions components/base/custom-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export const CustomButton = {
name: 'custom-button',
functional: true,
props: {
link : Object,
},
render (h, context) {
let element
let data = Object.assign({}, context.data)

if (context.props.link && context.parent.$router) {
// router-link case
element = context.parent.$root.$options.components['router-link']
data.props = Object.assign({tag: context.props.tag}, context.props.link)
data.attrs.role = 'button'
} else if (data.attrs && data.attrs.href) {
// href case
element = 'a'
data.attrs.role = 'button'
} else {
// button fallback
element = 'button'
}

return h(element, data, context.children)
}
}

export const CustomButtonMixin = {
props: {
href: String,
disabled: Boolean,
to: [String, Object],
exact: Boolean,
append: Boolean,
replace: Boolean,
activeClass: String,
exactActiveClass: String,
},
computed: {
link () {
return this.to && {
to: this.to,
exact: this.exact,
append: this.append,
replace: this.replace,
activeClass: this.activeClass,
exactActiveClass: this.exactActiveClass,
}
}
},
components : {
CustomButton
}
}
1 change: 1 addition & 0 deletions components/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './base-plugin.js'
export * from './custom-element.js'
export * from './custom-link.js'
export * from './custom-event.js'
export * from './custom-button.js'
export * from './dispatch-event-mixin.js'
export * from './dispatch-focus-mixin.js'
39 changes: 34 additions & 5 deletions components/button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ var vm = new Vue({
|`event-target`|Object| vm.$root | optional event target, defaults to root bus |
|`event-args`|Array| [] | optional event args |
|`href`|String|| link's href, renders anchor (see notes below) |
|`to`|String, Object| undefined | router-link property _(*)_ |
|`replace`|Boolean| false | router-link property _(*)_ |
|`append`|Boolean| false | router-link property _(*)_ |
|`exact`|Boolean| false | router-link property _(*)_ |
|`active-class`|String| router-link-active | router-link property _(*)_ |
|`exact-active-class`|String| router-link-exact-active | router-link property _(*)_ |

> (*) initializer attribute, not dynamic property
> _(*)_ Requires [vue-router](https://router.vuejs.org)
### text buttons

Expand Down Expand Up @@ -71,13 +77,25 @@ var vm = new Vue({

### Link buttons

#### simple Link

Using the href attribute will render `<a role="button" href="...">`

```html
<mdc-button href="#">Home</mdc-button>
```

Using the href attribute will render `<a role="button" href="...">`
#### router-link

If the `to` property is defined, the item behaves as a
[router-link](https://router.vuejs.org/en/api/router-link.html)

```html
<mdc-button to="/home">Home</mdc-button>
```

#### Accessibility Warning:

> Accessibility Warning:
> Buttons are expected to be triggered using the Space or Enter key,
> while links are expected to be triggered using the Enter key.
> Additionally, button and links have different behaviors on right click.
Expand All @@ -87,8 +105,19 @@ You might want to keep button behavior and do the navigation programatically:

```html
<mdc-button @click="location.href='#'">Home</mdc-button>
```
<mdc-button @click="$router.push(...)">Home</mdc-button>
```

### Triggering events

```html
<mdc-button event='my-event'>Do Something</mdc-button>
```

```javascript
// register event from some component context
this.$root.$on('my-event', () => doSomething())
```

### Reference
- <https://material.io/guidelines/components/buttons.html>

45 changes: 21 additions & 24 deletions components/button/demo.vue
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
<template>
<div class="mdc-demo">
<div class="mdc-demo--container">
<div class="mdc-demo-button">
<mdc-button :dense="dense" :compact="compact" :disabled="disabled"
:raised="raised" :stroked="stroked" :unelevated="unelevated">Button</mdc-button>
<mdc-button :dense="dense" :compact="compact" :disabled="disabled"
:raised="raised" :stroked="stroked" :unelevated="unelevated">Button</mdc-button>
<mdc-button :dense="dense" :compact="compact" :disabled="disabled"
:raised="raised" :stroked="stroked" :unelevated="unelevated">Button</mdc-button>
</div>
<div class="mdc-demo--container">
<div class="mdc-demo-button">
<mdc-button :dense="dense" :compact="compact" :disabled="disabled"
:raised="raised" :stroked="stroked" :unelevated="unelevated">Button</mdc-button>
<mdc-button :dense="dense" :compact="compact" :disabled="disabled"
:raised="raised" :stroked="stroked" :unelevated="unelevated">Button</mdc-button>
<mdc-button :dense="dense" :compact="compact" :disabled="disabled"
:raised="raised" :stroked="stroked" :unelevated="unelevated">Button</mdc-button>
</div>

<div class="mdc-demo--container">
<div class="button-type-select">
<mdc-radio label="baseline" name="button-type" v-model="type" checked></mdc-radio>
<mdc-radio label="raised" name="button-type" v-model="type" ></mdc-radio>
<mdc-radio label="unelevated" name="button-type" v-model="type"></mdc-radio>
<mdc-radio label="stroked" name="button-type" v-model="type"></mdc-radio>
</div>
</mdc-layout-cell>

<div class="button-style-select">
<mdc-checkbox label="disabled" v-model="disabled"></mdc-checkbox>
<mdc-checkbox label="compact" v-model="compact" ></mdc-checkbox>
<mdc-checkbox label="dense" v-model="dense"></mdc-checkbox>
</div>
</div>

<div class="mdc-demo--container">
<div class="button-type-select">
<mdc-radio label="baseline" name="button-type" v-model="type" checked></mdc-radio>
<mdc-radio label="raised" name="button-type" v-model="type" ></mdc-radio>
<mdc-radio label="unelevated" name="button-type" v-model="type"></mdc-radio>
<mdc-radio label="stroked" name="button-type" v-model="type"></mdc-radio>
</div>

<div class="button-style-select">
<mdc-checkbox label="disabled" v-model="disabled"></mdc-checkbox>
<mdc-checkbox label="compact" v-model="compact" ></mdc-checkbox>
<mdc-checkbox label="dense" v-model="dense"></mdc-checkbox>
</div>
</div>
</div>
</template>
Expand Down
3 changes: 2 additions & 1 deletion components/button/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {BasePlugin} from '../base'
import mdcButton from './mdc-button.vue'
import mdcButtonBase from './mdc-button-base.vue'

export {mdcButton}
export {mdcButtonBase, mdcButton}

export default BasePlugin({
mdcButton
Expand Down
24 changes: 24 additions & 0 deletions components/button/mdc-button-base.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<template>
<custom-button ref="root"
:class="classes" :style="styles"
:href="href" :link="link" :disabled="disabled"
@click="dispatchEvent">
<slot />
</custom-button>
</template>

<script>
import {DispatchEventMixin, CustomButtonMixin} from '../base'
import {RippleMixin} from '../ripple'
export default {
name: 'mdc-button-base',
mixins: [DispatchEventMixin, CustomButtonMixin, RippleMixin],
data () {
return {
classes: {},
styles: {}
}
}
}
</script>
33 changes: 3 additions & 30 deletions components/button/mdc-button.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
<template>
<custom-element
class="mdc-button"
:tag="isLink ? 'a': 'button'"
:href="isLink && href"
:role="isLink ? 'button' : undefined"
ref="root" :disabled="disabled"
:class="classes" :style="styles"
@click="dispatchEvent">
<slot />
</custom-element>
</template>

<script>
import {DispatchEventMixin, CustomElementMixin} from '../base'
import {RippleBase} from '../ripple'
import mdcButtonBase from './mdc-button-base.vue'
export default {
name: 'mdc-button',
mixins: [DispatchEventMixin, CustomElementMixin],
extends: mdcButtonBase,
props: {
href: String,
disabled: Boolean,
raised: Boolean,
unelevated: Boolean,
stroked: Boolean,
Expand All @@ -30,6 +14,7 @@ export default {
data () {
return {
classes: {
'mdc-button': true,
'mdc-button--raised': this.raised,
'mdc-button--unelevated': this.unelevated,
'mdc-button--stroked': this.stroked,
Expand All @@ -39,11 +24,6 @@ export default {
styles: {}
}
},
computed: {
isLink () {
return this.href && !this.disabled
}
},
watch: {
raised () {
this.$set(this.classes, 'mdc-button--raised', this.raised )
Expand All @@ -60,13 +40,6 @@ export default {
compact () {
this.$set(this.classes, 'mdc-button--compact', this.compact )
},
},
mounted () {
this.ripple = new RippleBase(this)
this.ripple.init()
},
beforeDestroy () {
this.ripple.destroy()
}
}
</script>

0 comments on commit 43a9d3f

Please sign in to comment.