@opengovsg/confetti

中文文档
0.9.7发布于 2026/11/02SEE LICENSE IN LICENSE.mdTypeScriptES Module

Widgets for Confetti

原始使用文档
文档目录15 节
  1. Installation
  2. Peer Dependencies
  3. Quick Start
  4. Analytics metadata
  5. Proxying requests
  6. Template Components
  7. Locale
  8. Static Components
  9. Visibility Hooks
  10. useVisibleAfterDelay
  11. useVisibleAfterScroll
  12. useVisibleAfterPageVisits
  13. useVisibleAfterSessionPageVisits
  14. useRespondentState
  15. Building Blocks

@opengovsg/confetti

Embeddable React survey widget for collecting user feedback.

npm version

Installation

# npm
npm install @opengovsg/confetti

# yarn
yarn add @opengovsg/confetti

# pnpm
pnpm add @opengovsg/confetti

Peer Dependencies

This package requires React 18.2.0+ or React 19:

npm install react@^18.2.0

Quick Start

  1. Add the widget to your page:
import { PopoverConfetti } from '@opengovsg/confetti'

function App() {
  return (
    <div style={{ position: 'fixed', bottom: '1rem', right: '1rem' }}>
      <PopoverConfetti
        surveyId="your-survey-id"
        publishableKey="your-publishable-key"
        metadata={{}}
      />
    </div>
  )
}

The template components load their script bundle and stylesheet from the Confetti CDN at runtime, so there's no CSS to import — including the host-rendered trigger button, which TriggerPopoverConfetti styles itself. (The lower-level @opengovsg/confetti/components primitives and the @opengovsg/confetti/static opt-out surface render in your own bundle and still require import '@opengovsg/confetti/confetti.css'.)

Note: You'll need the survey ID and publishable key from your Confetti dashboard. Make sure to whitelist your domain in your team settings.

Analytics metadata

Analytics metadata collection is disabled by default. Set autoInjectAnalyticsMetadata to true to include identifiers exposed by PostHog and Datadog RUM on the host page with each response:

<PopoverConfetti
  surveyId="your-survey-id"
  publishableKey="your-publishable-key"
  autoInjectAnalyticsMetadata
/>

The widget reads these SDKs from window.posthog and window.DD_RUM, and it never bundles either SDK. @datadog/browser-rum assigns window.DD_RUM for you as soon as it is imported, so Datadog needs no extra setup.

PostHog is different — you have to expose it yourself. It documents that most modern apps won't have window.posthog, so assign it wherever you initialise PostHog:

import posthog from 'posthog-js'

posthog.init('<your-project-token>', { api_host: '<your-api-host>' })

window.posthog = posthog

Any identifier whose SDK is missing is simply skipped, so a partial setup still submits the rest. These keys are collected when available:

KeySource
posthog_distinct_idposthog.get_distinct_id()
posthog_session_idposthog.get_session_id()
datadog_rum_session_idDD_RUM.getInternalContext()
datadog_rum_userDD_RUM.getUser(), JSON stringified
datadog_rum_user_id, datadog_rum_user_email, datadog_rum_user_nameDD_RUM.getUser() standard fields

Explicit values passed through metadata always take precedence over automatically collected values.

Use the Datadog session ID to find the session in Datadog Sessions Explorer with @session.id:<datadog_rum_session_id>.

  1. Add Confetti to your CSP headers. The widget loads its script and stylesheet from the CDN and calls the API, so allowlist all three:
script-src https://confetti.gov.sg;
style-src https://confetti.gov.sg;
connect-src https://confetti.gov.sg;

Proxying requests

Every template component accepts an optional proxyUrl. When set, the widget sends its API requests to {proxyUrl}/api/v1/cfti/... instead of https://confetti.gov.sg, and the CDN-backed shells also load their script and stylesheet from {proxyUrl}/widget/v1/.... Use this to route widget traffic through your own origin, for example when your CSP disallows third party hosts.

Confetti Cloud teams can provision a managed proxy from Team settings. Create the DNS-only CNAME shown there and wait for it to become live, then pass the resulting URL. If the hostname is in Cloudflare DNS, disable the orange cloud so Proxy status is DNS only — see the managed proxies guide.

<PopoverConfetti
  proxyUrl="https://c.example.gov.sg"
  surveyId="..."
  publishableKey="cfti_pk_..."
/>

When using a managed proxy, use that origin for all three CSP directives shown above instead of https://confetti.gov.sg.

Template Components

Choose the component that best fits your use case:

ComponentUse CaseBehavior
EmbeddedConfettiInline surveys, feedback sectionsShows questions in an inline manner with a submit button
ModalConfettiInterruptive, centered surveysCentered modal overlay, step-by-step questions, auto-submits
PopoverConfettiFloating feedback widgetsDismissible popover, step-by-step questions, auto-submits
TriggerPopoverConfettiFeedback launcher buttonRenders a trigger button that opens a dismissible popover survey on click; reopenable
StepperConfettiGuided surveysOne question at a time, auto-submits after last question

Locale

Requires @opengovsg/confetti 0.8.2 or later. Older versions keep the English survey — they do not fail to load.

