A React component demonstrating responsive typography with different implementation approaches. This component showcases how text can adapt to different screen sizes using various techniques like media queries, viewport calculations, and fluid calculations.
The Responsive Typography Sample component is built using the following structure:
import React from 'react';
import { Select, Typography } from 'antd';
import { DemoManager } from './sample-layout/DemoManager';
import { Info } from './sample-layout/Info';
import { LibraryTags as Lib, LibraryTagsContainer } from './sample-layout/LibraryTags';
import { Usage } from './sample-layout/Usage';
import './ResponsiveTypographySample.scss';
function ResponsiveTypographySample(props) {
const [mode, setMode] = React.useState('non-responsive');
const handleModeChange = value => {
setMode(value);
};
const renderInfo = () => {
return (
<>
<Info forceOpen>
<Typography.Paragraph>
Here you will find a quick demonstration. This is a sample <b>Responsive Typography</b> application.
</Typography.Paragraph>
<Typography.Paragraph>
Responsive Typography is the design of writing on any website. It allows the adaptation of the website
content to the size of any tablet, laptop, desktop, and mobile device. In this way, the writing can always
be completely readable. One of the <b>Media Query, Viewport Calculation, and Fluid Calc</b> options can be
used to apply Responsive Typography, or a non-responsive setting may be preferred.
</Typography.Paragraph>
</Info>
<Typography.Title level={4}>Controls</Typography.Title>
<Select
style={{ width: 300 }}
value={mode}
onChange={handleModeChange}
options={[
{ value: 'non-responsive', label: 'Non-Responsive' },
{ value: 'media-query', label: 'Responsive With Media Query' },
{ value: 'viewport_sized', label: 'Responsive With Viewport Calculation' },
{ value: 'viewport_fluid', label: 'Responsive With Fluid Calc' }
]}
/>
<Usage path={props.path} title={props.title} />
<LibraryTagsContainer>
<Lib.ReactLib /> <Lib.SassLib />
</LibraryTagsContainer>
</>
);
};
const renderDemo = () => {
return (
<div className={`typography-sample-container ${mode}`}>
<h1>Lorem Ipsum</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<p>Sample text content...</p>
</div>
);
};
return <DemoManager {...props} demoComponent={renderDemo()} infoComponent={renderInfo()} />;
}
export default ResponsiveTypographySample;
Non-Responsive Mode
Media Query Mode
Viewport Calculation Mode
Fluid Calculation Mode
.typography-sample-container {
margin: 5px;
overflow-y: auto;
width: 100%;
height: 100%;
&.non-responsive {
h1 { font-size: calc(var(--typog-nr) * 20); }
h2 { font-size: calc(var(--typog-nr) * 15); }
h3 { font-size: calc(var(--typog-nr) * 11.7); }
h4 { font-size: calc(var(--typog-nr) * 10.7); }
p { font-size: calc(var(--typog-nr) * 10); }
}
&.media-query {
h1 { font-size: calc(var(--typog-mq) * 20); }
h2 { font-size: calc(var(--typog-mq) * 15); }
h3 { font-size: calc(var(--typog-mq) * 11.7); }
h4 { font-size: calc(var(--typog-mq) * 10.7); }
p { font-size: calc(var(--typog-mq) * 10); }
}
&.viewport_sized {
h1 { font-size: 5.9vw; }
h2 { font-size: 3vh; }
h3 { font-size: 2.5vw; }
h4 { font-size: 2.2vw; }
p { font-size: 2vmin; }
}
&.viewport_fluid {
h1 { font-size: clamp(1.8rem, 2.5vw, 2.8rem); }
h2 { font-size: clamp(1.6rem, 2.2vw, 2.4rem); }
h3 { font-size: clamp(1.4rem, 2vw, 2.2rem); }
h4 { font-size: clamp(1.2rem, 1.8vw, 2rem); }
p { font-size: max(1.2rem, 1.2vw); }
}
}
src/
├── export-sample/
│ ├── ResponsiveTypographySample.jsx
│ ├── ResponsiveTypographySample.scss
│ ├── sample-layout/
│ │ ├── DemoManager.jsx
│ │ ├── Info.jsx
│ │ ├── LibraryTags.jsx
│ │ ├── Usage.jsx
│ │ └── ...
│ └── utils/
│ ├── color-util.js
│ └── random-util.js
└── main.jsx
npm install
npm run dev
npm run build