-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useMatchMedia and useBreakpoint (#14)
* feat: add useBreakpoint * feat: add useMatchMedia and allow to describe match media * test: test the usage of matchMedia * add matchMedia to the docs * move matchMedia and breakpoint and localstorage to `misc/`
- Loading branch information
Showing
19 changed files
with
470 additions
and
6 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 @@ | ||
<template> | ||
<div> | ||
<div>Current breakpoint {{ current || "none" }}</div> | ||
<div>valid breakpoints:</div> | ||
<p><b v-if="S">Small</b></p> | ||
<p><b v-if="L">Large</b></p> | ||
<p><b v-if="XL">Extra Large</b></p> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { useBreakpoint } from "vue-composable"; | ||
export default { | ||
name: "breakpoint-example", | ||
setup() { | ||
// it will return object with reactive properties {XL: boolean, L: boolean, S: boolean} | ||
return useBreakpoint({ XL: 1280, L: 768, S: "(min-width: 320px)" }); | ||
} | ||
}; | ||
</script> |
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,16 @@ | ||
<template> | ||
<div> | ||
<p>Screen less than 600px: <b>{{ matches }}</b></p> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { useMatchMedia } from "vue-composable"; | ||
export default { | ||
name: "match-media-example", | ||
setup() { | ||
return useMatchMedia("(max-width: 600px)"); | ||
} | ||
}; | ||
</script> |
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
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,73 @@ | ||
# Breakpoint | ||
|
||
Allows to get reactive object on the current windows size. | ||
|
||
::: warning | ||
If `Number` it checks the defined breakpoint against window.innerWidth. | ||
`bp >= window.innerWidth` | ||
If `MediaQuery` is passed it will not be able to resolve the current breakpoint | ||
::: | ||
|
||
## State | ||
|
||
The `useBreakpoint` function exposes the following reactive state: | ||
|
||
```js | ||
import { useBreakpoint } from "vue-composable"; | ||
|
||
const { current, /* properties from the arguments */ L, XL } = useBreakpoint({ | ||
L: 720, | ||
XL: 1280, | ||
S: "(min-width: 320px)" // also supports media query - it uses `useMatchMedia` | ||
}); | ||
``` | ||
|
||
| State | Type | Description | | ||
| ------- | -------- | --------------------------------------------------------------------- | | ||
| current | `String` | Smallest breakpoint key. where the breakpoint is an `Number` | | ||
| ...args | `Args` | returns the object with the same keys but with reactive boolean value | | ||
|
||
## Methods | ||
|
||
The `useBreakpoint` function exposes the following methods: | ||
|
||
```js | ||
import { useBreakpoint } from "vue-composable"; | ||
|
||
const { remove } = useBreakpoint(); | ||
``` | ||
|
||
| Signature | Description | | ||
| --------- | ----------------------------------- | | ||
| `remove` | Manually removes the event listener | | ||
|
||
## Example | ||
|
||
```vue | ||
<template> | ||
<div> | ||
<div>Current breakpoint {{ current || "none" }}</div> | ||
<div>valid breakpoints:</div> | ||
<p><b v-if="S">Small</b></p> | ||
<p><b v-if="L">Large</b></p> | ||
<p><b v-if="XL">Extra Large</b></p> | ||
</div> | ||
</template> | ||
<script> | ||
import { useBreakpoint } from "vue-composable"; | ||
export default { | ||
name: "breakpoint-example", | ||
setup() { | ||
// it will return object with reactive properties {XL: boolean, L: boolean, S: boolean} | ||
return useBreakpoint({ XL: 1280, L: 768, S: "(min-width: 320px)" }); | ||
} | ||
}; | ||
</script> | ||
``` | ||
|
||
### Code | ||
|
||
<ClientOnly> | ||
<breakpoint-example/> | ||
</ClientOnly> |
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,71 @@ | ||
# MatchMedia | ||
|
||
> The [matchMedia](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia). | ||
## Parameters | ||
|
||
```js | ||
import { useMatchMedia } from "vue-composable"; | ||
|
||
const matchMedia = useMatchMedia(mediaQueryString); | ||
``` | ||
|
||
| Parameters | Type | Description | | ||
| ---------------- | -------- | ----------------------------------------------- | | ||
| mediaQueryString | `String` | A string representing the media query to parse. | | ||
|
||
## State | ||
|
||
The `useMatchMedia` function exposes the following reactive state: | ||
|
||
```js | ||
import { useMatchMedia } from "vue-composable"; | ||
|
||
const { scrollTop, matches } = useMatchMedia(); | ||
``` | ||
|
||
| State | Type | Description | | ||
| -------------- | --------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | | ||
| mediaQueryList | [MediaQueryList](https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList) | List of objects stores information on a media query | | ||
| matches | `Boolean` | A Boolean that returns true if the document currently matches the media query list, or false if not. | | ||
|
||
## Methods | ||
|
||
The `useMatchMedia` function exposes the following methods: | ||
|
||
```js | ||
import { useMatchMedia } from "vue-composable"; | ||
|
||
const { remove } = useMatchMedia(); | ||
``` | ||
|
||
| Signature | Description | | ||
| --------- | ----------------------------------- | | ||
| `remove` | Manually removes the event listener | | ||
|
||
## Example | ||
|
||
```vue | ||
<template> | ||
<div> | ||
<p>Screen less than 600px: <b>{{ matches }}</b></p> | ||
</div> | ||
</template> | ||
<script> | ||
import { useMatchMedia } from "vue-composable"; | ||
export default { | ||
name: "match-media-example", | ||
setup() { | ||
return useMatchMedia("(max-width: 600px)"); | ||
} | ||
}; | ||
</script> | ||
``` | ||
|
||
### Code | ||
|
||
<ClientOnly> | ||
<match-media-example/> | ||
</ClientOnly> |
Oops, something went wrong.