Pass locale to show a translated survey. Author translations in the Confetti dashboard (English is always the source; Chinese, Malay, and Tamil are optional). Untranslated fields fall back to English.

The widget does not read the browser language on its own. Use useResolveLocale to map a BCP-47 tag (or the browser's language preferences) to 'en' | 'zh' | 'ms' | 'ta':

import { PopoverConfetti, useResolveLocale } from '@opengovsg/confetti'

function Feedback() {
  const locale = useResolveLocale()

  return (
    <PopoverConfetti
      surveyId="your-survey-id"
      publishableKey="your-publishable-key"
      locale={locale}
    />
  )
}

Pass preferred when the host app already knows the locale (for example next-intl). Pass explicit for an override that wins even when it does not match a Confetti locale — including an empty string, which falls back to English instead of preferred:

const locale = useResolveLocale({
  preferred: i18n.locale,
  explicit: searchParams.get('locale'),
})

To always show one language, pass locale directly and skip the hook:

<PopoverConfetti
  surveyId="your-survey-id"
  publishableKey="your-publishable-key"
  locale="zh"
/>
ValueLanguage
enEnglish (default)
zh中文
msBahasa Melayu
taதமிழ்

Widget chrome you did not customise — Skip, Next, Submit, Back, Close, Other, placeholders, and validation errors — still localises for the given locale. Omit locale, or pass 'en', to keep the English survey.

The same prop is accepted on the static components and on the building-block primitives. Import useResolveLocale from @opengovsg/confetti in those setups too. See the translations guide.

Static Components

The @opengovsg/confetti/static subpath ships self-contained builds of the same template components (EmbeddedConfetti, ModalConfetti, PopoverConfetti, StepperConfetti) plus the ConfettiTrigger composition helper. They render entirely from your own bundle with no runtime CDN dependency, so survey rendering updates only reach your site when you upgrade the package.

import '@opengovsg/confetti/confetti.css'

import { PopoverConfetti } from '@opengovsg/confetti/static'

Unlike the CDN-backed shells, the static components require the stylesheet import shown above.

Visibility Hooks

Control when your survey appears with these hooks:

useVisibleAfterDelay

Show the survey after a delay:

import { useVisibleAfterDelay } from '@opengovsg/confetti'

const { isVisible } = useVisibleAfterDelay({ delay: 5000 }) // 5 seconds

useVisibleAfterScroll

Show the survey after scrolling a certain distance:

import { useVisibleAfterScroll } from '@opengovsg/confetti'

const { isVisible } = useVisibleAfterScroll({ threshold: 500 }) // 500px

useVisibleAfterPageVisits

Show the survey once the visitor has loaded the current page a given number of times (persisted across sessions via localStorage):

import { useVisibleAfterPageVisits } from '@opengovsg/confetti'

const { isVisible, reset } = useVisibleAfterPageVisits({
  visits: 3,
  respondent: 'optional-respondent-identifier',
})

useVisibleAfterSessionPageVisits

Same as above, but the visit count is scoped to the current browser session via sessionStorage:

import { useVisibleAfterSessionPageVisits } from '@opengovsg/confetti'

const { isVisible, reset } = useVisibleAfterSessionPageVisits({ visits: 3 })

useRespondentState

Read a respondent's response and dismissal times without mounting a template. respondent is required (this hook does not generate an anonymous id).

import { useRespondentState } from '@opengovsg/confetti'

const respondentState = useRespondentState({
  surveyId: '<your-survey-id>',
  publishableKey: '<your-publishable-key>',
  respondent: '<user-id>',
})

Building Blocks

The lower-level primitives that power the template components are published from the @opengovsg/confetti/components subpath. Reach for these to compose your own survey UI.

import {
  ConfettiController,
  ConfettiProvider,
  SurveyContext,
  useSurvey,
} from '@opengovsg/confetti/components'

Unlike the template components, these primitives render in your own bundle, so import the stylesheet once in your app entry point:

import '@opengovsg/confetti/confetti.css'

版本与发布记录

@opengovsg/confetti 共发布 84 个版本,首次发布于 2026/01/22;过去 12 个月发布了 84 个版本。

版本发布日期距今
0.9.7latest2026/11/021 天前
0.9.62026/10/2113 天前
0.9.52026/10/1420 天前
0.9.42026/10/1222 天前
0.9.32026/10/0727 天前
0.9.22026/09/291 个月前

其他发布通道:snapshot → 0.0.0-snapshot-20260813020755。

关于 @opengovsg/confetti 的常见问题

@opengovsg/confetti 是什么?

五彩纸屑的小部件

如何安装 @opengovsg/confetti?

在项目目录运行 npm install @opengovsg/confetti,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.2.0 || ^19.0.0)、react-dom(^18.2.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@opengovsg/confetti 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@opengovsg/confetti 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@opengovsg/confetti 可以商用吗?

@opengovsg/confetti 使用 SEE LICENSE IN LICENSE.md 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@opengovsg/confetti 还在维护吗?

最新版本 0.9.7 发布于 2026/11/02(1 天前);至今共发布 84 个版本,其中过去 12 个月发布了 84 个;首次发布于 2026/01/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。