Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 8165abe

Browse files
committed
Add Heading component and story
1 parent fbd16a6 commit 8165abe

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

components/atoms/Heading/Heading.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import createMarkup from '@/functions/createMarkup'
2+
import PropTypes from 'prop-types'
3+
import React from 'react'
4+
5+
/**
6+
* @param props0
7+
* @param props0.children
8+
* @param props0.className
9+
* @param props0.id
10+
* @param props0.tag
11+
*/
12+
export default function Heading({children, className, id, tag}) {
13+
if (typeof children === 'string') {
14+
return React.createElement(tag, {
15+
className,
16+
id,
17+
dangerouslySetInnerHTML: createMarkup(children)
18+
})
19+
} else {
20+
return React.createElement(
21+
tag,
22+
{
23+
className,
24+
id
25+
},
26+
children
27+
)
28+
}
29+
}
30+
31+
Heading.propTypes = {
32+
children: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
33+
className: PropTypes.string,
34+
id: PropTypes.string,
35+
tag: PropTypes.oneOf(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
36+
}
37+
38+
Heading.defaultProps = {
39+
tag: 'h1'
40+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {Meta, Story, Canvas} from '@storybook/addon-docs/blocks'
2+
3+
import Heading from '.'
4+
5+
<Meta title="Components/Atoms/Heading" component={Heading} />
6+
7+
# Heading
8+
9+
Use this component inside other components when you need to dynamically set the heading tag that's used.
10+
For example, a card's heading might need to be `<h2>` in one place but an `<h3>` when used somewhere else.
11+
12+
This component is deliberatley unstyled. Pass down class names with the `className` prop.
13+
14+
<Canvas>
15+
<Story name="Component">
16+
<Heading>This is a heading</Heading>
17+
</Story>
18+
</Canvas>
19+
20+
# Tag
21+
22+
`<h1>` through `<h6>` tags are supported.
23+
24+
<Canvas>
25+
<Story name="Tag">
26+
<Heading tag="h1">This is an h1</Heading>
27+
<Heading tag="h2">This is an h2</Heading>
28+
<Heading tag="h3">This is an h3</Heading>
29+
<Heading tag="h4">This is an h4</Heading>
30+
<Heading tag="h5">This is an h5</Heading>
31+
<Heading tag="h6">This is an h6</Heading>
32+
</Story>
33+
</Canvas>
34+
35+
# Markup Support
36+
37+
This component supports various types of children content including strings of HTML and standard JSX. The following shows both content types being passed in as children.
38+
39+
<Canvas>
40+
<Story name="Markup Support">
41+
<Heading tag="h1">{'<strong>String</strong> of HTML Example'}</Heading>
42+
<Heading tag="h1">
43+
<strong>Object</strong> of JSX Example
44+
</Heading>
45+
</Story>
46+
</Canvas>
47+
48+
## Controls
49+
50+
export const Template = (args) => <Heading {...args}>{args.children}</Heading>
51+
52+
<Canvas>
53+
<Story
54+
name="Controls"
55+
args={{
56+
tag: 'h1',
57+
children: 'This is a heading'
58+
}}
59+
>
60+
{Template.bind({})}
61+
</Story>
62+
</Canvas>

components/atoms/Heading/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './Heading'

0 commit comments

Comments
 (0)