-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! Feat(web-react): Introduce Box component #DS-1595
- Loading branch information
Showing
4 changed files
with
69 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import React from 'react'; | ||
import Box from '../Box'; | ||
|
||
const BoxDefault = () => <Box>Content</Box>; | ||
const BoxDefault = () => ( | ||
<Box padding="space-1200" backgroundColor="tertiary"> | ||
Content | ||
</Box> | ||
); | ||
|
||
export default BoxDefault; |
57 changes: 42 additions & 15 deletions
57
packages/web-react/src/components/Box/useBoxStyleProps.ts
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,38 +1,65 @@ | ||
import classNames from 'classnames'; | ||
import { useClassNamePrefix } from '../../hooks'; | ||
import { BackgroundColorsDictionaryType, SpaceToken } from '../../types'; | ||
import { CSSProperties, ElementType } from 'react'; | ||
import { DirectionAxis } from '../../constants'; | ||
import { useClassNamePrefix, useSpacingStyle } from '../../hooks'; | ||
import { SpiritBoxProps } from '../../types'; | ||
|
||
export interface UseBoxStyleProps { | ||
backgroundColor?: BackgroundColorsDictionaryType; | ||
borderColor?: string; | ||
borderRadius?: string; | ||
borderWidth?: string; | ||
paddingTop?: SpaceToken; | ||
paddingBottom?: SpaceToken; | ||
paddingLeft?: SpaceToken; | ||
paddingRight?: SpaceToken; | ||
interface BoxCSSProperties extends CSSProperties { | ||
[key: string]: string | undefined | number; | ||
} | ||
|
||
export interface UseBoxStylePropsReturn { | ||
export interface UseBoxStyleProps<T> { | ||
/** className props */ | ||
classProps: string; | ||
/** Props for the box element. */ | ||
props: T; | ||
/** Style props for the box element */ | ||
styleProps: BoxCSSProperties; | ||
} | ||
|
||
export function useBoxStyleProps(props?: UseBoxStyleProps): UseBoxStylePropsReturn { | ||
export const useBoxStyleProps = ( | ||
props: Partial<SpiritBoxProps<ElementType>>, | ||
): UseBoxStyleProps<Partial<SpiritBoxProps<ElementType>>> => { | ||
const { | ||
backgroundColor, | ||
borderColor, | ||
borderRadius, | ||
borderWidth, | ||
padding, | ||
paddingX, | ||
paddingY, | ||
paddingTop, | ||
paddingBottom, | ||
paddingLeft, | ||
paddingRight, | ||
...restProps | ||
} = props || {}; | ||
const boxClass = useClassNamePrefix('Box'); | ||
|
||
const boxClasses = classNames(boxClass, {}); | ||
const boxStyle: BoxCSSProperties = { | ||
...useSpacingStyle(padding, 'box', DirectionAxis.X), | ||
...useSpacingStyle(padding, 'box', DirectionAxis.Y), | ||
...useSpacingStyle(paddingX, 'box', DirectionAxis.X), | ||
...useSpacingStyle(paddingY, 'box', DirectionAxis.Y), | ||
}; | ||
|
||
const boxBackgroundColor = backgroundColor ? `bg-${backgroundColor}` : ''; | ||
const boxPaddingBottom = paddingBottom ? paddingBottom.replace('space-', 'pb-') : ''; | ||
const boxPaddingTop = paddingTop ? paddingTop.replace('space-', 'pt-') : ''; | ||
const boxPaddingLeft = paddingLeft ? paddingLeft.replace('space-', 'pl-') : ''; | ||
const boxPaddingRight = paddingRight ? paddingRight.replace('space-', 'pr-') : ''; | ||
|
||
const boxClasses = classNames(boxClass, { | ||
[boxBackgroundColor]: backgroundColor, | ||
[boxPaddingBottom]: paddingBottom, | ||
[boxPaddingTop]: paddingTop, | ||
[boxPaddingLeft]: paddingLeft, | ||
[boxPaddingRight]: paddingRight, | ||
}); | ||
|
||
return { | ||
classProps: boxClasses, | ||
props: restProps, | ||
styleProps: boxStyle, | ||
}; | ||
} | ||
}; |
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