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

feat: Experimental INP fix #4560

Merged
merged 9 commits into from
Dec 12, 2024
Merged
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
4 changes: 3 additions & 1 deletion packages/sdk-components-react-radix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@
"@radix-ui/react-switch": "^1.1.1",
"@radix-ui/react-tabs": "^1.1.1",
"@radix-ui/react-tooltip": "^1.1.4",
"@radix-ui/react-use-controllable-state": "^1.1.0",
"@webstudio-is/css-engine": "workspace:*",
"@webstudio-is/icons": "workspace:*",
"@webstudio-is/react-sdk": "workspace:*",
"@webstudio-is/sdk": "workspace:*"
"@webstudio-is/sdk": "workspace:*",
"await-interaction-response": "^0.0.2"
},
"devDependencies": {
"@types/node": "^22.9.3",
Expand Down
30 changes: 26 additions & 4 deletions packages/sdk-components-react-radix/src/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import {
useEffect,
useRef,
useContext,
useCallback,
} from "react";
import * as DialogPrimitive from "@radix-ui/react-dialog";
import {
ReactSdkContext,
getClosestInstance,
type Hook,
} from "@webstudio-is/react-sdk/runtime";
import { useControllableState } from "@radix-ui/react-use-controllable-state";
import interactionResponse from "await-interaction-response";

/**
* Naive heuristic to determine if a click event will cause navigate
Expand Down Expand Up @@ -49,9 +52,22 @@ export const Dialog = forwardRef<
HTMLDivElement,
Omit<ComponentPropsWithoutRef<typeof DialogPrimitive.Root>, "defaultOpen">
>((props, _ref) => {
const { open, onOpenChange } = props;
const { renderer } = useContext(ReactSdkContext);

const [open, onOpenChange] = useControllableState({
prop: props.open,
defaultProp: false,
onChange: props.onOpenChange,
});

const onOpenChangeHandler = useCallback(
async (open: boolean) => {
await interactionResponse();
onOpenChange(open);
},
[onOpenChange]
);

/**
* Close the dialog when a navigable link within it is clicked.
*/
Expand All @@ -76,15 +92,21 @@ export const Dialog = forwardRef<
}

if (target.closest('[role="dialog"]')) {
onOpenChange?.(false);
onOpenChangeHandler?.(false);
}
};

window.addEventListener("click", handleClick);
return () => window.removeEventListener("click", handleClick);
}, [open, onOpenChange, renderer]);
}, [open, onOpenChangeHandler, renderer]);

return <DialogPrimitive.Root {...props} />;
return (
<DialogPrimitive.Root
{...props}
onOpenChange={onOpenChangeHandler}
open={open}
/>
);
});

/**
Expand Down
43 changes: 33 additions & 10 deletions packages/sdk-components-react-radix/src/tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
import {
type ComponentPropsWithoutRef,
type ForwardRefExoticComponent,
forwardRef,
type ComponentProps,
type RefAttributes,
} from "react";
import { type ComponentPropsWithoutRef, forwardRef, useCallback } from "react";
import { Root, List, Trigger, Content } from "@radix-ui/react-tabs";
import {
getClosestInstance,
getIndexWithinAncestorFromComponentProps,
type Hook,
} from "@webstudio-is/react-sdk/runtime";
import { useControllableState } from "@radix-ui/react-use-controllable-state";
import interactionResponse from "await-interaction-response";

export const Tabs = forwardRef<
HTMLDivElement,
Omit<ComponentPropsWithoutRef<typeof Root>, "value" | "onValueChange"> & {
value?: string;
onValueChange?: (value: string) => void;
}
>(({ defaultValue, ...props }, ref) => {
const [value, onValueChange] = useControllableState({
prop: props.value,
defaultProp: defaultValue,
onChange: props.onValueChange,
});

export const Tabs: ForwardRefExoticComponent<
Omit<ComponentProps<typeof Root>, "asChild"> & RefAttributes<HTMLDivElement>
> = Root;
const handleValueChange = useCallback(
async (value: string) => {
await interactionResponse();
onValueChange(value);
},
[onValueChange]
);

return (
<Root
ref={ref}
{...props}
value={value}
onValueChange={handleValueChange}
/>
);
});

export const TabsList = List;

Expand Down
3 changes: 2 additions & 1 deletion packages/sdk-components-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"@webstudio-is/react-sdk": "workspace:*",
"@webstudio-is/sdk": "workspace:*",
"colord": "^2.9.3",
"micromark": "^4.0.0"
"micromark": "^4.0.0",
"await-interaction-response": "^0.0.2"
},
"devDependencies": {
"@testing-library/react": "^14.2.2",
Expand Down
11 changes: 10 additions & 1 deletion packages/sdk-components-react/src/vimeo-play-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import {
type ElementRef,
type ComponentProps,
useContext,
useCallback,
} from "react";
import { VimeoContext } from "./vimeo";
import { Button, defaultTag } from "./button";
import interactionResponse from "await-interaction-response";

export { defaultTag };

Expand All @@ -14,10 +16,17 @@ type Props = ComponentProps<typeof Button>;
export const VimeoPlayButton = forwardRef<ElementRef<typeof defaultTag>, Props>(
(props, ref) => {
const vimeoContext = useContext(VimeoContext);

const handleClick = useCallback(async () => {
await interactionResponse();
vimeoContext.onInitPlayer();
}, [vimeoContext]);

if (vimeoContext.status !== "initial") {
return;
}
return <Button {...props} onClick={vimeoContext.onInitPlayer} ref={ref} />;

return <Button {...props} onClick={handleClick} ref={ref} />;
}
);

Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading