Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
54 changes: 50 additions & 4 deletions docs/pages/material-ui/api/pigment-container.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,57 @@
}
},
"name": "PigmentContainer",
"imports": [
"import PigmentContainer from '@mui/material/PigmentContainer';",
"import { PigmentContainer } from '@mui/material';"
"imports": ["import PigmentContainer from '@mui/material/PigmentContainer';"],
"classes": [
{
"key": "disableGutters",
"className": "MuiContainer-disableGutters",
"description": "Styles applied to the root element if `disableGutters={true}`.",
"isGlobal": false
},
{
"key": "fixed",
"className": "MuiContainer-fixed",
"description": "Styles applied to the root element if `fixed={true}`.",
"isGlobal": false
},
{
"key": "maxWidthLg",
"className": "MuiContainer-maxWidthLg",
"description": "Styles applied to the root element if `maxWidth=\"lg\"`.",
"isGlobal": false
},
{
"key": "maxWidthMd",
"className": "MuiContainer-maxWidthMd",
"description": "Styles applied to the root element if `maxWidth=\"md\"`.",
"isGlobal": false
},
{
"key": "maxWidthSm",
"className": "MuiContainer-maxWidthSm",
"description": "Styles applied to the root element if `maxWidth=\"sm\"`.",
"isGlobal": false
},
{
"key": "maxWidthXl",
"className": "MuiContainer-maxWidthXl",
"description": "Styles applied to the root element if `maxWidth=\"xl\"`.",
"isGlobal": false
},
{
"key": "maxWidthXs",
"className": "MuiContainer-maxWidthXs",
"description": "Styles applied to the root element if `maxWidth=\"xs\"`.",
"isGlobal": false
},
{
"key": "root",
"className": "MuiContainer-root",
"description": "Styles applied to the root element.",
"isGlobal": false
}
],
"classes": [],
"muiName": "MuiPigmentContainer",
"filename": "/packages/mui-material/src/PigmentContainer/PigmentContainer.tsx",
"inheritance": null,
Expand Down
5 changes: 1 addition & 4 deletions docs/pages/material-ui/api/pigment-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@
}
},
"name": "PigmentGrid",
"imports": [
"import PigmentGrid from '@mui/material/PigmentGrid';",
"import { PigmentGrid } from '@mui/material';"
],
"imports": ["import PigmentGrid from '@mui/material/PigmentGrid';"],
"classes": [],
"muiName": "MuiPigmentGrid",
"filename": "/packages/mui-material/src/PigmentGrid/PigmentGrid.tsx",
Expand Down
5 changes: 1 addition & 4 deletions docs/pages/material-ui/api/pigment-stack.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
}
},
"name": "PigmentStack",
"imports": [
"import PigmentStack from '@mui/material/PigmentStack';",
"import { PigmentStack } from '@mui/material';"
],
"imports": ["import PigmentStack from '@mui/material/PigmentStack';"],
"classes": [],
"muiName": "MuiPigmentStack",
"filename": "/packages/mui-material/src/PigmentStack/PigmentStack.tsx",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,42 @@
"description": "The system prop that allows defining system overrides as well as additional CSS styles."
}
},
"classDescriptions": {}
"classDescriptions": {
"disableGutters": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>disableGutters={true}</code>"
},
"fixed": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>fixed={true}</code>"
},
"maxWidthLg": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>maxWidth=\"lg\"</code>"
},
"maxWidthMd": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>maxWidth=\"md\"</code>"
},
"maxWidthSm": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>maxWidth=\"sm\"</code>"
},
"maxWidthXl": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>maxWidth=\"xl\"</code>"
},
"maxWidthXs": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>maxWidth=\"xs\"</code>"
},
"root": { "description": "Styles applied to the root element." }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const generateClassName = (componentName: string, slot: string, globalStatePrefi
// SwipeableDrawer uses Drawer classes without modifying them
return generateUtilityClass('MuiDrawer', slot, globalStatePrefix);
}
if (componentName === 'MuiPigmentContainer') {
return generateUtilityClass('MuiContainer', slot, globalStatePrefix);
}

