Skip to content

Commit

Permalink
Merge pull request #3 from cwist2/bugfix/fix-composable
Browse files Browse the repository at this point in the history
Fix composable use
  • Loading branch information
cwist2 authored Apr 16, 2021
2 parents edc3fc9 + 02eaac0 commit 4f14e0b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
18 changes: 5 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { App, inject, InjectionKey, reactive } from 'vue';
import { App, reactive } from 'vue';
import { matchMediaKey } from './useApi';

declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
Expand Down Expand Up @@ -80,16 +81,6 @@ function populateRules(
return rules;
}

export const VueMatchMediaPluginSymbol: InjectionKey<VueMatchMediaPlugin> = Symbol(
'VueMatchMediaPlugin'
);
export function useMatchMedia(): VueMatchMediaPlugin {
const matchMediaPlugin = inject(VueMatchMediaPluginSymbol);
if (!matchMediaPlugin) throw new Error('No VueMatchMediaPlugin provided!!!');

return matchMediaPlugin;
}

export function createVueMatchMediaPlugin(
options?: VueMatchMediaPluginOptions
): VueMatchMediaPlugin {
Expand Down Expand Up @@ -152,9 +143,10 @@ export function createVueMatchMediaPlugin(
);

app.config.globalProperties.$matchMedia = matchMediaObservable;

app.provide(VueMatchMediaPluginSymbol, matchMediaObservable);
app.provide(matchMediaKey, matchMediaObservable)
}
},
};
}

export { useMatchMedia } from './useApi';
17 changes: 17 additions & 0 deletions src/useApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { inject, InjectionKey } from 'vue';

const hasSymbol =
typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'

const PolySymbol = (name: string) =>
hasSymbol
? Symbol(name)
: name

export const matchMediaKey = /*#__PURE__*/ PolySymbol(
'mm'
) as InjectionKey<Record<string, boolean>>

export function useMatchMedia(): Record<string, boolean> {
return inject(matchMediaKey)!
}

0 comments on commit 4f14e0b

Please sign in to comment.