Skip to content

Commit a20db69

Browse files
authored
✨ feat: changed Divider to use hr by default (#139)
1 parent 9ec2444 commit a20db69

File tree

6 files changed

+61
-24
lines changed

6 files changed

+61
-24
lines changed

__tests__/components/Divider/ColoredDivider.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import { render } from '../../utils';
55
describe('<ColoredDivider />', () => {
66
it('should render a divider', () => {
77
const { container } = render(<ColoredDivider colorId="P10" />);
8-
expect(container.firstChild?.nodeName).toBe('DIV');
8+
expect(container.firstChild?.nodeName).toBe('HR');
99
});
1010
});
+21-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1+
import '@testing-library/jest-dom';
12
import React from 'react';
23
import { Divider } from 'lib';
34
import { render } from '../../utils';
45

56
describe('<Divider />', () => {
6-
it('should render a divider', () => {
7+
it('should render a default hr element', () => {
78
const { container } = render(<Divider />);
9+
expect(container.firstChild?.nodeName).toBe('HR');
10+
});
11+
12+
it('should render a div element when specified', () => {
13+
const { container } = render(<Divider<'div'> as="div" />);
814
expect(container.firstChild?.nodeName).toBe('DIV');
915
});
1016

11-
it('should accept the rest of the props', () => {
17+
it('should accept additional props', () => {
1218
render(
1319
<Divider
1420
unthemed
15-
className="divider"
21+
className="custom-divider"
1622
id="divider"
1723
orientation="vertical"
1824
style={{ color: 'red' }}
@@ -23,7 +29,18 @@ describe('<Divider />', () => {
2329
);
2430

2531
const divider = document.getElementById('divider');
26-
expect(divider?.nodeName).toBe('DIV');
32+
expect(divider?.nodeName).toBe('HR');
2733
expect(divider?.style.getPropertyValue('color')).toBe('red');
34+
expect(divider?.classList).toContain('custom-divider');
35+
});
36+
37+
it('should have role="separator"', () => {
38+
const { container } = render(<Divider />);
39+
expect(container.firstChild).toHaveAttribute('role', 'separator');
40+
});
41+
42+
it('should have the correct aria-orientation', () => {
43+
const { container } = render(<Divider orientation="vertical" />);
44+
expect(container.firstChild).toHaveAttribute('aria-orientation', 'vertical');
2845
});
2946
});

__tests__/components/Divider/StyledDivider.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ const styles = {
99
describe('<StyledDivider />', () => {
1010
it('should render a divider', () => {
1111
const { container } = render(<StyledDivider {...styles} />);
12-
expect(container.firstChild?.nodeName).toBe('DIV');
12+
expect(container.firstChild?.nodeName).toBe('HR');
1313
});
1414
});

src/components/Divider/Divider.tsx

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { cx } from '@emotion/css';
22
import React from 'react';
3-
import { ThemeProps, Orientation, SequentialVariant, Volume } from '../../types';
3+
import { ThemeProps, Orientation, SequentialVariant, Volume, AsReactType } from '../../types';
44
import { useThemeId } from '../../context/Theme';
55
import styles from './styles/Divider.module.css';
66

7-
export type DividerProps = React.HTMLAttributes<HTMLDivElement> &
7+
export type DividerElementType = 'div' | 'hr';
8+
9+
export type DividerProps<T extends DividerElementType = 'hr'> = AsReactType<T> &
10+
React.HTMLAttributes<HTMLElementTagNameMap[T]> &
811
ThemeProps & {
912
className?: string;
1013
orientation?: Orientation;
@@ -17,7 +20,8 @@ export type DividerProps = React.HTMLAttributes<HTMLDivElement> &
1720
* A divider is a vertical or horizontal rule
1821
* that can be one of several colors.
1922
*/
20-
export function Divider({
23+
export function Divider<T extends DividerElementType = 'hr'>({
24+
as,
2125
className,
2226
contrast = false,
2327
orientation = 'horizontal',
@@ -26,24 +30,24 @@ export function Divider({
2630
variant: initVariant = 'primary',
2731
volume,
2832
...props
29-
}: DividerProps): JSX.Element {
33+
}: DividerProps<T>): JSX.Element {
3034
const themeId = useThemeId(initThemeId);
3135
const variant = contrast ? 'contrast' : initVariant;
36+
const element = as || 'hr';
3237

33-
return (
34-
<div
35-
aria-hidden={true}
36-
className={cx(
37-
styles.divider,
38-
styles[`divider--${orientation}`],
39-
themeId && !unthemed && styles[`divider--${themeId}`],
40-
variant && styles[`divider--${variant}`],
41-
volume && styles[`divider--${volume}`],
42-
className,
43-
)}
44-
{...props}
45-
/>
46-
);
38+
return React.createElement(element, {
39+
'aria-orientation': orientation,
40+
className: cx(
41+
styles.divider,
42+
styles[`divider--${orientation}`],
43+
themeId && !unthemed && styles[`divider--${themeId}`],
44+
variant && styles[`divider--${variant}`],
45+
volume && styles[`divider--${volume}`],
46+
className,
47+
),
48+
role: 'separator',
49+
...props,
50+
});
4751
}
4852

4953
Divider.displayName = 'Divider';

src/components/Divider/stories/Divider.stories.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,15 @@ Vertical.args = {
148148
...defaultArgs,
149149
orientation: 'vertical',
150150
};
151+
152+
export const HorizontalRule = (args: DividerProps<'hr'>): JSX.Element => <Divider<'hr'> {...args} />;
153+
HorizontalRule.args = {
154+
...defaultArgs,
155+
as: 'hr',
156+
};
157+
158+
export const Div = (args: DividerProps<'div'>): JSX.Element => <Divider<'div'> {...args} />;
159+
Div.args = {
160+
...defaultArgs,
161+
as: 'div',
162+
};

src/components/Divider/styles/Divider.module.css

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
flex: none;
44
}
55

6+
hr.divider {
7+
border: none;
8+
}
9+
610
.divider--horizontal {
711
height: 1px;
812
width: 100%;

0 commit comments

Comments
 (0)