@@ -3,7 +3,7 @@ import { ref } from 'vue'
3
3
import type { ElementScriptTrigger } from ' ../types'
4
4
import { useElementScriptTrigger } from ' ../composables/useElementScriptTrigger'
5
5
import { useScript } from ' ../composables/useScript'
6
- import { onBeforeUnmount } from ' #imports'
6
+ import { onBeforeUnmount , onMounted , watch } from ' #imports'
7
7
8
8
const props = withDefaults (defineProps <{
9
9
trigger? : ElementScriptTrigger
@@ -17,30 +17,39 @@ const props = withDefaults(defineProps<{
17
17
})
18
18
19
19
const emit = defineEmits <{
20
- ready: []
20
+ ready: [ReturnType <typeof useScript >]
21
+ error: []
21
22
}>()
22
23
23
24
const rootEl = ref <HTMLDivElement | undefined >()
24
25
const containerEl = ref <HTMLDivElement | undefined >()
25
- const { $script } = useScript (` https://js.stripe.com/v3/pricing-table.js ` , {
26
+ const instance = useScript (` https://js.stripe.com/v3/pricing-table.js ` , {
26
27
trigger: useElementScriptTrigger ({ trigger: props .trigger , el: rootEl }),
27
28
})
29
+ const { $script } = instance
28
30
29
31
const pricingTable = ref <HTMLElement | undefined >()
30
- $script .then (() => {
31
- const StripePricingTable = window .customElements .get (' stripe-pricing-table' )!
32
- const stripePricingTable = new StripePricingTable ()
33
- stripePricingTable .setAttribute (' publishable-key' , props .publishableKey )
34
- stripePricingTable .setAttribute (' pricing-table-id' , props .pricingTableId )
35
- if (props .clientReferenceId )
36
- stripePricingTable .setAttribute (' client-reference-id' , props .clientReferenceId )
37
- if (props .customerEmail )
38
- stripePricingTable .setAttribute (' customer-email' , props .customerEmail )
39
- if (props .customerSessionClientSecret )
40
- stripePricingTable .setAttribute (' customer-session-client-secret' , props .customerSessionClientSecret )
41
- pricingTable .value = stripePricingTable
42
- rootEl .value ! .appendChild (stripePricingTable )
43
- emit (' ready' )
32
+ onMounted (() => {
33
+ $script .then (() => {
34
+ const StripePricingTable = window .customElements .get (' stripe-pricing-table' )!
35
+ const stripePricingTable = new StripePricingTable ()
36
+ stripePricingTable .setAttribute (' publishable-key' , props .publishableKey )
37
+ stripePricingTable .setAttribute (' pricing-table-id' , props .pricingTableId )
38
+ if (props .clientReferenceId )
39
+ stripePricingTable .setAttribute (' client-reference-id' , props .clientReferenceId )
40
+ if (props .customerEmail )
41
+ stripePricingTable .setAttribute (' customer-email' , props .customerEmail )
42
+ if (props .customerSessionClientSecret )
43
+ stripePricingTable .setAttribute (' customer-session-client-secret' , props .customerSessionClientSecret )
44
+ pricingTable .value = stripePricingTable
45
+ rootEl .value ! .appendChild (stripePricingTable )
46
+ emit (' ready' , instance )
47
+ })
48
+ watch ($script .status , (status ) => {
49
+ if (status === ' error' ) {
50
+ emit (' error' )
51
+ }
52
+ })
44
53
})
45
54
46
55
onBeforeUnmount (() => {
@@ -53,6 +62,7 @@ onBeforeUnmount(() => {
53
62
<div ref =" containerEl" />
54
63
<slot v-if =" $script.status.value === 'loading'" name =" loading" />
55
64
<slot v-if =" $script.status.value === 'awaitingLoad'" name =" awaitingLoad" />
65
+ <slot v-else-if =" $script.status.value === 'error'" name =" error" />
56
66
<slot />
57
67
</div >
58
68
</template >
0 commit comments