Skip to content

Commit

Permalink
fix: wait for html interop (#1731)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesblasco authored May 13, 2024
1 parent 2fc0002 commit 590f3c2
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions packages/stripe_web/lib/src/widgets/card_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class WebCardField extends StatefulWidget {
class WebStripeCardState extends State<WebCardField> with CardFieldContext {
CardEditController get controller => widget.controller;

late web.MutationObserver mutationObserver;
@override
void initState() {
// ignore: undefined_prefixed_name
Expand All @@ -64,6 +65,31 @@ class WebStripeCardState extends State<WebCardField> with CardFieldContext {
);
initStripe();
super.initState();

mutationObserver = web.MutationObserver(
((JSArray<web.MutationRecord> entries, web.MutationObserver observer) {
if (web.document.getElementById('card-element') != null) {
mutationObserver.disconnect();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
updateCardDetails(
const CardFieldInputDetails(complete: false),
controller,
);
element = WebStripe.js
.elements(createElementOptions())
.createCard(createOptions())
..mount('#card-element'.toJS)
..onBlur(requestBlur)
..onFocus(requestFocus)
..onChange(onCardChanged);
});
}
}.toJS),
);
mutationObserver.observe(
web.document,
web.MutationObserverInit(childList: true, subtree: true),
);
}

js.CardPaymentElement? get element =>
Expand All @@ -80,19 +106,6 @@ class WebStripeCardState extends State<WebCardField> with CardFieldContext {
dev.log('WARNING! Initial card data value has been ignored. \n'
'$kDebugPCIMessage');
}
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
updateCardDetails(
const CardFieldInputDetails(complete: false),
controller,
);
element = WebStripe.js
.elements(createElementOptions())
.createCard(createOptions())
..mount('#card-element'.toJS)
..onBlur(requestBlur)
..onFocus(requestFocus)
..onChange(onCardChanged);
});
}
});
}
Expand Down Expand Up @@ -142,13 +155,12 @@ class WebStripeCardState extends State<WebCardField> with CardFieldContext {
js.JsElementsCreateOptions createElementOptions() {
final textColor = widget.style?.textColor;
return js.JsElementsCreateOptions(
appearance:
js.ElementAppearance(
theme: js.ElementTheme.stripe,
variables: {
if (textColor != null) 'colorText': colorToCssString(textColor),
},
).toJson().jsify() as js.JsElementAppearance,
appearance: js.ElementAppearance(
theme: js.ElementTheme.stripe,
variables: {
if (textColor != null) 'colorText': colorToCssString(textColor),
},
).toJson().jsify() as js.JsElementAppearance,

Check warning on line 163 in packages/stripe_web/lib/src/widgets/card_field.dart

View workflow job for this annotation

GitHub Actions / Typo CI

jsify

"jsify" is a typo. Did you mean "ossify"?
);
}

Expand Down

0 comments on commit 590f3c2

Please sign in to comment.