Skip to content

Commit

Permalink
Fix reactivity, externalize Vue
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberAP committed Jul 26, 2020
1 parent a53b8f9 commit 04ca98a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
8 changes: 1 addition & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "vue-component-media-queries",
"version": "1.0.0-alpha.3",
"version": "1.0.0-alpha.4",
"description": "Component-based media query matcher for Vue",
"main": "dist/index.js",
"module": "dist/index.es.js",
"types": "dist/src/index.d.ts",
"scripts": {
"build": "rollup -c"
"build": "rollup -c",
"build:watch": "rollup -c -w"
},
"repository": {
"type": "git",
Expand All @@ -32,8 +33,7 @@
"rollup-plugin-terser": "^5.1.3",
"rollup-plugin-typescript2": "^0.25.3",
"tslib": "^1.10.0",
"typescript": "^3.7.4",
"vue": "^2.6.0"
"typescript": "^3.7.4"
},
"peerDependencies": {
"vue": "^2.6.0"
Expand Down
4 changes: 2 additions & 2 deletions src/MatchMedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Data = {
}

export const MatchMedia = Vue.extend({
name: 'MatchMedia',
inject: {
mediaQueries: {
default: null
Expand Down Expand Up @@ -66,7 +67,6 @@ export const MatchMedia = Vue.extend({
},
},
render(h): any {
const nodes = this.$scopedSlots.default!(this.slotProps);
return renderWrappedNodes(h, nodes, this.wrapperTag);
return renderWrappedNodes(h, this.$scopedSlots.default!(this.slotProps)!, this.wrapperTag);
},
});
28 changes: 17 additions & 11 deletions src/MediaQueryProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { renderWrappedNodes } from "./utils";

type MediaQueriesConfig = Record<string, string>;
type MediaEventListener = () => void;
type Matchers = [MediaQueryList, MediaEventListener][] | null;
type Fallback = string | string[];
type Data = {
mediaQueries: MediaQueriesProvision,
matchers: [MediaQueryList, MediaEventListener][]
}

export type MediaQueriesProvision = Record<string, boolean>;

export const MediaQueryProvider = Vue.extend({
name: 'MediaQueryProvider',
props: {
queries: {
type: Object as PropType<MediaQueriesConfig>,
Expand All @@ -20,20 +24,22 @@ export const MediaQueryProvider = Vue.extend({
default: 'span'
},
},
provide(this: { mediaQueries: MediaQueriesProvision }) {
provide(): { mediaQueries: MediaQueriesProvision } {
return { mediaQueries: this.mediaQueries };
},
data() {
data(): Data {
const mediaQueries = {} as MediaQueriesProvision;
const { fallback } = this;

if (fallback) {
if (Array.isArray(fallback)) {
Object.keys(fallback).forEach(key => mediaQueries[key] = true);
fallback.forEach(key => { mediaQueries[key] = true; });
} else {
mediaQueries[fallback] = true;
}
}
return { mediaQueries, matchers: [] as Matchers };

return { mediaQueries, matchers: [] };
},
beforeMount() {
const { queries, mediaQueries } = this;
Expand All @@ -43,21 +49,21 @@ export const MediaQueryProvider = Vue.extend({

const matcher = window.matchMedia(query);
const handler = (event: MediaQueryListEvent) => {
mediaQueries[key] = event.matches;
Vue.set(mediaQueries, key, event.matches);
};
// using deprecated method because of Safari's poor support for addEventListener
matcher.addListener(handler);
mediaQueries[key] = matcher.matches;
this.matchers!.push([matcher, handler as MediaEventListener]);
Vue.set(mediaQueries, key, matcher.matches);
this.matchers.push([matcher, handler as MediaEventListener]);
}
},
beforeDestroy() {
this.matchers!.forEach(([matcher, listener]) => {
this.matchers.forEach(([matcher, listener]) => {
matcher.removeListener(listener);
});
this.matchers = null;
delete this.matchers;
},
render(h): any {
return renderWrappedNodes(h, this.$slots.default, this.wrapperTag);
return renderWrappedNodes(h, this.$slots.default!, this.wrapperTag);
}
});
3 changes: 1 addition & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CreateElement, VNode } from 'Vue';

export const renderWrappedNodes = (h: CreateElement, nodes: undefined | VNode | VNode[], wrapperTag: string) => {
if (!nodes) return null;
export const renderWrappedNodes = (h: CreateElement, nodes: VNode | VNode[], wrapperTag: string) => {
if (Array.isArray(nodes)) {
if (nodes.length !== 1 || !nodes[0].tag) return h(wrapperTag, {}, nodes);
else return nodes[0];
Expand Down

0 comments on commit 04ca98a

Please sign in to comment.