[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
-
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
-
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.
-
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'
). -
Do not use the
Unstable_
prefix for components hooks, as the whole library is unstable right now (this is the case withUnstable_TrapFocus
.
cc @mui/core
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:5 (5 by maintainers)
Top GitHub Comments
Love it 👍. This will make the imports like this right?
Yes, looks a lot better than
ComponentProps.ts
. Joy components are exportingSlotKey
which is not props, it makes more sense to beComponent.types.ts
.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.
Sounds good.
Also, hooks make it easier to create components that are customizable further (i.e., expose the
componentsProps
prop)