Skip to content

VueStripeElements ​

Creates a Stripe Elements group (stripe.elements()) and provides it to the element components and composables inside it. Use it inside a VueStripeProvider, or in an app that installs the createVueStripe() plugin; without a provider, a Stripe.js load failure renders in this component's #error slot. How Vue Stripe works explains the provider chain.

Usage ​

vue
<template>
  <VueStripeProvider :publishable-key="publishableKey">
    <VueStripeElements :client-secret="clientSecret" :appearance="{ theme: 'stripe' }">
      <VueStripePaymentElement />
    </VueStripeElements>
  </VueStripeProvider>
</template>

<script setup lang="ts">
import '@vue-stripe/vue-stripe/style.css' // once, e.g. in main.ts
import {
  VueStripeProvider,
  VueStripeElements,
  VueStripePaymentElement
} from '@vue-stripe/vue-stripe'

const publishableKey = import.meta.env.VITE_STRIPE_PUBLISHABLE_KEY
const clientSecret = 'pi_xxx_secret_xxx' // From your backend
</script>

Without an Intent, pass mode / currency / amount instead of clientSecret (Collect payment details before creating an Intent walks through this flow):

vue
<VueStripeElements mode="payment" currency="usd" :amount="1099">
  <VueStripePaymentElement />
</VueStripeElements>

Props ​

PropTypeRequiredDescription
clientSecretstringNo*Client secret from PaymentIntent or SetupIntent. When provided, Elements uses the intent-based flow
mode'payment' | 'setup' | 'subscription'NoMode for deferred intent creation (without clientSecret). Required when clientSecret is not provided
currencystringNoCurrency code (e.g. 'usd', 'eur') for deferred intent creation. Required when using mode without clientSecret
amountnumberNoAmount in the smallest currency unit (e.g. cents for USD, whole yen for JPY; see Amounts and currencies). Required for 'payment' and 'subscription' modes
setupFutureUsage'off_session' | 'on_session'NoIndicates intent to save the payment method for future use
captureMethod'automatic' | 'automatic_async' | 'manual'NoControls when to capture funds from the customer's account
appearanceAppearanceNoAppearance API options to theme all child elements (theme, variables, rules, labels). Convenience prop — equivalent to options.appearance. Changes are applied live with elements.update(); a new but deep-equal object is not a change
fonts(CssFontSource | CustomFontSource)[]NoCustom fonts to load into the Elements iframe. Convenience prop — equivalent to options.fonts
localeStripeElementLocaleNoLocale to display Elements in: one of Stripe's supported locales ('auto', 'en', 'fr', ...); a value outside the list is a type error. Convenience prop — equivalent to options.locale. Changes are applied live with elements.update(). Which Checkout integrations follow a locale: Locale
optionsStripeElementsOptionsNoAny other option passed to stripe.elements() (e.g. allowedPaymentMethodTypes in the mode-based flow, loader)

* Required for Payment Element and some other elements. Optional for Card Element. When clientSecret is omitted, use mode/currency/amount for deferred intent creation; see Collect payment details before creating an Intent.

A new clientSecret, mode, currency, amount or setupFutureUsage calls stripe.elements() again to create a new Elements group. Changes to the appearance and locale props are applied to the existing group with elements.update(), so mounted elements keep what the customer typed (keys inside options are read when the group is created). The values are compared by content: a re-render that passes a new but deep-equal object (e.g. an inline :appearance="{ ... }") does not call update().

Amounts and currencies ​

amount, like every amount in the Stripe API, is an integer in the currency's smallest unit. Which unit that is depends on the currency (Stripe: Supported currencies):

  • Most currencies have two decimals: 1099 with usd is $10.99, with eur €10.99.
  • Zero-decimal currencies such as JPY and KRW have no smaller unit: 2000 with jpy is ¥2,000. Multiplying the price by 100, as for cents, charges 100 times too much.
  • Three-decimal currencies (BHD, JOD, KWD, OMR, TND) count thousandths, and Stripe requires the last digit to be 0: 5120 with kwd is 5.120 KWD.
  • A few currencies are special cases. ISK and HUF take two decimals in the API: 50000 with isk is 500 ISK, and 500000 with huf is 5,000 HUF. Intl.NumberFormat formats ISK without decimals, so a price label that divides by its fraction digits shows 50,000 ISK. Stripe's page lists these cases, TWD among them, with their rules.

paymentMethodTypes was removed in v6

