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

Toolbar: Use Ariakit instead of Reakit #51623

Merged
merged 8 commits into from
Jun 20, 2023
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
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
### Internal

- `ClipboardButton`: Convert to TypeScript ([#51334](https://github.com/WordPress/gutenberg/pull/51334)).
- `Toolbar`: Replace `reakit` dependency with `@ariakit/react` ([#51623](https://github.com/WordPress/gutenberg/pull/51623)).

## 25.1.0 (2023-06-07)

Expand Down
1 change: 1 addition & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
],
"types": "build-types",
"dependencies": {
"@ariakit/react": "^0.2.10",
"@babel/runtime": "^7.16.0",
"@emotion/cache": "^11.7.1",
"@emotion/css": "^11.7.1",
Expand Down
3 changes: 0 additions & 3 deletions packages/components/src/toolbar/stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ Default.args = {
</ToolbarGroup>
<ToolbarGroup>
<ToolbarItem>
{ /* There is an issue here with TS not recognizing the
* `RenderProp` being passed.
* @ts-expect-error */ }
{ ( toggleProps ) => (
<DropdownMenu
icon={ alignLeft }
Expand Down
6 changes: 2 additions & 4 deletions packages/components/src/toolbar/toolbar-context/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/**
* External dependencies
*/
import type { ToolbarStateReturn } from 'reakit/Toolbar';
import type { ToolbarStore } from '@ariakit/react/toolbar';

/**
* WordPress dependencies
*/
import { createContext } from '@wordpress/element';

const ToolbarContext = createContext< ToolbarStateReturn | undefined >(
undefined
);
const ToolbarContext = createContext< ToolbarStore | undefined >( undefined );

export default ToolbarContext;
28 changes: 13 additions & 15 deletions packages/components/src/toolbar/toolbar-item/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { ToolbarItem as BaseToolbarItem } from 'reakit/Toolbar';
import { ToolbarItem as BaseToolbarItem } from '@ariakit/react/toolbar';
import type { ForwardedRef } from 'react';

/**
Expand All @@ -14,18 +14,16 @@ import warning from '@wordpress/warning';
* Internal dependencies
*/
import ToolbarContext from '../toolbar-context';
import type { ToolbarItemProps } from './types';

function ToolbarItem(
{
children,
as: Component,
...props
}: React.ComponentPropsWithoutRef< typeof BaseToolbarItem >,
{ children, as: Component, ...props }: ToolbarItemProps,
ref: ForwardedRef< any >
) {
const accessibleToolbarState = useContext( ToolbarContext );
const accessibleToolbarStore = useContext( ToolbarContext );
const isRenderProp = typeof children === 'function';

if ( typeof children !== 'function' && ! Component ) {
if ( ! isRenderProp && ! Component ) {
warning(
'`ToolbarItem` is a generic headless component. You must pass either a `children` prop as a function or an `as` prop as a component. ' +
'See https://developer.wordpress.org/block-editor/components/toolbar-item/'
Expand All @@ -35,24 +33,24 @@ function ToolbarItem(

const allProps = { ...props, ref, 'data-toolbar-item': true };

if ( ! accessibleToolbarState ) {
if ( ! accessibleToolbarStore ) {
if ( Component ) {
return <Component { ...allProps }>{ children }</Component>;
}
if ( typeof children !== 'function' ) {
if ( ! isRenderProp ) {
return null;
}
return children( allProps );
}

const render = isRenderProp ? children : Component && <Component />;

return (
<BaseToolbarItem
{ ...accessibleToolbarState }
{ ...allProps }
as={ Component }
>
{ children }
</BaseToolbarItem>
store={ accessibleToolbarStore }
render={ render }
/>
);
}

Expand Down
27 changes: 27 additions & 0 deletions packages/components/src/toolbar/toolbar-item/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* External dependencies
*/
import type {
ReactElement,
ReactNode,
ElementType,
HTMLAttributes,
RefAttributes,
} from 'react';

export type ToolbarItemProps = Omit< HTMLAttributes< any >, 'children' > & {
/**
* Component type that will be used to render the toolbar item.
*/
as?: ElementType;
diegohaz marked this conversation as resolved.
Show resolved Hide resolved
/**
* A function that receives the props that should be spread onto the element
* that will be rendered as a toolbar item. If the `as` prop is not provided,
* this prop will accept a ReactNode instead.
*/
children?:
| ReactNode
diegohaz marked this conversation as resolved.
Show resolved Hide resolved
| ( (
props: HTMLAttributes< any > & RefAttributes< any >
) => ReactElement | null );
};
14 changes: 5 additions & 9 deletions packages/components/src/toolbar/toolbar/toolbar-container.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { useToolbarState, Toolbar } from 'reakit/Toolbar';
import { useToolbarStore, Toolbar } from '@ariakit/react/toolbar';
import type { ForwardedRef } from 'react';

/**
Expand All @@ -21,22 +21,18 @@ function UnforwardedToolbarContainer(
{ label, ...props }: WordPressComponentProps< ToolbarProps, 'div', false >,
ref: ForwardedRef< any >
) {
// https://reakit.io/docs/basic-concepts/#state-hooks
// Passing baseId for server side rendering (which includes snapshots)
// If an id prop is passed to Toolbar, toolbar items will use it as a base for their ids
const toolbarState = useToolbarState( {
loop: true,
baseId: props.id,
const toolbarStore = useToolbarStore( {
focusLoop: true,
rtl: isRTL(),
} );

return (
// This will provide state for `ToolbarButton`'s
<ToolbarContext.Provider value={ toolbarState }>
<ToolbarContext.Provider value={ toolbarStore }>
<Toolbar
ref={ ref }
aria-label={ label }
{ ...toolbarState }
store={ toolbarStore }
{ ...props }
/>
</ToolbarContext.Provider>
Expand Down
Loading