Skip to content

Commit

Permalink
refactor: continue refactor hooks: useBodyScrollLock, useVirtualEleme…
Browse files Browse the repository at this point in the history
…ntRef, usePortalContainer (#1038)
  • Loading branch information
NikitaCG authored Oct 4, 2023
1 parent 44d7160 commit ca3bd9a
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React from 'react';

import {CSSTransition} from 'react-transition-group';

import {useBodyScrollLock} from '../../hooks';
import {Portal} from '../Portal';
import type {DOMProps, QAProps} from '../types';
import {FocusTrap} from '../utils/FocusTrap';
import {block} from '../utils/cn';
import {getCSSTransitionClassNames} from '../utils/transition';
import {useBodyScrollLock} from '../utils/useBodyScrollLock';
import {useLayer} from '../utils/useLayer';
import type {LayerCloseReason, LayerExtendableProps} from '../utils/useLayer';
import {useRestoreFocus} from '../utils/useRestoreFocus';
Expand Down
2 changes: 1 addition & 1 deletion src/components/Popup/__stories__/Popup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import React from 'react';

import type {Meta, StoryFn} from '@storybook/react';

import {useVirtualElementRef} from '../../../hooks';
import {Button} from '../../Button';
import {TextInput} from '../../controls';
import {useVirtualElementRef} from '../../utils/useVirtualElementRef';
import {Popup} from '../Popup';
import type {PopupPlacement, PopupProps} from '../Popup';

Expand Down
2 changes: 1 addition & 1 deletion src/components/Portal/Portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import ReactDOM from 'react-dom';

import {usePortalContainer} from '../utils/usePortalContainer';
import {usePortalContainer} from '../../hooks';

export interface PortalProps {
container?: HTMLElement;
Expand Down
4 changes: 0 additions & 4 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ export * from './utils/class-transform';
export * from './utils/event-broker';
export {getComponentName} from './utils/getComponentName';
export * from './utils/withEventBrokerDomHandlers';
export * from './utils/useBodyScrollLock';
export * from './utils/useEventBroker';
export * from './utils/useLayer';
export * from './utils/useVirtualElementRef';
export {Lang, configure} from './utils/configure';
export * from './utils/PortalProvider';
export * from './utils/usePortalContainer';
export * from './utils/useSelect';
export * from './utils/useListNavigation';
export * from './utils/useForkRef';
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './useBodyScrollLock';
export * from './useOutsideClick';
export * from './usePortalContainer';
export * from './useVirtualElementRef';
17 changes: 17 additions & 0 deletions src/hooks/useBodyScrollLock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--GITHUB_BLOCK-->

# useBodyScrollLock

<!--/GITHUB_BLOCK-->

```tsx
import {useBodyScrollLock} from '@gravity-ui/uikit';
```

The `useBodyScrollLock` hook helps to blocks scrolling on the body element.

## Properties

| Name | Description | Type | Default |
| :------ | :---------- | :-------: | :-----: |
| enabled | Enable flag | `boolean` | |
2 changes: 2 additions & 0 deletions src/hooks/useBodyScrollLock/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {useBodyScrollLock} from './useBodyScrollLock';
export type {BodyScrollLockProps, UseBodyScrollLockProps} from './useBodyScrollLock';
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from 'react';

export interface BodyScrollLockProps {
export interface UseBodyScrollLockProps {
enabled: boolean;
}

export type BodyScrollLockProps = UseBodyScrollLockProps;

let locks = 0;
let storedBodyStyle: string | undefined;

export function useBodyScrollLock({enabled}: BodyScrollLockProps) {
export function useBodyScrollLock({enabled}: UseBodyScrollLockProps) {
React.useLayoutEffect(() => {
if (enabled) {
locks++;
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions src/hooks/usePortalContainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--GITHUB_BLOCK-->

# usePortalContainer

<!--/GITHUB_BLOCK-->

```tsx
import {usePortalContainer} from '@gravity-ui/uikit';
```

The `usePortalContainer` hook returns portal container element in the current scope

## Result

Container element for portal. `HTMLElement | null`
3 changes: 3 additions & 0 deletions src/hooks/usePortalContainer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export {usePortalContainer} from './usePortalContainer';
export {PortalProvider} from './PortalProvider';
export type {UsePortalContainerResult} from './usePortalContainer';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import React from 'react';

import {PortalContext} from './PortalProvider';

export function usePortalContainer(): HTMLElement | null {
export type UsePortalContainerResult = HTMLElement | null;

export function usePortalContainer(): UsePortalContainerResult {
const context = React.useContext(PortalContext);

let defaultContainer = null;
Expand Down
22 changes: 22 additions & 0 deletions src/hooks/useVirtualElementRef/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--GITHUB_BLOCK-->

# useVirtualElementRef

<!--/GITHUB_BLOCK-->

```tsx
import {useVirtualElementRef} from '@gravity-ui/uikit';
```

The `useVirtualElementRef` hook helps to create virtual element for popup

## Properties

| Name | Description | Type | Default |
| :------------- | :----------------------------------------------- | :----------------------------------------: | :-----: |
| rect | Position of virtual element relative to viewport | `{top, bottom, left, right}: {[x]:number}` | |
| contextElement | DOM-context of virtual element | `Element` | |

## Result

Virtual element ref link. `React.RefObject`
6 changes: 6 additions & 0 deletions src/hooks/useVirtualElementRef/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {useVirtualElementRef} from './useVirtualElementRef';
export type {
VirtualElementRect,
UseVirtualElementRefProps,
UseVirtualElementRefResult,
} from './useVirtualElementRef';
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@ export type VirtualElementRect = {

export interface UseVirtualElementRefProps {
/**
* Position of virtual element in relation to viewport
* Position of virtual element relative to viewport
*/
rect?: VirtualElementRect | null;

/**
* DOM-context of virual element
* DOM-context of virtual element
*/
contextElement?: Element;
}

export type UseVirtualElementRefResult = React.MutableRefObject<VirtualElement>;

const initialPosition = {top: 0, right: 0, bottom: 0, left: 0};

// React hook for creating virtual element for popup
export function useVirtualElementRef(
props: UseVirtualElementRefProps = {},
): React.RefObject<VirtualElement> {
): UseVirtualElementRefResult {
const {rect, contextElement} = props;
const rectRef = React.useRef(initialPosition);

Expand Down

0 comments on commit ca3bd9a

Please sign in to comment.