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

Commit 82a9f12

Browse files
committed
docs: update responsive-styles docs
1 parent 55a73e9 commit 82a9f12

File tree

1 file changed

+107
-12
lines changed

1 file changed

+107
-12
lines changed

website/pages/responsive-styles.mdx

Lines changed: 107 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ you provide array values to add mobile-first responsive styles.
1717
> We use the `@media(min-width)` media queries to ensure values are
1818
> mobile-first.
1919
20+
Responsive syntax relies on the breakpoints defined in the theme object. Chakra
21+
UI provides default breakpoints, here's what it looks like:
22+
23+
```js
24+
const breakpoints = {
25+
sm: '30em', // 480px upwards
26+
md: '48em', // 768px upwards
27+
lg: '62em', // 992px upwards
28+
xl: '80em', // 1280px upwards
29+
'2xl': '96em' // 1536px upwards
30+
}
31+
```
32+
33+
To make styles responsive, you can use either the array or object syntax.
34+
35+
## The Array syntax
36+
37+
All style props accept arrays as values for mobile-first responsive styles. This
38+
is the recommended method.
39+
2040
```vue
2141
<template>
2242
<c-box
@@ -76,28 +96,103 @@ It'll generate a CSS that looks like this
7696
}
7797
```
7898

79-
8099
___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%'`
81100

82-
The equivalent of this style if you passed it as an object.
101+
To interpret array responsive values, Chakra UI converts the values defined in `theme.breakpoints`
102+
and sorts them in ascending order. Afterward, we map the
103+
values defined in the array to the breakpoints
83104

84105
```js
85-
// First, create an alias for breakpoints
86-
const breakpoints = ["30em", "48em", "62em", "80em"];
87-
// aliases
88-
breakpoints.sm = breakpoints[0];
89-
breakpoints.md = breakpoints[1];
90-
breakpoints.lg = breakpoints[2];
91-
breakpoints.xl = breakpoints[3];
106+
// This is the default breakpoint
107+
const breakpoints = {
108+
sm: '30em',
109+
md: '48em',
110+
lg: '62em',
111+
xl: '80em',
112+
'2xl': '96em'
113+
}
114+
115+
// Internally, we transform to this
116+
const breakpoints = ['0em', '30em', '48em', '62em', '80em', '96em']
92117
```
93118

119+
Here's how to interpret this syntax:
94120

95-
then
121+
- `300px`: From `0em` upwards
122+
- `400px`: From `30em` upwards
123+
- `500px`: From `48em` upwards
96124

97-
```vue
98-
<c-box :width="{ base: 1, sm: 1 / 2, md: 1 / 4 }" />
125+
> To skip certain breakpoints, you can pass `null` to any position in the array
126+
> to avoid generating unnecessary CSS.
127+
128+
## The Object syntax
129+
130+
You can also define responsive values with breakpoint aliases in an object. Any
131+
undefined alias key will define the base, non-responsive value.
132+
133+
Let's say you have a `CText` that looks like this:
134+
135+
```vue live=false
136+
<c-text font-size='40px'>This is a text</c-text>
137+
```
138+
139+
To make the `fontSize` responsive using the object syntax, here's what you need
140+
to do:
141+
142+
```vue live=false
143+
<c-text :font-size="{ base: '24px', md: '40px', lg: '56px' }">
144+
This is responsive text
145+
</c-text>
146+
```
147+
148+
> **Remember, Chakra UI uses the min-width media query for responsive design**.
149+
> The breakpoints are: `sm = 30em`, `md = 48em`, `lg = 62em`, `xl = 80em`
150+
151+
Here's how to interpret this syntax:
152+
153+
- `base`: From `0em` upwards
154+
- `md`: From `48em` upwards
155+
- `lg`: From `62em` upwards
156+
157+
## Customizing Breakpoints
158+
159+
In some scenarios, you might need to define custom breakpoints for your
160+
application. We recommended using common aliases like `sm`, `md`, `lg`, and
161+
`xl`.
162+
163+
To define custom breakpoints, pass them as an object containing the aliases.
164+
165+
> Note: Ensure the css unit of your breakpoints are the same. Use all `px` or
166+
> all `em`, don't mix them.
167+
168+
```js
169+
// 1. Import the utilities
170+
import { extendTheme } from '@chakra-ui/vue'
171+
172+
// 2. Update the breakpoints as key-value pairs
173+
const customBreakpoints = {
174+
sm: '320px',
175+
md: '768px',
176+
lg: '960px',
177+
xl: '1200px',
178+
'2xl': '1536px'
179+
}
180+
181+
// 3. Extend the theme
182+
Vue.use(Chakra, {
183+
extendTheme: {
184+
breakpoints: customBreakpoints
185+
...
186+
}
187+
}
188+
189+
// 4. Now you can use the custom breakpoints
190+
<c-box :width={ base: '100%', sm: '50%', md: '25%' } />
99191
```
100192
193+
> Note: If you're using **pixels** as breakpoint values make sure to **always**
194+
> provide a value for the `2xl` breakpoint, which by its default pixels value is
195+
> **"1536px"**.
101196
102197
## Demo
103198

0 commit comments

Comments
 (0)