Stripe.js Endive rejects paymentMethodTypes on stripe.elements() as an integration error, so VueStripeElements no longer has this prop (and drops the key if it appears in options). Choose the payment methods instead with:

  • Mode-based flow (deferred Intent: mode / currency / amount): pass allowedPaymentMethodTypes through options:

    vue
    <VueStripeElements
      mode="payment"
      currency="usd"
      :amount="1099"
      :options="{ allowedPaymentMethodTypes: ['card', 'klarna'] }"
    >
      <VueStripePaymentElement />
    </VueStripeElements>
  • clientSecret flow: set allowed_payment_method_types on the PaymentIntent / SetupIntent on your server, or manage payment methods in the Stripe Dashboard. allowedPaymentMethodTypes is not valid together with clientSecret.

No Intent yet? Pass mode

With neither clientSecret nor mode, Stripe.js Endive throws "Unable to select session" from its iframe for some elements (verified with the Address Element); error monitoring reports it as an uncaught error. When you do not have an Intent yet, pass mode / currency / amount so Elements has a session to work with.

Convenience props vs. options

appearance, fonts, and locale are first-class convenience props so you can bind them directly without nesting them inside options. If you set the same key in both options and a dedicated prop, the dedicated prop wins.

vue
<!-- These two are equivalent -->
<VueStripeElements :appearance="{ theme: 'night' }" />
<VueStripeElements :options="{ appearance: { theme: 'night' } }" />

Options Object ​

The options prop takes Stripe's StripeElementsOptions (also accepted: a plain object of stripe.elements() options). appearance is typed as Stripe's Appearance, and fonts as an array of CssFontSource | CustomFontSource. All of these are re-exported from the package:

ts
// Listing of the exported option types, not a sample
import type {
  Appearance,
  CssFontSource,
  CustomFontSource,
  StripeElementsOptions
} from '@vue-stripe/vue-stripe'

See stripe.elements() options for every key.

Appearance API ​

The appearance prop (or options.appearance) themes every element in the group. It takes Stripe's Appearance object:

KeyTypeDescription
theme'stripe' | 'night' | 'flat'Base theme
variablesRecord<string, string>Design tokens such as colorPrimary, colorBackground, colorText, colorDanger, fontFamily, fontSizeBase, spacingUnit, borderRadius
rulesRecord<string, Record<string, string>>CSS-like rules per selector, e.g. .Input, .Input:focus, .Input--invalid, .Label, .Error
labels'auto' | 'above' | 'floating'Label position
vue
<VueStripeElements
  :client-secret="secret"
  :appearance="{
    theme: 'stripe',
    variables: { colorPrimary: '#0570de', borderRadius: '4px' },
    rules: { '.Input:focus': { border: '1px solid #0570de' } }
  }"
>

Custom fonts load through fonts (or options.fonts): pass { cssSrc } for a stylesheet or { family, src, weight, style } for a font file, then name the family in variables.fontFamily:

vue
<!-- Rendered inside VueStripeProvider -->
<script setup lang="ts">
import { VueStripeElements, VueStripePaymentElement } from '@vue-stripe/vue-stripe'
import type { CssFontSource } from '@vue-stripe/vue-stripe'

defineProps<{ clientSecret: string }>()

const fonts: CssFontSource[] = [{ cssSrc: 'https://fonts.googleapis.com/css?family=Roboto' }]
</script>

<template>
  <VueStripeElements
    :client-secret="clientSecret"
    :fonts="fonts"
    :appearance="{ variables: { fontFamily: 'Roboto, sans-serif' } }"
  >
    <VueStripePaymentElement />
  </VueStripeElements>
</template>

Changes to appearance are applied to the mounted elements with elements.update(), including changes made in place to a reactive object. An inline object is safe: a re-render that rebuilds it with the same content does not update Elements. Style elements has full theme examples, including switching themes at runtime.

Events ​

