Skip to content

Commit

Permalink
spike(hero): use Heading instead of allowing generic h1, h2, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
billhimmelsbach committed Apr 1, 2024
1 parent 6cd5acc commit 0c2c037
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/components/Hero/Hero.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import classnames from 'classnames';
import { createElement } from 'react';
import type { HeadingLevel } from '../../types/headingLevel';
import type { HeadingType } from '../Headings/Heading';
import { Heading } from '../Headings/Heading';
import { HeroImage } from './HeroImage';
import './hero.less';
import { useBackgroundImage } from './useBackgroundImage';
Expand Down Expand Up @@ -51,25 +52,41 @@ export default function Hero({
if (isKnockout) heroCnames.push('m-hero__knockout');
if (imageIsPhoto) heroCnames.push('m-hero__overlay');

// NOTE: This is a mapping of the Hero component's HeadingLevel to the Heading component's
// HeadingType but we could also use the HeadingType directly in the Hero component with some
// refactoring of the Heading component or run a regex replace on the HeadingLevel in the Hero
const HeadingLevelToHeadingType: Record<string, HeadingType> = {
h1: '1',
h2: '2',
h3: '3',
h4: '4',
h5: '5',
display: 'display',
eyebrow: 'eyebrow',
slug: 'slug'
};

return (
<div className={classnames(heroCnames)} style={heroStyles} {...properties}>
<div className='m-hero_wrapper' ref={wrapperReference}>
<div className='m-hero_text' style={textStyles} data-testid='hero-text'>
{createElement(
headingLevel,
{
className: `m-hero_heading ${headingLevel}`,
'data-testid': 'hero-heading'
},
heading
)}
{createElement(
subheadingLevel,
{
className: `m-hero_subhead ${subheadingLevel}`,
'data-testid': 'hero-subheading'
},
subheading
<Heading
className='m-hero_heading'
data-testid='hero-heading'
type={HeadingLevelToHeadingType[headingLevel]}
>
{heading}
</Heading>
{subheadingLevel === 'p' ? (
<p className='m-hero_subhead'>{subheading}</p>
) : (
<Heading
className='m-hero_subhead'
data-testid='hero-subheading'
type={HeadingLevelToHeadingType[subheadingLevel]}
>
{subheading}
</Heading>
)}
</div>
<HeroImage image={image} altText={imageAltText} />
Expand Down

0 comments on commit 0c2c037

Please sign in to comment.