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

Commit f444711

Browse files
Merge pull request #505 from carwack/bug/504-responsive-styles-breaking
fix(theme): Responsive styles breaking
2 parents 09c90d8 + 8944b13 commit f444711

File tree

6 files changed

+147
-21
lines changed

6 files changed

+147
-21
lines changed

.changeset/young-eagles-stare.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@chakra-ui/vue": patch
3+
"@chakra-ui/theme-vue": patch
4+
"chakra-ui-docs": patch
5+
---
6+
7+
fix(theme): Responsive styles breaking

packages/chakra-ui-core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
"max-overall-size": "85KB"
3737
},
3838
"dependencies": {
39-
"@chakra-ui/styled-system": "^1.12.2",
40-
"@chakra-ui/theme-vue": "^0.3.2",
39+
"@chakra-ui/styled-system": "^1.15.0",
40+
"@chakra-ui/theme-vue": "^0.3.2-next.0",
4141
"@popperjs/core": "^2.4.0",
4242
"animejs": "^3.1.0",
4343
"aria-hidden": "^1.1.1",

packages/chakra-ui-core/src/CHeading/tests/__snapshots__/CHeading.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports[`should render correctly 1`] = `
99
font-family: var(--chakra-fonts-heading);
1010
}
1111
12-
@media screen and (min-width: 62em) {
12+
@media screen and (min-width: 48em) {
1313
.emotion-0 {
1414
font-size: var(--chakra-fontSizes-4xl);
1515
}

packages/chakra-ui-theme/src/theme/breakpoints.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
* @description These breakpoint styles were adapted from [@chakra-ui](https://chakra-ui.com/)
33
*/
44

5-
const _breakpoints = ['30em', '48em', '62em', '80em']
6-
7-
_breakpoints.sm = _breakpoints[0]
8-
_breakpoints.md = _breakpoints[1]
9-
_breakpoints.lg = _breakpoints[2]
10-
_breakpoints.xl = _breakpoints[3]
5+
const _breakpoints = {
6+
sm: '30em',
7+
md: '48em',
8+
lg: '62em',
9+
xl: '80em',
10+
'2xl': '96em'
11+
}
1112

1213
export const createBreakpoints = (
1314
config = {}

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

yarn.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,6 +2245,14 @@
22452245
"@chakra-ui/utils" "1.8.2"
22462246
csstype "^3.0.6"
22472247

2248+
"@chakra-ui/styled-system@^1.15.0":
2249+
version "1.15.0"
2250+
resolved "https://registry.yarnpkg.com/@chakra-ui/styled-system/-/styled-system-1.15.0.tgz#fe117ace12b452379e4df69f4779395ead55917f"
2251+
integrity sha512-LnsKeiYkUuJ+NMTwueiX0Mj8CW9XAMJrJxpQm/X3GY5L5PO7Hv6wW725Ovqdy4mhG3IK7S8444FthpsDv/luHw==
2252+
dependencies:
2253+
"@chakra-ui/utils" "1.9.1"
2254+
csstype "^3.0.9"
2255+
22482256
"@chakra-ui/[email protected]":
22492257
version "1.8.2"
22502258
resolved "https://registry.yarnpkg.com/@chakra-ui/utils/-/utils-1.8.2.tgz#5a9f1f67c5f2232769fe7d009fcf96eebf3c2b4e"
@@ -2255,6 +2263,16 @@
22552263
framesync "5.3.0"
22562264
lodash.mergewith "4.6.2"
22572265

2266+
"@chakra-ui/[email protected]":
2267+
version "1.9.1"
2268+
resolved "https://registry.yarnpkg.com/@chakra-ui/utils/-/utils-1.9.1.tgz#e34bf74f9f6048da1ec2a5728db4cc80d0158e0e"
2269+
integrity sha512-Tue8JfpzOqeHd8vSqAnX1l/Y3Gg456+BXFP/TH6mCIeqMAMbrvv25vDskds0wlXRjMYdmpqHxCEzkalFrscGHA==
2270+
dependencies:
2271+
"@types/lodash.mergewith" "4.6.6"
2272+
css-box-model "1.2.1"
2273+
framesync "5.3.0"
2274+
lodash.mergewith "4.6.2"
2275+
22582276
"@changesets/apply-release-plan@^3.0.1":
22592277
version "3.0.2"
22602278
resolved "https://registry.yarnpkg.com/@changesets/apply-release-plan/-/apply-release-plan-3.0.2.tgz#e8f77518f065181717cc998d60b9dcaeb7387870"
@@ -10545,6 +10563,11 @@ csstype@^3.0.6:
1054510563
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.0.8.tgz#d2266a792729fb227cd216fb572f43728e1ad340"
1054610564
integrity sha512-jXKhWqXPmlUeoQnF/EhTtTl4C9SnrxSH/jZUih3jmO6lBKr99rP3/+FmrMj4EFpOXzMtXHAZkd3x0E6h6Fgflw==
1054710565

10566+
csstype@^3.0.9:
10567+
version "3.0.10"
10568+
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.0.10.tgz#2ad3a7bed70f35b965707c092e5f30b327c290e5"
10569+
integrity sha512-2u44ZG2OcNUO9HDp/Jl8C07x6pU/eTR3ncV91SiK3dhG9TWvRVsCoJw14Ckx5DgWkzGA3waZWO3d7pgqpUI/XA==
10570+
1054810571
csv-generate@^3.2.4:
1054910572
version "3.2.4"
1055010573
resolved "https://registry.yarnpkg.com/csv-generate/-/csv-generate-3.2.4.tgz#440dab9177339ee0676c9e5c16f50e2b3463c019"

0 commit comments

Comments
 (0)