|
| 1 | +import React, { ReactNode } from 'react'; |
| 2 | + |
| 3 | +import { PortableText } from '@portabletext/react'; |
| 4 | +import styled from 'styled-components'; |
| 5 | + |
| 6 | +import { BodyLong, BodyShort, Detail, Heading, Label } from '@navikt/ds-react'; |
| 7 | +import { useSprakContext } from '@navikt/familie-sprakvelger'; |
| 8 | + |
| 9 | +import { useApp } from '../../../context/AppContext'; |
| 10 | +import { FlettefeltVerdier, LocaleRecordBlock, Typografi } from '../../../typer/sanity/sanity'; |
| 11 | + |
| 12 | +const StyledLabel = styled(Label)` |
| 13 | + display: block; |
| 14 | +`; |
| 15 | + |
| 16 | +interface Props { |
| 17 | + typografi?: Typografi; |
| 18 | + style?: React.CSSProperties; |
| 19 | + children?: ReactNode; |
| 20 | +} |
| 21 | + |
| 22 | +const StyledBodyLong = styled(BodyLong)` |
| 23 | + :empty { |
| 24 | + height: 1.625rem; |
| 25 | + } |
| 26 | +`; |
| 27 | + |
| 28 | +export function TypografiWrapper({ typografi, style, children }: Props) { |
| 29 | + switch (typografi) { |
| 30 | + case Typografi.StegHeadingH1: |
| 31 | + return ( |
| 32 | + <Heading level={'1'} size={'xsmall'} style={style}> |
| 33 | + {children} |
| 34 | + </Heading> |
| 35 | + ); |
| 36 | + case Typografi.ModalHeadingH1: |
| 37 | + return ( |
| 38 | + <Heading level={'1'} size={'large'} style={style}> |
| 39 | + {children} |
| 40 | + </Heading> |
| 41 | + ); |
| 42 | + case Typografi.ForsideHeadingH1: |
| 43 | + return ( |
| 44 | + <Heading level={'1'} size={'xlarge'} style={style}> |
| 45 | + {children} |
| 46 | + </Heading> |
| 47 | + ); |
| 48 | + case Typografi.HeadingH2: |
| 49 | + return ( |
| 50 | + <Heading level={'2'} size={'xsmall'} spacing style={style}> |
| 51 | + {children} |
| 52 | + </Heading> |
| 53 | + ); |
| 54 | + case Typografi.BodyLong: |
| 55 | + return <StyledBodyLong style={style}>{children}</StyledBodyLong>; |
| 56 | + case Typografi.BodyShort: |
| 57 | + return <BodyShort style={style}>{children}</BodyShort>; |
| 58 | + case Typografi.Label: |
| 59 | + return ( |
| 60 | + <StyledLabel spacing style={style}> |
| 61 | + {children} |
| 62 | + </StyledLabel> |
| 63 | + ); |
| 64 | + case Typografi.Detail: |
| 65 | + return <Detail style={style}>{children}</Detail>; |
| 66 | + case undefined: |
| 67 | + return <div style={style}>{children}</div>; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +const TekstBlock: React.FC<{ |
| 72 | + block: LocaleRecordBlock | undefined; |
| 73 | + flettefelter?: FlettefeltVerdier; |
| 74 | + typografi?: Typografi; |
| 75 | +}> = ({ block, flettefelter, typografi }) => { |
| 76 | + const [valgtLocale] = useSprakContext(); |
| 77 | + const { flettefeltTilTekst } = useApp(); |
| 78 | + |
| 79 | + return block ? ( |
| 80 | + <PortableText |
| 81 | + value={block[valgtLocale]} |
| 82 | + components={{ |
| 83 | + block: ({ children }) => ( |
| 84 | + <TypografiWrapper typografi={typografi} style={{ minHeight: '1rem' }}> |
| 85 | + {children} |
| 86 | + </TypografiWrapper> |
| 87 | + ), |
| 88 | + marks: { |
| 89 | + flettefelt: props => { |
| 90 | + if (props?.value?.flettefeltVerdi) { |
| 91 | + return ( |
| 92 | + <span> |
| 93 | + {flettefeltTilTekst(props.value.flettefeltVerdi, flettefelter)} |
| 94 | + </span> |
| 95 | + ); |
| 96 | + } else { |
| 97 | + throw new Error(`Fant ikke flettefeltVerdi`); |
| 98 | + } |
| 99 | + }, |
| 100 | + link: props => { |
| 101 | + return ( |
| 102 | + <a |
| 103 | + target={props.value.blank ? '_blank' : '_self'} |
| 104 | + rel={'noopener noreferrer'} |
| 105 | + href={encodeURI(props.value.href)} |
| 106 | + > |
| 107 | + {props.text} |
| 108 | + </a> |
| 109 | + ); |
| 110 | + }, |
| 111 | + }, |
| 112 | + }} |
| 113 | + /> |
| 114 | + ) : null; |
| 115 | +}; |
| 116 | + |
| 117 | +export default TekstBlock; |
0 commit comments