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

Composite: accept store props on top level component #64832

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
- `DropdownMenu` v2: refactor to overloaded naming convention ([#64654](https://github.com/WordPress/gutenberg/pull/64654)).
- `DropdownMenu` v2: add `GroupLabel` subcomponent ([#64854](https://github.com/WordPress/gutenberg/pull/64854)).
- `Composite` V2: fix Storybook docgen ([#64682](https://github.com/WordPress/gutenberg/pull/64682)).
- `Composite` V2: accept store props on top-level component ([#64832](https://github.com/WordPress/gutenberg/pull/64832)).

## 28.6.0 (2024-08-21)

Expand Down
37 changes: 36 additions & 1 deletion packages/components/src/composite/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,44 @@ export const Composite = Object.assign(
HTMLDivElement,
WordPressComponentProps< CompositeProps, 'div', false >
>( function Composite(
{ children, store, disabled = false, ...props },
{
// Composite store props
activeId,
defaultActiveId,
setActiveId,
focusLoop = false,
focusWrap = false,
focusShift = false,
virtualFocus = false,
orientation = 'both',
rtl = false,

// Composite component props
children,
disabled = false,

// To be removed
store: storeProp,

// Rest props
...props
},
ref
) {
const newStore = Ariakit.useCompositeStore( {
activeId,
defaultActiveId,
setActiveId,
focusLoop,
focusWrap,
focusShift,
virtualFocus,
orientation,
rtl,
} );

const store = storeProp || newStore;
Copy link
Member

Choose a reason for hiding this comment

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

Any good reason to prefer || to ?? here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Main reasons is that I copied the same style as the ariakit repo when dealing with store fallbacks:

https://github.com/ariakit/ariakit/blob/main/packages/ariakit-react-core/src/composite/composite.tsx#L145-L146

Also, store shouldn't have falsy values ('', 0) which would cause || to behave differently than ??


const contextValue = useMemo(
() => ( {
store,
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/composite/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ export type CompositeStoreProps = {
rtl?: Ariakit.CompositeStoreProps[ 'rtl' ];
};

export type CompositeProps = {
export type CompositeProps = CompositeStoreProps & {
/**
* Object returned by the `useCompositeStore` hook.
*/
store: Ariakit.CompositeStore;
store?: Ariakit.CompositeStore;
/**
* Allows the component to be rendered as a different HTML element or React
* component. The value can be a React element or a function that takes in the
Expand Down
Loading