Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 6225fa7

Browse files
Merge pull request #203 from TylerAPfledderer/feat/c-visibility
Create Media Query components and `useQuery` composable
2 parents 38d78a6 + e98e3f8 commit 6225fa7

File tree

12 files changed

+228
-0
lines changed

12 files changed

+228
-0
lines changed

packages/c-media-query/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# @chakra-ui/media-query
2+
3+
Media query components and composables
4+
5+
## Installation
6+
7+
```sh
8+
yarn add @chakra-ui/media-query
9+
# or
10+
npm i @chakra-ui/media-query
11+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<template>
2+
<div>
3+
<c-show above="sm">
4+
<div>Hey! I'll show above sm (480px)</div>
5+
</c-show>
6+
<c-hide below="md">
7+
<div>Hello!! I'll hide below md (768px)</div>
8+
</c-hide>
9+
<c-hide breakpoint="(max-width: 400px)">
10+
<div>Hello!! I'll be hidden at or below 400px</div>
11+
</c-hide>
12+
<c-show breakpoint="(max-width: 400px)">
13+
<div>Hello!! I'll be shown at or below 400px</div>
14+
</c-show>
15+
</div>
16+
</template>
17+
<script setup lang="ts">
18+
import { CShow, CHide } from "../src/index"
19+
</script>

packages/c-media-query/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './src'

packages/c-media-query/package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "@chakra-ui/c-media-query",
3+
"description": "Chakra UI Vue | Media query components and composables",
4+
"version": "0.0.0-alpha.0",
5+
"main": "dist/chakra-ui-c-media-query.cjs.js",
6+
"module": "dist/chakra-ui-c-media-query.esm.js",
7+
"author": "Jonathan Bakebwa <[email protected]>",
8+
"homepage": "https://github.com/chakra-ui/chakra-ui-vue-next#readme",
9+
"license": "MIT",
10+
"files": [
11+
"dist"
12+
],
13+
"exports": {
14+
".": {
15+
"require": "./dist/chakra-ui-c-media-query.cjs.js",
16+
"default": "./dist/chakra-ui-c-media-query.esm.js"
17+
}
18+
},
19+
"repository": "https://github.com/chakra-ui/chakra-ui-vue-next/tree/master/packages/c-media-query",
20+
"bugs": {
21+
"url": "https://github.com/chakra-ui/chakra-ui-vue-next/issues"
22+
},
23+
"sideEffects": false,
24+
"scripts": {
25+
"clean": "rimraf dist"
26+
},
27+
"dependencies": {
28+
"@chakra-ui/vue-system": "0.1.0-alpha.10",
29+
"@chakra-ui/vue-utils": "0.1.0-alpha.10",
30+
"@vueuse/core": "4.9.1"
31+
},
32+
"devDependencies": {
33+
"vue": "^3.2.37"
34+
},
35+
"peerDependencies": {
36+
"vue": "^3.1.4"
37+
},
38+
"publishConfig": {
39+
"access": "public"
40+
}
41+
}

packages/c-media-query/src/c-hide.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { h, defineComponent, PropType, unref } from "vue"
2+
import { getValidChildren } from "@chakra-ui/vue-utils"
3+
import { CVisibility } from "./c-visibility"
4+
import { useQuery } from "./use-query"
5+
import { ComponentWithProps } from "@chakra-ui/vue-system"
6+
import { CShowProps } from "./c-show"
7+
8+
export interface CHideProps extends CShowProps {}
9+
10+
/**
11+
* Wrapper component to hide child elements based on breakpoint value
12+
*/
13+
export const CHide: ComponentWithProps<CHideProps> = defineComponent({
14+
props: {
15+
breakpoint: {
16+
type: String as PropType<CHideProps["breakpoint"]>,
17+
},
18+
above: {
19+
type: String as PropType<CHideProps["above"]>,
20+
},
21+
below: {
22+
type: String as PropType<CHideProps["below"]>,
23+
},
24+
},
25+
setup(props, { slots }) {
26+
const query = useQuery(props)
27+
return () => (
28+
<CVisibility breakpoint={query.value} hide>
29+
{() => getValidChildren(slots)}
30+
</CVisibility>
31+
)
32+
},
33+
})

packages/c-media-query/src/c-show.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { h, defineComponent, PropType, unref } from "vue"
2+
import { getValidChildren } from "@chakra-ui/vue-utils"
3+
import { CVisibility } from "./c-visibility"
4+
import { useQuery } from "./use-query"
5+
import { ComponentWithProps } from "@chakra-ui/vue-system"
6+
7+
export interface CShowProps {
8+
/**
9+
* A custom css media query that determines when the `slots` are rendered.
10+
* Will render `slots` if that query resolves to `true`.
11+
*/
12+
breakpoint?: string
13+
/**
14+
* A value from the `breakpoints` section in the theme. Will render `slots`
15+
* from that breakpoint and below. Default breakpoint values: `sm`, `md`, `lg`, `xl`, `2xl`.
16+
*/
17+
below?: string
18+
/**
19+
* A value from the `breakpoints` section in the theme. Will render `slots`
20+
* from that breakpoint and above. Default breakpoint values: `sm`, `md`, `lg`, `xl`, `2xl`.
21+
*/
22+
above?: string
23+
}
24+
25+
/**
26+
* Wrapper component to show child elements based on breakpoint value
27+
*/
28+
export const CShow: ComponentWithProps<CShowProps> = defineComponent({
29+
props: {
30+
breakpoint: {
31+
type: String as PropType<CShowProps["breakpoint"]>,
32+
},
33+
above: {
34+
type: String as PropType<CShowProps["above"]>,
35+
},
36+
below: {
37+
type: String as PropType<CShowProps["below"]>,
38+
},
39+
},
40+
setup(props, { slots }) {
41+
const query = useQuery(props)
42+
return () => {
43+
return (
44+
<CVisibility breakpoint={query.value}>
45+
{() => getValidChildren(slots)}
46+
</CVisibility>
47+
)
48+
}
49+
},
50+
})
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { computed, defineComponent, PropType } from "vue"
2+
import { useMediaQuery } from "@vueuse/core"
3+
import { ComponentWithProps } from "@chakra-ui/vue-system"
4+
5+
export interface CVisbilityProps {
6+
/**
7+
* Test
8+
*/
9+
breakpoint: string
10+
hide?: boolean
11+
}
12+
13+
export const CVisibility: ComponentWithProps<CVisbilityProps> = defineComponent(
14+
{
15+
props: {
16+
breakpoint: {
17+
type: String as PropType<CVisbilityProps["breakpoint"]>,
18+
required: true,
19+
},
20+
hide: Boolean as PropType<CVisbilityProps["hide"]>,
21+
},
22+
setup(props, { slots }) {
23+
const show = useMediaQuery(props.breakpoint)
24+
25+
const isVisible = computed(() => (props.hide ? !show.value : show.value))
26+
27+
return () => (isVisible.value ? slots.default!() : null)
28+
},
29+
}
30+
)

packages/c-media-query/src/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./c-show"
2+
export * from "./c-hide"
3+
export * from "./c-visibility"
4+
export * from "./use-query"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { useTheme } from "@chakra-ui/vue-system"
2+
import { computed } from "vue"
3+
4+
const getBreakpoint = (theme: Record<string, any>, value: any) => {
5+
return theme?.breakpoints?.[value] ?? value
6+
}
7+
8+
export interface UseQueryProps {
9+
breakpoint?: string
10+
below?: string
11+
above?: string
12+
}
13+
14+
/**
15+
* Returns a media query value based on theme breakpoint or given query string.
16+
*/
17+
export function useQuery(props: UseQueryProps) {
18+
const { breakpoint = "", below, above } = props
19+
20+
const theme = useTheme()
21+
const bpBelow = getBreakpoint(theme, below)
22+
const bpAbove = getBreakpoint(theme, above)
23+
24+
const query = computed(() => {
25+
if (bpAbove) return `(min-width: ${bpAbove})`
26+
27+
if (bpBelow) return `(max-width: ${bpBelow})`
28+
29+
return breakpoint
30+
})
31+
32+
return query
33+
}

packages/c-media-query/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"include": ["src", "./index.tsx", "./index.ts"]
4+
}

0 commit comments

Comments
 (0)