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

Upgrade material-components-web to v2.0 #223

Merged
merged 4 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules/
yarn-error.log
reports/
dist/
.idea/
yarn.lock
2 changes: 2 additions & 0 deletions components/list/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export default {
this.mdcList.singleSelection = this.singleSelection
this.mdcList.wrapFocus = this.wrapFocus
this.mdcList.vertical = this.vertical
} else {
this.$el.setAttribute('tabindex', '0')
}
},
beforeDestroy () {
Expand Down
64 changes: 58 additions & 6 deletions components/menu/Menu.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div
:tabindex="mdcMenu ? (mdcMenu.open ? 0 : -1) : (open ? 0 : -1)"
:tabindex="tabIndex"
class="mdc-menu mdc-menu-surface"
@MDCMenu:selected="onSelect"
>
Expand All @@ -10,6 +10,7 @@

<script>
import { Corner, MDCMenu } from '@material/menu'
import { DefaultFocusState } from '@material/menu/constants'

import { baseComponentMixin, themeClassMixin } from '../base'

Expand All @@ -34,11 +35,15 @@ export default {
},
absolutePositionX: {
type: Number,
default: -1
default: null
},
absolutePositionY: {
type: Number,
default: -1
default: null
},
hoistToBody: {
type: Boolean,
default: false
},
fixed: {
type: Boolean,
Expand All @@ -47,6 +52,10 @@ export default {
wrapFocus: {
type: Boolean,
default: true
},
defaultFocusState: {
type: [Number, String],
default: null
}
},
data () {
Expand All @@ -63,6 +72,32 @@ export default {
set (value) {
this.$emit('change', value)
}
},
focusState () {
if (!this.defaultFocusState) return null

const upperCaseFocusState = String(this.defaultFocusState).toUpperCase()
if (isNaN(this.defaultFocusState) &&
DefaultFocusState.hasOwnProperty(upperCaseFocusState)
) {
return DefaultFocusState[upperCaseFocusState]
}

const numberFocusState = Number(this.defaultFocusState)
if (!isNaN(this.defaultFocusState) &&
DefaultFocusState.hasOwnProperty(numberFocusState)
) {
return numberFocusState
}

return null
},
tabIndex () {
if (this.$slots.default[0].componentOptions.tag.toLowerCase() === 'm-list') {
return -1
}

return this.mdcMenu ? (this.mdcMenu.open ? 0 : -1) : (this.open ? 0 : -1)
}
},
watch: {
Expand All @@ -72,6 +107,11 @@ export default {
quickOpen () {
this.mdcMenu.quickOpen = this.quickOpen
},
hoistToBody () {
if (this.hoistToBody) {
this.mdcMenu.hoistMenuToBody()
}
},
fixed () {
this.mdcMenu.setFixedPosition(this.fixed)
},
Expand All @@ -83,18 +123,23 @@ export default {
}
},
absolutePositionX () {
if (this.absolutePositionX > -1 && this.absolutePositionY > -1) {
if (this.absolutePositionX !== null) {
this.mdcMenu.setAbsolutePosition(this.absolutePositionX, this.absolutePositionY)
}
},
absolutePositionY () {
if (this.absolutePositionX > -1 && this.absolutePositionY > -1) {
if (this.absolutePositionY !== null) {
this.mdcMenu.setAbsolutePosition(this.absolutePositionX, this.absolutePositionY)
}
},
wrapFocus () {
this.mdcMenu.wrapFocus = this.wrapFocus
},
defaultFocusState () {
if (this.focusState !== null) {
this.mdcMenu.setDefaultFocusState(this.focusState)
}
},
'mdcMenu.open' () {
this.model = this.mdcMenu.open
}
Expand All @@ -107,14 +152,21 @@ export default {
subtree: true
})
this.mdcMenu = MDCMenu.attachTo(this.$el)
if (this.hoistToBody) {
this.mdcMenu.hoistMenuToBody()
}
this.mdcMenu.setFixedPosition(this.fixed)
if (this.anchorCorner !== '') {
this.mdcMenu.setAnchorCorner(Corner[this.anchorCorner.toUpperCase()])
}
if (this.absolutePositionX > -1 && this.absolutePositionY > -1) {
if (this.absolutePositionX !== null || this.absolutePositionY !== null) {
this.mdcMenu.setAbsolutePosition(this.absolutePositionX, this.absolutePositionY)
}
this.mdcMenu.wrapFocus = this.wrapFocus

if (this.focusState !== null) {
this.mdcMenu.setDefaultFocusState(this.focusState)
}
},
beforeDestroy () {
this.slotObserver.disconnect()
Expand Down
11 changes: 9 additions & 2 deletions components/menu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ data() {

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| quickOpen | Boolean | false | deactivates menu animation |
| open | Boolean | false | open the menu (could be v-modeled) |
| quickOpen | Boolean | false | Deactivates menu animation. |
| open | Boolean | false | Open the menu (could be v-modeled). |
| anchorCorner | String | `TOP_START` | Sets the corner that the menu surface will be anchored to. Can be one of the following: `TOP_LEFT`, `TOP_RIGHT`, `BOTTOM_LEFT`, `BOTTOM_RIGHT`, `TOP_START`, `TOP_END`, `BOTTOM_START`, `BOTTOM_END` |
| absolutePositionX | Number | null | Sets the absolute x position of the menu. Should only be used when the menu is hoisted or using fixed positioning. |
| absolutePositionY | Number | null | Sets the absolute y position of the menu. Should only be used when the menu is hoisted or using fixed positioning. |
| hoistToBody | Boolean | false | Removes the menu-surface element from the DOM and appends it to the body element. Should be used to overcome overflow: hidden issues. |
| fixed | Boolean | false | Sets whether the menu surface is using fixed positioning. |
| wrapFocus| Boolean| true | Sets the list to allow the up arrow on the first element to focus the last element of the list and vice versa. |
| defaultFocusState | [Number, String] | null | Sets default focus state where the menu should focus every time when menu is opened. Focuses the list root (`LIST_ROOT`) element by default. Can be one of the following: `NONE`, `LIST_ROOT`, `FIRST_ITEM`, `LAST_ITEM` |

### Events

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"test:updateSnapshot": "yarn test --updateSnapshot"
},
"dependencies": {
"material-components-web": "^1.1.0"
"material-components-web": "^2.0.0"
},
"peerDependencies": {
"vue": "^2.6.10",
Expand Down