What is @leafygreen-ui/typography?
@leafygreen-ui/typography is a React component library that provides a set of typography components designed to align with MongoDB's design system. It allows developers to easily implement consistent and accessible typography styles in their applications.
What are @leafygreen-ui/typography's main functionalities?
Basic Typography
This feature allows you to use predefined typography components like H1 and Body to create headings and body text with consistent styling.
import { H1, Body } from '@leafygreen-ui/typography';
function App() {
return (
<div>
<H1>Heading 1</H1>
<Body>Body text goes here.</Body>
</div>
);
}
Customizing Typography
This feature allows you to customize the typography components by adding custom CSS classes to change their appearance.
import { H2, Subtitle } from '@leafygreen-ui/typography';
function CustomTypography() {
return (
<div>
<H2 className="custom-heading">Custom Heading</H2>
<Subtitle className="custom-subtitle">Custom Subtitle</Subtitle>
</div>
);
}
// CSS
.custom-heading {
color: #4caf50;
}
.custom-subtitle {
font-style: italic;
}
Responsive Typography
This feature demonstrates how to create responsive typography that changes based on the screen size using media queries.
import { H3, Overline } from '@leafygreen-ui/typography';
import { useMediaQuery } from 'react-responsive';
function ResponsiveTypography() {
const isMobile = useMediaQuery({ maxWidth: 767 });
return (
<div>
<H3>{isMobile ? 'Mobile Heading' : 'Desktop Heading'}</H3>
<Overline>{isMobile ? 'Mobile Overline' : 'Desktop Overline'}</Overline>
</div>
);
}
Other packages similar to @leafygreen-ui/typography
typography
The 'typography' package is a powerful toolkit for building websites with beautiful typography. It provides a wide range of customization options and plugins to enhance the typography of your site. Compared to @leafygreen-ui/typography, it offers more flexibility and is not tied to a specific design system.
styled-components
The 'styled-components' package allows you to write actual CSS code to style your components. It provides a way to create reusable, styled typography components. While it does not offer predefined typography components like @leafygreen-ui/typography, it gives you the freedom to create custom styles.
material-ui
The 'material-ui' package is a popular React UI framework that includes a comprehensive set of typography components. It follows Google's Material Design guidelines and offers a wide range of customization options. Compared to @leafygreen-ui/typography, it provides a more extensive set of UI components beyond just typography.
Typography
Installation
Yarn
yarn add @leafygreen-ui/typography
NPM
npm install @leafygreen-ui/typography
H1
Example
import { H1, H2, Subtitle, Body, InlineCode, Disclaimer, Overline } from '@leafygreen-ui/typography';
<H1>Heading 1</H1>
<H2>Heading 2</H2>
<Subtitle>Subtitle</Subtitle>
<Body weight="medium">Body</Body>
<InlineCode>Code</InlineCode>
<Disclaimer>Disclaimer</Disclaimer>
<Overline>Overline</Overline>
Output HTML
<h1 class="leafygreen-ui-wbskfk">Heading 1</h1>
<h2 class="leafygreen-ui-1t0mh6j">Heading 2</h2>
<h6 class="leafygreen-ui-1dmxpt6">Subtitle</h6>
<p class="leafygreen-ui-wkgw79">Body</p>
<code class="leafygreen-ui-18bk0d8">Code</code>
<small class="leafygreen-ui-1cggyhz">Disclaimer</small>
<div class="leafygreen-ui-vezyzr">Overline</div>
Properties
All props extend the HTMLElementProps of their root tag, however <Body />
and <Overline/>
accept extra props
Component | Root |
---|
H1 | h1 |
H2 | h2 |
Subtitle | h6 |
Body | p |
InlineCode | code |
Disclaimer | small |
Overline | p |
Body
Prop | Type | Description | Default |
---|
weight | regular , medium | font-weight applied to typography element | regular |
Overline
Prop | Type | Description | Default |
---|
component | HTMLElement or React.ReactElement | The component or HTML tag to be rendered by the <Box /> component. Note: This will supersede the behavior of any other props. | p |