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

debugging chromatic #2936

Closed
wants to merge 4 commits 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
18 changes: 9 additions & 9 deletions build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ async function build(path) {
// 2. It could have fully replaced esbuild (as it uses that internally),
// but at the moment its esbuild version is somewhat outdated.
// It’s also harder to configure and esbuild docs are more thorough.
await tsup.build({
entry: [file],
format: ['cjs', 'esm'],
dts: { only: true },
outDir: dist,
silent: true,
external: [/@radix-ui\/.+/],
});
console.log(`Built ${path}/dist/index.d.ts`);
// await tsup.build({
// entry: [file],
// format: ['cjs', 'esm'],
// dts: { only: true },
// outDir: dist,
// silent: true,
// external: [/@radix-ui\/.+/],
// });
// console.log(`Built ${path}/dist/index.d.ts`);
}

globSync('packages/*/*').forEach(build);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"@types/fs-extra": "^11",
"@types/jest": "^27.4.1",
"@types/jest-axe": "^3.5.3",
"@types/react": "^18.0.5",
"@types/react-dom": "^18.0.0",
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"@types/react-test-renderer": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.19.0",
"@typescript-eslint/parser": "^5.19.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/react/announce/src/Announce.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const Announce = React.forwardRef<AnnounceElement, AnnounceProps>((props, forwar
</Primitive.div>

{/* portal into live region for screen reader announcements */}
{region && ReactDOM.createPortal(<div>{children}</div>, region)}
<>{region && ReactDOM.createPortal(<div>{children}</div>, region)}</>
</React.Fragment>
);
});
Expand Down
20 changes: 12 additions & 8 deletions packages/react/navigation-menu/src/NavigationMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,18 @@ const NavigationMenuIndicator = React.forwardRef<
const context = useNavigationMenuContext(INDICATOR_NAME, props.__scopeNavigationMenu);
const isVisible = Boolean(context.value);

return context.indicatorTrack
? ReactDOM.createPortal(
<Presence present={forceMount || isVisible}>
<NavigationMenuIndicatorImpl {...indicatorProps} ref={forwardedRef} />
</Presence>,
context.indicatorTrack
)
: null;
return (
<>
{context.indicatorTrack
? ReactDOM.createPortal(
<Presence present={forceMount || isVisible}>
<NavigationMenuIndicatorImpl {...indicatorProps} ref={forwardedRef} />
</Presence>,
context.indicatorTrack
)
: null}
</>
);
});

NavigationMenuIndicator.displayName = INDICATOR_NAME;
Expand Down
16 changes: 10 additions & 6 deletions packages/react/portal/src/Portal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import ReactDOM from 'react-dom';
import { Primitive } from '@radix-ui/react-primitive';
import { useLayoutEffect } from '@radix-ui/react-use-layout-effect';
// import { useLayoutEffect } from '@radix-ui/react-use-layout-effect';

