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

[docs-infra] Simplify CSS classes extraction in API docs generator #39808

Merged
merged 10 commits into from
Nov 30, 2023

Conversation

michaldudak
Copy link
Member

@michaldudak michaldudak commented Nov 8, 2023

Currently we have mostly duplicated logic for CSS classes extraction in API docs builder and two similar fields in the resulting API description JSON files: styles and classes. This PR unifies the processing of CSS classes and gets rid of the styles field.
The format of the classes field was also changed to make it easier to use in the docs: the classes and globalClasses fields inside the classes object were changed to an object with several properties:

old:

"classes": {
  "classes": ["active", "disabled", "focusVisible"],
  "globalClasses": {
    "active": "Mui-active",
    "disabled": "Mui-disabled",
    "focusVisible": "Mui-focusVisible"
  }
}

new:

"classes": [
  {
    "key": "active",
    "className": "Mui-active",
    "description": "State class applied to the root `button` element if `active={true}`.",
    "isGlobal": true
  },
  {
    "key": "disabled",
    "className": "Mui-disabled",
    "description": "State class applied to the root `button` element if `disabled={true}`.",
    "isGlobal": true
  },
  {
    "key": "focusVisible",
    "className": "Mui-focusVisible",
    "description": "State class applied to the root `button` element if `focusVisible={true}`.",
    "isGlobal": true
  }
]

(while the description field isn't strictly necessary here, it was added to match props definitions)

The docs were adapted to use this new format.

Another change was moving the responsibility of figuring out the actual class name to the API docs builder. Previously, we had logic for this in many different places - in some cases the .Mui- prefix was added in the docs, in other in the docs builder. With this change the docs will display the class name exactly as it was defined in the JSON file, without adding any prefixes. This will allow to configure how classes are generated in the API docs builder. This was my primary motivation for this PR, as I need to customize the prefix of Base UI CSS classes.

This PR does not introduce changes visible to our users (except for a couple of corrected strings).

I recommend reviewing it by commit.

Fixes #39867

@michaldudak michaldudak added the scope: docs-infra Specific to the docs-infra product label Nov 8, 2023
@mui-bot
Copy link

mui-bot commented Nov 8, 2023

Netlify deploy preview

https://deploy-preview-39808--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against da5c2fb

@michaldudak michaldudak force-pushed the simplify-css-classes-extraction branch from 1192007 to ad741a8 Compare November 9, 2023 08:48
@michaldudak michaldudak force-pushed the simplify-css-classes-extraction branch from ad741a8 to 04ed3d5 Compare November 9, 2023 08:51
@michaldudak michaldudak requested a review from a team November 9, 2023 09:07
@michaldudak michaldudak marked this pull request as ready for review November 9, 2023 09:07
@michaldudak michaldudak force-pushed the simplify-css-classes-extraction branch from 04ed3d5 to 93d34a5 Compare November 9, 2023 09:17
Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

Looks much more readable like that.

I reviewed the docs part. Still need some time to review in depth the new JSON generation process, and compare the preview with the prod

const result: { classNames: string[]; descriptions: Record<string, string> } = {
classNames: [],
descriptions: {},
muiName: string;
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to use an enum here? Otherwise it's a pain when we navigate this kind of code and don't know exactly which convention is used

Copy link
Member Author

Choose a reason for hiding this comment

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

Could you give an example? Where exactly would you see an enum?

Copy link
Member

Choose a reason for hiding this comment

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

I misunderstood what this property is about. I thought it was storing the mui project (core, base, joy, ...) but it's the component name

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it's the name of the component used in style overrides object (such as MuiButton).

Comment on lines 143 to 149
...getClassesToC({
t,
componentName: pageContent.name,
componentStyles,
componentClasses,
}),
componentSlots?.length > 0 && createTocEntry('slots'),
hasClasses && createTocEntry('classes'),
Copy link
Member

Choose a reason for hiding this comment

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

With this modification, you will get a ToC with

  • classes (expanded)
  • slots
  • classes

Should replace the hasClasses && createTocEntry('classes'), by ...getClassesToC({ }) to respect the page order

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, thanks!
It won't actually ever call createTocEntry('classes') because I introduced a bug in hasClasses ;)

Comment on lines +13 to +14
const options: ApiDisplayOptions[] = ['collapsed', 'expanded', 'table'];
const DEFAULT_LAYOUT: ApiDisplayOptions = 'expanded';
Copy link
Member

Choose a reason for hiding this comment

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

This typo fix might need extra work.

In getOption and getRandomOption the loclastorage can still return 'expended'.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, indeed. I'll make sure it uses a default value when local storage has something unknown.

@alexfauquette alexfauquette requested review from alexfauquette and a team November 9, 2023 14:00
@mnajdova
Copy link
Member

Nice work. A bit related to this, I've created this issue: #39867 we should not show the rule name for the state classes as you can't override them without bumping the specificity.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Nov 17, 2023
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Nov 20, 2023
@michaldudak
Copy link
Member Author

@mnajdova fixed in a394217

@michaldudak
Copy link
Member Author

@alexfauquette, I'd appreciate if you could take a look at the remaining changes. I need this PR to work on #39467 further.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Nov 22, 2023
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Nov 29, 2023
Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@michaldudak michaldudak merged commit c6d17be into mui:master Nov 30, 2023
19 checks passed
@michaldudak michaldudak deleted the simplify-css-classes-extraction branch November 30, 2023 08:30
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Nov 30, 2023
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Dec 1, 2023
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Dec 1, 2023
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Dec 6, 2023
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Dec 6, 2023
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: docs-infra Specific to the docs-infra product
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[docs-infra] Rule names shouldn't be displayed on the state classes in the API pages
4 participants