Skip to content

Commit 7e5c339

Browse files
committed
feat(title): add property to change heading level and design modifications
1 parent 547e6c7 commit 7e5c339

File tree

5 files changed

+64
-20
lines changed

5 files changed

+64
-20
lines changed

packages/title/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ const TitleDefault = () => (
2525
);
2626
export default TitleDefault;
2727
```
28+
29+
By default, using this component produces a level 2 title (level 1 is restricted to the title bar of the page). If you want another title level you can specify the `heading` property with the value `h2`, `h3`, `h4`. For now `h5` and `h6` are not supported.
30+
31+
```javascript
32+
const TitleDefault = () => <Title heading="h3">Sample Title</Title>;
33+
export default TitleDefault;
34+
```

packages/title/src/Title.stories.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ Default.args = {
2626
title: 'Sample Title',
2727
classModifier: '',
2828
className: '',
29+
heading: 'h2',
2930
};

packages/title/src/Title.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
import React, { ComponentPropsWithoutRef } from 'react';
22
import { getComponentClassName } from '@axa-fr/react-toolkit-core';
33

4-
type TitleProps = ComponentPropsWithoutRef<'h1'> & {
4+
type Headings = 'h2' | 'h3' | 'h4';
5+
6+
type TitleProps = ComponentPropsWithoutRef<'h2'> & {
57
classModifier?: string;
8+
heading?: Headings;
69
};
710

811
const Title = ({
912
className,
1013
classModifier,
1114
children,
15+
heading: Heading = 'h2',
1216
...otherProps
1317
}: TitleProps) => {
1418
const componentClassName = getComponentClassName(
1519
className,
1620
classModifier,
1721
'af-title'
1822
);
23+
1924
return (
20-
<h1 className={componentClassName} {...otherProps}>
25+
<Heading className={componentClassName} {...otherProps}>
2126
{children}
22-
</h1>
27+
</Heading>
2328
);
2429
};
2530

packages/title/src/__tests__/Title.spec.tsx

+27-10
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ describe('Title', () => {
88
render(<Title>A title</Title>);
99

1010
// Assert
11-
expect(screen.getByText(/A title/)).toBeInTheDocument();
11+
expect(
12+
screen.getByRole('heading', { name: /A title/, level: 2 })
13+
).toBeInTheDocument();
1214
});
1315

1416
it('should have default class', () => {
1517
// Act
1618
render(<Title>A title</Title>);
1719

1820
// Assert
19-
expect(screen.getByText(/A title/)).toHaveClass('af-title', {
21+
expect(
22+
screen.getByRole('heading', { name: /A title/, level: 2 })
23+
).toHaveClass('af-title', {
2024
exact: true,
2125
});
2226
});
@@ -26,7 +30,9 @@ describe('Title', () => {
2630
render(<Title className="custom-class">A title</Title>);
2731

2832
// Assert
29-
expect(screen.getByText(/A title/)).toHaveClass('custom-class', {
33+
expect(
34+
screen.getByRole('heading', { name: /A title/, level: 2 })
35+
).toHaveClass('custom-class', {
3036
exact: true,
3137
});
3238
});
@@ -40,12 +46,11 @@ describe('Title', () => {
4046
);
4147

4248
// Assert
43-
expect(screen.getByText(/A title/)).toHaveClass(
44-
'custom-class custom-class--modifier',
45-
{
46-
exact: true,
47-
}
48-
);
49+
expect(
50+
screen.getByRole('heading', { name: /A title/, level: 2 })
51+
).toHaveClass('custom-class custom-class--modifier', {
52+
exact: true,
53+
});
4954
});
5055

5156
it('should not have classModifier attribute', () => {
@@ -57,6 +62,18 @@ describe('Title', () => {
5762
);
5863

5964
// Assert
60-
expect(screen.getByText(/A title/)).not.toHaveAttribute('classModifier');
65+
expect(
66+
screen.getByRole('heading', { name: /A title/, level: 2 })
67+
).not.toHaveAttribute('classModifier');
68+
});
69+
70+
it('should have correct heading level', () => {
71+
// Act
72+
render(<Title heading="h3">A title</Title>);
73+
74+
// Assert
75+
expect(
76+
screen.getByRole('heading', { name: /A title/, level: 3 })
77+
).toBeInTheDocument();
6178
});
6279
});

packages/title/src/title.scss

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
@import '@axa-fr/react-toolkit-core/src/common/scss/core.scss';
22

3+
h2.af-title {
4+
font-size: 1.5rem;
5+
}
6+
7+
h3.af-title {
8+
font-size: 1.25rem;
9+
}
10+
11+
h4.af-title {
12+
font-size: 1.125rem;
13+
}
14+
15+
h3.af-title::after,
16+
h4.af-title::after {
17+
bottom: 0.1rem;
18+
}
19+
320
.af-title {
421
position: relative;
522
overflow: hidden;
623
font-weight: 600;
24+
color: $color-texte;
725

826
&::after {
9-
background-color: $brand-primary;
10-
border: none;
11-
bottom: 0.3125rem;
12-
clear: left;
27+
background-color: $color-gray-1;
28+
bottom: 0.3rem;
1329
content: ' ';
14-
float: left;
1530
height: 1px;
16-
margin-left: 0.875rem;
17-
padding: 0;
31+
margin: 0.5rem 1rem;
1832
position: absolute;
1933
width: 100%;
2034
z-index: 1;

0 commit comments

Comments
 (0)