-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95b68da
commit 2b6ae05
Showing
6 changed files
with
82 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,7 @@ | |
|
||
@import "./prism-plus.css"; | ||
@import "./prism-vsc-dark-plus.css"; | ||
|
||
:root { | ||
scroll-padding-top: 72px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,53 @@ | ||
import type { HTMLAttributes } from 'react'; | ||
import cn from '@/utils/cn'; | ||
|
||
type HeadingProps = { | ||
as?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'; | ||
} & HTMLAttributes<HTMLHeadingElement>; | ||
type HTMLHeadingProps = HTMLAttributes<HTMLHeadingElement>; | ||
|
||
function Heading({ as = 'h2', ...attributes }: HeadingProps) { | ||
const HeadingTag = as; | ||
type HeadingProps = { | ||
Component?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'; | ||
} & HTMLHeadingProps; | ||
|
||
return <HeadingTag {...attributes} />; | ||
function Heading({ | ||
Component = 'h2', | ||
id, | ||
className, | ||
children, | ||
...attributes | ||
}: HeadingProps) { | ||
return ( | ||
<Component | ||
id={id} | ||
className={cn('group relative', className)} | ||
{...attributes} | ||
> | ||
<a | ||
href={id} | ||
className="absolute -left-6 no-underline opacity-0 group-hover:opacity-100" | ||
> | ||
# | ||
</a> | ||
{children} | ||
</Component> | ||
); | ||
} | ||
|
||
export const H1 = (props: HTMLHeadingProps) => ( | ||
<Heading Component="h1" {...props} /> | ||
); | ||
export const H2 = (props: HTMLHeadingProps) => ( | ||
<Heading Component="h2" {...props} /> | ||
); | ||
export const H3 = (props: HTMLHeadingProps) => ( | ||
<Heading Component="h3" {...props} /> | ||
); | ||
export const H4 = (props: HTMLHeadingProps) => ( | ||
<Heading Component="h4" {...props} /> | ||
); | ||
export const H5 = (props: HTMLHeadingProps) => ( | ||
<Heading Component="h5" {...props} /> | ||
); | ||
export const H6 = (props: HTMLHeadingProps) => ( | ||
<Heading Component="h6" {...props} /> | ||
); | ||
|
||
export default Heading; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
import Heading from '.'; | ||
import Heading, { H1, H2, H3, H4, H5, H6 } from '.'; | ||
|
||
describe('Heading component', () => { | ||
it('should render correct element', () => { | ||
const name = 'The heading text'; | ||
|
||
render(<Heading>{name}</Heading>); | ||
|
||
const heading = screen.getByRole('heading', { name }); | ||
const heading = screen.getByRole('heading'); | ||
|
||
expect(heading).toBeInTheDocument(); | ||
expect(heading).toHaveTextContent(name); | ||
expect(heading).toHaveTextContent(`#${name}`); | ||
expect(heading.tagName).toBe('H2'); | ||
}); | ||
|
||
it.each([ | ||
[H1, 'H1'], | ||
[H2, 'H2'], | ||
[H3, 'H3'], | ||
[H4, 'H4'], | ||
[H5, 'H5'], | ||
[H6, 'H6'], | ||
])('should render correct tag name', (Component, expected) => { | ||
const name = `The ${expected} tag`; | ||
|
||
render(<Component>{name}</Component>); | ||
|
||
const heading = screen.getByRole('heading'); | ||
|
||
expect(heading).toHaveTextContent(`#${name}`); | ||
expect(heading.tagName).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import Heading from './Heading'; | ||
|
||
export * from './Heading'; | ||
|
||
export default Heading; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters