-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(component): add responsive breakpoints for Flex props (#113)
BREAKING CHANGE: Flex component flexDirection prop now defaults to responsive object. Flex component props renamed to be more contextual. If you had direction="row" this will now set your all breakpoints to flex-direction: row instead of column for mobile/row for desktop. Flex: direction -> flexDirection wrap -> flexWrap Flex.Item: basis -> flexBasis grow -> flexGrow order -> flexOrder shrink -> flexShrink * feat(component): add responsive breakpoints for Flex props * refactor(component): change flex prop names * refactor: change variable name to be more descriptive * refactor: move flexed item props into same file * refactor: moved locations of new flex changes * refactor: rename variables * feat: resolve PR comments * fix: remove unused imports * fix: remove unused argument
- Loading branch information
1 parent
a93b1b0
commit 335c640
Showing
21 changed files
with
293 additions
and
147 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,10 @@ | ||
import React from 'react'; | ||
|
||
import { BoxProps } from '../../Box'; | ||
import { FlexedItemProps } from '../types'; | ||
|
||
import { StyledFlexItem } from './styled'; | ||
|
||
export type FlexItemProps = BoxProps & | ||
Partial<{ | ||
alignSelf: 'auto' | 'flex-start' | 'flex-end' | 'center' | 'baseline' | 'stretch'; | ||
basis: 'auto' | 'fill' | 'min-content' | 'max-content' | 'fit-content' | 'content' | string; | ||
grow: number; | ||
order: number; | ||
shrink: number; | ||
}>; | ||
export type FlexItemProps = BoxProps & FlexedItemProps; | ||
|
||
export const FlexItem: React.FC<FlexItemProps> = props => <StyledFlexItem {...props} />; |
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,116 @@ | ||
import { ThemeInterface } from '@bigcommerce/big-design-theme'; | ||
import { FlattenSimpleInterpolation } from 'styled-components'; | ||
|
||
import { Responsive } from '../../types'; | ||
|
||
type SingleAlignContent = | ||
| 'normal' | ||
| 'start' | ||
| 'end' | ||
| 'flex-start' | ||
| 'flex-end' | ||
| 'center' | ||
| 'baseline' | ||
| 'space-between' | ||
| 'space-around' | ||
| 'space-evenly' | ||
| 'stretch'; | ||
type ResponsiveAlignContent = Responsive<SingleAlignContent>; | ||
type AlignContent = SingleAlignContent | ResponsiveAlignContent; | ||
|
||
type SingleAlignItems = | ||
| 'normal' | ||
| 'flex-start' | ||
| 'flex-end' | ||
| 'center' | ||
| 'start' | ||
| 'end' | ||
| 'self-start' | ||
| 'self-end' | ||
| 'baseline' | ||
| 'stretch'; | ||
type ResponsiveAlignItems = Responsive<SingleAlignItems>; | ||
type AlignItems = SingleAlignItems | ResponsiveAlignItems; | ||
|
||
type SingleAlignSelf = | ||
| 'auto' | ||
| 'normal' | ||
| 'self-start' | ||
| 'self-end' | ||
| 'flex-start' | ||
| 'flex-end' | ||
| 'center' | ||
| 'baseline' | ||
| 'stretch'; | ||
type ResponsiveAlignSelf = Responsive<SingleAlignSelf>; | ||
type AlignSelf = SingleAlignSelf | ResponsiveAlignSelf; | ||
|
||
type SingleFlexDirection = 'row' | 'column' | 'row-reverse' | 'column-reverse'; | ||
type ResponsiveFlexDirection = Responsive<SingleFlexDirection>; | ||
type FlexDirection = SingleFlexDirection | ResponsiveFlexDirection; | ||
|
||
type SingleFlexWrap = 'nowrap' | 'wrap' | 'wrap-reversed'; | ||
type ResponsiveFlexWrap = Responsive<SingleFlexWrap>; | ||
type FlexWrap = SingleFlexWrap | ResponsiveFlexWrap; | ||
|
||
type SingleJustifyContent = | ||
| 'normal' | ||
| 'start' | ||
| 'end' | ||
| 'flex-start' | ||
| 'flex-end' | ||
| 'center' | ||
| 'left' | ||
| 'right' | ||
| 'baseline' | ||
| 'space-between' | ||
| 'space-around' | ||
| 'space-evenly' | ||
| 'stretch'; | ||
type ResponsiveJustifyContent = Responsive<SingleJustifyContent>; | ||
type JustifyContent = SingleJustifyContent | ResponsiveJustifyContent; | ||
|
||
type SingleFlexBasis = 'auto' | 'fill' | 'min-content' | 'max-content' | 'fit-content' | 'content' | string; | ||
type ResponsiveFlexBasis = Responsive<SingleFlexBasis>; | ||
type FlexBasis = SingleFlexBasis | ResponsiveFlexBasis; | ||
|
||
type SingleFlexGrow = number; | ||
type ResponsiveFlexGrow = Responsive<SingleFlexGrow>; | ||
type FlexGrow = SingleFlexGrow | ResponsiveFlexGrow; | ||
|
||
type SingleFlexOrder = number; | ||
type ResponsiveFlexOrder = Responsive<SingleFlexOrder>; | ||
type FlexOrder = SingleFlexOrder | ResponsiveFlexOrder; | ||
|
||
type SingleFlexShrink = number; | ||
type ResponsiveFlexShrink = Responsive<SingleFlexShrink>; | ||
type FlexShrink = SingleFlexShrink | ResponsiveFlexShrink; | ||
|
||
export type FlexedProps = Partial<{ | ||
alignContent: AlignContent; | ||
alignItems: AlignItems; | ||
flexDirection: FlexDirection; | ||
flexWrap: FlexWrap; | ||
justifyContent: JustifyContent; | ||
}>; | ||
|
||
export type FlexedItemProps = Partial<{ | ||
alignSelf: AlignSelf; | ||
flexBasis: FlexBasis; | ||
flexGrow: FlexGrow; | ||
flexOrder: FlexOrder; | ||
flexShrink: FlexShrink; | ||
}>; | ||
|
||
export interface FlexedOverload { | ||
(flexedProp: AlignContent, theme: ThemeInterface, cssKey: 'align-content'): FlattenSimpleInterpolation; | ||
(flexedProp: AlignItems, theme: ThemeInterface, cssKey: 'align-items'): FlattenSimpleInterpolation; | ||
(flexedProp: FlexDirection, theme: ThemeInterface, cssKey: 'flex-direction'): FlattenSimpleInterpolation; | ||
(flexedProp: FlexWrap, theme: ThemeInterface, cssKey: 'flex-wrap'): FlattenSimpleInterpolation; | ||
(flexedProp: JustifyContent, theme: ThemeInterface, cssKey: 'justify-content'): FlattenSimpleInterpolation; | ||
(flexedProp: AlignSelf, theme: ThemeInterface, cssKey: 'align-self'): FlattenSimpleInterpolation; | ||
(flexedProp: FlexBasis, theme: ThemeInterface, cssKey: 'flex-basis'): FlattenSimpleInterpolation; | ||
(flexedProp: FlexGrow, theme: ThemeInterface, cssKey: 'flex-grow'): FlattenSimpleInterpolation; | ||
(flexedProp: FlexOrder, theme: ThemeInterface, cssKey: 'order'): FlattenSimpleInterpolation; | ||
(flexedProp: FlexShrink, theme: ThemeInterface, cssKey: 'flex-shrink'): FlattenSimpleInterpolation; | ||
} |
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,61 @@ | ||
import { breakpointsOrder, Breakpoints, ThemeInterface } from '@bigcommerce/big-design-theme'; | ||
import { css, FlattenSimpleInterpolation } from 'styled-components'; | ||
|
||
import { FlexedItemProps, FlexedOverload, FlexedProps } from './types'; | ||
|
||
export const withFlexedContainer = () => css<FlexedProps>` | ||
${({ alignContent, theme }) => alignContent && getFlexedStyles(alignContent, theme, 'align-content')}; | ||
${({ alignItems, theme }) => alignItems && getFlexedStyles(alignItems, theme, 'align-items')}; | ||
${({ flexDirection, theme }) => flexDirection && getFlexedStyles(flexDirection, theme, 'flex-direction')}; | ||
${({ flexWrap, theme }) => flexWrap && getFlexedStyles(flexWrap, theme, 'flex-wrap')}; | ||
${({ justifyContent, theme }) => justifyContent && getFlexedStyles(justifyContent, theme, 'justify-content')}; | ||
`; | ||
|
||
export const withFlexedItems = () => css<FlexedItemProps>` | ||
${({ alignSelf, theme }) => alignSelf && getFlexedStyles(alignSelf, theme, 'align-self')}; | ||
${({ flexBasis, theme }) => flexBasis && getFlexedStyles(flexBasis, theme, 'flex-basis')}; | ||
${({ flexGrow, theme }) => flexGrow && getFlexedStyles(flexGrow, theme, 'flex-grow')}; | ||
${({ flexOrder, theme }) => flexOrder && getFlexedStyles(flexOrder, theme, 'order')}; | ||
${({ flexShrink, theme }) => flexShrink && getFlexedStyles(flexShrink, theme, 'flex-shrink')}; | ||
`; | ||
|
||
const getFlexedStyles: FlexedOverload = ( | ||
flexedProp: any, | ||
theme: ThemeInterface, | ||
cssKey: any, | ||
): FlattenSimpleInterpolation => { | ||
if (typeof flexedProp === 'object') { | ||
return getResponsiveFlex(flexedProp, theme, cssKey); | ||
} | ||
|
||
if (typeof flexedProp === 'string' || typeof flexedProp === 'number') { | ||
return getSimpleFlex(flexedProp, cssKey); | ||
} | ||
|
||
return []; | ||
}; | ||
|
||
const getSimpleFlex = (flexedProp: string | number, cssKey: string): FlattenSimpleInterpolation => css` | ||
${cssKey}: ${flexedProp} | ||
`; | ||
|
||
const getResponsiveFlex: FlexedOverload = ( | ||
flexedProp: any, | ||
theme: ThemeInterface, | ||
cssKey: string, | ||
): FlattenSimpleInterpolation[] => { | ||
const breakpointKeys = Object.keys(flexedProp).sort( | ||
(firstBreakpoint, secondBreakpoint) => | ||
breakpointsOrder.indexOf(firstBreakpoint as keyof Breakpoints) - | ||
breakpointsOrder.indexOf(secondBreakpoint as keyof Breakpoints), | ||
); | ||
|
||
return (breakpointKeys as Array<keyof Breakpoints>).map( | ||
breakpointKey => | ||
css` | ||
${theme.breakpoints[breakpointKey]} { | ||
${getSimpleFlex(flexedProp[breakpointKey], cssKey)} | ||
} | ||
`, | ||
); | ||
}; |
Oops, something went wrong.