Skip to content

Commit d97f138

Browse files
authored
[code-infra] Convert a few docs modules to ts (#45548)
1 parent 03c0b90 commit d97f138

12 files changed

+221
-78
lines changed

docs/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
"@types/gtag.js": "^0.0.20",
126126
"@types/json2mq": "^0.2.2",
127127
"@types/node": "^20.17.24",
128+
"@types/nprogress": "^0.2.3",
128129
"@types/prop-types": "^15.7.14",
129130
"@types/react": "^19.0.10",
130131
"@types/react-dom": "^19.0.4",

docs/src/MuiPage.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import * as React from 'react';
2+
import standardNavIcons from './modules/components/AppNavIcons';
3+
4+
export type MuiPageIcon = keyof typeof standardNavIcons | React.ComponentType;
25

36
export interface MuiPage {
47
pathname: string;
58
query?: object;
69
children?: MuiPage[];
710
disableDrawer?: boolean;
8-
icon?: string | React.ComponentType;
11+
icon?: MuiPageIcon;
912
/**
1013
* Indicates if the pages are regarding some legacy API.
1114
*/

docs/src/featureToggle.js renamed to docs/src/featureToggle.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// need to use commonjs export so that @mui/internal-markdown can use
2-
module.exports = {
1+
export default {
32
enable_website_banner: true,
43
enable_docsnav_banner: true,
54
enable_job_banner: false,

docs/src/modules/components/AppFrame.js renamed to docs/src/modules/components/AppFrame.tsx

+12-5
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ function nProgressDone() {
3737
export function NextNProgressBar() {
3838
const router = useRouter();
3939
React.useEffect(() => {
40-
const handleRouteChangeStart = (url, { shallow }) => {
40+
const handleRouteChangeStart = (url: string, { shallow }: { shallow: boolean }) => {
4141
if (!shallow) {
4242
nProgressStart();
4343
}
4444
};
4545

46-
const handleRouteChangeDone = (url, { shallow }) => {
46+
const handleRouteChangeDone = (url: string, { shallow }: { shallow: boolean }) => {
4747
if (!shallow) {
4848
nProgressDone();
4949
}
@@ -96,7 +96,7 @@ const RootDiv = styled('div')(({ theme }) => {
9696

9797
const StyledAppBar = styled(AppBar, {
9898
shouldForwardProp: (prop) => prop !== 'disablePermanent',
99-
})(({ theme }) => {
99+
})<{ disablePermanent: boolean }>(({ theme }) => {
100100
return {
101101
padding: theme.spacing(1.5),
102102
transition: theme.transitions.create('width'),
@@ -131,7 +131,7 @@ const StyledAppBar = styled(AppBar, {
131131

132132
const NavIconButton = styled(IconButton, {
133133
shouldForwardProp: (prop) => prop !== 'disablePermanent',
134-
})(({ theme }) => ({
134+
})<{ disablePermanent: boolean }>(({ theme }) => ({
135135
variants: [
136136
{
137137
props: {
@@ -164,7 +164,14 @@ const StyledAppNavDrawer = styled(AppNavDrawer)(({ theme }) => ({
164164

165165
export const HEIGHT = 57;
166166

167-
export default function AppFrame(props) {
167+
export interface AppFrameProps {
168+
BannerComponent?: React.ElementType;
169+
children: React.ReactNode;
170+
className?: string;
171+
disableDrawer?: boolean;
172+
}
173+
174+
export default function AppFrame(props: AppFrameProps) {
168175
const { children, disableDrawer = false, className, BannerComponent = AppFrameBanner } = props;
169176
const t = useTranslate();
170177

docs/src/modules/components/AppNavDrawer.js renamed to docs/src/modules/components/AppNavDrawer.tsx

+65-26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import PropTypes from 'prop-types';
33
import Button from '@mui/material/Button';
44
import Divider from '@mui/material/Divider';
5-
import { styled, alpha, ThemeProvider } from '@mui/material/styles';
5+
import { styled, alpha, ThemeProvider, Theme } from '@mui/material/styles';
66
import List from '@mui/material/List';
77
import Drawer from '@mui/material/Drawer';
88
import Typography from '@mui/material/Typography';
@@ -21,17 +21,18 @@ import DoneRounded from '@mui/icons-material/DoneRounded';
2121
import LogoWithCopyMenu from 'docs/src/components/action/LogoWithCopyMenu';
2222
import AppNavDrawerItem from 'docs/src/modules/components/AppNavDrawerItem';
2323
import { pageToTitleI18n } from 'docs/src/modules/utils/helpers';
24-
import PageContext from 'docs/src/modules/components/PageContext';
24+
import PageContext, { ProductVersion } from 'docs/src/modules/components/PageContext';
2525
import { useTranslate } from '@mui/docs/i18n';
2626
import MuiProductSelector from 'docs/src/modules/components/MuiProductSelector';
27+
import { MuiPage } from 'docs/src/MuiPage';
2728

2829
// TODO: Collapse should expose an API to customize the duration based on the height.
29-
function transitionTheme(theme) {
30+
function transitionTheme(theme: Theme) {
3031
return {
3132
...theme,
3233
transitions: {
3334
...theme.transitions,
34-
getAutoHeightDuration: (height) => {
35+
getAutoHeightDuration: (height: number) => {
3536
if (!height) {
3637
return 0;
3738
}
@@ -45,9 +46,9 @@ function transitionTheme(theme) {
4546
};
4647
}
4748

48-
const savedScrollTop = {};
49+
const savedScrollTop: Record<string, number> = {};
4950

50-
const customButtonStyles = (theme) => ({
51+
const customButtonStyles = (theme: Theme) => ({
5152
pl: 1,
5253
pr: '6px',
5354
height: 26,
@@ -56,22 +57,26 @@ const customButtonStyles = (theme) => ({
5657
letterSpacing: '0.01rem',
5758
});
5859

59-
function ProductDrawerButton(props) {
60+
interface ProductDrawerButtonProps {
61+
productName: string;
62+
}
63+
64+
function ProductDrawerButton(props: ProductDrawerButtonProps) {
6065
const [open, setOpen] = React.useState(false);
61-
const anchorRef = React.useRef(null);
66+
const anchorRef = React.useRef<HTMLButtonElement>(null);
6267

6368
const handleToggle = () => {
6469
setOpen((prevOpen) => !prevOpen);
6570
};
6671

67-
const handleClose = (event) => {
68-
if (anchorRef.current && anchorRef.current.contains(event.target)) {
72+
const handleClose = (event: MouseEvent | TouchEvent) => {
73+
if (anchorRef.current && anchorRef.current.contains(event.target as Node)) {
6974
return;
7075
}
7176
setOpen(false);
7277
};
7378

74-
function handleListKeyDown(event) {
79+
function handleListKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
7580
if (event.key === 'Tab') {
7681
event.preventDefault();
7782
setOpen(false);
@@ -138,7 +143,13 @@ ProductDrawerButton.propTypes = {
138143
productName: PropTypes.string,
139144
};
140145

141-
function ProductIdentifier(props) {
146+
interface ProductIdentifierProps {
147+
name: string;
148+
metadata: string;
149+
versionSelector: React.ReactNode;
150+
}
151+
152+
function ProductIdentifier(props: ProductIdentifierProps) {
142153
const { name, metadata, versionSelector } = props;
143154
return (
144155
<Box sx={{ flexGrow: 1 }}>
@@ -171,13 +182,19 @@ ProductIdentifier.propTypes = {
171182
// To match scrollMarginBottom
172183
const browserUrlPreviewMarge = 120;
173184

174-
function PersistScroll(props) {
185+
interface PersistScrollProps {
186+
children: React.ReactNode;
187+
enabled: boolean;
188+
slot: string;
189+
}
190+
191+
function PersistScroll(props: PersistScrollProps) {
175192
const { slot, children, enabled } = props;
176-
const rootRef = React.useRef();
193+
const rootRef = React.useRef<HTMLDivElement>(null);
177194

178195
useEnhancedEffect(() => {
179196
const scrollContainer = rootRef.current ? rootRef.current.parentElement : null;
180-
const activeDrawerLink = scrollContainer.querySelector('.app-drawer-active');
197+
const activeDrawerLink = scrollContainer?.querySelector('.app-drawer-active');
181198

182199
if (!enabled || !scrollContainer || !activeDrawerLink || !activeDrawerLink.scrollIntoView) {
183200
return undefined;
@@ -235,24 +252,38 @@ const AppNavPaperComponent = styled('div')(() => {
235252
width: 'var(--MuiDocs-navDrawer-width)',
236253
boxShadow: 'none',
237254
border: '0 !important', // TODO add a Paper slot
238-
overflowY: 'unset !important', // TODO add a Paper slot
255+
overflowY: 'unset !important' as 'unset', // TODO add a Paper slot
239256
boxSizing: 'border-box', // TODO have CssBaseline in the Next.js layout
240257
};
241258
});
242259

243-
function renderNavItems(options) {
260+
interface RenderNavItemsOptions {
261+
onClose: () => void;
262+
pages: MuiPage[];
263+
activePageParents: MuiPage[];
264+
depth: number;
265+
t: (key: string) => string;
266+
}
267+
268+
function renderNavItems(options: RenderNavItemsOptions) {
244269
const { pages, ...params } = options;
245270

246271
return (
247-
<List>{pages.reduce((items, page) => reduceChildRoutes({ items, page, ...params }), [])}</List>
272+
<List>
273+
{pages.reduce(
274+
(items, page) => reduceChildRoutes({ items, page, ...params }),
275+
[] as React.ReactNode[],
276+
)}
277+
</List>
248278
);
249279
}
250280

251-
/**
252-
* @param {object} context
253-
* @param {import('docs/src/pages').MuiPage} context.page
254-
*/
255-
function reduceChildRoutes(context) {
281+
interface ReduceChildRoutesContext extends Omit<RenderNavItemsOptions, 'pages'> {
282+
items: React.ReactNode[];
283+
page: MuiPage;
284+
}
285+
286+
function reduceChildRoutes(context: ReduceChildRoutesContext): React.ReactNode[] {
256287
const { onClose, activePageParents, items, depth, t } = context;
257288
const { page } = context;
258289
if (page.inSideNav === false) {
@@ -339,18 +370,26 @@ function reduceChildRoutes(context) {
339370
// So: <SwipeableDrawer disableBackdropTransition={false} />
340371
const iOS = typeof navigator !== 'undefined' && /iPad|iPhone|iPod/.test(navigator.userAgent);
341372

342-
export default function AppNavDrawer(props) {
373+
export interface AppNavDrawerProps {
374+
className?: string;
375+
disablePermanent: boolean;
376+
mobileOpen: boolean;
377+
onClose: () => void;
378+
onOpen: () => void;
379+
}
380+
381+
export default function AppNavDrawer(props: AppNavDrawerProps) {
343382
const { className, disablePermanent, mobileOpen, onClose, onOpen } = props;
344383
const { activePageParents, pages, productIdentifier } = React.useContext(PageContext);
345-
const [anchorEl, setAnchorEl] = React.useState(null);
384+
const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(null);
346385
const t = useTranslate();
347386
const mobile = useMediaQuery((theme) => theme.breakpoints.down('lg'));
348387
const swipeableDrawer = disablePermanent || mobile;
349388

350389
const drawer = React.useMemo(() => {
351390
const navItems = renderNavItems({ onClose, pages, activePageParents, depth: 0, t });
352391

353-
const renderVersionSelector = (versions) => {
392+
const renderVersionSelector = (versions: ProductVersion[]) => {
354393
if (!versions?.length) {
355394
return null;
356395
}

0 commit comments

Comments
 (0)