Files
element-call/src/typography/Typography.tsx

257 lines
5.1 KiB
TypeScript
Raw Normal View History

2022-05-04 17:09:48 +01:00
/*
Copyright 2022 New Vector Ltd
2022-05-04 17:09:48 +01:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { createElement, forwardRef, ReactNode } from "react";
2022-01-04 16:00:13 -08:00
import classNames from "classnames";
import { Link as RouterLink } from "react-router-dom";
import * as H from "history";
2022-01-04 16:00:13 -08:00
import styles from "./Typography.module.css";
interface TypographyProps {
children: ReactNode;
fontWeight?: string;
className?: string;
overflowEllipsis?: boolean;
as?: string;
}
export const Headline = forwardRef<HTMLHeadingElement, TypographyProps>(
2022-01-04 17:09:27 -08:00
(
{
as: Component = "h1",
children,
className,
fontWeight,
overflowEllipsis,
...rest
},
ref
) => {
return createElement(
Component,
{
...rest,
className: classNames(
styles[fontWeight ?? ""],
2022-01-04 17:09:27 -08:00
{ [styles.overflowEllipsis]: overflowEllipsis },
className
),
ref,
},
children
2022-01-04 16:00:13 -08:00
);
}
);
export const Title = forwardRef<HTMLHeadingElement, TypographyProps>(
2022-01-04 17:09:27 -08:00
(
{
as: Component = "h2",
children,
className,
fontWeight,
overflowEllipsis,
...rest
},
ref
) => {
return createElement(
Component,
{
...rest,
className: classNames(
styles[fontWeight ?? ""],
2022-01-04 17:09:27 -08:00
{ [styles.overflowEllipsis]: overflowEllipsis },
className
),
ref,
},
children
2022-01-04 16:00:13 -08:00
);
}
);
export const Subtitle = forwardRef<HTMLParagraphElement, TypographyProps>(
2022-01-04 17:09:27 -08:00
(
{
as: Component = "h3",
children,
className,
fontWeight,
overflowEllipsis,
...rest
},
ref
) => {
return createElement(
Component,
{
...rest,
className: classNames(
styles[fontWeight ?? ""],
2022-01-04 17:09:27 -08:00
{ [styles.overflowEllipsis]: overflowEllipsis },
className
),
ref,
},
children
2022-01-04 16:00:13 -08:00
);
}
);
export const Body = forwardRef<HTMLParagraphElement, TypographyProps>(
2022-01-04 17:09:27 -08:00
(
{
as: Component = "p",
children,
className,
fontWeight,
overflowEllipsis,
...rest
},
ref
) => {
return createElement(
Component,
{
...rest,
className: classNames(
styles[fontWeight ?? ""],
2022-01-04 17:09:27 -08:00
{ [styles.overflowEllipsis]: overflowEllipsis },
className
),
ref,
},
children
2022-01-04 16:00:13 -08:00
);
}
);
export const Caption = forwardRef<HTMLParagraphElement, TypographyProps>(
2022-01-04 17:09:27 -08:00
(
{
as: Component = "p",
children,
className,
fontWeight,
overflowEllipsis,
...rest
},
ref
) => {
return createElement(
Component,
{
...rest,
className: classNames(
2022-01-04 17:09:27 -08:00
styles.caption,
styles[fontWeight ?? ""],
2022-01-04 17:09:27 -08:00
{ [styles.overflowEllipsis]: overflowEllipsis },
className
),
ref,
},
children
2022-01-04 16:00:13 -08:00
);
}
);
export const Micro = forwardRef<HTMLParagraphElement, TypographyProps>(
2022-01-04 17:09:27 -08:00
(
{
as: Component = "p",
children,
className,
fontWeight,
overflowEllipsis,
...rest
},
ref
) => {
return createElement(
Component,
{
...rest,
className: classNames(
2022-01-04 17:09:27 -08:00
styles.micro,
styles[fontWeight ?? ""],
2022-01-04 17:09:27 -08:00
{ [styles.overflowEllipsis]: overflowEllipsis },
className
),
ref,
},
children
2022-01-04 16:00:13 -08:00
);
}
);
interface LinkProps extends TypographyProps {
to?: H.LocationDescriptor<unknown>;
color?: string;
href?: string;
}
export const Link = forwardRef<HTMLAnchorElement, LinkProps>(
2022-01-04 16:00:13 -08:00
(
{
2022-01-06 13:14:15 -08:00
as,
2022-01-04 16:00:13 -08:00
children,
className,
color = "link",
href,
2022-01-06 13:14:15 -08:00
to,
2022-01-04 16:00:13 -08:00
fontWeight,
2022-01-04 17:09:27 -08:00
overflowEllipsis,
2022-01-04 16:00:13 -08:00
...rest
},
ref
) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const Component: string | RouterLink = as || (to ? RouterLink : "a");
let externalLinkProps: { href: string; target: string; rel: string };
2022-01-04 16:00:13 -08:00
if (href) {
externalLinkProps = {
href,
2022-01-04 16:00:13 -08:00
target: "_blank",
rel: "noreferrer noopener",
};
}
return createElement(
Component,
{
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
...externalLinkProps,
...rest,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
to: to,
className: classNames(
2022-01-04 17:09:27 -08:00
styles[color],
styles[fontWeight ?? ""],
2022-01-04 17:09:27 -08:00
{ [styles.overflowEllipsis]: overflowEllipsis },
className
),
ref: ref,
},
children
2022-01-04 16:00:13 -08:00
);
}
);