Title
The Title component renders a heading element (h1 to h6) based on the order prop. It allows for flexible typography styles and can be customized with additional class names.
Features
- Dynamic Heading Levels: The
Titlecomponent renders different heading elements (h1,h2,h3,h4,h5,h6) based on theorderprop. - Customizable Styles: Predefined typography styles are applied according to the heading level, but additional class names can be passed for further customization.
- Semantic HTML: The component ensures that the correct heading level is used for better semantic structure and accessibility.
Props
TitleProps
- order: A number between
1and6representing the heading level (h1toh6). This prop is required. - className: An optional string to add custom styles or override the default heading styles.
- children: The content to be displayed inside the heading.
- Any other props from
React.ComponentPropsWithoutRef<'head'>can be passed, such asid,style, or event handlers.
Example
import React from 'react';
import Title from './path/to/Title';
// Example 1: Rendering an h1 element
const HeadingOne = () => <Title order={1}>This is an H1 Heading</Title>;
// Example 2: Rendering an h3 element with custom styles
const HeadingThree = () => <Title order={3} className="custom-heading">This is an H3 Heading</Title>;
// Example 3: Rendering an h6 element
const HeadingSix = () => <Title order={6}>This is an H6 Heading</Title>;
Title Component
The Title component renders a heading element (h1 to h6) based on the order prop. It allows for flexible typography styles and can be customized with additional class names.
Features
- Dynamic Heading Levels: The
Titlecomponent renders different heading elements (h1,h2,h3,h4,h5,h6) based on theorderprop. - Customizable Styles: Predefined typography styles are applied according to the heading level, but additional class names can be passed for further customization.
- Semantic HTML: The component ensures that the correct heading level is used for better semantic structure and accessibility.
Props
TitleProps
- order: A number between
1and6representing the heading level (h1toh6). This prop is required. - className: An optional string to add custom styles or override the default heading styles.
- children: The content to be displayed inside the heading.
- Any other props from
React.ComponentPropsWithoutRef<'head'>can be passed, such asid,style, or event handlers.
Example
import React from 'react';
import Title from './path/to/Title';
// Example 1: Rendering an h1 element
const HeadingOne = () => <Title order={1}>This is an H1 Heading</Title>;
// Example 2: Rendering an h3 element with custom styles
const HeadingThree = () => <Title order={3} className="custom-heading">This is an H3 Heading</Title>;
// Example 3: Rendering an h6 element
const HeadingSix = () => <Title order={6}>This is an H6 Heading</Title>;
How It Works
- The
Titlecomponent takes anorderprop, which determines the heading level (h1toh6). - Based on the
ordervalue, the appropriate heading tag is dynamically rendered. - The component applies predefined typography styles based on the heading level, using the
titleStylefunction to manage the styles.
titleStyle Function
The titleStyle function uses the cn utility from dotori-utils to apply conditional styles based on the order prop. The following styles are applied:
- h1 (
order: 1):typo-3xl700 - h2 (
order: 2):typo-xl700 - h3 (
order: 3):typo-lg700 - h4 (
order: 4):typo-md700 - h5 (
order: 5):typo-sm700 - h6 (
order: 6):typo-xs700
Default Behavior
- If no additional
classNameis provided, the component defaults to the predefined styles based on theorder. - The heading element (
h1,h2, etc.) is dynamically determined by theorderprop, ensuring proper semantic HTML.
Notes
- The
Titlecomponent is useful for maintaining consistent typography across your application while ensuring that headings are semantically correct. - By passing a
className, you can override or extend the default styles for further customization. - It is important to use the correct heading levels to maintain a structured and accessible document outline.