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

Components: Add Menu #29645

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
58 changes: 29 additions & 29 deletions package-lock.json

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

10 changes: 5 additions & 5 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
"@wordpress/primitives": "file:../primitives",
"@wordpress/rich-text": "file:../rich-text",
"@wordpress/warning": "file:../warning",
"@wp-g2/components": "^0.0.160",
"@wp-g2/context": "^0.0.160",
"@wp-g2/styles": "^0.0.160",
"@wp-g2/utils": "^0.0.160",
"@wp-g2/components": "^0.0.162",
"@wp-g2/context": "^0.0.162",
"@wp-g2/styles": "^0.0.162",
"@wp-g2/utils": "^0.0.162",
"classnames": "^2.2.5",
"dom-scroll-into-view": "^1.2.1",
"downshift": "^6.0.15",
Expand All @@ -62,7 +62,7 @@
"react-resize-aware": "^3.1.0",
"react-spring": "^8.0.20",
"react-use-gesture": "^9.0.0",
"reakit": "^1.3.5",
"reakit": "1.3.5",
"rememo": "^3.0.0",
"tinycolor2": "^1.4.2",
"uuid": "^8.3.0"
Expand Down
85 changes: 85 additions & 0 deletions packages/components/src/ui/menu/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* External dependencies
*/
import { contextConnect, useContextSystem } from '@wp-g2/context';
import { cx } from '@wp-g2/styles';
// eslint-disable-next-line no-restricted-imports
import { Menu as ReakitMenu } from 'reakit';

/**
* Internal dependencies
*/
import { usePopoverResizeUpdater } from '../popover/utils';
import { View } from '../view';
import { MenuContext } from './context';
import * as styles from './styles';

/**
* `Menu` is an actionable component that displays a list of actions, links, or informative content.
*
* @example
* ```jsx
* <Menu>
* <MenuItem>...</MenuItem>
* <MenuItem>...</MenuItem>
* <MenuItem>...</MenuItem>
* </Menu>
* ```
*
* @see https://reakit.io/docs/menu/#menu
*
* @param {import('@wp-g2/create-styles').ViewOwnProps<import('./types').Props, 'div'>} props
* @param {import('react').Ref<any>} forwardedRef
*/
function Menu( props, forwardedRef ) {
const { as, children, className, menu, ...otherProps } = useContextSystem(
props,
'Menu'
);

const resizeListener = usePopoverResizeUpdater( {
onResize: menu?.unstable_update,
Copy link
Member

Choose a reason for hiding this comment

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

If we're going to use unstable features from packages, we should make sure we use the exact version for that package on package.json so as to avoid issues like #23332.

} );

const contextProps = {
menu,
};

const classes = cx( styles.Menu, className );
const menuProps = menu || {};
const Component = as || menu ? ReakitMenu : View;

return (
<MenuContext.Provider value={ contextProps }>
{ /* @ts-ignore TS doesn't like the union of PolymorphicComponent and Reakit Component but they are indeed renderable */ }
<Component
hideOnClickOutside={ false }
{ ...menuProps }
{ ...otherProps }
className={ classes }
ref={ forwardedRef }
>
{ resizeListener }
{ children }
</Component>
</MenuContext.Provider>
);
}

/**
* `Menu` is an actionable component that displays a list of actions, links, or informative content.
*
* @example
* ```jsx
* <Menu>
* <MenuItem>...</MenuItem>
* <MenuItem>...</MenuItem>
* <MenuItem>...</MenuItem>
* </Menu>
* ```
*
* @see https://reakit.io/docs/menu/#menu
*/
const ConnectedMenu = contextConnect( Menu, 'Menu' );

export default ConnectedMenu;
8 changes: 8 additions & 0 deletions packages/components/src/ui/menu/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* WordPress dependencies
*/
import { createContext, useContext } from '@wordpress/element';

/** @type {import('react').Context<{ menu?: import('reakit').MenuStateReturn }>} */
export const MenuContext = createContext( {} );
export const useMenuContext = () => useContext( MenuContext );
54 changes: 54 additions & 0 deletions packages/components/src/ui/menu/header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* External dependencies
*/
import { contextConnect, useContextSystem } from '@wp-g2/context';
import { cx } from '@wp-g2/styles';
import { pick } from 'lodash';

/**
* Internal dependencies
*/
import * as baseButtonStyles from '../base-button/styles';
import { Heading } from '../heading';
import * as styles from './styles';

const sizeStyles = pick( baseButtonStyles, [ 'large', 'small', 'xSmall' ] );

/**
* @typedef OwnProps
* @property {keyof sizeStyles} size Size of the menu header.
*/

/**
* @typedef {import('../heading').HeadingProps & OwnProps} Props
*/

/**
*
* @param {import('@wp-g2/create-styles').ViewOwnProps<Props, 'div'>} props
* @param {import('react').Ref<any>} forwardedRef
*/
function MenuHeader( props, forwardedRef ) {
const {
children,
className,
size,
level = 5,
...otherProps
} = useContextSystem( props, 'MenuHeader' );

const classes = cx( styles.MenuHeader, sizeStyles[ size ], className );

return (
<Heading
level={ level }
{ ...otherProps }
className={ classes }
ref={ forwardedRef }
>
{ children }
</Heading>
);
}

export default contextConnect( MenuHeader, 'MenuHeader' );
5 changes: 5 additions & 0 deletions packages/components/src/ui/menu/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { default as Menu } from './component';
export { default as MenuHeader } from './header';
export { default as MenuItem } from './item';

export * from './context';
Loading