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: Add info to create compound component regarding createComponent #3057

Merged
merged 7 commits into from
Dec 4, 2024
32 changes: 32 additions & 0 deletions modules/docs/mdx/CREATING_COMPOUND_COMPONENTS.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,44 @@ document to learn about what a compound component is.
This document will go through building a simplified Disclosure component to help solidify the
concepts. We will cover:

- [Non Coordinated Components](#non-coordinated-components)
- [Models](#models)
- [Container Components](#disclosure-component)
- [Sub-components](#disclosuretarget-component)
- [Model Composition](#model-composition)
- [Behavior hooks](#behavior-hooks)

## Non Coordinated Components

In most cases you'll create compound components that have a model and share information across sub
components. However, in the case where information doesn't need to be shared, you'll create a non
coordinated component. These components often represent some styled element with no associated role
or behavior and don't rely on state and events such as a `Card`, `Flex` or `Button` component. Use
`createComponent` factory function in these scenarios.
mannycarrera4 marked this conversation as resolved.
Show resolved Hide resolved

### `createComponent`

Use `createComponent` when you want to create a rendered element with _no behavior_. This is useful
for elements that you want to use for styling purposes like container elements, or sub components
that are simple rendered elements. This utility function will wrap your component in a
`React.ForwardRef` and allow you to add sub components as well.
mannycarrera4 marked this conversation as resolved.
Show resolved Hide resolved

```tsx
export const Card = createComponent('div')({
displayName: 'Card',
subComponents: {
Heading: CardHeading, // this is also using createComponent
},
Component: ({children, ...elemProps}: CardProps, ref, Element) => {
return (
<Box as={Element} {...elemProps} ref={ref}>
{children}
</Box>
);
},
});
```

## Models

A model is composed of state and events. The shape of the model used by components looks like this:
Expand Down
Loading