/* -------------------------------------------------------------------------------------------------
* Portal
Expand All @@ -20,12 +20,16 @@ interface PortalProps extends PrimitiveDivProps {

const Portal = React.forwardRef<PortalElement, PortalProps>((props, forwardedRef) => {
const { container: containerProp, ...portalProps } = props;
const [mounted, setMounted] = React.useState(false);
useLayoutEffect(() => setMounted(true), []);
const [mounted] = React.useState(true);
// useLayoutEffect(() => setMounted(true), []);
const container = containerProp || (mounted && globalThis?.document?.body);
return container
? ReactDOM.createPortal(<Primitive.div {...portalProps} ref={forwardedRef} />, container)
: null;
return (
<>
{container
? ReactDOM.createPortal(<Primitive.div {...portalProps} ref={forwardedRef} />, container)
: null}
</>
);
});

Portal.displayName = PORTAL_NAME;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/presence/src/Presence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function getElementRef(element: React.ReactElement) {
// Pre React 19 there's a getter on `element.props.ref` that throws a warning when attempting to access it.
// This is safe to rely on. (As in... obviously, old React versions won't change).
// https://github.com/facebook/react/blob/408258268edb5acdfdbf77bc6e0b0dc6396c0e6f/packages/react/src/jsx/ReactJSXElement.js#L89-L99
const getter = Object.getOwnPropertyDescriptor(element.props, 'ref')?.get;
const getter = Object.getOwnPropertyDescriptor(element.props, 'ref')?.get as any;
const hasPropWarning = getter && 'isReactWarning' in getter && getter.isReactWarning;

if (hasPropWarning) {
Expand Down
24 changes: 14 additions & 10 deletions packages/react/select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -405,16 +405,20 @@ const SelectContent = React.forwardRef<SelectContentElement, SelectContentProps>

if (!context.open) {
const frag = fragment as Element | undefined;
return frag
? ReactDOM.createPortal(
<SelectContentProvider scope={props.__scopeSelect}>
<Collection.Slot scope={props.__scopeSelect}>
<div>{props.children}</div>
</Collection.Slot>
</SelectContentProvider>,
frag
)
: null;
return (
<>
{frag
? ReactDOM.createPortal(
<SelectContentProvider scope={props.__scopeSelect}>
<Collection.Slot scope={props.__scopeSelect}>
<div>{props.children}</div>
</Collection.Slot>
</SelectContentProvider>,
frag
)
: null}
</>
);
}

return <SelectContentImpl {...props} ref={forwardedRef} />;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/slot/src/Slot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function getElementRef(element: React.ReactElement) {
// Pre React 19 there's a getter on `element.props.ref` that throws a warning when attempting to access it.
// This is safe to rely on. (As in... obviously, old React versions won't change).
// https://github.com/facebook/react/blob/408258268edb5acdfdbf77bc6e0b0dc6396c0e6f/packages/react/src/jsx/ReactJSXElement.js#L89-L99
const getter = Object.getOwnPropertyDescriptor(element.props, 'ref')?.get;
const getter = Object.getOwnPropertyDescriptor(element.props, 'ref')?.get as any;
const hasPropWarning = getter && 'isReactWarning' in getter && getter.isReactWarning;

if (hasPropWarning) {
Expand Down
196 changes: 100 additions & 96 deletions packages/react/toast/src/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -560,108 +560,112 @@ const ToastImpl = React.forwardRef<ToastImplElement, ToastImplProps>(
)}

<ToastInteractiveProvider scope={__scopeToast} onClose={handleClose}>
{ReactDOM.createPortal(
<Collection.ItemSlot scope={__scopeToast}>
<DismissableLayer.Root
asChild
onEscapeKeyDown={composeEventHandlers(onEscapeKeyDown, () => {
if (!context.isFocusedToastEscapeKeyDownRef.current) handleClose();
context.isFocusedToastEscapeKeyDownRef.current = false;
})}
>
<Primitive.li
// Ensure toasts are announced as status list or status when focused
role="status"
aria-live="off"
aria-atomic
tabIndex={0}
data-state={open ? 'open' : 'closed'}
data-swipe-direction={context.swipeDirection}
{...toastProps}
ref={composedRefs}
style={{ userSelect: 'none', touchAction: 'none', ...props.style }}
onKeyDown={composeEventHandlers(props.onKeyDown, (event) => {
if (event.key !== 'Escape') return;
onEscapeKeyDown?.(event.nativeEvent);
if (!event.nativeEvent.defaultPrevented) {
context.isFocusedToastEscapeKeyDownRef.current = true;
handleClose();
}
})}
onPointerDown={composeEventHandlers(props.onPointerDown, (event) => {
if (event.button !== 0) return;
pointerStartRef.current = { x: event.clientX, y: event.clientY };
})}
onPointerMove={composeEventHandlers(props.onPointerMove, (event) => {
if (!pointerStartRef.current) return;
const x = event.clientX - pointerStartRef.current.x;
const y = event.clientY - pointerStartRef.current.y;
const hasSwipeMoveStarted = Boolean(swipeDeltaRef.current);
const isHorizontalSwipe = ['left', 'right'].includes(context.swipeDirection);
const clamp = ['left', 'up'].includes(context.swipeDirection)
? Math.min
: Math.max;
const clampedX = isHorizontalSwipe ? clamp(0, x) : 0;
const clampedY = !isHorizontalSwipe ? clamp(0, y) : 0;
const moveStartBuffer = event.pointerType === 'touch' ? 10 : 2;
const delta = { x: clampedX, y: clampedY };
const eventDetail = { originalEvent: event, delta };
if (hasSwipeMoveStarted) {
swipeDeltaRef.current = delta;
handleAndDispatchCustomEvent(TOAST_SWIPE_MOVE, onSwipeMove, eventDetail, {
discrete: false,
});
} else if (isDeltaInDirection(delta, context.swipeDirection, moveStartBuffer)) {
swipeDeltaRef.current = delta;
handleAndDispatchCustomEvent(TOAST_SWIPE_START, onSwipeStart, eventDetail, {
discrete: false,
});
(event.target as HTMLElement).setPointerCapture(event.pointerId);
} else if (Math.abs(x) > moveStartBuffer || Math.abs(y) > moveStartBuffer) {
// User is swiping in wrong direction so we disable swipe gesture
// for the current pointer down interaction
pointerStartRef.current = null;
}
<>
{ReactDOM.createPortal(
<Collection.ItemSlot scope={__scopeToast}>
<DismissableLayer.Root
asChild
onEscapeKeyDown={composeEventHandlers(onEscapeKeyDown, () => {
if (!context.isFocusedToastEscapeKeyDownRef.current) handleClose();
context.isFocusedToastEscapeKeyDownRef.current = false;
})}
onPointerUp={composeEventHandlers(props.onPointerUp, (event) => {
const delta = swipeDeltaRef.current;
const target = event.target as HTMLElement;
if (target.hasPointerCapture(event.pointerId)) {
target.releasePointerCapture(event.pointerId);
}
swipeDeltaRef.current = null;
pointerStartRef.current = null;
if (delta) {
const toast = event.currentTarget;
>
<Primitive.li
// Ensure toasts are announced as status list or status when focused
role="status"
aria-live="off"
aria-atomic
tabIndex={0}
data-state={open ? 'open' : 'closed'}
data-swipe-direction={context.swipeDirection}
{...toastProps}
ref={composedRefs}
style={{ userSelect: 'none', touchAction: 'none', ...props.style }}
onKeyDown={composeEventHandlers(props.onKeyDown, (event) => {
if (event.key !== 'Escape') return;
onEscapeKeyDown?.(event.nativeEvent);
if (!event.nativeEvent.defaultPrevented) {
context.isFocusedToastEscapeKeyDownRef.current = true;
handleClose();
}
})}
onPointerDown={composeEventHandlers(props.onPointerDown, (event) => {
if (event.button !== 0) return;
pointerStartRef.current = { x: event.clientX, y: event.clientY };
})}
onPointerMove={composeEventHandlers(props.onPointerMove, (event) => {
if (!pointerStartRef.current) return;
const x = event.clientX - pointerStartRef.current.x;
const y = event.clientY - pointerStartRef.current.y;
const hasSwipeMoveStarted = Boolean(swipeDeltaRef.current);
const isHorizontalSwipe = ['left', 'right'].includes(context.swipeDirection);
const clamp = ['left', 'up'].includes(context.swipeDirection)
? Math.min
: Math.max;
const clampedX = isHorizontalSwipe ? clamp(0, x) : 0;
const clampedY = !isHorizontalSwipe ? clamp(0, y) : 0;
const moveStartBuffer = event.pointerType === 'touch' ? 10 : 2;
const delta = { x: clampedX, y: clampedY };
const eventDetail = { originalEvent: event, delta };
if (
isDeltaInDirection(delta, context.swipeDirection, context.swipeThreshold)
if (hasSwipeMoveStarted) {
swipeDeltaRef.current = delta;
handleAndDispatchCustomEvent(TOAST_SWIPE_MOVE, onSwipeMove, eventDetail, {
discrete: false,
});
} else if (
isDeltaInDirection(delta, context.swipeDirection, moveStartBuffer)
) {
handleAndDispatchCustomEvent(TOAST_SWIPE_END, onSwipeEnd, eventDetail, {
discrete: true,
swipeDeltaRef.current = delta;
handleAndDispatchCustomEvent(TOAST_SWIPE_START, onSwipeStart, eventDetail, {
discrete: false,
});
} else {
handleAndDispatchCustomEvent(
TOAST_SWIPE_CANCEL,
onSwipeCancel,
eventDetail,
{
(event.target as HTMLElement).setPointerCapture(event.pointerId);
} else if (Math.abs(x) > moveStartBuffer || Math.abs(y) > moveStartBuffer) {
// User is swiping in wrong direction so we disable swipe gesture
// for the current pointer down interaction
pointerStartRef.current = null;
}
})}
onPointerUp={composeEventHandlers(props.onPointerUp, (event) => {
const delta = swipeDeltaRef.current;
const target = event.target as HTMLElement;
if (target.hasPointerCapture(event.pointerId)) {
target.releasePointerCapture(event.pointerId);
}
swipeDeltaRef.current = null;
pointerStartRef.current = null;
if (delta) {
const toast = event.currentTarget;
const eventDetail = { originalEvent: event, delta };
if (
isDeltaInDirection(delta, context.swipeDirection, context.swipeThreshold)
) {
handleAndDispatchCustomEvent(TOAST_SWIPE_END, onSwipeEnd, eventDetail, {
discrete: true,
}
);
});
} else {
handleAndDispatchCustomEvent(
TOAST_SWIPE_CANCEL,
onSwipeCancel,
eventDetail,
{
discrete: true,
}
);
}
// Prevent click event from triggering on items within the toast when
// pointer up is part of a swipe gesture
toast.addEventListener('click', (event) => event.preventDefault(), {
once: true,
});
}
// Prevent click event from triggering on items within the toast when
// pointer up is part of a swipe gesture
toast.addEventListener('click', (event) => event.preventDefault(), {
once: true,
});
}
})}
/>
</DismissableLayer.Root>
</Collection.ItemSlot>,
context.viewport
)}
})}
/>
</DismissableLayer.Root>
</Collection.ItemSlot>,
context.viewport
)}
</>
</ToastInteractiveProvider>
</>
);
Expand Down
Loading
Loading