1
1
import React from 'react' ;
2
2
import { Heading } from '@navikt/ds-react' ;
3
- import { onlyText } from 'utils/react-children' ;
4
3
import { classNames } from 'utils/classnames' ;
5
4
import { Level , levelToSize , Size } from 'types/typo-style' ;
6
- import { CopyLink } from 'components/_common/copyLink/copyLink' ;
7
5
8
6
// eslint-disable-next-line css-modules/no-unused-class
9
7
import style from './Header.module.scss' ;
@@ -12,22 +10,37 @@ type Props = {
12
10
children : React . ReactNode ;
13
11
level : Level ;
14
12
size ?: Size ;
15
- hideCopyButton ?: boolean ;
16
13
anchorId ?: string ;
14
+ addAnchorId ?: boolean ;
17
15
className ?: string ;
18
16
} ;
19
17
20
- export const Header = ( { children, size, level, hideCopyButton, anchorId, className } : Props ) => {
18
+ export const Header = ( {
19
+ children,
20
+ size,
21
+ level,
22
+ anchorId,
23
+ className,
24
+ addAnchorId = true , // Can be set to false if anchor is added outside of Header
25
+ } : Props ) => {
21
26
const anchor = anchorId ? ( anchorId . startsWith ( '#' ) ? anchorId : `#${ anchorId } ` ) : undefined ;
22
-
23
27
const fallbackSizeByLevel = levelToSize [ level ] || 'large' ;
24
28
25
29
return (
26
- < div className = { classNames ( style . header , className ) } id = { anchorId } tabIndex = { - 1 } >
30
+ < div
31
+ className = { classNames ( style . header , className ) }
32
+ id = { addAnchorId ? anchorId : undefined }
33
+ tabIndex = { - 1 }
34
+ >
27
35
< Heading size = { size || fallbackSizeByLevel } level = { level } >
28
- { children }
36
+ { anchor && level === '2' ? (
37
+ < a href = { anchor } className = { style . anchor } >
38
+ { children }
39
+ </ a >
40
+ ) : (
41
+ children
42
+ ) }
29
43
</ Heading >
30
- { anchor && ! hideCopyButton && < CopyLink heading = { onlyText ( children ) } anchor = { anchor } /> }
31
44
</ div >
32
45
) ;
33
46
} ;
0 commit comments