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

Divider: Improve default behavior when in vertical orientation #39316

Merged
merged 8 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
49 changes: 26 additions & 23 deletions packages/components/src/divider/stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { ComponentMeta, ComponentStory } from '@storybook/react';
*/
import { Text } from '../../text';
import { Divider } from '..';
import { Flex } from '../../flex';

const meta: ComponentMeta< typeof Divider > = {
component: Divider,
Expand All @@ -30,41 +31,43 @@ const meta: ComponentMeta< typeof Divider > = {
};
export default meta;

const HorizontalTemplate: ComponentStory< typeof Divider > = ( args ) => (
const Template: ComponentStory< typeof Divider > = ( args ) => (
<div>
<Text>Some text before the divider</Text>
<Divider { ...args } />
<Text>Some text after the divider</Text>
</div>
);

const VerticalTemplate: ComponentStory< typeof Divider > = ( args ) => {
const styles = {
display: 'flex',
alignItems: 'stretch',
justifyContent: 'start',
};

return (
<div style={ styles }>
<Text>Some text before the divider</Text>
<Divider { ...args } />
<Text>Some text after the divider</Text>
</div>
);
};

export const Horizontal: ComponentStory<
typeof Divider
> = HorizontalTemplate.bind( {} );
export const Horizontal: ComponentStory< typeof Divider > = Template.bind( {} );
Horizontal.args = {
margin: 2,
};

export const Vertical: ComponentStory< typeof Divider > = VerticalTemplate.bind(
{}
);
export const Vertical: ComponentStory< typeof Divider > = Template.bind( {} );
Vertical.args = {
...Horizontal.args,
orientation: 'vertical',
};

// Inside a `flex` container, the divider will need to be `stretch` aligned in order to be visible.
export const InFlexContainer: ComponentStory< typeof Divider > = ( args ) => {
return (
<Flex align="stretch">
<Text>
Some text before the divider Some text before the divider Some
text before the divider Some text before the divider Some text
before the divider Some text before the divider Some text before
the divider
</Text>
<Divider { ...args } />
<Text>
Some text after the divider Some text after the divider Some
text after the divider
</Text>
</Flex>
);
};
InFlexContainer.args = {
...Vertical.args,
};
9 changes: 9 additions & 0 deletions packages/components/src/divider/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ const renderMargin = ( {
} )()
);

const renderDisplay = ( {
'aria-orientation': orientation = 'horizontal',
}: Props ) => {
return orientation === 'vertical'
? css( { display: 'inline' } )
: undefined;
};

const renderBorder = ( {
'aria-orientation': orientation = 'horizontal',
}: Props ) => {
Expand All @@ -67,6 +75,7 @@ export const DividerView = styled.hr< Props >`
border: 0;
margin: 0;

${ renderDisplay }
${ renderBorder }
${ renderSize }
${ renderMargin }
Expand Down
16 changes: 15 additions & 1 deletion packages/components/src/divider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ export interface OwnProps {
marginEnd?: SpaceInput;
}

interface SeparatorPropsWithAdditionalDescription {
/**
* Divider's orientation. When using inside a flex container, you may need to make sure the divider is `stretch` aligned
* in order for it to be visible.
*
* @default 'horizontal'
*/
orientation?: SeparatorProps[ 'orientation' ];
}

export interface Props
extends Omit< SeparatorProps, 'children' | 'unstable_system' >,
extends Omit<
SeparatorProps,
'children' | 'unstable_system' | 'orientation'
>,
SeparatorPropsWithAdditionalDescription,
OwnProps {}