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

[core] Use local useInteractions #1247

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion packages/react/src/dialog/root/useDialogRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
useClick,
useDismiss,
useFloatingRootContext,
useInteractions,
type OpenChangeReason as FloatingUIOpenChangeReason,
} from '@floating-ui/react';
import { useControlled } from '../../utils/useControlled';
Expand All @@ -20,6 +19,7 @@ import {
type OpenChangeReason,
translateOpenChangeReason,
} from '../../utils/translateOpenChangeReason';
import { useInteractions } from '../../utils/useInteractions';

export function useDialogRoot(parameters: useDialogRoot.Parameters): useDialogRoot.ReturnValue {
const {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/menu/root/useMenuRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
useDismiss,
useFloatingRootContext,
useHover,
useInteractions,
useListNavigation,
useRole,
useTypeahead,
Expand All @@ -21,6 +20,7 @@ import { TYPEAHEAD_RESET_MS } from '../../utils/constants';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';
import type { TextDirection } from '../../direction-provider/DirectionContext';
import { useScrollLock } from '../../utils/useScrollLock';
import { useInteractions } from '../../utils/useInteractions';

const EMPTY_ARRAY: never[] = [];

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/popover/root/usePopoverRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
useDismiss,
useFloatingRootContext,
useHover,
useInteractions,
useRole,
type FloatingRootContext,
} from '@floating-ui/react';
Expand All @@ -24,6 +23,7 @@ import {
type OpenChangeReason,
} from '../../utils/translateOpenChangeReason';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';
import { useInteractions } from '../../utils/useInteractions';

export function usePopoverRoot(params: usePopoverRoot.Parameters): usePopoverRoot.ReturnValue {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
useDismiss,
useFloatingRootContext,
useHover,
useInteractions,
type FloatingRootContext,
} from '@floating-ui/react';
import { useControlled } from '../../utils/useControlled';
Expand All @@ -20,6 +19,7 @@ import {
type OpenChangeReason,
} from '../../utils/translateOpenChangeReason';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';
import { useInteractions } from '../../utils/useInteractions';

export function usePreviewCardRoot(
params: usePreviewCardRoot.Parameters,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/select/root/useSelectRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
useClick,
useDismiss,
useFloatingRootContext,
useInteractions,
useListNavigation,
useRole,
useTypeahead,
Expand All @@ -19,6 +18,7 @@ import { warn } from '../../utils/warn';
import type { SelectRootContext } from './SelectRootContext';
import type { SelectIndexContext } from './SelectIndexContext';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';
import { useInteractions } from '../../utils/useInteractions';

export function useSelectRoot<T>(params: useSelectRoot.Parameters<T>): useSelectRoot.ReturnValue {
const {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/tooltip/root/useTooltipRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
useFloatingRootContext,
useFocus,
useHover,
useInteractions,
type FloatingRootContext,
} from '@floating-ui/react';
import { useControlled } from '../../utils/useControlled';
Expand All @@ -22,6 +21,7 @@ import {
type OpenChangeReason,
} from '../../utils/translateOpenChangeReason';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation';
import { useInteractions } from '../../utils/useInteractions';

export function useTooltipRoot(params: useTooltipRoot.Parameters): useTooltipRoot.ReturnValue {
const {
Expand Down
40 changes: 40 additions & 0 deletions packages/react/src/utils/useInteractions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as React from 'react';
import type { ElementProps } from '@floating-ui/react';
import type { GenericHTMLProps } from './types';
import { mergeReactProps } from './mergeReactProps';

/**
* Merges an array of interaction hooks' props into prop getters, allowing
* event handler functions to be composed together without overwriting one
* another.
* @see https://floating-ui.com/docs/useInteractions
*/
export function useInteractions(propsList: Array<ElementProps>) {
const referenceDeps = propsList.map((key) => key.reference);
const floatingDeps = propsList.map((key) => key.floating);
const itemDeps = propsList.map((key) => key.item);

const getReferenceProps = React.useCallback(
(userProps?: GenericHTMLProps) => mergeReactProps(userProps, propsList, 'reference'),
// eslint-disable-next-line react-hooks/exhaustive-deps
referenceDeps,
);

const getFloatingProps = React.useCallback(
(userProps?: GenericHTMLProps) => mergeReactProps(userProps, propsList, 'floating'),
// eslint-disable-next-line react-hooks/exhaustive-deps
floatingDeps,
);

const getItemProps = React.useCallback(
(userProps?: GenericHTMLProps & { selected?: boolean; active?: boolean }) =>
mergeReactProps(userProps, propsList, 'item'),
// eslint-disable-next-line react-hooks/exhaustive-deps
itemDeps,
);

return React.useMemo(
() => ({ getReferenceProps, getFloatingProps, getItemProps }),
[getReferenceProps, getFloatingProps, getItemProps],
);
}
Loading