-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(configprovider): Add fallbackDisplayKey prop (#350)
- Add `fallbackDisplayKey` prop to `<ConfigProvider />`. Defaults to `default` but setting to another key (e.g. `large`) would use that as the default value Closes #331
- Loading branch information
Showing
12 changed files
with
323 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { | ||
getMediaQueries, | ||
getSingleResolutionProps, | ||
} from './withResolution.logic' | ||
|
||
describe('getMediaQueries', () => { | ||
it('should return a max and min value base on the available aliases', () => { | ||
const out = getMediaQueries('test', { | ||
test: { | ||
minWidth: '1px', | ||
maxWidth: '2px', | ||
}, | ||
}) | ||
|
||
expect(out).toEqual({ test: '(min-width: 1px) and (max-width: 2px)' }) | ||
}) | ||
|
||
it('should return a max value only when no min value is provided', () => { | ||
const out = getMediaQueries('test', { | ||
test: { | ||
maxWidth: '2px', | ||
}, | ||
}) | ||
|
||
expect(out).toEqual({ test: '(max-width: 2px)' }) | ||
}) | ||
|
||
it('should return a min value only when no max value is provided', () => { | ||
const out = getMediaQueries('test', { | ||
test: { | ||
minWidth: '1px', | ||
}, | ||
}) | ||
|
||
expect(out).toEqual({ test: '(min-width: 1px)' }) | ||
}) | ||
|
||
it('should return an empty string if an invalid value is passed', () => { | ||
const out = getMediaQueries('test2', { | ||
test: { | ||
invalidValue: 'meow', | ||
}, | ||
}) | ||
|
||
expect(out).toEqual({}) | ||
}) | ||
}) | ||
|
||
describe('getSingleResolutionProps', () => { | ||
it('should return the object as is if there are no resolution or the "show" props', () => { | ||
const props = { a: 1, b: 2 } | ||
const out = getSingleResolutionProps({ | ||
props, | ||
resolutionKeys: [], | ||
shouldShow: { | ||
small: false, | ||
large: false, | ||
}, | ||
fallbackDisplayKey: 'default', | ||
}) | ||
|
||
expect(out).toEqual(props) | ||
}) | ||
|
||
it('should remove the "show" prop', () => { | ||
const props = { a: 1, b: 2, show: 'small, large' } | ||
const out = getSingleResolutionProps({ | ||
props, | ||
resolutionKeys: [], | ||
shouldShow: { | ||
small: false, | ||
large: false, | ||
}, | ||
fallbackDisplayKey: 'default', | ||
}) | ||
|
||
expect(out.show).not.toBeDefined() | ||
}) | ||
|
||
it('should pick the resolution keys based on "shouldShow"', () => { | ||
const props = { a: { small: 1, large: 2 }, b: { small: 3, large: 4 } } | ||
const out = getSingleResolutionProps({ | ||
props, | ||
resolutionKeys: ['a', 'b'], | ||
shouldShow: { | ||
small: true, | ||
large: false, | ||
}, | ||
fallbackDisplayKey: 'default', | ||
}) | ||
|
||
expect(out).toEqual({ | ||
a: 1, | ||
b: 3, | ||
}) | ||
}) | ||
|
||
it('should not change keys if they are not specified as "resolutionKeys"', () => { | ||
const props = { | ||
a: { small: 1, large: 2 }, | ||
b: { small: 3, large: 4 }, | ||
c: { small: 5, large: 6 }, | ||
} | ||
const out = getSingleResolutionProps({ | ||
props, | ||
resolutionKeys: ['a', 'b'], | ||
shouldShow: { | ||
small: true, | ||
large: false, | ||
}, | ||
fallbackDisplayKey: 'default', | ||
}) | ||
|
||
expect(out.c).toEqual(props.c) | ||
}) | ||
|
||
it('should fall back to the default resolution key when none is provided', () => { | ||
const props = { | ||
a: { small: 1, large: 2 }, | ||
b: { small: 3, large: 4 }, | ||
c: { small: 5, large: 6 }, | ||
} | ||
const out = getSingleResolutionProps({ | ||
props, | ||
resolutionKeys: ['a', 'b'], | ||
shouldShow: { | ||
small: false, | ||
large: false, | ||
}, | ||
fallbackDisplayKey: 'large', | ||
}) | ||
|
||
expect(out).toEqual({ | ||
a: 2, | ||
b: 4, | ||
c: { | ||
small: 5, | ||
large: 6, | ||
}, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+5.03 KB
storybook/stories/configProvider/__screenshots__/fallbackDisplayKey.spec.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+13.1 KB
...ybook/stories/configProvider/__screenshots__/fallbackDisplayKey_mobile.spec.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// @flow | ||
import * as React from 'react' | ||
import { ConfigProvider, Grid } from 'gymnast' | ||
import { colors } from '../../shared' | ||
|
||
export default () => ( | ||
<ConfigProvider fallbackDisplayKey="fallback"> | ||
<Grid padding="0 L/2"> | ||
<Grid padding="0 L/2 L" size={{ large: 2, fallback: 10 }}> | ||
<Grid | ||
style={colors.colors1} | ||
padding="L/2" | ||
align="center" | ||
justify="center" | ||
> | ||
Only size on large screens is specified, all others use the fallback | ||
size | ||
</Grid> | ||
</Grid> | ||
</Grid> | ||
</ConfigProvider> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Some times it is inconvenient to specify all display keys when you only want to change one value on one resolution. `fallbackDisplayKey` allows you to provide a fallback value. | ||
|
||
For instance, if you want to set size to 2 columns on large displays but to 10 on all other ones, you could do: | ||
|
||
```javascript | ||
<Grid size={{ large: 2, medium: 10, small: 10 }}> | ||
``` | ||
|
||
This is fine when you have 2 or 3 breakpoints but it quickly becomes cumbersome. Instead you can also do: | ||
|
||
```javascript | ||
<Grid size={{ large: 2, default: 10 }}> | ||
``` | ||
|
||
Note that `'default'` is the default value for the `fallbackDisplayKey` prop but you can modify it using `<ConfigProvider />`. For instance: | ||
|
||
```javascript | ||
<ConfigProvider fallbackDisplayKey="fallback"> | ||
<Grid size={{ large: 2, fallback: 10 }}> | ||
</ConfigProvider> | ||
``` |
Oops, something went wrong.