Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Margins not only for Box #256

Merged
merged 10 commits into from
Jul 13, 2020
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 0 additions & 65 deletions packages/fuselage/src/components/Box/Margins/index.js

This file was deleted.

10 changes: 0 additions & 10 deletions packages/fuselage/src/components/Box/Margins/spec.js

This file was deleted.

3 changes: 1 addition & 2 deletions packages/fuselage/src/components/Box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const useBoxOnlyProps = (props) => {
}
});

props.className = prependClassName(props.className, 'rcx-box');
props.className = prependClassName(props.className, 'rcx-box rcx-box--full');

return props;
};
Expand Down Expand Up @@ -95,6 +95,5 @@ Box.defaultProps = {

export { default as AnimatedVisibility } from './AnimatedVisibility';
export { default as Flex } from './Flex';
export { default as Margins } from './Margins';
export { default as Position, PositionAnimated } from './Position';
export { default as Scrollable } from './Scrollable';
14 changes: 10 additions & 4 deletions packages/fuselage/src/components/Box/styles.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
.rcx-box {
&,
&::before,
&::after {
@extend %box;
@extend %box;

&--full {
@extend %box--full;

&::before,
&::after {
@extend %box;
@extend %box--full;
}
}
}
3 changes: 2 additions & 1 deletion packages/fuselage/src/components/Chip/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';

import { Avatar } from '../Avatar';
import { Box, Flex, Margins } from '../Box';
import { Box, Flex } from '../Box';
import { Icon } from '../Icon';
import Margins from '../Margins';

const ThumbDefault = ({ url }) => <Avatar size='x20' url={url}/>;
const RemoveDefault = () => <Icon name='cross' size='x16' />;
Expand Down
94 changes: 94 additions & 0 deletions packages/fuselage/src/components/Margins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import React, { useCallback } from 'react';
import { css } from '@rocket.chat/css-in-js';

import { appendClassName } from '../../helpers/appendClassName';
import { createPropType } from '../../helpers/createPropType';
import { patchChildren } from '../../helpers/patchChildren';
import { useStyle } from '../../hooks/useStyle';
import { margin } from '../../styleTokens';
import { BoxTransforms, useComposedBoxTransform } from '../Box/transforms';

function Margins(props) {
const {
children,
className,
all,
block,
blockStart,
blockEnd,
inline,
inlineStart,
inlineEnd,
} = props;

const transformFn = useCallback((props) => {
if (all !== undefined && props.margin === undefined) {
props.margin = all;
}

if (block !== undefined && props.marginBlock === undefined) {
props.marginBlock = block;
}

if (blockStart !== undefined && props.marginBlockStart === undefined) {
props.marginBlockStart = blockStart;
}

if (blockEnd !== undefined && props.marginBlockEnd === undefined) {
props.marginBlockEnd = blockEnd;
}

if (inline !== undefined && props.marginInline === undefined) {
props.marginInline = inline;
}

if (inlineStart !== undefined && props.marginInlineStart === undefined) {
props.marginInlineStart = inlineStart;
}

if (inlineEnd !== undefined && props.marginInlineEnd === undefined) {
props.marginInlineEnd = inlineEnd;
}

return props;
}, [all, block, blockEnd, blockStart, inline, inlineEnd, inlineStart]);

const marginsClassName = useStyle(css`
&:not(.rcx-box--full) {
${ props.all && css`margin: ${ margin(props.all) };` }
${ props.inline && css`margin-inline: ${ margin(props.inline) };` }
${ props.inlineStart && css`margin-inline-start: ${ margin(props.inlineStart) };` }
${ props.inlineEnd && css`margin-inline-end: ${ margin(props.inlineEnd) };` }
${ props.block && css`margin-block: ${ margin(props.block) };` }
${ props.blockStart && css`margin-block-start: ${ margin(props.blockStart) };` }
${ props.blockEnd && css`margin-block-end: ${ margin(props.blockEnd) };` }
}
`, props);

const patchedChildren = patchChildren(children, (childProps) => ({
className: appendClassName(
childProps.className,
appendClassName(
className,
marginsClassName,
),
),
}));

return <BoxTransforms.Provider
children={patchedChildren}
value={useComposedBoxTransform(transformFn)}
/>;
}

Margins.propTypes = {
all: createPropType(margin),
block: createPropType(margin),
blockStart: createPropType(margin),
blockEnd: createPropType(margin),
inline: createPropType(margin),
inlineStart: createPropType(margin),
inlineEnd: createPropType(margin),
};

export default Margins;
23 changes: 23 additions & 0 deletions packages/fuselage/src/components/Margins/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import ReactDOM from 'react-dom';

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

it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<Margins />, div);
ReactDOM.unmountComponentAtNode(div);
});

it('patches non-`Box` children', () => {
const root = document.createElement('div');
ReactDOM.render(<Margins all='10px'>
<div />
</Margins>, root);

const div = root.firstChild;
expect(getComputedStyle(div).marginLeft).toBe('10px');
expect(getComputedStyle(div).marginTop).toBe('10px');
expect(getComputedStyle(div).marginRight).toBe('10px');
expect(getComputedStyle(div).marginBottom).toBe('10px');
});
Loading