Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/sour-carrots-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@coinbase/onchainkit": minor
---

feat: Add option to useOpenUrl to handle non-mini-app behavior in a custom way
23 changes: 17 additions & 6 deletions packages/onchainkit/src/minikit/hooks/useOpenUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@ import sdk from '@farcaster/miniapp-sdk';
import { useCallback } from 'react';
import { useMiniKit } from './useMiniKit';

export type UseOpenUrlParams = {
fallback?: (url: string) => void;
};

/**
* Opens a new url, if in a frame context, using the openUrl sdk action, otherwise opens in a new tab
* @param url - The URL to open.
* @returns void
* Opens a new url, if in a frame context, using the openUrl sdk action, otherwise opens via the fallback function.
*
* @param options - The options for the useOpenUrl hook.
* @param options.fallback - The fallback function to use if the context is not available. Defaults to opening in a new tab.
*/
export function useOpenUrl() {
export function useOpenUrl(
{ fallback }: UseOpenUrlParams = {
fallback: (url) => {
window.open(url, '_blank');
},
},
) {
const { context } = useMiniKit();

return useCallback(
(url: string) => {
if (context) {
sdk.actions.openUrl(url);
} else {
window.open(url, '_blank');
fallback?.(url);
}
},
[context],
[context, fallback],
);
}
5 changes: 2 additions & 3 deletions packages/onchainkit/src/minikit/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
export { MiniKitProvider } from './MiniKitProvider';
export type { MiniKitProviderProps } from './types';
export { SafeArea } from './components/SafeArea';
export type { SafeAreaProps } from './components/SafeArea';
export { SafeArea, type SafeAreaProps } from './components/SafeArea';
export { useMiniKit } from './hooks/useMiniKit';
export { useOpenUrl } from './hooks/useOpenUrl';
export { useOpenUrl, type UseOpenUrlParams } from './hooks/useOpenUrl';
export { useAuthenticate, parseSignInMessage } from './hooks/useAuthenticate';
export { useViewProfile } from './hooks/useViewProfile';
export { useAddFrame } from './hooks/useAddFrame';
Expand Down