return generateUtilityClass(componentName, slot, globalStatePrefix);
};
Expand All @@ -22,12 +25,7 @@ export const projectSettings: ProjectSettings = {
{
name: 'material',
rootPath: path.join(process.cwd(), 'packages/mui-material'),
entryPointPath: [
'src/index.d.ts',
'src/PigmentStack/PigmentStack.tsx',
'src/PigmentContainer/PigmentContainer.tsx',
'src/PigmentGrid/PigmentGrid.tsx',
],
entryPointPath: 'src/index.d.ts',
},
{
name: 'lab',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import type {
import createDescribeableProp from '../utils/createDescribeableProp';
import generatePropDescription from '../utils/generatePropDescription';
import type { TypeScriptProject } from '../utils/createTypeScriptProject';
import getComponentImports from '../utils/getComponentImports';
import getComponentTypeScriptProject from '../utils/getComponentTypeScriptProject';
import parseSlotsAndClasses from '../utils/parseSlotsAndClasses';
import { getExternalProps } from '../utils/getPropsFromComponentNode';
import generateApiTranslations from '../utils/generateApiTranslation';
Expand Down Expand Up @@ -616,42 +618,6 @@ const attachPropsTable = (
return propErrors;
};

/**
* Helper to get the import options
* @param name The name of the component
* @param filename The filename where its defined (to infer the package)
* @returns an array of import command
*/
const defaultGetComponentImports = (name: string, filename: string) => {
const githubPath = toGitHubPath(filename);
const rootImportPath = githubPath.replace(
/\/packages\/mui(?:-(.+?))?\/src\/.*/,
(match, pkg) => `@mui/${pkg}`,
);

const subdirectoryImportPath = githubPath.replace(
/\/packages\/mui(?:-(.+?))?\/src\/([^\\/]+)\/.*/,
(match, pkg, directory) => `@mui/${pkg}/${directory}`,
);

let namedImportName = name;
const defaultImportName = name;

if (githubPath.includes('Unstable_')) {
namedImportName = `Unstable_${name} as ${name}`;
}

const useNamedImports = rootImportPath === '@mui/base';

const subpathImport = useNamedImports
? `import { ${namedImportName} } from '${subdirectoryImportPath}';`
: `import ${defaultImportName} from '${subdirectoryImportPath}';`;

const rootImport = `import { ${namedImportName} } from '${rootImportPath}';`;

return [subpathImport, rootImport];
};

const attachTable = (
reactApi: ComponentReactApi,
params: ParsedProperty[],
Expand Down Expand Up @@ -806,7 +772,6 @@ export default async function generateComponentApi(
reactApi.props = {};
}

const { getComponentImports = defaultGetComponentImports } = projectSettings;
const componentJsdoc = parseDoctrine(reactApi.description);

// We override `reactApi.description` with `componentJsdoc.description` because
Expand All @@ -827,7 +792,9 @@ export default async function generateComponentApi(

reactApi.filename = filename;
reactApi.name = componentInfo.name;
reactApi.imports = getComponentImports(componentInfo.name, filename);
reactApi.imports = projectSettings.getComponentImports
? projectSettings.getComponentImports(componentInfo.name, filename)
: getComponentImports(componentInfo.name, filename, project, componentInfo.importName);
reactApi.muiName = componentInfo.muiName;
reactApi.apiPathname = componentInfo.apiPathname;
reactApi.EOL = EOL;
Expand Down Expand Up @@ -859,10 +826,12 @@ export default async function generateComponentApi(
}
}

// Subpath-only components are missing from the root exports.
const componentProject = getComponentTypeScriptProject(project, filename, componentInfo.name);
const slotsAndClassesErrors: string[] = [];
if (!projectSettings.skipSlotsAndClasses) {
const { slots, classes, errors } = parseSlotsAndClasses({
typescriptProject: project,
typescriptProject: componentProject,
projectSettings,
componentName: reactApi.name,
muiName: reactApi.muiName,
Expand Down Expand Up @@ -891,7 +860,7 @@ export default async function generateComponentApi(
project,
);

const externalProps = getExternalProps(project, reactApi.name);
const externalProps = getExternalProps(componentProject, reactApi.name);
const errors = [
...attachPropsTable(reactApi, projectSettings.propsSettings, externalProps),
...slotsAndClassesErrors,
Expand Down
6 changes: 5 additions & 1 deletion packages-internal/api-docs-builder/src/types/utils.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ export type ComponentInfo = {
*/
filename: string;
/**
* Component name as imported in the docs, in the global MUI namespace.
* Component name in the global MUI namespace.
*/
name: string;
/**
* Local name in import examples. Defaults to the component name.
*/
importName?: string;
/**
* Component name with `Mui` prefix, in the global HTML page namespace.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export interface CreateTypeScriptProjectOptions {
* Use an array to target more than one entrypoint.
*
* @example 'src/index.d.ts'
* @example ['src/index.d.ts', 'src/PigmentStack/PigmentStack.tsx']
* `PigmentStack` cannot be included in the `index.d.ts` file because it is using Pigment CSS specific API.
*/
entryPointPath?: string | string[];
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import findComponents from './findComponents';

describe('findComponents', () => {
let directory: string;

beforeEach(() => {
directory = fs.mkdtempSync(path.join(os.tmpdir(), 'mui-find-components-'));
});

afterEach(() => {
fs.rmSync(directory, { recursive: true });
});

it('finds component names with digits, including unstable components', () => {
const filenames = [
'Button.js',
'Grid2.tsx',
'Widget2.tsx',
'Widget2Item.tsx',
'Widget2ItemIndicator.tsx',
'Unstable_Widget2Item.tsx',
'Version42Button.tsx',
];
filenames.forEach((filename) => {
fs.writeFileSync(path.join(directory, filename), '');
});

expect(findComponents(directory)).to.have.deep.members(
filenames.map((filename) => ({
filename: path.join(directory, filename),
indexFilename: null,
})),
);
});

it('ignores non-component files', () => {
[
'index.ts',
'widget2Classes.ts',
'useWidget2.tsx',
'Widget2.d.ts',
'Widget2.test.tsx',
'Widget2.spec.tsx',
'Widget2.tsx.preview',
'2Widget.tsx',
].forEach((filename) => {
fs.writeFileSync(path.join(directory, filename), '');
});

expect(findComponents(directory)).to.deep.equal([]);
});

it('finds nested components with the index file from their directory', () => {
const componentDirectory = path.join(directory, 'Unstable_Widget2Item');
fs.mkdirSync(componentDirectory);
const filename = path.join(componentDirectory, 'Widget2Item.tsx');
const indexFilename = path.join(componentDirectory, 'index.ts');
fs.writeFileSync(filename, '');
fs.writeFileSync(indexFilename, '');

expect(findComponents(directory)).to.deep.equal([{ filename, indexFilename }]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import path from 'path';
import findIndexFile from './findIndexFile';

const componentRegex = /^(Unstable_)?([A-Z][a-z]*)+2?\.(js|tsx)/;
const componentRegex = /^(Unstable_)?([A-Z][a-z0-9]*)+\.(js|tsx)$/;

/**
* Returns the component source in a flat array.
Expand Down
Loading