@thinkpixellab-public/px-vue 3.0.7 → 3.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Use for global mobile awareness in javascript. This doesn't actually handle mobile detection
|
|
3
|
+
but provides a consistent interface for communicating between a single component that does
|
|
4
|
+
the detection and other components that subscribe to an eventbus and update a property called
|
|
5
|
+
isMobile.
|
|
6
|
+
|
|
7
|
+
Sample usage using a css variable for detecting mobile (this keeps css and js in sync):
|
|
8
|
+
|
|
9
|
+
### MyLayout.vue (or other global component)
|
|
10
|
+
|
|
11
|
+
// add PxBaseMobileAware as a mixin
|
|
12
|
+
import PxBaseMobileAware from '@thinkpixellab-public/px-vue/components/PxBaseMobileAware.vue';
|
|
13
|
+
mixins: [PxBaseMobileAware],
|
|
14
|
+
|
|
15
|
+
// add a resize handler and call setGlobalIsMobile
|
|
16
|
+
resize() {
|
|
17
|
+
if (this.$el) {
|
|
18
|
+
const cssMobile = getComputedStyle(this.$el).getPropertyValue('--is-mobile');
|
|
19
|
+
this.setGlobalIsMobile(cssMobile.toLowerCase().trim() == 'true');
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
// add a css variable that changes with the viewport
|
|
24
|
+
|
|
25
|
+
.my-layout {
|
|
26
|
+
--is-mobile: false;
|
|
27
|
+
@include media-until-mobile() {
|
|
28
|
+
--is-mobile: true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
### MyComponent.vue
|
|
33
|
+
|
|
34
|
+
// use PxBaseMobileAware as a mixin
|
|
35
|
+
import PxBaseMobileAware from '@thinkpixellab-public/px-vue/components/PxBaseMobileAware.vue';
|
|
36
|
+
mixins: [PxBaseMobileAware]
|
|
37
|
+
|
|
38
|
+
// use the isMobile property as needed in a template
|
|
39
|
+
<div> This is mobile: {{isMobile ? 'yup' : 'nope' }} </div>
|
|
40
|
+
|
|
41
|
+
// or in code
|
|
42
|
+
watch: {
|
|
43
|
+
isMobile(nv) {
|
|
44
|
+
// do something when isMobile changes
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
-->
|
|
50
|
+
|
|
51
|
+
<script>
|
|
52
|
+
export default {
|
|
53
|
+
data() {
|
|
54
|
+
return { isMobile: null };
|
|
55
|
+
},
|
|
56
|
+
mounted() {
|
|
57
|
+
if (this.$globalOn) {
|
|
58
|
+
this.$globalOn('mobile-changed', isMobile => {
|
|
59
|
+
this.isMobile = isMobile;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
methods: {
|
|
64
|
+
setGlobalIsMobile(isMobile) {
|
|
65
|
+
if (this.$globalEmit) {
|
|
66
|
+
this.$globalEmit('mobile-changed', isMobile);
|
|
67
|
+
this.isMobile = isMobile;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
</script>
|