Skip to content

Commit

Permalink
react-positioning and popup menus (#17339)
Browse files Browse the repository at this point in the history
* Port usePopper

* remove unnecessary dep

* export types

* cleanup types

* cleaning

* Add tests

* removed enum

* fix lint

* Change files

* remove rtl option

* fix syncpack

* update API

* update ADPI

* Update packages/react-positioning/package.json

Co-authored-by: Oleksandr Fediashov <alexander.mcgarret@gmail.com>

* rename popperRef to containerRef

* move usecallbackref

* update API

* removed escaped

* remove see tag

* Add comment about potential bug in concurrent

* fix syncpack

* Update packages/react-positioning/package.json

Co-authored-by: Elizabeth Craig <ecraig12345@gmail.com>

* Update packages/react-positioning/package.json

Co-authored-by: Elizabeth Craig <ecraig12345@gmail.com>

* Fix export

* fix mouseleave trigger

Co-authored-by: Oleksandr Fediashov <alexander.mcgarret@gmail.com>
Co-authored-by: Elizabeth Craig <ecraig12345@gmail.com>
  • Loading branch information
3 people authored Mar 22, 2021
1 parent 869c2e5 commit fc0d872
Show file tree
Hide file tree
Showing 37 changed files with 1,462 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Improve popup menu examples",
"packageName": "@fluentui/react-examples",
"email": "lingfan.gao@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Use positioning for Menu",
"packageName": "@fluentui/react-menu",
"email": "lingfan.gao@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Initial package",
"packageName": "@fluentui/react-positioning",
"email": "lingfan.gao@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Add useCallbackRef and useFirstMount hooks",
"packageName": "@fluentui/react-utilities",
"email": "lingfan.gao@microsoft.com",
"dependentChangeType": "patch"
}
66 changes: 32 additions & 34 deletions packages/react-examples/src/react-menu/Menu/Menu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,37 @@ export const MenuTriggerInteractions = () => {
};

export const MenuSelectionExample = () => (
<>
<Menu>
<MenuTrigger>
<button>Toggle menu</button>
</MenuTrigger>
<Menu>
<MenuTrigger>
<button>Toggle menu</button>
</MenuTrigger>

<MenuList>
<MenuGroup>
<MenuGroupHeader>Checkbox group</MenuGroupHeader>
<MenuItemCheckbox icon={<CutIcon />} name="edit" value="cut" checkmark={<AcceptIcon />}>
Cut
</MenuItemCheckbox>
<MenuItemCheckbox icon={<PasteIcon />} name="edit" value="paste" checkmark={<AcceptIcon />}>
Paste
</MenuItemCheckbox>
<MenuItemCheckbox icon={<EditIcon />} name="edit" value="edit" checkmark={<AcceptIcon />}>
Edit
</MenuItemCheckbox>
</MenuGroup>
<MenuDivider />
<MenuGroup>
<MenuGroupHeader>Radio group</MenuGroupHeader>
<MenuItemRadio icon={<CutIcon />} name="font" value="segoe" checkmark={<AcceptIcon />}>
Segoe
</MenuItemRadio>
<MenuItemRadio icon={<PasteIcon />} name="font" value="calibri" checkmark={<AcceptIcon />}>
Caliri
</MenuItemRadio>
<MenuItemRadio icon={<EditIcon />} name="font" value="arial" checkmark={<AcceptIcon />}>
Arial
</MenuItemRadio>
</MenuGroup>
</MenuList>
</Menu>
</>
<MenuList>
<MenuGroup>
<MenuGroupHeader>Checkbox group</MenuGroupHeader>
<MenuItemCheckbox icon={<CutIcon />} name="edit" value="cut" checkmark={<AcceptIcon />}>
Cut
</MenuItemCheckbox>
<MenuItemCheckbox icon={<PasteIcon />} name="edit" value="paste" checkmark={<AcceptIcon />}>
Paste
</MenuItemCheckbox>
<MenuItemCheckbox icon={<EditIcon />} name="edit" value="edit" checkmark={<AcceptIcon />}>
Edit
</MenuItemCheckbox>
</MenuGroup>
<MenuDivider />
<MenuGroup>
<MenuGroupHeader>Radio group</MenuGroupHeader>
<MenuItemRadio icon={<CutIcon />} name="font" value="segoe" checkmark={<AcceptIcon />}>
Segoe
</MenuItemRadio>
<MenuItemRadio icon={<PasteIcon />} name="font" value="calibri" checkmark={<AcceptIcon />}>
Caliri
</MenuItemRadio>
<MenuItemRadio icon={<EditIcon />} name="font" value="arial" checkmark={<AcceptIcon />}>
Arial
</MenuItemRadio>
</MenuGroup>
</MenuList>
</Menu>
);
4 changes: 4 additions & 0 deletions packages/react-menu/etc/react-menu.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { ComponentProps } from '@fluentui/react-utilities';
import { ObjectShorthandProps } from '@fluentui/react-utilities';
import { PositioningProps } from '@fluentui/react-positioning';
import * as React from 'react';
import { ShorthandProps } from '@fluentui/react-utilities';

