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

Wrap Typography #1886

Merged
merged 10 commits into from
Jul 21, 2023
165 changes: 165 additions & 0 deletions packages/odyssey-react-mui/src/Typography.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*!
* Copyright (c) 2023-present, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (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 {
Typography as MuiTypography,
TypographyProps as MuiTypographyProps,
} from "@mui/material";
import { ElementType, ReactNode } from "react";

export const typographyVariantValues = {
h1: "h1",
h2: "h2",
h3: "h3",
h4: "h4",
h5: "h5",
h6: "h6",
body: "body1",
caption: "subtitle1",
jordankoschei-okta marked this conversation as resolved.
Show resolved Hide resolved
legend: "legend",
};

export type TypographyProps = {
children: ReactNode;
classes?: object;
component?: ElementType;
variant?: keyof typeof typographyVariantValues;
};

export const Typography = ({
children,
classes,
component,
variant = "body",
}: TypographyProps) => {
if (!component) {
component = variant;
if (variant === "body") {
component = "p";
}
if (variant === "caption") {
component = "h6";
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the result of some cleverness in MUI that makes things more difficult for us.

We want to allow consumers to change the component prop independently of the variant. The component prop, strangely, is required in this implementation. It can't be undefined.

This conditional does the following:

  • If component is explicitly defined, move on
  • If not, set component to variant
  • If variant is one of the two possibilities that doesn't map directly to an HTML element, set the component manually

It's ugly, it's brute-force, and it gets the job done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To throw another wrench in: we should probably alias the body variant to <P> for consistency, since <body> has a very different use. <Caption> is also a little 😬 because <caption> is a <table>-only element in HTML.

I'm in favor of this pattern, but it might start to feel limiting for some folks.


return (
<MuiTypography
children={children}
classes={classes}
component={component}
variant={
typographyVariantValues[variant] as MuiTypographyProps["variant"]
jordankoschei-okta marked this conversation as resolved.
Show resolved Hide resolved
}
/>
);
};

Typography.displayName = "Typography";

export const H1 = ({ children, classes, component }: TypographyProps) => (
<Typography
children={children}
classes={classes}
component={component}
variant="h1"
/>
);

H1.displayName = "H1";

export const H2 = ({ children, classes, component }: TypographyProps) => (
<Typography
children={children}
classes={classes}
component={component}
variant="h2"
/>
);

H2.displayName = "H2";

export const H3 = ({ children, classes, component }: TypographyProps) => (
<Typography
children={children}
classes={classes}
component={component}
variant="h3"
/>
);

H3.displayName = "H3";

export const H4 = ({ children, classes, component }: TypographyProps) => (
<Typography
children={children}
classes={classes}
component={component}
variant="h4"
/>
);

H4.displayName = "H4";

export const H5 = ({ children, classes, component }: TypographyProps) => (
<Typography
children={children}
classes={classes}
component={component}
variant="h5"
/>
);

H5.displayName = "H5";

export const H6 = ({ children, classes, component }: TypographyProps) => (
<Typography
children={children}
classes={classes}
component={component}
variant="h6"
/>
);

H6.displayName = "H6";

export const Body = ({ children, classes, component }: TypographyProps) => (
<Typography
children={children}
classes={classes}
component={component}
variant="body"
/>
);

Body.displayName = "Body";

export const Caption = ({ children, classes, component }: TypographyProps) => (
<Typography
children={children}
classes={classes}
component={component}
variant="caption"
/>
);

Caption.displayName = "Caption";

export const Legend = ({ children, classes, component }: TypographyProps) => (
<Typography
children={children}
classes={classes}
component={component}
variant="legend"
/>
);

Legend.displayName = "Legend";
4 changes: 1 addition & 3 deletions packages/odyssey-react-mui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export {
/** @deprecated Will be removed in a future Odyssey version in lieu of a wrapped version. */
ScopedCssBaseline,
ThemeProvider,
/** @deprecated Will be removed in a future Odyssey version in lieu of a wrapped version. */
Typography,
} from "@mui/material";

export type {
Expand All @@ -59,7 +57,6 @@ export type {
PaperProps,
ScopedCssBaselineProps,
ThemeOptions,
TypographyProps,
} from "@mui/material";

export * from "./Autocomplete";
Expand Down Expand Up @@ -102,4 +99,5 @@ export * from "./theme";
export * from "./Toast";
export * from "./ToastStack";
export * from "./Tooltip";
export * from "./Typography";
export * from "./useUniqueId";
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
Canvas,
Meta,
Title,
Subtitle,
Description,
Primary,
Controls,
Stories,
} from "@storybook/addon-docs";
import { Story } from "@storybook/blocks";
import * as TypographyStories from "./Typography.stories";

<Meta of={TypographyStories} />

<Title of={TypographyStories} />
<Subtitle of={TypographyStories} />
<Description of={TypographyStories} />
<Primary of={TypographyStories} />

Typography can be rendered with the `<Typography>` component itself, or via one of its
semantic aliases (`<H1>`, `<H2>`, `<Body>`, `<Caption>`, etc.)

<Controls />
<Stories />
Loading