Skip to content

Commit

Permalink
Implement react-focus-management and Menu usage (#17137)
Browse files Browse the repository at this point in the history
* react-focus-management

* Add first char navigation

* add default value for setFocusByFirstCharacter

* remove spec for now

* narrow props

* comment

* Integrate to fluent provider

* update tsconfig.base.json

* fix deps

* fix deps

* memoize context value

* update naming

* change naming

* fix tsdoc

* add README

* remove spec for now

* revert menu spec deletion

* fix tests, update version

* update version

* update version

* rename and cleanup

* Change files

* update beachball for new package

* remove test setup

* Update packages/react-focus-management/README.md

Co-authored-by: Elizabeth Craig <[email protected]>

* remove import

* update deps

* fixes

* Update packages/react-focus-management/README.md

Co-authored-by: Oleksandr Fediashov <[email protected]>

* shared character search hook as util

* remove document check

* update API

* update API

Co-authored-by: Elizabeth Craig <[email protected]>
Co-authored-by: Oleksandr Fediashov <[email protected]>
  • Loading branch information
3 people authored Feb 24, 2021
1 parent f8aad9b commit eac7564
Show file tree
Hide file tree
Showing 36 changed files with 549 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Bootstrap react-focus-management",
"packageName": "@fluentui/react-focus-management",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Support keyboard navigation",
"packageName": "@fluentui/react-menu",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Use react-focus-management",
"packageName": "@fluentui/react-provider",
"email": "[email protected]",
"dependentChangeType": "patch"
}
4 changes: 4 additions & 0 deletions packages/react-focus-management/.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-focus-management/.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-focus-management/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@fluentui/react-focus-management

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
42 changes: 42 additions & 0 deletions packages/react-focus-management/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# @fluentui/react-focus-management