Expand Down Expand Up @@ -146,12 +147,15 @@ export interface MenuListState extends MenuListProps {

// @public
export interface MenuProps extends MenuListProps {
align?: PositioningProps['align'];
children: React.ReactNode;
defaultOpen?: boolean;
menuPopup?: ShorthandProps<React.HTMLAttributes<HTMLElement>>;
onContext?: boolean;
// (undocumented)
onHover?: boolean;
open?: boolean;
position?: PositioningProps['position'];
}

// @public (undocumented)
Expand Down
1 change: 1 addition & 0 deletions packages/react-menu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@fluentui/react-focus-management": "^9.0.0-alpha.8",
"@fluentui/react-context-selector": "^0.53.4",
"@fluentui/react-make-styles": "^9.0.0-alpha.12",
"@fluentui/react-positioning": "^9.0.0-alpha.0",
"@fluentui/react-provider": "^9.0.0-alpha.12",
"@fluentui/react-utilities": "^9.0.0-alpha.9",
"tslib": "^1.10.0"
Expand Down
11 changes: 11 additions & 0 deletions packages/react-menu/src/components/Menu/Menu.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { ObjectShorthandProps, ShorthandProps } from '@fluentui/react-utilities';
import { MenuListProps } from '../MenuList/index';
import { PositioningProps } from '@fluentui/react-positioning';

/**
* Extends and drills down Menulist props to simplify API
Expand Down Expand Up @@ -28,6 +29,16 @@ export interface MenuProps extends MenuListProps {
menuPopup?: ShorthandProps<React.HTMLAttributes<HTMLElement>>;

/**
* Where the menu is positioned with respect to the trigger
*/
position?: PositioningProps['position'];

/**
* How the menu is aligned wtih respect to the trigger
*/
align?: PositioningProps['align'];

/*
* Opens the menu on hover
*/
onHover?: boolean;
Expand Down
16 changes: 14 additions & 2 deletions packages/react-menu/src/components/Menu/useMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { usePopper } from '@fluentui/react-positioning';
import {
makeMergeProps,
resolveShorthandProps,
Expand Down Expand Up @@ -36,6 +37,8 @@ export const useMenu = (props: MenuProps, ref: React.Ref<HTMLElement>, defaultPr
{
ref: useMergedRefs(ref, React.useRef(null)),
menuPopup: { as: 'div' },
position: 'below',
align: 'start',
triggerId,
},
defaultProps,
Expand All @@ -51,8 +54,7 @@ export const useMenu = (props: MenuProps, ref: React.Ref<HTMLElement>, defaultPr
console.warn('Menu can only take one MenuTrigger and one MenuList as children');
}

// TODO use Popper for the trigger ref
const triggerRef = React.useRef<HTMLElement>() as React.MutableRefObject<HTMLElement>;
const { targetRef: triggerRef, containerRef } = usePopper({ align: state.align, position: state.position });
state.triggerRef = triggerRef;
children.forEach(child => {
if (child.type === MenuTrigger) {
Expand All @@ -62,6 +64,16 @@ export const useMenu = (props: MenuProps, ref: React.Ref<HTMLElement>, defaultPr
}
});

state.menuPopup.children = (Component, p) => {
return (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
<Component ref={containerRef} {...p}>
{state.menuList}
</Component>
);
};

const [open, setOpen] = useControllableValue(state.open, state.defaultOpen);
// TODO fix useControllableValue typing
state.open = open !== undefined ? open : state.open;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('useTriggerElement', () => {
it.each([
['click', true, fireEvent.click],
['mouseenter', true, fireEvent.mouseEnter],
['mouseleave', true, fireEvent.mouseLeave],
['mouseleave', false, fireEvent.mouseLeave],
])('should on %s event call setOpen with %s when onHover is set', (_, expectedValue, triggerEvent) => {
// Arrange
const spy = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ export const useTriggerElement = (state: UseTriggerElementState): MenuTriggerSta

const onMouseLeave = useEventCallback((e: React.MouseEvent) => {
if (onHover && !onContext) {
setOpen(true);
setOpen(false);
}
child.props?.onMouseLeave?.(e);
});

const onClick = useEventCallback((e: React.MouseEvent) => {
if (!onContext) {
setOpen(true);
setOpen(!open);
}
child.props?.onClick?.(e);
});
Expand Down
4 changes: 4 additions & 0 deletions packages/react-positioning/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["plugin:@fluentui/eslint-plugin/react"],
"root": true
}
34 changes: 34 additions & 0 deletions packages/react-positioning/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
*.api.json
*.config.js
*.log
*.nuspec
*.test.*
*.yml
.editorconfig
.eslintrc*
.eslintcache
.gitattributes
.gitignore
.vscode
coverage
dist/storybook
dist/*.stats.html
dist/*.stats.json
dist/demo
fabric-test*
gulpfile.js
images
index.html
jsconfig.json
node_modules
results
src/**/*
!src/**/examples/*.tsx
!src/**/docs/**/*.md
!src/**/*.types.ts
temp
tsconfig.json
tsd.json
tslint.json
typings
visualtests
15 changes: 15 additions & 0 deletions packages/react-positioning/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@fluentui/react-positioning

Copyright (c) Microsoft Corporation

All rights reserved.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note: Usage of the fonts and icons referenced in Fluent UI React is subject to the terms listed at https://aka.ms/fluentui-assets-license
25 changes: 25 additions & 0 deletions packages/react-positioning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# @fluentui/react-positioning

A react hook wrapper around [Popper.js](https://popper.js.org/) for Fluent UI.

These are not production-ready utilities and **should never be used in product**. This space is useful for testing new utilities whose APIs might change before final release.

## Usage

```tsx
import React from 'react';
import { usePopper } from '@fluentui/react-positioning';

function PopupExample: React.FC = ({ children }) => {
const {targetRef, containerRef} = usePopper();
const [open, setOpen] = React.useState(false);

const onClick = () => setOpen(s => !s);
return (
<>
<button ref={targetRef} onClick={onClick}> Toggle popup </button>
{ open && <div ref={containerRef}>{children}</div> }
</>
)
}
```
3 changes: 3 additions & 0 deletions packages/react-positioning/config/api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@fluentui/scripts/api-extractor/api-extractor.common.json"
}
69 changes: 69 additions & 0 deletions packages/react-positioning/etc/react-positioning.api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
## API Report File for "@fluentui/react-positioning"

> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts

import * as PopperJs from '@popperjs/core';
import * as React from 'react';

// @public (undocumented)
export type Alignment = 'top' | 'bottom' | 'start' | 'end' | 'center';

// @public (undocumented)
export type Boundary = PopperJs.Boundary | 'scrollParent' | 'window';

// @public (undocumented)
export type Offset = OffsetFunction | [number | null | undefined, number | null | undefined];

// @public (undocumented)
export type OffsetFunction = (param: OffsetFunctionParam) => [number | null | undefined, number | null | undefined];

// @public (undocumented)
export type OffsetFunctionParam = {
popper: PopperJs.Rect;
reference: PopperJs.Rect;
placement: PopperJs.Placement;
};

// @public (undocumented)
export interface PopperOptions extends PositioningProps {
enabled?: boolean;
// (undocumented)
onStateUpdate?: (state: Partial<PopperJs.State>) => void;
positioningDependencies?: React.DependencyList;
}

// @public (undocumented)
export type PopperRefHandle = {
updatePosition: () => void;
};

// @public (undocumented)
export type Position = 'above' | 'below' | 'before' | 'after';

// @public (undocumented)
export interface PositioningProps {
align?: Alignment;
autoSize?: 'height' | 'width' | boolean;
containerRef?: React.Ref<PopperRefHandle>;
flipBoundary?: Boundary;
offset?: Offset;
overflowBoundary?: Boundary;
position?: Position;
positionFixed?: boolean;
unstable_disableTether?: boolean | 'all';
unstable_pinned?: boolean;
}

// @public
export function usePopper(options?: PopperOptions): {
targetRef: React.MutableRefObject<any>;
containerRef: React.MutableRefObject<any>;
arrowRef: React.MutableRefObject<any>;
};


// (No @packageDocumentation comment for this package)

```
5 changes: 5 additions & 0 deletions packages/react-positioning/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { createConfig } = require('@fluentui/scripts/jest/jest-resources');

const config = createConfig({});

module.exports = config;
3 changes: 3 additions & 0 deletions packages/react-positioning/just.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { preset } from '@fluentui/scripts';

preset();
Loading

0 comments on commit fc0d872

Please sign in to comment.