npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@twenty1tools/whatif-react

v0.2.0

Published

React wrapper for What If? SDK

Readme

What If? React SDK

Official React wrapper for the What If? SDK - a beautiful, customizable widget for collecting contextual micro feedback.

Installation

npm install @twenty1tools/whatif-react
# or
pnpm add @twenty1tools/whatif-react
# or
yarn add @twenty1tools/whatif-react

Quick Start

1. Wrap your app with WhatIfProvider

import { WhatIfProvider } from '@twenty1tools/whatif-react';

function App() {
  return (
    <WhatIfProvider 
      projectId="your-project-id"
      theme={{
        darkMode: 'auto',
        colors: {
          primary: '#f97316',
        },
      }}
    >
      <YourApp />
    </WhatIfProvider>
  );
}

2. Use the useWhatIf hook

import { useWhatIf } from '@twenty1tools/whatif-react';

function FeedbackButton() {
  const { ask } = useWhatIf();

  const handleClick = () => {
    ask('question-id-123', {
      context: { page: 'checkout' },
    });
  };

  return (
    <button onClick={handleClick}>
      Give Feedback
    </button>
  );
}

API Reference

WhatIfProvider

The provider component that initializes the What If? SDK.

Props

  • projectId (string, required): Your What If? project ID
  • baseUrl (string, optional): API base URL (default: /api)
  • theme (ThemeConfig, optional): Theme configuration object
  • children (ReactNode, required): Your app components
<WhatIfProvider
  projectId="your-project-id"
  baseUrl="/api"
  theme={{
    darkMode: 'auto',
    position: 'bottom-right',
    colors: {
      primary: '#3b82f6',
    },
  }}
>
  {children}
</WhatIfProvider>

useWhatIf

Hook to access What If? functionality.

Returns

  • ask(questionId: string, options?: AskOptions): Promise<void> - Display a question widget
  • setTheme(theme: ThemeConfig): void - Update the theme
const { ask, setTheme } = useWhatIf();

// Show a question
await ask('question-123', {
  context: { page: 'pricing' },
  userFingerprint: 'user-abc',
});

// Update theme
setTheme({
  colors: {
    primary: '#10b981',
  },
});

Examples

Next.js App Router

// app/providers.tsx
'use client';

import { WhatIfProvider } from '@twenty1tools/whatif-react';

export function Providers({ children }: { children: React.ReactNode }) {
  return (
    <WhatIfProvider
      projectId={process.env.NEXT_PUBLIC_WHATIF_PROJECT_ID!}
      theme={{
        darkMode: 'auto',
      }}
    >
      {children}
    </WhatIfProvider>
  );
}

// app/layout.tsx
import { Providers } from './providers';

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        <Providers>{children}</Providers>
      </body>
    </html>
  );
}

// app/page.tsx
'use client';

import { useWhatIf } from '@twenty1tools/whatif-react';

export default function HomePage() {
  const { ask } = useWhatIf();

  return (
    <button onClick={() => ask('welcome-question')}>
      Share Feedback
    </button>
  );
}

Next.js Pages Router

// pages/_app.tsx
import type { AppProps } from 'next/app';
import { WhatIfProvider } from '@twenty1tools/whatif-react';

export default function App({ Component, pageProps }: AppProps) {
  return (
    <WhatIfProvider projectId="your-project-id">
      <Component {...pageProps} />
    </WhatIfProvider>
  );
}

// pages/index.tsx
import { useWhatIf } from '@twenty1tools/whatif-react';

export default function HomePage() {
  const { ask } = useWhatIf();

  return (
    <button onClick={() => ask('question-123')}>
      Give Feedback
    </button>
  );
}

Conditional Question Triggering

'use client';

import { useWhatIf } from '@twenty1tools/whatif-react';
import { useEffect } from 'react';

export default function CheckoutPage() {
  const { ask } = useWhatIf();

  useEffect(() => {
    // Ask after user spends 10 seconds on page
    const timer = setTimeout(() => {
      ask('checkout-question', {
        context: { page: 'checkout', duration: '10s' },
      });
    }, 10000);

    return () => clearTimeout(timer);
  }, [ask]);

  return <div>Checkout Page</div>;
}

Dynamic Theme Switching

'use client';

import { useWhatIf } from '@twenty1tools/whatif-react';
import { useState } from 'react';

export default function Settings() {
  const { setTheme } = useWhatIf();
  const [isDark, setIsDark] = useState(false);

  const toggleTheme = () => {
    setIsDark(!isDark);
    setTheme({
      darkMode: isDark ? 'light' : 'dark',
    });
  };

  return (
    <button onClick={toggleTheme}>
      Toggle Theme
    </button>
  );
}

Theming

The React SDK supports all theming options from the core SDK. See the core SDK documentation for detailed theming options.

Example Themes

Minimal Design

<WhatIfProvider
  projectId="your-project-id"
  theme={{
    shadow: { enabled: false },
    border: {
      radius: '4px',
      buttonRadius: '4px',
    },
    colors: {
      primary: '#000000',
    },
  }}
>
  {children}
</WhatIfProvider>

Playful Design

<WhatIfProvider
  projectId="your-project-id"
  theme={{
    border: {
      radius: '24px',
      buttonRadius: '24px',
    },
    colors: {
      primary: '#ec4899',
      ratingFilled: '#f472b6',
    },
    animation: {
      duration: 600,
      easing: 'cubic-bezier(0.68, -0.55, 0.265, 1.55)',
    },
  }}
>
  {children}
</WhatIfProvider>

TypeScript Support

This package is fully typed with TypeScript. All types are re-exported for convenience:

import type { 
  ThemeConfig, 
  DarkMode, 
  WidgetPosition, 
  ColorPalette,
  AskOptions,
} from '@twenty1tools/whatif-react';

Related Packages

License

MIT

Support