Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Title): add title font customization #241

Merged
merged 7 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions src/plugins/Title/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,62 @@ import {Plugin, PluginWidgetProps} from '../../typings';
import {cn} from '../../utils/cn';
import {PLUGIN_ROOT_ATTR_NAME} from '../constants';

import './Title.scss';
import type {TitleFontDataProps} from './types';

export type PluginTitleSize = 'xl' | 'l' | 'm' | 's' | 'xs';
import './Title.scss';

export interface PluginTitleProps extends PluginWidgetProps {
data: {
size: PluginTitleSize;
text: string;
showInTOC: boolean;
} & PluginWidgetProps['data'];
} & TitleFontDataProps &
PluginWidgetProps['data'];
}

const b = cn('dashkit-plugin-title');

export class PluginTitle extends React.Component<PluginTitleProps> {
render() {
const {data} = this.props;
export const PluginTitle = React.forwardRef<HTMLDivElement, PluginTitleProps>(
function PluginTitle_(props, ref) {
const {data} = props;
const text = data.text ? data.text : '';
const size = data.size ? data.size : false;

const styles = getFontStyles(data);

const id = data.showInTOC && text ? encodeURIComponent(text) : undefined;

return (
<div id={id} className={b({size})} {...{[PLUGIN_ROOT_ATTR_NAME]: 'title'}}>
<div
ref={ref}
id={id}
style={styles}
className={b({size})}
{...{[PLUGIN_ROOT_ATTR_NAME]: 'title'}}
>
{text}
</div>
);
},
);

function getFontStyles(props: TitleFontDataProps) {
const {size, fontSize, lineHeight} = props;
const styles: React.CSSProperties = {};

if (lineHeight) {
styles.lineHeight = `${lineHeight}px`;
}
if (fontSize) {
styles.fontSize = `${fontSize}px`;

const shouldSetCustomLineHeight = !lineHeight && !size;
if (shouldSetCustomLineHeight) {
Marginy605 marked this conversation as resolved.
Show resolved Hide resolved
const recommendedLineHeight = Math.round(fontSize * 1.25);
styles.lineHeight = `${recommendedLineHeight}px`;
}
}

return styles;
}

const plugin: Plugin<PluginTitleProps> = {
Expand Down
94 changes: 89 additions & 5 deletions src/plugins/Title/__stories__/Title.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';

import {Flex} from '@gravity-ui/uikit';
import {Card, Flex} from '@gravity-ui/uikit';
import {Meta, StoryObj} from '@storybook/react';

import {PluginTitle, PluginTitleSize} from '../Title';
import {PluginTitle} from '../Title';
import {PluginTitleSize} from '../types';

export default {
title: 'Components/Title',
Expand All @@ -18,12 +19,95 @@ export const Size: Story = {
render: ({data: _, ...args}) => (
<Flex direction="column" gap={2}>
{sizes.map((size) => (
<Card key={size}>
<PluginTitle
data={{size, text: `Title size=${size}`, showInTOC: true}}
{...args}
/>
</Card>
))}
<Card>
<PluginTitle
key={size}
data={{size, text: `Title size=${size}`, showInTOC: true}}
data={{
fontSize: 40,
lineHeight: 100,
text: `Title fontSize=40, lineHeight=100`,
showInTOC: true,
Marginy605 marked this conversation as resolved.
Show resolved Hide resolved
}}
{...args}
/>
))}
</Card>
<Card>
<PluginTitle
data={{
size: 'l',
fontSize: 40,
text: `Title size=l, fontSize=40`,
showInTOC: true,
}}
{...args}
/>
</Card>
<Card>
<PluginTitle
data={{
size: 'l',
fontSize: 40,
lineHeight: 20,
text: `Title size=l, fontSize=40, lineHeight=20 🤷`,
showInTOC: true,
}}
{...args}
/>
</Card>
<Card>
<PluginTitle
data={{
size: 'l',
fontSize: 40,
lineHeight: 70,
text: `Title size=l, fontSize=40, lineHeight=70`,
showInTOC: true,
}}
{...args}
/>
</Card>
<Card>
<PluginTitle
data={{
size: 'l',
lineHeight: 70,
text: `Title size=l, lineHeight=70`,
showInTOC: true,
}}
{...args}
/>
</Card>
<Card>
<PluginTitle
data={{
fontSize: 40,
text: `Title fontSize=40`,
showInTOC: true,
}}
{...args}
/>
</Card>
</Flex>
),
};

export const Default: Story = {
render: ({data, ...args}) => (
<Card>
<PluginTitle data={data} {...args} />
</Card>
),
args: {
data: {
size: 'm',
text: `Title`,
showInTOC: true,
},
},
};
24 changes: 24 additions & 0 deletions src/plugins/Title/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {PluginTitleSize, TitleFontParams} from './types';

export const TITLE_DEFAULT_SIZES: Record<PluginTitleSize, TitleFontParams> = {
xl: {
fontSize: 32,
lineHeight: 40,
},
l: {
fontSize: 24,
lineHeight: 28,
},
m: {
fontSize: 20,
lineHeight: 24,
},
s: {
fontSize: 17,
lineHeight: 24,
},
xs: {
fontSize: 15,
lineHeight: 20,
},
};
2 changes: 2 additions & 0 deletions src/plugins/Title/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './Title';
export {default as pluginTitle} from './Title';
export {PluginTitleSize, TitleFontParams} from './types';
export {TITLE_DEFAULT_SIZES} from './constants';
18 changes: 18 additions & 0 deletions src/plugins/Title/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type PluginTitleSize = 'xl' | 'l' | 'm' | 's' | 'xs';

export interface TitleFontParams {
fontSize: number;
lineHeight: number;
}

export type TitleFontDataProps =
| {
size: PluginTitleSize;
fontSize?: number;
lineHeight?: number;
}
| {
size?: PluginTitleSize;
fontSize: number;
lineHeight?: number;
};
Loading