question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[RFC] Code organization in @mui/base

See original GitHub issue

🖼 Background

As we are in an early stage of @mui/base development, it’s worth establishing code organization rules to follow when creating new components. We could simply adopt the rules from @mui/material and other existing packages, however, a new package gives us an opportunity to take another look at it and see if we can introduce any improvements.

💡 Proposal

  1. Separate hooks from unstyled components and place them in their own top-level directories:

    mui-base/
      src/
        ButtonUnstyled/
        useButton/
        ...
    

    This would have the following advantages:

    • better separation of hooks and components
    • each top-level building block would be imported from a directory matching its name
  2. Adopt the following file structure in components/hooks directories:

    Component/
      Component.tsx
      Component.spec.tsx
      Component.test.tsx
      Component.types.ts
      componentClasses.ts
      index.ts
      ... (other if necessary)
    
    useHook/
      useHook.ts
      useHook.test.tsx
      useHook.types.ts
      ... (other if necessary)
    

    The primary change is renaming the *Props.ts files into *.types.ts. During the development of existing unstyled components, it became clear it’s necessary to export much more than just props. ButtonUnstyled, MenuUnstyled, and SelectUnstyled already follow this pattern.

  3. Use named exports instead of default ones. This has been discussed in #21862 already. For component authors, it would eliminate the need to switch between default and named exports when re-exporting from index files. It would also make it easier for developers to switch from top-level (import { ButtonUnstyled } from '@mui/base') to deep imports (import { ButtonUnstyled } from '@mui/base/ButtonUnstyled').

  4. Do not use the Unstable_ prefix for components hooks, as the whole library is unstable right now (this is the case with Unstable_TrapFocus.


cc @mui/core

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
siriwatknpcommented, Mar 14, 2022
  1. Separate hooks from unstyled components and place them in their own top-level directories:

Love it 👍. This will make the imports like this right?

import useButton from '@mui/base/useButton';
import useAutocomplete from '@mui/base/useAutocomplete';

Component.types.ts

Yes, looks a lot better than ComponentProps.ts. Joy components are exporting SlotKey which is not props, it makes more sense to be Component.types.ts.

Use named exports instead of default ones

I don’t think it is worth going that far. In this case, it is not like “A” is better than “B” so I would keep the existing way of import and focus on other improvements instead.

Do not use the Unstable_ prefix for components hooks, as the whole library is unstable right now (this is the case with Unstable_TrapFocus.

Sounds good.

1reaction
michaldudakcommented, Aug 15, 2022

Material UI and Joy UI will likely be built on top of the hooks directly, mostly to improve the DX with the React dev tools and marginally improve the runtime with fewer layers of React components

Also, hooks make it easier to create components that are customizable further (i.e., expose the componentsProps prop)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing MUI Core v5.0
You can find the initial RFC plan for v5 in issue #20012. A new brand. Material UI is now MUI! Head to the...
Read more >
@mui/base examples - CodeSandbox
Learn how to use @mui/base by viewing and forking example apps that make use of @mui/base on CodeSandbox. ; mui-base-with-tailwind-css ; base-cra-typescript.
Read more >
@mui/base | Yarn - Package Manager
This package hosts unstyled React components and hooks that can be used for creating custom design systems. Installation. Install the package in your...
Read more >
MUI (@MUI_hq) / Twitter
Introducing MUI Base: the headless alternative to Material UI! ... Simple no-frills code editor with syntax highlighting - GitHub ...
Read more >
@material-ui/core - Awesome JS
[blog] Fix font size of code blocks on iOS @oliviertassinari ... We started converting the remaining JS components in MUI Base to TypeScript....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found