**Focus management components for [Fluent UI React](https://developer.microsoft.com/en-us/fluentui)**

Experimental library for focus management that leverages [ability-helpers](https://github.com/microsoft/ability-helpers).

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

The provider needs to be wrapped around your application:

```tsx
<FocusManagementProvider>{children}</FocusManagementProvider>
```

The API currently only supports declarative data-\* attributes that are returned using the exported react hooks:

```tsx
const Item: React.FC = ({ children }) => <div tabIndex={0}>Item</div>;

const ArrowNavigationExample: React.FC = ({ children }) => {
const attrs = useArrowNavigationGroup({ circular: true });

return (
<div {...attrs}>
<Item />
<Item />
<Item />
<Item />
<Item />
<Item />
</div>
);
};

const App: React.FC = () => {
return (
<FocusManagementProvider>
<ArrowNavigationExample />
</FocusManagementProvider>
);
};
```
3 changes: 3 additions & 0 deletions packages/react-focus-management/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"
}
58 changes: 58 additions & 0 deletions packages/react-focus-management/etc/react-focus-management.api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## API Report File for "@fluentui/react-focus-management"

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

import { getAbilityHelpersAttribute } from 'ability-helpers';
import * as React from 'react';
import { Types } from 'ability-helpers';

// @public (undocumented)
export interface FocusManagementProvideProps extends React.HTMLAttributes<HTMLElement> {
customRoot?: boolean;
// (undocumented)
dir?: 'ltr' | 'rtl';
document?: Document;
}

// @public
export const FocusManagementProvider: React.FunctionComponent<FocusManagementProvideProps>;

// @public (undocumented)
export interface FocusManagementProviderState extends FocusManagementProvideProps {
// Warning: (ae-forgotten-export) The symbol "FocusManagementContextValue" needs to be exported by the entry point index.d.ts
//
// (undocumented)
contextValue: FocusManagementContextValue;
// (undocumented)
dir: FocusManagementProvideProps['dir'];
}

export { getAbilityHelpersAttribute }

// @public (undocumented)
export const renderFocusManagementProvider: (state: FocusManagementProviderState) => JSX.Element;

// @public
export const useArrowNavigationGroup: (options?: UseArrowNavigationGroupOptions) => Types.AbilityHelpersDOMAttribute;

// @public (undocumented)
export interface UseArrowNavigationGroupOptions {
circular?: boolean;
}

// @public
export const useFocusFinders: () => {
findAllFocusable: (root: HTMLElement, matcher: (el: HTMLElement) => boolean) => HTMLElement[];
findFirstFocusable: (root: HTMLElement) => HTMLElement | null | undefined;
findLastFocusable: (root: HTMLElement) => HTMLElement | null | undefined;
};

// @public (undocumented)
export const useFocusManagementProvider: (props: FocusManagementProvideProps, ref: React.Ref<HTMLElement>) => FocusManagementProviderState;


// (No @packageDocumentation comment for this package)

```
5 changes: 5 additions & 0 deletions packages/react-focus-management/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-focus-management/just.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { preset } from '@fluentui/scripts';

preset();
61 changes: 61 additions & 0 deletions packages/react-focus-management/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "@fluentui/react-focus-management",
"version": "9.0.0-alpha.0",
"description": "Utilities for focus management and facade for ability-helpers",
"main": "lib-commonjs/index.js",
"module": "lib/index.js",
"typings": "lib/index.d.ts",
"sideEffects": [
"lib/version.js"
],
"repository": {
"type": "git",
"url": "https://github.com/microsoft/fluentui"
},
"license": "MIT",
"scripts": {
"build": "just-scripts build",
"bundle": "just-scripts bundle",
"clean": "just-scripts clean",
"code-style": "just-scripts code-style",
"just": "just-scripts",
"lint": "just-scripts lint",
"start": "just-scripts dev:storybook",
"start-test": "just-scripts jest-watch",
"test": "just-scripts test",
"update-snapshots": "just-scripts jest -u"
},
"devDependencies": {
"@fluentui/eslint-plugin": "^1.0.0-beta.2",
"@fluentui/scripts": "^1.0.0",
"@testing-library/react": "^10.4.9",
"@testing-library/react-hooks": "^5.0.3",
"@types/react": "16.9.42",
"@types/react-dom": "16.9.10",
"@types/react-test-renderer": "^16.0.0",
"react": "16.8.6",
"react-app-polyfill": "~1.0.1",
"react-dom": "16.8.6",
"react-test-renderer": "^16.3.0"
},
"dependencies": {
"@fluentui/react-utilities": "^9.0.0-alpha.1",
"@fluentui/set-version": "^8.0.0-beta.2",
"ability-helpers": "^0.4.3",
"tslib": "^1.10.0"
},
"peerDependencies": {
"@types/react": ">=16.8.0 <18.0.0",
"@types/react-dom": ">=16.8.0 <18.0.0",
"react": ">=16.8.0 <18.0.0",
"react-dom": ">=16.8.0 <18.0.0"
},
"beachball": {
"tag": "alpha",
"disallowedChangeTypes": [
"major",
"minor",
"patch"
]
}
}
83 changes: 83 additions & 0 deletions packages/react-focus-management/src/FocusManagementProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import * as React from 'react';
import { makeMergeProps, useMergedRefs } from '@fluentui/react-utilities';
import { getCurrentAbilityHelpers, createAbilityHelpers, Types as AHTypes } from 'ability-helpers';
import { internal__FocusManagementContext, FocusManagementContextValue } from './focusManagementContext';

export interface FocusManagementProvideProps extends React.HTMLAttributes<HTMLElement> {
dir?: 'ltr' | 'rtl';

/**
* The document, which can be null during server render in SSR
*/
document?: Document;

/**
* The root is automatically set as the `body` element of the ownerDocument.
* This prop needs to be set if a custom root is used
*/
customRoot?: boolean;
}

export interface FocusManagementProviderState extends FocusManagementProvideProps {
dir: FocusManagementProvideProps['dir'];

contextValue: FocusManagementContextValue;
}

const mergeProps = makeMergeProps<FocusManagementProviderState>();

export const useFocusManagementProvider = (
props: FocusManagementProvideProps,
ref: React.Ref<HTMLElement>,
): FocusManagementProviderState => {
const rootRef = useMergedRefs(ref, React.useRef<HTMLElement>(null));
const state = mergeProps(
{
ref: rootRef,
as: 'div',
},
{},
props,
);

state.dir = state.dir || 'ltr';

const ahOptions = { autoRoot: {} };
if (state.customRoot) {
delete ahOptions.autoRoot;
}

// only one instance per window of ability helpers should exist
let ahInstance: AHTypes.AbilityHelpersCore | undefined = undefined;
if (state.document?.defaultView) {
ahInstance =
getCurrentAbilityHelpers(state.document.defaultView) ||
createAbilityHelpers(state.document.defaultView, ahOptions);
}
// memoize context value so that it's stable
state.contextValue = React.useMemo(() => ({ focusable: ahInstance?.focusable, ahInstance }), [ahInstance]);

return state;
};

export const renderFocusManagementProvider = (state: FocusManagementProviderState) => {
return (
<internal__FocusManagementContext.Provider value={state.contextValue}>
{state.children}
</internal__FocusManagementContext.Provider>
);
};

/**
* A React provider that manages and exposes an ability-helpers instance for focus management
*/
export const FocusManagementProvider: React.FunctionComponent<FocusManagementProvideProps> = React.forwardRef<
HTMLDivElement,
FocusManagementProvideProps
>((props: FocusManagementProvideProps, ref: React.Ref<HTMLDivElement>) => {
const state = useFocusManagementProvider(props, ref);

return renderFocusManagementProvider(state);
});

FocusManagementProvider.displayName = 'FocusManagementProvider';
33 changes: 33 additions & 0 deletions packages/react-focus-management/src/focusManagementContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as React from 'react';
import { Types as AHTypes } from 'ability-helpers';

// NOTE: very likely contents will change as usage is specced out properly
// creating for now to unblock initial experimentation and usage

/**
* Context value, all members here can be undefined since the API relies heavily on window/document which are not
* present during server render in SSR
*/
export interface FocusManagementContextValue {
/**
* Ability helpers focusable API
*/
focusable?: AHTypes.FocusableAPI;

/**
* Raw Ability helpers instance
*/
ahInstance?: AHTypes.AbilityHelpersCore;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export const internal__FocusManagementContext = React.createContext<FocusManagementContextValue>(
({} as unknown) as FocusManagementContextValue,
);

/**
* Exposes the entire focus management context
* Should be used in the package but not exported in the public API
*/
export const useFocusManagementContext = (): FocusManagementContextValue =>
React.useContext(internal__FocusManagementContext);
2 changes: 2 additions & 0 deletions packages/react-focus-management/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './useArrowNavigationGroup';
export * from './useFocusFinders';
Loading

0 comments on commit eac7564

Please sign in to comment.