Skip to content

Commit

Permalink
feature: use Element in Box + necessary fix changes (#1440)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti10le authored Nov 10, 2021
1 parent 04a5f18 commit c1da52c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 74 deletions.
6 changes: 6 additions & 0 deletions .changeset/eight-deers-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@marigold/components": patch
"@marigold/system": patch
---

feature: use Element in Box + necessary fix changes
12 changes: 5 additions & 7 deletions packages/components/src/ActionGroup/ActionGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Children } from 'react';
import flattenChildren from 'react-keyed-flatten-children';

import { ResponsiveStyleValue, useStyles } from '@marigold/system';
import { ResponsiveStyleValue } from '@marigold/system';
import { ComponentProps } from '@marigold/types';

import { Box } from '../Box';
Expand All @@ -20,9 +20,9 @@ export const ActionGroup: React.FC<ActionGroupProps> = ({
className,
...props
}) => {
const childClassName = useStyles({
css: verticalAlignment ? { marginBottom: space } : { marginRight: space },
});
const childStyle = verticalAlignment
? { marginBottom: space }
: { marginRight: space };
return (
<Box variant={`actionGroup.${variant}`} className={className} {...props}>
{Children.map(
Expand All @@ -32,9 +32,7 @@ export const ActionGroup: React.FC<ActionGroupProps> = ({
child !== undefined && (
<Box
as={verticalAlignment ? 'div' : 'span'}
className={
i < Children.count(children) - 1 ? childClassName : undefined
}
css={i < Children.count(children) - 1 ? childStyle : undefined}
>
{child}
</Box>
Expand Down
108 changes: 58 additions & 50 deletions packages/components/src/Box/Box.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createElement, forwardRef } from 'react';
import { ResponsiveStyleValue, useStyles } from '@marigold/system';
import React, { forwardRef } from 'react';
import { CSSObject, Element, ResponsiveStyleValue } from '@marigold/system';
import {
PolymorphicPropsWithRef,
PolymorphicComponentWithRef,
Expand All @@ -8,6 +8,7 @@ import {
export type BoxOwnProps = {
className?: string;
variant?: string | string[];
css?: CSSObject;

display?: ResponsiveStyleValue<string>;

Expand Down Expand Up @@ -63,6 +64,7 @@ export const Box: PolymorphicComponentWithRef<BoxOwnProps, 'div'> = forwardRef(
{
variant,
as = 'div',
css,
children,
className,
display,
Expand Down Expand Up @@ -106,52 +108,58 @@ export const Box: PolymorphicComponentWithRef<BoxOwnProps, 'div'> = forwardRef(
...props
},
ref
) => {
const cn = useStyles({
element: as,
variant,
className,
css: {
display,
height,
width,
minWidth,
maxWidth,
position,
top,
bottom,
right,
left,
zIndex,
p,
px,
py,
pt,
pb,
pl,
pr,
m,
mx,
my,
mt,
mb,
ml,
mr,
flexDirection,
flexWrap,
flexShrink,
flexGrow,
alignItems,
justifyContent,
bg,
border,
borderRadius,
boxShadow,
opacity,
overflow,
transition,
},
});
return createElement(as, { ...props, ref, className: cn }, children);
}
) => (
<Element
as={as}
ref={ref}
variant={variant}
css={{
...{
display,
height,
width,
minWidth,
maxWidth,
position,
top,
bottom,
right,
left,
zIndex,
p,
px,
py,
pt,
pb,
pl,
pr,
m,
mx,
my,
mt,
mb,
ml,
mr,
flexDirection,
flexWrap,
flexShrink,
flexGrow,
alignItems,
justifyContent,
bg,
border,
borderRadius,
boxShadow,
opacity,
overflow,
transition,
},
...css,
}}
className={className}
{...props}
>
{children}
</Element>
)
);
5 changes: 2 additions & 3 deletions packages/components/src/Stack/Stack.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { ResponsiveStyleValue, useClassname } from '@marigold/system';
import { ResponsiveStyleValue } from '@marigold/system';

import { Box } from '../Box';

Expand All @@ -22,15 +22,14 @@ export const Stack: React.FC<StackProps> = ({
children,
...props
}) => {
const className = useClassname({ '> * + *': { pt: space } });
return (
<Box
{...props}
as={as}
display="flex"
flexDirection="column"
alignItems={ALIGNMENT[align]}
className={className}
css={{ '> * + *': { pt: space } }}
>
{children}
</Box>
Expand Down
27 changes: 13 additions & 14 deletions packages/components/src/Text/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { forwardRef } from 'react';
import { ResponsiveStyleValue, useStyles } from '@marigold/system';
import { ResponsiveStyleValue } from '@marigold/system';
import {
PolymorphicComponentWithRef,
PolymorphicPropsWithRef,
Expand Down Expand Up @@ -33,17 +33,16 @@ export const Text: PolymorphicComponentWithRef<TextOwnProps, 'span'> =
...props
},
ref
) => {
const cn = useStyles({
className,
variant: `text.${variant}`,
css: { textAlign: align, color, cursor, outline, userSelect },
});

return (
<Box {...props} as={as} className={cn} ref={ref}>
{children}
</Box>
);
}
) => (
<Box
{...props}
as={as}
variant={`text.${variant}`}
css={{ textAlign: align, color, cursor, outline, userSelect }}
className={className}
ref={ref}
>
{children}
</Box>
)
);

0 comments on commit c1da52c

Please sign in to comment.