+
+ {operation.summary}
+
+ {operation.description ? (
+
+ ) : null}
+
+
+ {method.toUpperCase()}
+
+
+
+ {path}
+
-
);
}
diff --git a/packages/react-openapi/src/ScalarApiButton.tsx b/packages/react-openapi/src/ScalarApiButton.tsx
index 074fc32fe..bc6650eca 100644
--- a/packages/react-openapi/src/ScalarApiButton.tsx
+++ b/packages/react-openapi/src/ScalarApiButton.tsx
@@ -1,23 +1,33 @@
'use client';
-import { useApiClientModal } from '@scalar/api-client-react';
-import React from 'react';
+import { ApiClientModalProvider, useApiClientModal } from '@scalar/api-client-react';
+import React, { useImperativeHandle, useRef } from 'react';
+import { createPortal } from 'react-dom';
import { useOpenAPIOperationContext } from './OpenAPIOperationContext';
+import { useEventCallback } from 'usehooks-ts';
/**
* Button which launches the Scalar API Client
*/
-export function ScalarApiButton({ method, path }: { method: string; path: string }) {
- const client = useApiClientModal();
- const { onOpenClient } = useOpenAPIOperationContext();
+export function ScalarApiButton({
+ method,
+ path,
+ specUrl,
+}: {
+ method: string;
+ path: string;
+ specUrl: string;
+}) {
+ const [isOpen, setIsOpen] = React.useState(false);
+ const controllerRef = useRef
(null);
return (
+
+ {isOpen &&
+ createPortal(
+ ,
+ document.body,
+ )}
);
}
+
+function ScalarModal(props: {
+ method: string;
+ path: string;
+ specUrl: string;
+ controllerRef: React.Ref;
+}) {
+ return (
+
+
+
+ );
+}
+
+type ScalarModalControllerRef = {
+ openClient: (() => void) | undefined;
+};
+
+function ScalarModalController(props: {
+ method: string;
+ path: string;
+ controllerRef: React.Ref;
+}) {
+ const client = useApiClientModal();
+ const openClient = client?.open;
+ useImperativeHandle(
+ props.controllerRef,
+ () => ({ openClient: openClient ? () => openClient() : undefined }),
+ [openClient],
+ );
+
+ // Open the client when the component is mounted.
+ const { onOpenClient } = useOpenAPIOperationContext();
+ const trackOpening = useEventCallback(() => {
+ onOpenClient({ method: props.method, path: props.path });
+ });
+ React.useEffect(() => {
+ if (openClient) {
+ openClient();
+ trackOpening();
+ }
+ }, [openClient]);
+ return null;
+}