-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathParagraph.tsx
61 lines (56 loc) · 1.56 KB
/
Paragraph.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import styles from '@digdir/designsystemet-css/baseline/typography/paragraph.module.css';
import { Slot } from '@radix-ui/react-slot';
import cl from 'clsx/lite';
import type { HTMLAttributes } from 'react';
import { forwardRef } from 'react';
export type ParagraphProps = {
/**
* Changes text sizing
*
* @default 'md'
*
*/
size?: 'xs' | 'sm' | 'md' | 'lg';
/** Adds margin-bottom */
spacing?: boolean;
/**
* Adjusts styling for paragraph length
* @default 'default'
*/
variant?: 'long' | 'default' | 'short';
/**
* Change the default rendered element for the one passed as a child, merging their props and behavior.
* @default false
*/
asChild?: boolean;
} & HTMLAttributes<HTMLParagraphElement>;
const lineHeightMap = {
short: 'ds-line-height--sm',
default: 'ds-line-height--md',
long: 'ds-line-height--lg',
};
/**
* Use `Paragraph` to display text with paragraph text styles.
*
* @example
* <Paragraph size='lg'>Paragraph</Paragraph>
*/
export const Paragraph = forwardRef<HTMLParagraphElement, ParagraphProps>(
({ className, spacing, size = 'md', asChild, variant, ...rest }, ref) => {
const Component = asChild ? Slot : 'p';
return (
<Component
ref={ref}
className={cl(
styles['ds-paragraph'],
spacing && styles['ds-paragraph--spacing'],
styles[`ds-paragraph--${size}`],
styles[lineHeightMap[variant ?? 'default'] as keyof typeof styles],
className,
)}
{...rest}
/>
);
},
);
Paragraph.displayName = 'Paragraph';