You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, Responsive System overrides do not cascade, which is to say: if you add an override for screen class `X`, those overrides will only be applied when the user's browser/device matches screen class `X`. But in some cases, you might want to apply overrides on `X` and also anything larger/smaller than `X`.
164
+
165
+
This is where `cascadeMode` comes in.
166
+
167
+
Let's take an example. We'll assume that we have 4 screen classes, "xs", "sm", "md", and "lg" and that we have a `Button` component that can be either `normal` or `large`. On most screen sizes, we want our button to be `normal` size, but on `xs` screens (like mobile phones) we want to make the button nice and big so that it's easier to tap.
168
+
169
+
There are 2 ways that you can do this with the default configuration of Responsive System:
170
+
171
+
```jsx
172
+
<Button
173
+
// default is "normal"
174
+
buttonSize="normal"
175
+
// override on xs screens
176
+
xs={{ buttonSize:'large' }}
177
+
/>
178
+
```
179
+
180
+
This approach is called "desktop-first" because the default values pertain to larger screens, and we provide overrides for smaller screens.
181
+
182
+
We could also write this in a "mobile-first" way like this:
183
+
184
+
```jsx
185
+
<Button
186
+
// default to "large"
187
+
buttonSize="large"
188
+
// override on sm, md, and lg screens
189
+
sm={{ buttonSize:'normal' }}
190
+
md={{ buttonSize:'normal' }}
191
+
lg={{ buttonSize:'normal' }}
192
+
/>
193
+
```
194
+
195
+
In this particular case, the "desktop-first" approach is shorter and easier to understand, but sometimes the reverse will be true. Using the default `cascadeMode` ("no-cascade") allows you to choose which approach works best on a case-by-case basis. You pick your default values and then apply overrides for any screen sizes that need special treatment.
196
+
197
+
For folks who find value in having a consistent "desktop-first" or "mobile-first" approach throughout their apps, we also provide `cascadeMode = "desktop-first"` and `cascadeMode = "mobile-first"`. In these modes, you always provide default values for either your largest screen class ("desktop-first") or your smallest ("mobile-first") and your overrides will _cascade_.
198
+
199
+
Going back to our example, if we had used `cascadeMode = "desktop-first"` we would still write:
200
+
201
+
```jsx
202
+
<Button
203
+
// desktop-first default value
204
+
buttonSize="normal"
205
+
// override on xs screens AND anything smaller
206
+
xs={{ buttonSize:'large' }}
207
+
/>
208
+
```
209
+
210
+
The desktop-first cascade would automatically add our overrides on any screen classes smaller that `xs`, but no such screen classes exist, so the cascade doesn't come into play here.
211
+
212
+
On the other hand, if we had used `cascadeMode = "mobile-first"`, we could take advantage of the mobile-first cascade:
213
+
214
+
```jsx
215
+
<Button
216
+
// mobile-first default value
217
+
buttonSize="large"
218
+
// override on sm AND anything larger
219
+
sm={{ buttonSize:'normal' }}
220
+
/>
221
+
```
222
+
223
+
The mobile-first cascade says "apply these overrides on _this_ screen class _and_ any larger screen classes too".
224
+
225
+
Put another way:
226
+
227
+
- "no-cascade" - `sm` overrides apply only on `sm` screens
228
+
- "desktop-first" - `sm` overrides apply on `sm` and `xs` screens
229
+
- "mobile-first" = `sm` overrides apply on `sm`, `md`, and `lg`
230
+
231
+
If you'd like to enable desktop/mobile-first cascading, you can pass the `cascadeMode` option to `createResponsiveSystem`.
By default, Responsive System will apply any screen-size-specific overrides right on top of your base props, but sometimes you need more control over how your overrides are applied.
0 commit comments