In one of my React components, I want to use props when creating a styled-component to determine which tag to use. Specifically I am looking for what's in props.headingLevel to decide which heading level to create. E.g. h1 or h2.
All of the styled-components examples show components being created outside of the React component class or function, however I have only got this working by creating the styled-component inside the function when props is available. I know you can use props inside of the styled-component via the props passed to the styled-component itself within the React function but at this point it's too late.
So my question is twofold. Is it acceptable to create styled-components inside React components functions? And, is there a better way to manage dynamic tags, e.g. creating a styled-component tag based on context or props passed?
Here is my component:
// Modules
import React from 'react';
import styled from 'styled-components';
import PropTypes from 'prop-types';
// Data
import styleData from '../../../data/styles';
const SupplementaryTitle = styled.span`
display: block;
text-align: right;
`
const Title = (props) => {
const HeadingLevel = props.headingLevel ? props.headingLevel : 'h2';
const StyledHeading = styled(HeadingLevel)`
margin: 0;
font-weight: ${styleData['typography_settings']['title'][props.typographySize]['weight']};
font-size: ${styleData['typography_settings']['title'][props.typographySize]['size']};
font-family: ${styleData['typography_settings']['title'][props.typographySize]['font']};
line-height: ${styleData['typography_settings']['title'][props.typographySize]['line_height']};
`
const hasSupplementaryTitle = props.supplementaryTitle;
if (hasSupplementaryTitle) {
return (
<StyledHeading>
{props.children}
<SupplementaryTitle>{props.supplementaryTitle}</SupplementaryTitle>
</StyledHeading>
)
}
return (
<StyledHeading>{props.children}</StyledHeading>
)
}
Title.propTypes = {
children: PropTypes.string.isRequired,
headingLevel: PropTypes.string,
typographySize: PropTypes.oneOf([
'small', 'medium', 'large'
]).isRequired,
supplementaryTitle: PropTypes.string,
}
export default Title;