Skip to content

Commit

Permalink
[core] Refactor data passing (#1249)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks authored Jan 8, 2025
1 parent 576effb commit bcbceda
Show file tree
Hide file tree
Showing 25 changed files with 319 additions and 1,146 deletions.
9 changes: 8 additions & 1 deletion docs/reference/generated/menu-positioner.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"props": {
"align": {
"type": "'start' | 'center' | 'end'",
"default": "'center'",
"description": "How to align the popup relative to the specified side."
},
"alignOffset": {
Expand All @@ -13,6 +14,7 @@
},
"side": {
"type": "'bottom' | 'inline-end' | 'inline-start' | 'left' | 'right' | 'top'",
"default": "'bottom'",
"description": "Which side of the anchor element to align the popup against.\nMay automatically change to avoid collisions."
},
"sideOffset": {
Expand Down Expand Up @@ -42,13 +44,18 @@
"sticky": {
"type": "boolean",
"default": "false",
"description": "Whether to maintain the menu in the viewport after\nthe anchor element is scrolled out of view."
"description": "Whether to maintain the popup in the viewport after\nthe anchor element was scrolled out of view."
},
"positionMethod": {
"type": "'absolute' | 'fixed'",
"default": "'absolute'",
"description": "Determines which CSS `position` property to use."
},
"trackAnchor": {
"type": "boolean",
"default": "true",
"description": "Whether the popup tracks any layout shift of its positioning anchor."
},
"className": {
"type": "string | (state) => string",
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state."
Expand Down
7 changes: 6 additions & 1 deletion docs/reference/generated/popover-positioner.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@
"sticky": {
"type": "boolean",
"default": "false",
"description": "Whether to maintain the popup in the viewport after\nthe anchor element is scrolled out of view."
"description": "Whether to maintain the popup in the viewport after\nthe anchor element was scrolled out of view."
},
"positionMethod": {
"type": "'absolute' | 'fixed'",
"default": "'absolute'",
"description": "Determines which CSS `position` property to use."
},
"trackAnchor": {
"type": "boolean",
"default": "true",
"description": "Whether the popup tracks any layout shift of its positioning anchor."
},
"className": {
"type": "string | (state) => string",
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state."
Expand Down
7 changes: 6 additions & 1 deletion docs/reference/generated/preview-card-positioner.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@
"sticky": {
"type": "boolean",
"default": "false",
"description": "Whether to maintain the popup in the viewport after\nthe anchor element is scrolled out of view."
"description": "Whether to maintain the popup in the viewport after\nthe anchor element was scrolled out of view."
},
"positionMethod": {
"type": "'absolute' | 'fixed'",
"default": "'absolute'",
"description": "Determines which CSS `position` property to use."
},
"trackAnchor": {
"type": "boolean",
"default": "true",
"description": "Whether the popup tracks any layout shift of its positioning anchor."
},
"className": {
"type": "string | (state) => string",
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state."
Expand Down
8 changes: 4 additions & 4 deletions docs/reference/generated/select-positioner.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"props": {
"align": {
"type": "'start' | 'center' | 'end'",
"default": "'start'",
"default": "'center'",
"description": "How to align the popup relative to the specified side."
},
"alignOffset": {
Expand Down Expand Up @@ -44,17 +44,17 @@
"sticky": {
"type": "boolean",
"default": "false",
"description": "Whether to maintain the select menu in the viewport after\nthe anchor element is scrolled out of view."
"description": "Whether to maintain the popup in the viewport after\nthe anchor element was scrolled out of view."
},
"positionMethod": {
"type": "'absolute' | 'fixed'",
"default": "'absolute'",
"description": "The CSS position method for positioning the Select popup element."
"description": "Determines which CSS `position` property to use."
},
"trackAnchor": {
"type": "boolean",
"default": "true",
"description": "Whether the select popup continuously tracks its anchor after the initial positioning upon mount."
"description": "Whether the popup tracks any layout shift of its positioning anchor."
},
"className": {
"type": "string | (state) => string",
Expand Down
5 changes: 5 additions & 0 deletions docs/reference/generated/tooltip-positioner.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
"default": "'absolute'",
"description": "Determines which CSS `position` property to use."
},
"trackAnchor": {
"type": "boolean",
"default": "true",
"description": "Whether the popup tracks any layout shift of its positioning anchor."
},
"className": {
"type": "string | (state) => string",
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state."
Expand Down
2 changes: 1 addition & 1 deletion docs/src/app/(private)/experiments/anchor-positioning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function AnchorPositioning() {
positionerStyles,
arrowStyles,
arrowRef,
renderedSide,
side: renderedSide,
arrowUncentered,
} = useAnchorPositioning({
side,
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/dialog/root/DialogRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from 'react';
import PropTypes from 'prop-types';
import { DialogRootContext, useOptionalDialogRootContext } from './DialogRootContext';
import { DialogContext } from '../utils/DialogContext';
import { type CommonParameters, useDialogRoot } from './useDialogRoot';
import { type SharedParameters, useDialogRoot } from './useDialogRoot';

/**
* Groups all parts of the dialog.
Expand Down Expand Up @@ -48,7 +48,7 @@ const DialogRoot = function DialogRoot(props: DialogRoot.Props) {
};

namespace DialogRoot {
export interface Props extends CommonParameters {
export interface Props extends SharedParameters {
children?: React.ReactNode;
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/react/src/dialog/root/useDialogRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
translateOpenChangeReason,
} from '../../utils/translateOpenChangeReason';

export function useDialogRoot(parameters: useDialogRoot.Parameters): useDialogRoot.ReturnValue {
export function useDialogRoot(params: useDialogRoot.Parameters): useDialogRoot.ReturnValue {
const {
defaultOpen,
dismissible,
Expand All @@ -30,7 +30,7 @@ export function useDialogRoot(parameters: useDialogRoot.Parameters): useDialogRo
onNestedDialogOpen,
onOpenChange: onOpenChangeParameter,
open: openParam,
} = parameters;
} = params;

const [open, setOpenUnwrapped] = useControlled({
controlled: openParam,
Expand Down Expand Up @@ -163,7 +163,7 @@ export function useDialogRoot(parameters: useDialogRoot.Parameters): useDialogRo
]);
}

export interface CommonParameters {
export interface SharedParameters {
/**
* Whether the dialog is currently open.
*/
Expand Down Expand Up @@ -196,7 +196,7 @@ export interface CommonParameters {
}

export namespace useDialogRoot {
export interface Parameters extends RequiredExcept<CommonParameters, 'open' | 'onOpenChange'> {
export interface Parameters extends RequiredExcept<SharedParameters, 'open' | 'onOpenChange'> {
/**
* Callback to invoke when a nested dialog is opened.
*/
Expand Down
32 changes: 16 additions & 16 deletions packages/react/src/menu/positioner/MenuPositioner.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import {
FloatingNode,
useFloatingNodeId,
useFloatingParentNodeId,
useFloatingTree,
} from '@floating-ui/react';
import { FloatingNode, useFloatingNodeId, useFloatingParentNodeId } from '@floating-ui/react';
import { MenuPositionerContext } from './MenuPositionerContext';
import { useMenuRootContext } from '../root/MenuRootContext';
import type { Align, Side } from '../../utils/useAnchorPositioning';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { useForkRef } from '../../utils/useForkRef';
import { useMenuPositioner } from './useMenuPositioner';
import { HTMLElementType } from '../../utils/proptypes';
import { BaseUIComponentProps } from '../../utils/types';
import { popupStateMapping } from '../../utils/popupStateMapping';
import { CompositeList } from '../../composite/list/CompositeList';
import { InternalBackdrop } from '../../utils/InternalBackdrop';
import { HTMLElementType, refType } from '../../utils/proptypes';
import { useMenuPortalContext } from '../portal/MenuPortalContext';

/**
Expand All @@ -43,6 +38,7 @@ const MenuPositioner = React.forwardRef(function MenuPositioner(
collisionPadding = 5,
arrowPadding = 5,
sticky = false,
trackAnchor = true,
...otherProps
} = props;

Expand All @@ -54,13 +50,10 @@ const MenuPositioner = React.forwardRef(function MenuPositioner(
itemLabels,
mounted,
nested,
setOpen,
modal,
} = useMenuRootContext();
const keepMounted = useMenuPortalContext();

const { events: menuEvents } = useFloatingTree()!;

const nodeId = useFloatingNodeId();
const parentNodeId = useFloatingParentNodeId();

Expand Down Expand Up @@ -89,9 +82,8 @@ const MenuPositioner = React.forwardRef(function MenuPositioner(
sticky,
nodeId,
parentNodeId,
menuEvents,
setOpen,
keepMounted,
trackAnchor,
});

const state: MenuPositioner.State = React.useMemo(
Expand All @@ -112,15 +104,15 @@ const MenuPositioner = React.forwardRef(function MenuPositioner(
arrowRef: positioner.arrowRef,
arrowUncentered: positioner.arrowUncentered,
arrowStyles: positioner.arrowStyles,
floatingContext: positioner.floatingContext,
floatingContext: positioner.context,
}),
[
positioner.side,
positioner.align,
positioner.arrowRef,
positioner.arrowUncentered,
positioner.arrowStyles,
positioner.floatingContext,
positioner.context,
],
);

Expand Down Expand Up @@ -172,6 +164,7 @@ MenuPositioner.propTypes /* remove-proptypes */ = {
// └─────────────────────────────────────────────────────────────────────┘
/**
* How to align the popup relative to the specified side.
* @default 'center'
*/
align: PropTypes.oneOf(['center', 'end', 'start']),
/**
Expand All @@ -185,6 +178,7 @@ MenuPositioner.propTypes /* remove-proptypes */ = {
*/
anchor: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
HTMLElementType,
refType,
PropTypes.object,
PropTypes.func,
]),
Expand Down Expand Up @@ -247,6 +241,7 @@ MenuPositioner.propTypes /* remove-proptypes */ = {
/**
* Which side of the anchor element to align the popup against.
* May automatically change to avoid collisions.
* @default 'bottom'
*/
side: PropTypes.oneOf(['bottom', 'inline-end', 'inline-start', 'left', 'right', 'top']),
/**
Expand All @@ -255,11 +250,16 @@ MenuPositioner.propTypes /* remove-proptypes */ = {
*/
sideOffset: PropTypes.number,
/**
* Whether to maintain the menu in the viewport after
* the anchor element is scrolled out of view.
* Whether to maintain the popup in the viewport after
* the anchor element was scrolled out of view.
* @default false
*/
sticky: PropTypes.bool,
/**
* Whether the popup tracks any layout shift of its positioning anchor.
* @default true
*/
trackAnchor: PropTypes.bool,
} as any;

export { MenuPositioner };
Loading

0 comments on commit bcbceda

Please sign in to comment.