EventPayloadDescription
@readyStripeElementsEmitted after the Elements instance is created. The payload is the StripeElements instance
@errorstringEmitted when Elements creation fails, when an appearance / locale update is rejected, or, under the plugin without a VueStripeProvider, when Stripe.js fails to load (the plugin logs that failure itself). The payload is the error message
@update-end-Emitted when an elements.update() call has been applied to the mounted elements (Stripe's update-end event), e.g. after an appearance or locale change

elements.update() is asynchronous

Since Stripe.js Dahlia, elements.update() returns a Promise. VueStripeElements waits for it when it applies appearance / locale changes; a rejected update sets the component's error state and surfaces as @error.

vue
<VueStripeElements :client-secret="clientSecret" @ready="onReady" @error="onError">
  <VueStripePaymentElement />
</VueStripeElements>

@update-end ​

VueStripeElements listens to the group's update-end event (elements.on('update-end', handler)) and re-emits it, so you can react once Stripe has finished applying an update, for example to hide a spinner after switching themes:

vue
<template>
  <VueStripeElements
    mode="payment"
    currency="usd"
    :amount="1099"
    :appearance="appearance"
    @update-end="applying = false"
  >
    <VueStripePaymentElement />
  </VueStripeElements>
  <button @click="switchTheme">Switch theme</button>
  <span v-if="applying">Applying...</span>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { VueStripeElements, VueStripePaymentElement } from '@vue-stripe/vue-stripe'

const theme = ref<'stripe' | 'night'>('stripe')
// Only a change of content updates Elements; an inline object with the same content does not.
const appearance = computed(() => ({ theme: theme.value }))
const applying = ref(false)

function switchTheme() {
  applying.value = true
  theme.value = theme.value === 'stripe' ? 'night' : 'stripe'
}
</script>

The event fires for every elements.update() call on the group, including calls you make yourself through useStripeElements().elements. A new clientSecret, mode, currency, amount or setupFutureUsage creates a new group instead of updating it: that emits @ready, not @update-end. The listener moves to the new group, and an event from the replaced group is never re-emitted. Stripe.js 10.0.0 types the group with on('update-end') only; VueStripeElements calls off() on unmount and on re-creation when the runtime provides it.

Slots ​

SlotSlot PropsDescription
default-Rendered once the Elements group is created
#loading-Rendered while the group is created (default: "Initializing Elements...")
#error{ error: string }Rendered when creating the group fails, or, without a VueStripeProvider, when Stripe.js fails to load
vue
<VueStripeElements :client-secret="secret">
  <template #loading>
    <div>Initializing payment form...</div>
  </template>

  <template #error="{ error }">
    <div class="error">{{ error }}</div>
  </template>

  <VueStripePaymentElement />
</VueStripeElements>

Provides ​

The component exposes nothing on a template ref. It provides these values to descendants:

KeyTypeDescription
elementsRef<StripeElements | null>The Elements instance
loadingRef<boolean>Whether Elements is loading
errorRef<string | null>Error message if initialization failed

Read them with the useStripeElements composable.

Styling ​

The component renders a plain wrapper div around the default slot once the group exists. While loading and on failure it renders:

html
<!-- while loading (#loading slot replaces the message) -->
<div class="vue-stripe-elements-loading">
  <div class="vue-stripe-loading-message">Initializing Elements...</div>
</div>

<!-- on failure (#error slot replaces the message) -->
<div class="vue-stripe-elements-error">
  <div class="vue-stripe-error-message">...</div>
</div>

The default styles for these classes ship in the package stylesheet. Import it once in your app:

ts
import '@vue-stripe/vue-stripe/style.css'

The fields inside Stripe's iframes are styled with the Appearance API, not with CSS.

TypeScript ​

The component's props, event payloads and slot props are typed on Vue 2.7 and Vue 3: vue-tsc checks a handler's parameter against the payload in Events, and a slot prop against Slots. The props type is exported as VueStripeElementsProps.

vue
<script setup lang="ts">
import { VueStripeElements, VueStripePaymentElement } from '@vue-stripe/vue-stripe'
import type { Appearance, StripeElements } from '@vue-stripe/vue-stripe'

const props = defineProps<{ clientSecret: string }>()

const appearance: Appearance = {
  theme: 'stripe',
  variables: {
    colorPrimary: '#0570de'
  }
}

const onReady = (elements: StripeElements) => {
  // elements is fully typed
  console.log(elements.getElement('payment'))
}
</script>

<template>
  <VueStripeElements :client-secret="props.clientSecret" :appearance="appearance" @ready="onReady">
    <VueStripePaymentElement />
  </VueStripeElements>
</template>

vue-tsc and // in a v-if element

vue-tsc 3.3 reads // inside an attribute value of an element that has v-if as the start of a comment. A URL written inline there, such as :fonts="[{ cssSrc: 'https://...' }]", makes every handler on that element lose its payload type (TS7006, implicit any). This happens with any component, not only VueStripeElements. Bind the value from a script constant instead, as in Appearance API.

See Also ​