In React development, maintaining consistent typography across different parts of the application can be challenging. Often, developers find themselves repeating styles for text elements, leading to a cluttered and difficult-to-manage codebase ๐Ÿ˜”. This document outlines the problem and introduces a solution: using a centralized Typography component.

The Problem with Repetitive Inline Styling

Repetitive inline styling for text elements is a common issue. It not only clutters your code but also makes it difficult to maintain and update styles consistently across your application.

Bad Practice Example ๐Ÿ“›

Hereโ€™s an example of what to avoid:

<h1 className="text-3xl font-bold">Login Page</h1>
<p className="text-[#61748E] font-medium  pt-2">
  hello world
</p>

The Solution: Centralized Typography Component

A better approach is to use a centralized Typography component. This approach ensures consistency, adheres to the DRY (Donโ€™t Repeat Yourself) principle, and simplifies updates to your siteโ€™s typography.

Good Practice Example โœ…

Hereโ€™s how you can use the Typography component:

<Typography variant="T_Bold_H5" className="mx-auto">Login Page</Typography>

<Button>
  <Typography variant="T_SemiBold_H6">Continue with Google</Typography>
</Button>

Benefits of Using a Centralized Typography Component

  • Consistency: Ensures that typography is consistent throughout the application.
  • Maintainability: Makes it easier to update and maintain styles.
  • Adherence to DRY Principle: Avoids repetition of similar code, saving development time.
  • Flexibility: Allows for easy changes to the siteโ€™s overall typography from a single location.
  • Responsiveness: Ensures that typography is responsive across different screen sizes.

Typography Component Overview

The Typography component is designed to handle various text styles and animations. Hereโ€™s a brief overview of its structure and usage:

// Import statements
import React from "react";
import { Link, useNavigate } from "react-router-dom";
import { motion as m } from "framer-motion";

// Component definition...

Component Props

  • variant: Specifies the text style variant.
  • children: The content to be rendered.
  • className: Additional CSS classes.
  • maxLines: Maximum number of lines to display.
  • navigate: Direction of navigation for links.
  • link: URL for navigation.
  • target: Target attribute for the link.
  • animate: Type of animation.
  • disableSelect: If true, disables text selection.

Usage Example

<Typography variant="T_Bold_H1" animate="move">
  Welcome to Our Site
</Typography>

Example Typography.jsx

Click on the following dropdown to view a sample Typography.jsx file with all the available variants and animations. But this typography component is not limited to the above variants and animations. You can add as many as you want. You can also add your own custom variants and animations.

By integrating this Typography component into your React project, you can significantly enhance the readability and maintainability of your code, ensuring a consistent and professional appearance across your application.