@@ -3,47 +3,46 @@ import React from 'react';
33import { clsx } from 'clsx' ;
44import { customClassSwitcher } from '~/core' ;
55
6- const RENDER_AS_ENUMS = [
7- {
8- label : 'H1' ,
9- tag : 'h1'
10- } ,
11- {
12- label : 'H2' ,
13- tag : 'h2'
14- } ,
15- {
16- label : 'H3' ,
17- tag : 'h3'
18- } ,
19- {
20- label : 'H4' ,
21- tag : 'h4'
22- } ,
23- {
24- label : 'H5' ,
25- tag : 'h5'
26- } ,
27- {
28- label : 'H6' ,
29- tag : 'h6'
30- }
31- ] ;
6+ export type HeadingTag = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' ;
327
8+ /**
9+ * Heading component that renders HTML heading elements (h1-h6)
10+ * with customizable styling.
11+ */
3312export type HeadingProps = {
34- as ?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' ;
13+ /** HTML heading tag to render */
14+ as ?: HeadingTag ;
15+ /** Custom root class for specialized styling */
3516 customRootClass ?: string ;
17+ /** Additional class names to apply */
3618 className ?: string ;
19+ /** Content of the heading */
3720 children ?: React . ReactNode ;
38- props ?: any ;
39- } ;
21+ } & React . HTMLAttributes < HTMLHeadingElement > ;
4022
41- const Heading = ( { children, as = 'h1' , customRootClass = '' , className = '' , ...props } : HeadingProps ) => {
42- const rootClass = customClassSwitcher ( customRootClass , as || 'h1' ) ;
23+ /**
24+ * Renders a heading element with customizable tag and styling
25+ */
26+ const Heading = ( {
27+ children,
28+ as = 'h1' ,
29+ customRootClass = '' ,
30+ className = '' ,
31+ ...props
32+ } : HeadingProps ) => {
33+ const rootClass = customClassSwitcher ( customRootClass , as ) ;
4334
44- const tag = RENDER_AS_ENUMS . find ( ( item ) => item . tag === as ) ? as : 'h1' ;
45- return React . createElement ( tag , { className : clsx ( rootClass , className ) , ...props } , children ) ;
35+ // Check if the heading tag is valid
36+ if ( ! [ 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ] . includes ( as ) ) {
37+ as = 'h1' ;
38+ }
39+
40+ return React . createElement ( as , {
41+ className : clsx ( rootClass , className ) ,
42+ ...props
43+ } , children ) ;
4644} ;
45+
4746Heading . displayName = 'Heading' ;
4847
4948export default Heading ;
0 commit comments