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

feat: Normalize component structure and exports #2728

Closed
wants to merge 19 commits into from

Conversation

jpandersen87
Copy link
Contributor

@jpandersen87 jpandersen87 commented Jan 22, 2024

Summary

Everything noted below is under the pretense of NOT breaking existing usage patterns. All changes are intended to be additions, with JSDoc deprecation comments used where appropriate.

  • ensure all compositing components have a [Component]Base component that focuses on root element rendering, if applicable
  • create components for all currently inlined elements that require logic (ex: className composition)
    • ensure all new components have basic tests (render, className) in addition to moving tests from parent component test that are within its area of concern
  • ensure component type exports
    • normalize exported Base[Component]Props as type holding custom props
    • normalize exported [Component]Props as React.ComponentPropsWithRef<typeof [Component]>
    • normalize [Component]Ref as React.ComponentRef<typeof [Component]>
      • Parent components that pass on their forwardRef to these components can use this ref type for React.ForwardRefRenderFunction or wherever else appropriate as a reliable alias
    • typeof shortcutting to ensure changes to ref type or prop types in forwardRefFn are always reflected
    • Use React.[Component]PropsWith[out]Ref for props that are intended to be directly forwarded to child component
      • This type automatically handles the ref typing so that forwardRefs are compatible (ref from JSX.IntrinsicElements is type-incompatible)
  • all components are forwardRef, always passing the forwardRef to most appropriate child component
  • compositing components expose child component prop bags that are passed on (which will now include ref)
  • JSDoc deprecation marking of certain props in favor of new ones
    • divProps -> headerProps
    • dedicated prop that was used as children -> children
  • other minor enhancements
    • normalize isX name format for booleans (adding deprecation markers where appropriate)
    • ex: allowing boolean props to be undefined for falseyness
    • exposing props for emitting on certain events to make components less of a black box (ex: emit currently open accordion items when changed via onChange prop on Accordion)
  • export any other types or functions that can be useful

Related Issues or PRs

#2667
#2666
#2587
#2232
#2599
#2000

How To Test

  • Use this branch for your local project's version of react-uswds
  • Ensure all exports are available
  • If possible, verify that existing code using react-uswds is unbroken

Screenshots (optional)

- ensure all components have an exported prop type
- normalize `Base<Component>Props` as type holding custom props
- normalize `<ComponentType>Props` as type union of `Base<Component>Props` (if exists) and wrapper element props type
- export misc types and helper functions
@jpandersen87 jpandersen87 requested a review from a team as a code owner January 22, 2024 23:48
@jpandersen87
Copy link
Contributor Author

Moving to draft as I also include forwardRef support and fix some components I missed.

@jpandersen87
Copy link
Contributor Author

Finished first pass. There are some tough problems to solve with existing forwardRef components with limited scope, container components that would leave internal input child without a ref path, and inconsistent prop patterns of child component props expected in container prop root or in subproperty.

- onChange property on Accordion exposed to emit currently open items
- AccordionItem broken down into individual components
- Relevant tests from Accordion moved to individual component tests
- Stories/tests get test data from test-fixtures
- All components are forwardRef
- AccordionItem content marked as deprecated in favor of children
- New props exposing refs to composed children
- Misc enhancements
- Broken into modular components with tests
- Relevant tests moved to subcomponents
- Ref props to subcomponents from Alert added
- add Base components focused on just their root element
- Accordion/AccordionItem/Alert components handle all child component compositing using individual subcomponents
- Ref props swapped for component props (which includes ref)
- subcomponent breakout
- tests
- use Accordion subcomponents where possible in Banner subcomponents
- use Grid component instead of manual div
@jpandersen87 jpandersen87 changed the title feat: Normalize component prop exports, export misc types/functions feat: Normalize component structure and exports Jan 25, 2024
- asCustom typings moved to separate file
- core tooltip logic moved to new hook
- subcomponent breakouts
- typing of TooltipTrigger incomplete due to forwardRef/asCustom complexity
@jpandersen87
Copy link
Contributor Author

I have an almost-complete implementation of forwardRef+asCustom via TooltipTrigger but getting the typescript typing correct has been a roadblock.

- revert all asCustom components (except tooltip) to originals for now
- misc fixes so that linting, tests, and building pass
- sitealert uses `Alert` instead of manual element
- headingLevel added to `SiteAlert`
- isX booleans
- Alert type made optional
- sb story titles normalized to uppercase
@werdnanoslen
Copy link
Contributor

werdnanoslen commented Mar 21, 2024

Hi @jpandersen87 , what are your plans/advice for others going forward in this workstream?

@jpandersen87
Copy link
Contributor Author

There's some major hurdles in regards to creating a type-safe pattern that can be applied to all components so that they're created via forwardRef and can forward that ref to the nearest relevant wrapper/child.

@jpandersen87
Copy link
Contributor Author

I may retry this later where I at least create forwardRef versions of simpler components so at least there's forward momentum in this goal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants