UNPKG

8.34 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import composeClasses from '@mui/utils/composeClasses';
7import { styled, internal_createExtendSxProp } from "../zero-styled/index.js";
8import memoTheme from "../utils/memoTheme.js";
9import { useDefaultProps } from "../DefaultPropsProvider/index.js";
10import capitalize from "../utils/capitalize.js";
11import createSimplePaletteValueFilter from "../utils/createSimplePaletteValueFilter.js";
12import { getTypographyUtilityClass } from "./typographyClasses.js";
13import { jsx as _jsx } from "react/jsx-runtime";
14const v6Colors = {
15 primary: true,
16 secondary: true,
17 error: true,
18 info: true,
19 success: true,
20 warning: true,
21 textPrimary: true,
22 textSecondary: true,
23 textDisabled: true
24};
25const extendSxProp = internal_createExtendSxProp();
26const useUtilityClasses = ownerState => {
27 const {
28 align,
29 gutterBottom,
30 noWrap,
31 paragraph,
32 variant,
33 classes
34 } = ownerState;
35 const slots = {
36 root: ['root', variant, ownerState.align !== 'inherit' && `align${capitalize(align)}`, gutterBottom && 'gutterBottom', noWrap && 'noWrap', paragraph && 'paragraph']
37 };
38 return composeClasses(slots, getTypographyUtilityClass, classes);
39};
40export const TypographyRoot = styled('span', {
41 name: 'MuiTypography',
42 slot: 'Root',
43 overridesResolver: (props, styles) => {
44 const {
45 ownerState
46 } = props;
47 return [styles.root, ownerState.variant && styles[ownerState.variant], ownerState.align !== 'inherit' && styles[`align${capitalize(ownerState.align)}`], ownerState.noWrap && styles.noWrap, ownerState.gutterBottom && styles.gutterBottom, ownerState.paragraph && styles.paragraph];
48 }
49})(memoTheme(({
50 theme
51}) => ({
52 margin: 0,
53 variants: [{
54 props: {
55 variant: 'inherit'
56 },
57 style: {
58 // Some elements, like <button> on Chrome have default font that doesn't inherit, reset this.
59 font: 'inherit',
60 lineHeight: 'inherit',
61 letterSpacing: 'inherit'
62 }
63 }, ...Object.entries(theme.typography).filter(([variant, value]) => variant !== 'inherit' && value && typeof value === 'object').map(([variant, value]) => ({
64 props: {
65 variant
66 },
67 style: value
68 })), ...Object.entries(theme.palette).filter(createSimplePaletteValueFilter()).map(([color]) => ({
69 props: {
70 color
71 },
72 style: {
73 color: (theme.vars || theme).palette[color].main
74 }
75 })), ...Object.entries(theme.palette?.text || {}).filter(([, value]) => typeof value === 'string').map(([color]) => ({
76 props: {
77 color: `text${capitalize(color)}`
78 },
79 style: {
80 color: (theme.vars || theme).palette.text[color]
81 }
82 })), {
83 props: ({
84 ownerState
85 }) => ownerState.align !== 'inherit',
86 style: {
87 textAlign: 'var(--Typography-textAlign)'
88 }
89 }, {
90 props: ({
91 ownerState
92 }) => ownerState.noWrap,
93 style: {
94 overflow: 'hidden',
95 textOverflow: 'ellipsis',
96 whiteSpace: 'nowrap'
97 }
98 }, {
99 props: ({
100 ownerState
101 }) => ownerState.gutterBottom,
102 style: {
103 marginBottom: '0.35em'
104 }
105 }, {
106 props: ({
107 ownerState
108 }) => ownerState.paragraph,
109 style: {
110 marginBottom: 16
111 }
112 }]
113})));
114const defaultVariantMapping = {
115 h1: 'h1',
116 h2: 'h2',
117 h3: 'h3',
118 h4: 'h4',
119 h5: 'h5',
120 h6: 'h6',
121 subtitle1: 'h6',
122 subtitle2: 'h6',
123 body1: 'p',
124 body2: 'p',
125 inherit: 'p'
126};
127const Typography = /*#__PURE__*/React.forwardRef(function Typography(inProps, ref) {
128 const {
129 color,
130 ...themeProps
131 } = useDefaultProps({
132 props: inProps,
133 name: 'MuiTypography'
134 });
135 const isSxColor = !v6Colors[color];
136 // TODO: Remove `extendSxProp` in v7
137 const props = extendSxProp({
138 ...themeProps,
139 ...(isSxColor && {
140 color
141 })
142 });
143 const {
144 align = 'inherit',
145 className,
146 component,
147 gutterBottom = false,
148 noWrap = false,
149 paragraph = false,
150 variant = 'body1',
151 variantMapping = defaultVariantMapping,
152 ...other
153 } = props;
154 const ownerState = {
155 ...props,
156 align,
157 color,
158 className,
159 component,
160 gutterBottom,
161 noWrap,
162 paragraph,
163 variant,
164 variantMapping
165 };
166 const Component = component || (paragraph ? 'p' : variantMapping[variant] || defaultVariantMapping[variant]) || 'span';
167 const classes = useUtilityClasses(ownerState);
168 return /*#__PURE__*/_jsx(TypographyRoot, {
169 as: Component,
170 ref: ref,
171 className: clsx(classes.root, className),
172 ...other,
173 ownerState: ownerState,
174 style: {
175 ...(align !== 'inherit' && {
176 '--Typography-textAlign': align
177 }),
178 ...other.style
179 }
180 });
181});
182process.env.NODE_ENV !== "production" ? Typography.propTypes /* remove-proptypes */ = {
183 // ┌────────────────────────────── Warning ──────────────────────────────┐
184 // │ These PropTypes are generated from the TypeScript type definitions. │
185 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
186 // └─────────────────────────────────────────────────────────────────────┘
187 /**
188 * Set the text-align on the component.
189 * @default 'inherit'
190 */
191 align: PropTypes.oneOf(['center', 'inherit', 'justify', 'left', 'right']),
192 /**
193 * The content of the component.
194 */
195 children: PropTypes.node,
196 /**
197 * Override or extend the styles applied to the component.
198 */
199 classes: PropTypes.object,
200 /**
201 * @ignore
202 */
203 className: PropTypes.string,
204 /**
205 * The color of the component.
206 * It supports both default and custom theme colors, which can be added as shown in the
207 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
208 */
209 color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['primary', 'secondary', 'success', 'error', 'info', 'warning', 'textPrimary', 'textSecondary', 'textDisabled']), PropTypes.string]),
210 /**
211 * The component used for the root node.
212 * Either a string to use a HTML element or a component.
213 */
214 component: PropTypes.elementType,
215 /**
216 * If `true`, the text will have a bottom margin.
217 * @default false
218 */
219 gutterBottom: PropTypes.bool,
220 /**
221 * If `true`, the text will not wrap, but instead will truncate with a text overflow ellipsis.
222 *
223 * Note that text overflow can only happen with block or inline-block level elements
224 * (the element needs to have a width in order to overflow).
225 * @default false
226 */
227 noWrap: PropTypes.bool,
228 /**
229 * If `true`, the element will be a paragraph element.
230 * @default false
231 * @deprecated Use the `component` prop instead. This prop will be removed in v7. See [Migrating from deprecated APIs](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) for more details.
232 */
233 paragraph: PropTypes.bool,
234 /**
235 * @ignore
236 */
237 style: PropTypes.object,
238 /**
239 * The system prop that allows defining system overrides as well as additional CSS styles.
240 */
241 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
242 /**
243 * Applies the theme typography styles.
244 * @default 'body1'
245 */
246 variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['body1', 'body2', 'button', 'caption', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'inherit', 'overline', 'subtitle1', 'subtitle2']), PropTypes.string]),
247 /**
248 * The component maps the variant prop to a range of different HTML element types.
249 * For instance, subtitle1 to `<h6>`.
250 * If you wish to change that mapping, you can provide your own.
251 * Alternatively, you can use the `component` prop.
252 * @default {
253 * h1: 'h1',
254 * h2: 'h2',
255 * h3: 'h3',
256 * h4: 'h4',
257 * h5: 'h5',
258 * h6: 'h6',
259 * subtitle1: 'h6',
260 * subtitle2: 'h6',
261 * body1: 'p',
262 * body2: 'p',
263 * inherit: 'p',
264 * }
265 */
266 variantMapping: PropTypes /* @typescript-to-proptypes-ignore */.object
267} : void 0;
268export default Typography;
\No newline at end of file