1
1
import { cx } from '@emotion/css' ;
2
2
import React from 'react' ;
3
- import { ThemeProps , Orientation , SequentialVariant , Volume } from '../../types' ;
3
+ import { ThemeProps , Orientation , SequentialVariant , Volume , AsReactType } from '../../types' ;
4
4
import { useThemeId } from '../../context/Theme' ;
5
5
import styles from './styles/Divider.module.css' ;
6
6
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 ] > &
8
11
ThemeProps & {
9
12
className ?: string ;
10
13
orientation ?: Orientation ;
@@ -17,7 +20,8 @@ export type DividerProps = React.HTMLAttributes<HTMLDivElement> &
17
20
* A divider is a vertical or horizontal rule
18
21
* that can be one of several colors.
19
22
*/
20
- export function Divider ( {
23
+ export function Divider < T extends DividerElementType = 'hr' > ( {
24
+ as,
21
25
className,
22
26
contrast = false ,
23
27
orientation = 'horizontal' ,
@@ -26,24 +30,24 @@ export function Divider({
26
30
variant : initVariant = 'primary' ,
27
31
volume,
28
32
...props
29
- } : DividerProps ) : JSX . Element {
33
+ } : DividerProps < T > ) : JSX . Element {
30
34
const themeId = useThemeId ( initThemeId ) ;
31
35
const variant = contrast ? 'contrast' : initVariant ;
36
+ const element = as || 'hr' ;
32
37
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
+ } ) ;
47
51
}
48
52
49
53
Divider . displayName = 'Divider' ;
0 commit comments