Skip to content

Commit

Permalink
Composite: fix legacy implementation passing store prop (#65821)
Browse files Browse the repository at this point in the history
* Composite: fix legacy implementation passing store prop

* Simplify code

* Improve comment

* Apply same fix to the top level Composite too

* CHANGELOG

---

Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
  • Loading branch information
3 people committed Oct 3, 2024
1 parent 6e8539b commit d90de23
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 41 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- `ToolsPanel`: atomic one-step state update when (un)registering panels ([#65564](https://github.com/WordPress/gutenberg/pull/65564)).
- `Navigator`: fix `isInitial` logic ([#65527](https://github.com/WordPress/gutenberg/pull/65527)).
- `ToggleGroupControl`: Fix arrow key navigation in RTL ([#65735](https://github.com/WordPress/gutenberg/pull/65735)).
- `Composite`: fix legacy support for the store prop ([#65821](https://github.com/WordPress/gutenberg/pull/65821)).

## 28.8.0 (2024-09-19)

Expand Down
12 changes: 7 additions & 5 deletions packages/components/src/composite/group-label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export const CompositeGroupLabel = forwardRef<
WordPressComponentProps< CompositeGroupLabelProps, 'div', false >
>( function CompositeGroupLabel( props, ref ) {
const context = useCompositeContext();

// @ts-expect-error The store prop is undocumented and only used by the
// legacy compat layer. The `store` prop is documented, but its type is
// obfuscated to discourage its use outside of the component's internals.
const store = ( props.store ?? context.store ) as Ariakit.CompositeStore;

return (
<Ariakit.CompositeGroupLabel
store={ context.store as Ariakit.CompositeStore }
{ ...props }
ref={ ref }
/>
<Ariakit.CompositeGroupLabel store={ store } { ...props } ref={ ref } />
);
} );
14 changes: 7 additions & 7 deletions packages/components/src/composite/group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export const CompositeGroup = forwardRef<
WordPressComponentProps< CompositeGroupProps, 'div', false >
>( function CompositeGroup( props, ref ) {
const context = useCompositeContext();
return (
<Ariakit.CompositeGroup
store={ context.store as Ariakit.CompositeStore }
{ ...props }
ref={ ref }
/>
);

// @ts-expect-error The store prop is undocumented and only used by the
// legacy compat layer. The `store` prop is documented, but its type is
// obfuscated to discourage its use outside of the component's internals.
const store = ( props.store ?? context.store ) as Ariakit.CompositeStore;

return <Ariakit.CompositeGroup store={ store } { ...props } ref={ ref } />;
} );
14 changes: 7 additions & 7 deletions packages/components/src/composite/hover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export const CompositeHover = forwardRef<
WordPressComponentProps< CompositeHoverProps, 'div', false >
>( function CompositeHover( props, ref ) {
const context = useCompositeContext();
return (
<Ariakit.CompositeHover
store={ context.store as Ariakit.CompositeStore }
{ ...props }
ref={ ref }
/>
);

// @ts-expect-error The store prop is undocumented and only used by the
// legacy compat layer. The `store` prop is documented, but its type is
// obfuscated to discourage its use outside of the component's internals.
const store = ( props.store ?? context.store ) as Ariakit.CompositeStore;

return <Ariakit.CompositeGroup store={ store } { ...props } ref={ ref } />;
} );
7 changes: 6 additions & 1 deletion packages/components/src/composite/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export const Composite = Object.assign(
},
ref
) {
const store = Ariakit.useCompositeStore( {
// @ts-expect-error The store prop is undocumented and only used by the
// legacy compat layer.
const storeProp = props.store as Ariakit.CompositeStore;
const internalStore = Ariakit.useCompositeStore( {
activeId,
defaultActiveId,
setActiveId,
Expand All @@ -85,6 +88,8 @@ export const Composite = Object.assign(
rtl,
} );

const store = storeProp ?? internalStore;

const contextValue = useMemo(
() => ( {
store,
Expand Down
14 changes: 7 additions & 7 deletions packages/components/src/composite/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export const CompositeItem = forwardRef<
WordPressComponentProps< CompositeItemProps, 'button', false >
>( function CompositeItem( props, ref ) {
const context = useCompositeContext();
return (
<Ariakit.CompositeItem
store={ context.store as Ariakit.CompositeStore }
{ ...props }
ref={ ref }
/>
);

// @ts-expect-error The store prop is undocumented and only used by the
// legacy compat layer. The `store` prop is documented, but its type is
// obfuscated to discourage its use outside of the component's internals.
const store = ( props.store ?? context.store ) as Ariakit.CompositeStore;

return <Ariakit.CompositeItem store={ store } { ...props } ref={ ref } />;
} );
14 changes: 7 additions & 7 deletions packages/components/src/composite/row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export const CompositeRow = forwardRef<
WordPressComponentProps< CompositeRowProps, 'div', false >
>( function CompositeRow( props, ref ) {
const context = useCompositeContext();
return (
<Ariakit.CompositeRow
store={ context.store as Ariakit.CompositeStore }
{ ...props }
ref={ ref }
/>
);

// @ts-expect-error The store prop is undocumented and only used by the
// legacy compat layer. The `store` prop is documented, but its type is
// obfuscated to discourage its use outside of the component's internals.
const store = ( props.store ?? context.store ) as Ariakit.CompositeStore;

return <Ariakit.CompositeRow store={ store } { ...props } ref={ ref } />;
} );
14 changes: 7 additions & 7 deletions packages/components/src/composite/typeahead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export const CompositeTypeahead = forwardRef<
WordPressComponentProps< CompositeTypeaheadProps, 'div', false >
>( function CompositeTypeahead( props, ref ) {
const context = useCompositeContext();
return (
<Ariakit.CompositeTypeahead
store={ context.store as Ariakit.CompositeStore }
{ ...props }
ref={ ref }
/>
);

// @ts-expect-error The store prop is undocumented and only used by the
// legacy compat layer. The `store` prop is documented, but its type is
// obfuscated to discourage its use outside of the component's internals.
const store = ( props.store ?? context.store ) as Ariakit.CompositeStore;

return <Ariakit.CompositeRow store={ store } { ...props } ref={ ref } />;
} );

0 comments on commit d90de23

Please sign in to comment.