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

Commit b718722

Browse files
committed
docs: add responsive styles
1 parent 3fc5158 commit b718722

File tree

2 files changed

+273
-1
lines changed

2 files changed

+273
-1
lines changed

docs/.vitepress/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ function getSetupSidebar() {
5050
text: 'Features',
5151
children: [
5252
{ text: 'Style Props', link: '/pages/features/style-props' },
53-
{ text: 'Gradient', link: '/pages/features/gradient' }
53+
{ text: 'Gradient', link: '/pages/features/gradient' },
54+
{ text: 'Responsive Styles', link: '/pages/features/responsive-styles' }
5455
]
5556
},
5657
{
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
# Responsive Styles
2+
3+
Chakra UI supports responsive styles out of the box. Instead of manually adding
4+
`@media` queries and adding nested styles throughout your code, Chakra UI allows
5+
you to provide object and array values to add mobile-first responsive styles.
6+
7+
> We use the `@media(min-width)` media query to ensure your interfaces are
8+
> mobile-first.
9+
10+
Responsive syntax relies on the breakpoints defined in the theme object. Chakra
11+
UI provides default breakpoints, here's what it looks like:
12+
13+
```js
14+
import { createBreakpoints } from "@chakra-ui/theme-tools"
15+
16+
const breakpoints = createBreakpoints({
17+
sm: "30em",
18+
md: "48em",
19+
lg: "62em",
20+
xl: "80em",
21+
"2xl": "96em",
22+
})
23+
```
24+
25+
To make styles responsive, you can use either the array or object syntax.
26+
27+
## The Array syntax
28+
29+
All style props accept arrays as values for mobile-first responsive styles. This
30+
is the recommended method.
31+
32+
Let's say you have a `Box` with the following properties:
33+
34+
```jsx live=false
35+
<c-box bg="red.200" w="400px">
36+
This is a box
37+
</c-box>
38+
```
39+
40+
To make the `width` or `w` responsive using the array syntax, here's what you
41+
need to do:
42+
43+
```jsx live=false
44+
<c-box bg="red.200" :w=[300, 400, 500]>
45+
This is a box
46+
</c-box>
47+
```
48+
49+
To interpret array responsive values, Chakra UI converts the values defined in
50+
`theme.breakpoints` and sorts them in ascending order. Afterward, we map the
51+
values defined in the array to the breakpoints
52+
53+
```js
54+
import { createBreakpoints } from "@chakra-ui/theme-tools"
55+
56+
// This is the default breakpoint
57+
const breakpoints = createBreakpoints({
58+
sm: "30em",
59+
md: "48em",
60+
lg: "62em",
61+
xl: "80em",
62+
"2xl": "96em",
63+
})
64+
65+
// Internally, we transform to this
66+
const breakpoints = ["0em", "30em", "48em", "62em", "80em", "96em"]
67+
```
68+
69+
Here's how to interpret this syntax:
70+
71+
- `300px`: From `0em` upwards
72+
- `400px`: From `30em` upwards
73+
- `500px`: From `48em` upwards
74+
75+
> To skip certain breakpoints, you can pass `null` to any position in the array
76+
> to avoid generating unnecessary CSS.
77+
78+
## The Object syntax
79+
80+
You can also define responsive values with breakpoint aliases in an object. Any
81+
undefined alias key will define the base, non-responsive value.
82+
83+
Let's say you have a `Text` that looks like this:
84+
85+
```jsx live=false
86+
<c-text fontSize="40px">This is a text</c-text>
87+
```
88+
89+
To make the `fontSize` responsive using the object syntax, here's what you need
90+
to do:
91+
92+
```jsx live=false
93+
<c-text :fontSize={ base: "24px", md: "40px", lg: "56px" }>
94+
This is responsive text
95+
</c-text>
96+
```
97+
98+
> **Remember, Chakra UI uses the min-width media query for responsive design**.
99+
> The breakpoints are: `sm = 30em`, `md = 48em`, `lg = 62em`, `xl = 80em`
100+
101+
Here's how to interpret this syntax:
102+
103+
- `base`: From `0em` upwards
104+
- `md`: From `48em` upwards
105+
- `lg`: From `62em` upwards
106+
107+
## More Examples
108+
109+
This works for every style prop in the theme specification, which means you can
110+
change the style of most properties at a given breakpoint.
111+
112+
```jsx live=false
113+
<>
114+
<c-box
115+
:height={
116+
base: "100%", // 0-48em
117+
md: "50%", // 48em-80em,
118+
xl: "25%", // 80em+
119+
}
120+
bg="teal.400"
121+
:width=[
122+
"100%", // 0-30em
123+
"50%", // 30em-48em
124+
"25%", // 48em-62em
125+
"15%", // 62em+
126+
]
127+
/>
128+
{/* responsive font size */}
129+
<c-box :fontSize=["sm", "md", "lg", "xl"]>Font Size</c-box>
130+
{/* responsive margin */}
131+
<c-box :mt=[2, 4, 6, 8] width="full" height="24px" bg="tomato" />
132+
{/* responsive padding */}
133+
<c-box bg="papayawhip" :p=[2, 4, 6, 8]>
134+
Padding
135+
</c-box>
136+
</>
137+
```
138+
139+
## Under the hood
140+
141+
This shortcut is an alternative to writing media queries out by hand. Given the
142+
following:
143+
144+
```jsx live=false
145+
<c-box :width=[1, 1 / 2, 1 / 4] />
146+
or
147+
<c-box :width="['100%', 0.5, 0.25]" />
148+
```
149+
150+
It'll generate a CSS that looks like this
151+
152+
```css
153+
.Box {
154+
width: 100%;
155+
}
156+
157+
@media screen and (min-width: 40em) {
158+
.Box {
159+
width: 50%;
160+
}
161+
}
162+
163+
@media screen and (min-width: 52em) {
164+
.Box {
165+
width: 25%;
166+
}
167+
}
168+
```
169+
170+
___NOTE___: In the shortcut example `'100%'` is used instead of `1` because in the default Chakra UI Vue theme, `theme.sizes[1] = 0.25rem`. This means that using a prop like `:width="1"` will render a width of 4px and not `'100%'`
171+
172+
The equivalent of this style if you passed it as an object.
173+
174+
```js
175+
// First, create an alias for breakpoints
176+
const breakpoints = ["30em", "48em", "62em", "80em"];
177+
// aliases
178+
breakpoints.sm = breakpoints[0];
179+
breakpoints.md = breakpoints[1];
180+
breakpoints.lg = breakpoints[2];
181+
breakpoints.xl = breakpoints[3];
182+
```
183+
184+
then
185+
186+
```vue
187+
<c-box :width="{ base: 1, sm: 1 / 2, md: 1 / 4 }" />
188+
```
189+
190+
## Customizing Breakpoints
191+
192+
In some scenarios, you might need to define custom breakpoints for your
193+
application. We recommended using common aliases like `sm`, `md`, `lg`, and
194+
`xl`.
195+
196+
To define custom breakpoints, install `@chakra-ui/theme-tools`, and use the
197+
`createBreakpoints` utility we provide.
198+
199+
> Note: Ensure the css unit of your breakpoints are the same. Use all `px` or
200+
> all `em`, don't mix them.
201+
202+
```jsx live=false
203+
// 1. Import the utilities
204+
import { extendTheme } from "@chakra-ui/vue-next"
205+
import { createBreakpoints } from "@chakra-ui/theme-tools"
206+
207+
// 2. Update the breakpoints as key-value pairs
208+
const breakpoints = createBreakpoints({
209+
sm: "320px",
210+
md: "768px",
211+
lg: "960px",
212+
xl: "1200px",
213+
"2xl": "1536px",
214+
})
215+
216+
// 3. Extend the theme
217+
const theme = extendTheme({ breakpoints })
218+
219+
// 4. Now you can use the custom breakpoints
220+
function Example() {
221+
return <c-box :width={ base: "100%", sm: "50%", md: "25%" } />
222+
}
223+
```
224+
225+
> Note: If you're using **pixels** as breakpoint values make sure to **always**
226+
> provide a value for the `2xl` breakpoint, which by its default pixels value is
227+
> **"1536px"**.
228+
229+
## Demo
230+
231+
Here's a simple example of a marketing page component that uses a stacked layout
232+
on small screens, and a side-by-side layout on larger screens **(resize your
233+
browser to see it in action)**:
234+
235+
```vue
236+
<c-box p="4" :display={ md: "flex" }>
237+
<c-box flexShrink="0">
238+
<c-image
239+
borderRadius="lg"
240+
:width={ md: 40 }
241+
src="https://bit.ly/2jYM25F"
242+
alt="Person paying for a purchase"
243+
/>
244+
</c-box>
245+
<c-box :mt={ base: 4, md: 0 } :ml={ md: 6 }>
246+
<c-text
247+
fontWeight="bold"
248+
textTransform="uppercase"
249+
fontSize="sm"
250+
letterSpacing="wide"
251+
color="teal.600"
252+
>
253+
Marketing
254+
</c-text>
255+
<c-link
256+
mt="1"
257+
display="block"
258+
fontSize="lg"
259+
lineHeight="normal"
260+
fontWeight="semibold"
261+
href="#"
262+
>
263+
Finding customers for your new business
264+
</c-link>
265+
<c-text mt="2" color="gray.500">
266+
Getting a new business off the ground is a lot of hard work. Here are five
267+
ideas you can use to find your first customers.
268+
</c-text>
269+
</c-box>
270+
</c-box>
271+
```

0 commit comments

Comments
 (0)