Skip to content

Commit

Permalink
fixup! Feat(web-react): Introduce Box component #DS-1595
Browse files Browse the repository at this point in the history
  • Loading branch information
curdaj committed Dec 18, 2024
1 parent 322f91f commit 743cc03
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
14 changes: 11 additions & 3 deletions packages/web-react/src/components/Box/Box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ const defaultProps: Partial<SpiritBoxProps> = {
const Box = <T extends ElementType = 'div'>(props: SpiritBoxProps<T>) => {
const propsWithDefaults = { ...defaultProps, ...props };
const { elementType: ElementTag = 'div', children, ...restProps } = propsWithDefaults;
const { classProps } = useBoxStyleProps(restProps);
const { styleProps, props: otherProps } = useStyleProps(restProps);

const { classProps, props: modifiedProps, styleProps: boxStyle } = useBoxStyleProps(restProps);
const { styleProps, props: otherProps } = useStyleProps(modifiedProps);

const boxStyleProps = {
style: {
...styleProps.style,
...boxStyle,
},
};

return (
<ElementTag {...otherProps} className={classNames(classProps, styleProps.className)} style={styleProps.style}>
<ElementTag {...otherProps} {...boxStyleProps} className={classNames(classProps, styleProps.className)}>
{children}
</ElementTag>
);
Expand Down
6 changes: 5 additions & 1 deletion packages/web-react/src/components/Box/demo/BoxDefault.tsx
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 packages/web-react/src/components/Box/useBoxStyleProps.ts
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,
};
}
};
13 changes: 11 additions & 2 deletions packages/web-react/src/types/box.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElementType } from 'react';
import { ElementType, JSXElementConstructor } from 'react';
import {
BackgroundColorsDictionaryType,
ChildrenProps,
Expand All @@ -17,9 +17,18 @@ export interface BoxBaseProps extends ChildrenProps, StyleProps {
/** The border width of the box. */
borderWidth?: string;
/** Padding of the box. */
padding?: SpaceToken;
/** Horizontal padding of the box. */
paddingX?: SpaceToken;
/** Vertical padding of the box. */
paddingY?: SpaceToken;
/** Padding top of the box. */
paddingTop?: SpaceToken;
/** Padding bottom of the box. */
paddingBottom?: SpaceToken;
/** Padding left of the box. */
paddingLeft?: SpaceToken;
/** Padding right of the box. */
paddingRight?: SpaceToken;
}

Expand All @@ -29,7 +38,7 @@ export type BoxProps<E extends ElementType> = {
*
* @default 'button'
*/
elementType?: E;
elementType?: E | JSXElementConstructor<unknown>;
} & BoxBaseProps;

export type SpiritBoxProps<E extends ElementType = 'div'> = BoxProps<E> &
Expand Down

0 comments on commit 743cc03

Please sign in to comment.