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 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.
243
+
Overrides are applied via [deepmerge](https://www.npmjs.com/package/deepmerge) which means that you can easily apply overrides in arbitrarily-nested objects.
244
244
245
245
Let's say you've got a component that accepts an `object` as a prop. The intrinsic `style` prop is a great example!
246
246
@@ -264,50 +264,34 @@ And we can override the style on `small` screens.
264
264
/>
265
265
```
266
266
267
-
In this case, we might want to override the `backgroundColor` but to leave the `height` alone. But, by default the `small.style`is going to completely override the base `style`and the result on small screens will effectively be:
267
+
A naive override algorithm would simply replace the base `style` with `small.style` and the result would be `style = { backgroundColor: 'blue' }`.
268
268
269
-
```jsx
270
-
// no height! Bummer...
271
-
<div style={{ backgroundColor:'blue' }}>
272
-
```
269
+
However, by utilizing `deepmerge` we get `style = { height: 100, backgroundColor: 'blue' }` which is what we'd expect.
273
270
274
-
We can fix this by providing a `function` as a prop rather than an object. The function will be invoked with the base props as an argument so that you can have full control over how the overrides are handled.
Merging arrays is tricky, but we've chosen a method that seems like a sane default. If there's a need for customization of the array-merge algorithm, we could support that in a future release.
287
274
288
-
And voila! Your overrides are applied on your terms.
275
+
For now, the behavior is to treat them like we treat objects. That is, if the base array defines an item at `[0]` and the override array defines an item at `[0]`, the `override[0]` will be merged on top of `base[0]`. Here are a few examples:
289
276
290
-
### Optimizing
277
+
```txt
278
+
base [1, 2, 3, 4]
279
+
override [5, 6, 7]
280
+
result [5, 6, 7, 4]
281
+
```
291
282
292
-
Pro tip: if you've got a bunch of screen classes that all need to perform custom overrides in the same way, you can create a helper function.
283
+
```txt
284
+
base [1, 2, 3]
285
+
override [5, 6, 7, 8]
286
+
result [5, 6, 7, 8]
287
+
```
293
288
294
-
```jsx
295
-
functionmakeOverrideFn(overrides) {
296
-
// function that returns a function!
297
-
return (baseProps) => {
298
-
return {
299
-
...baseProps,
300
-
style: { ...baseProps.style, ...overrides },
301
-
};
302
-
};
303
-
}
289
+
Objects inside of an array are merged too! Objects and arrays can be nested arbitrarily deep, but keep in mind that merging deeply-nested structures will require our merge algorithm to do more work which could impact performance. It's best to keep your props as shallow as possible.
0 commit comments