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
<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):
<VueStripeElements mode="payment" currency="usd" :amount="1099">
<VueStripePaymentElement />
</VueStripeElements>Props
| Prop | Type | Required | Description |
|---|---|---|---|
clientSecret | string | No* | Client secret from PaymentIntent or SetupIntent. When provided, Elements uses the intent-based flow |
mode | 'payment' | 'setup' | 'subscription' | No | Mode for deferred intent creation (without clientSecret). Required when clientSecret is not provided |
currency | string | No | Currency code (e.g. 'usd', 'eur') for deferred intent creation. Required when using mode without clientSecret |
amount | number | No | Amount 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' | No | Indicates intent to save the payment method for future use |
captureMethod | 'automatic' | 'automatic_async' | 'manual' | No | Controls when to capture funds from the customer's account |
appearance | Appearance | No | Appearance 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)[] | No | Custom fonts to load into the Elements iframe. Convenience prop — equivalent to options.fonts |
locale | StripeElementLocale | No | Locale 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 |
options | StripeElementsOptions | No | Any 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:
1099withusdis $10.99, witheur€10.99. - Zero-decimal currencies such as JPY and KRW have no smaller unit:
2000withjpyis ¥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:
5120withkwdis 5.120 KWD. - A few currencies are special cases. ISK and HUF take two decimals in the API:
50000withiskis 500 ISK, and500000withhufis 5,000 HUF.Intl.NumberFormatformats 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): passallowedPaymentMethodTypesthroughoptions:vue<VueStripeElements mode="payment" currency="usd" :amount="1099" :options="{ allowedPaymentMethodTypes: ['card', 'klarna'] }" > <VueStripePaymentElement /> </VueStripeElements>clientSecretflow: setallowed_payment_method_typeson the PaymentIntent / SetupIntent on your server, or manage payment methods in the Stripe Dashboard.allowedPaymentMethodTypesis not valid together withclientSecret.
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.
<!-- 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:
// 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:
| Key | Type | Description |
|---|---|---|
theme | 'stripe' | 'night' | 'flat' | Base theme |
variables | Record<string, string> | Design tokens such as colorPrimary, colorBackground, colorText, colorDanger, fontFamily, fontSizeBase, spacingUnit, borderRadius |
rules | Record<string, Record<string, string>> | CSS-like rules per selector, e.g. .Input, .Input:focus, .Input--invalid, .Label, .Error |
labels | 'auto' | 'above' | 'floating' | Label position |
<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:
<!-- 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
| Event | Payload | Description |
|---|---|---|
@ready | StripeElements | Emitted after the Elements instance is created. The payload is the StripeElements instance |
@error | string | Emitted 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.
<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:
<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
| Slot | Slot Props | Description |
|---|---|---|
| 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 |
<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:
| Key | Type | Description |
|---|---|---|
elements | Ref<StripeElements | null> | The Elements instance |
loading | Ref<boolean> | Whether Elements is loading |
error | Ref<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:
<!-- 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:
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.
<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
- Style elements - Appearance API examples, the stylesheet and the wrapper markup
- useStripeElements - Access Elements in child components
- VueStripeProvider - Parent provider component
- createVueStripe - App-wide alternative to the provider
- How Vue Stripe works - The provider chain and when each layer renders
- Accept a payment - The Payment Element with
VueStripeElements - Stripe Appearance API - Every variable and rule selector
