-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
86 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { storiesOf } from '@storybook/vue' | ||
import { createComponent } from '@vue/composition-api' | ||
import Doc from '../../../__stories__/components/Doc' | ||
import { useMedia } from '../../..' | ||
|
||
const Demo = createComponent({ | ||
setup() { | ||
const isWide = useMedia('(min-width: 480px)') | ||
return { isWide } | ||
}, | ||
render() { | ||
const { isWide } = this | ||
return <div>Screen is wide(min-width: 480px): {isWide ? 'Yes' : 'No'}</div> | ||
} | ||
}) | ||
|
||
const Docs = () => <Doc md={require('./doc.md')}></Doc> | ||
|
||
storiesOf('Sensor|useMedia', module) | ||
.add('Docs', () => Docs as any) | ||
.add('Demo', () => Demo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# useMedia | ||
|
||
Vue hook that tracks state of a CSS media query. | ||
|
||
> Browser environment is required | ||
## Usage | ||
|
||
```jsx | ||
import { createComponent } from '@vue/composition-api' | ||
import { useMouse } from 'vuses' | ||
|
||
const Demo = createComponent({ | ||
setup() { | ||
const isWide = useMedia('(min-width: 480px)') | ||
return { isWide } | ||
}, | ||
render() { | ||
const { isWide } = this | ||
return <div>Screen is wide(min-width: 480px): {isWide ? 'Yes' : 'No'}</div> | ||
} | ||
}) | ||
``` | ||
|
||
## Reference | ||
|
||
```typescript | ||
function useMedia(query: string): Ref<boolean> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ref, onMounted, onUnmounted, Ref } from '@vue/composition-api' | ||
import { | ||
checkBrowser, | ||
addEventListener, | ||
removeEventListener | ||
} from '../../../utils' | ||
|
||
export default function useMedia(query: string): Ref<boolean> { | ||
checkBrowser(useMedia.name) | ||
|
||
const state = ref(window.matchMedia(query).matches) | ||
let mounted = true | ||
let mql: MediaQueryList = window.matchMedia(query) | ||
|
||
const update = () => { | ||
if (!mounted) return | ||
state.value = !!mql.matches | ||
} | ||
|
||
onMounted(() => { | ||
update() | ||
addEventListener(mql, 'change', update) | ||
}) | ||
|
||
onUnmounted(() => { | ||
mounted = false | ||
removeEventListener(mql, 'change', update) | ||
}) | ||
|
||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters