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

components: Remove @emotion/css from Spacer #33055

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
/**
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import type { Ref } from 'react';

/**
* Internal dependencies
*/
import { createComponent } from '../ui/utils';
import { useSpacer } from './hook';
import {
useContextSystem,
contextConnect,
PolymorphicComponentProps,
} from '../ui/context';
import type { Props } from './types';
import { SpacerView } from './styles';

const DEFAULT_PROPS = {
marginBottom: 2,
};

function Spacer(
props: PolymorphicComponentProps< Props, 'div' >,
forwardedRef: Ref< any >
) {
const contextProps = useContextSystem( props, 'Spacer' );

return (
<SpacerView
ref={ forwardedRef }
{ ...DEFAULT_PROPS }
{ ...contextProps }
/>
);
}

/**
* `Spacer` is a primitive layout component that providers inner (`padding`) or outer (`margin`) space in-between components. It can also be used to adaptively provide space within an `HStack` or `VStack`.
Expand All @@ -27,10 +57,6 @@ import { useSpacer } from './hook';
* }
* ```
*/
const Spacer = createComponent( {
as: 'div',
useHook: useSpacer,
name: 'Spacer',
} );
const ConnectedSpacer = contextConnect( Spacer, 'Spacer' );

export default Spacer;
export default ConnectedSpacer;
172 changes: 0 additions & 172 deletions packages/components/src/spacer/hook.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/components/src/spacer/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as Spacer } from './component';
export { useSpacer } from './hook';
export type { SpacerProps } from './hook';
export type { Props as SpacerProps } from './types';
53 changes: 53 additions & 0 deletions packages/components/src/spacer/stories/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* External dependencies
*/
import { number } from '@storybook/addon-knobs';

/**
* Internal dependencies
*/
import { Spacer } from '..';

export default {
component: Spacer,
title: 'Components (Experimental)/Spacer',
};

const PROPS = [
'marginTop',
'marginBottom',
'marginLeft',
'marginRight',
'marginX',
'marginY',
'margin',

'paddingTop',
'paddingBottom',
'paddingLeft',
'paddingRight',
'paddingX',
'paddingY',
'padding',
];

const BlackBox = () => (
<div
style={ { backgroundColor: 'black', width: '100px', height: '100px' } }
/>
);

export const _default = () => {
const props = PROPS.reduce(
( acc, prop ) => ( { ...acc, [ prop ]: number( prop, undefined ) } ),
{}
);

return (
<>
<BlackBox />
<Spacer { ...props }>This is the spacer</Spacer>
ciampo marked this conversation as resolved.
Show resolved Hide resolved
<BlackBox />
</>
);
};
53 changes: 53 additions & 0 deletions packages/components/src/spacer/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* External dependencies
*/
import styled from '@emotion/styled';
import { css } from '@emotion/react';
// eslint-disable-next-line no-restricted-imports
import type { CSSProperties } from 'react';

/**
* Internal dependencies
*/
import type { InnerProps } from './types';
import { space } from '../ui/utils/space';

const isDefined = < T >( o: T ): o is Exclude< T, null | undefined > =>
typeof o !== 'undefined' && o !== null;
ciampo marked this conversation as resolved.
Show resolved Hide resolved

const renderProperty = (
name: keyof InnerProps,
cssKeys: ( keyof CSSProperties )[] = [ name as keyof CSSProperties ]
) => ( props: InnerProps ) => {
const value = props[ name ];

if ( ! isDefined( value ) ) {
return undefined;
}

const resolvedValue = space( value );
return css(
cssKeys.reduce(
( acc, key ) => ( { ...acc, [ key ]: resolvedValue } ),
{}
)
);
};

export const SpacerView = styled.div< InnerProps >`
${ renderProperty( 'margin' ) }
${ renderProperty( 'marginY', [ 'marginTop', 'marginBottom' ] ) }
${ renderProperty( 'marginX', [ 'marginLeft', 'marginRight' ] ) }
${ renderProperty( 'marginTop' ) }
${ renderProperty( 'marginBottom' ) }
${ renderProperty( 'marginLeft' ) }
${ renderProperty( 'marginRight' ) }

${ renderProperty( 'padding' ) }
${ renderProperty( 'paddingY', [ 'paddingTop', 'paddingBottom' ] ) }
${ renderProperty( 'paddingX', [ 'paddingLeft', 'paddingRight' ] ) }
${ renderProperty( 'paddingTop' ) }
${ renderProperty( 'paddingBottom' ) }
${ renderProperty( 'paddingLeft' ) }
${ renderProperty( 'paddingRight' ) }
`;
Loading