Skip to content

Commit 409dd11

Browse files
committedJul 10, 2017
feat: create focused interaction for buttons
1 parent 7197dc8 commit 409dd11

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed
 

‎src/components/MdButton/MdButton.vue

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
22
import MdComponent from 'core/MdComponent'
3-
import ripple from 'core/mixins/ripple'
3+
import MdFocused from 'core/mixins/MdFocused/MdFocused'
4+
import MdRipple from 'core/mixins/MdRipple/MdRipple'
45
import MdRouterLinkProps from 'core/MdRouterLinkProps'
56
import MdButtonContent from './MdButtonContent'
67
@@ -9,7 +10,10 @@
910
components: {
1011
MdButtonContent
1112
},
12-
mixins: [ripple],
13+
mixins: [
14+
MdRipple,
15+
MdFocused
16+
],
1317
props: {
1418
href: String,
1519
type: {
@@ -31,7 +35,8 @@
3135
class: [
3236
this.$mdActiveTheme,
3337
{
34-
'md-ripple-off': !this.mdRipple
38+
'md-ripple-off': !this.mdRipple,
39+
'md-focused': this.mdHasFocus
3540
}
3641
],
3742
attrs: {

‎src/components/MdList/MdListItem/MdListItemMixin.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import ripple from 'core/mixins/ripple'
1+
import MdRipple from 'core/mixins/MdRipple/MdRipple'
22
import MdListItemContent from './MdListItemContent'
33

44
export default {
5-
mixins: [ripple],
5+
mixins: [MdRipple],
66
components: {
77
MdListItemContent
88
},
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import MdReactive from 'core/MdReactive'
2+
3+
const eventTarget = document.body
4+
5+
let supportsPassiveEvent = false
6+
7+
let MdFocused = new MdReactive({
8+
currentElement: null
9+
})
10+
11+
function checkPassiveEventSupport () {
12+
try {
13+
var opts = Object.defineProperty({}, 'passive', {
14+
get () {
15+
supportsPassiveEvent = { passive: true }
16+
}
17+
})
18+
window.addEventListener('test', null, opts)
19+
} catch (e) {}
20+
}
21+
22+
function setKeyboardInteraction ({ keyCode, target }) {
23+
MdFocused.currentElement = target
24+
}
25+
26+
function setMouseAndTouchInteraction (event) {
27+
MdFocused.currentElement = null
28+
}
29+
30+
function createKeyboardEvents () {
31+
eventTarget.addEventListener('keyup', setKeyboardInteraction)
32+
}
33+
34+
function createPointerEvents () {
35+
eventTarget.addEventListener('pointerup', setMouseAndTouchInteraction)
36+
}
37+
38+
function createMSPointerEvents () {
39+
eventTarget.addEventListener('MSPointerUp', setMouseAndTouchInteraction)
40+
}
41+
42+
function createMouseAndTouchEvents () {
43+
eventTarget.addEventListener('mouseup', setMouseAndTouchInteraction)
44+
45+
if ('ontouchend' in window) {
46+
eventTarget.addEventListener('touchend', setMouseAndTouchInteraction, supportsPassiveEvent)
47+
}
48+
}
49+
50+
function bindEvents () {
51+
if (window.PointerEvent) {
52+
createPointerEvents()
53+
} else if (window.MSPointerEvent) {
54+
createMSPointerEvents()
55+
} else {
56+
createMouseAndTouchEvents()
57+
}
58+
59+
createKeyboardEvents()
60+
}
61+
62+
checkPassiveEventSupport()
63+
bindEvents()
64+
65+
export default {
66+
data: () => ({
67+
mdHasFocus: false
68+
}),
69+
computed: {
70+
focusedElement () {
71+
return MdFocused.currentElement
72+
}
73+
},
74+
watch: {
75+
focusedElement (el) {
76+
this.mdHasFocus = el === this.$el
77+
}
78+
}
79+
}
File renamed without changes.

0 commit comments

Comments
 (0)