From 356b276e8283f6ce8160e976fcaa6cdcce40da95 Mon Sep 17 00:00:00 2001 From: Ahmed Halat Date: Tue, 24 Sep 2024 15:12:17 -0400 Subject: [PATCH] chore: swap to using afterNextRender (#43) * chore: swap to using afterNextRender * apply code review changes Co-authored-by: Aleksander Bodurri --------- Co-authored-by: Aleksander Bodurri --- .../ngx-turnstile/src/lib/ngx-turnstile.component.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/projects/ngx-turnstile/src/lib/ngx-turnstile.component.ts b/projects/ngx-turnstile/src/lib/ngx-turnstile.component.ts index 71dc344..4641f0d 100644 --- a/projects/ngx-turnstile/src/lib/ngx-turnstile.component.ts +++ b/projects/ngx-turnstile/src/lib/ngx-turnstile.component.ts @@ -8,6 +8,7 @@ import { EventEmitter, OnDestroy, Inject, + afterNextRender, } from '@angular/core'; import { DOCUMENT } from '@angular/common'; import { TurnstileOptions } from './interfaces/turnstile-options'; @@ -38,7 +39,7 @@ type SupportedVersion = '0'; template: ``, exportAs: 'ngx-turnstile', }) -export class NgxTurnstileComponent implements AfterViewInit, OnDestroy { +export class NgxTurnstileComponent implements OnDestroy { @Input() siteKey!: string; @Input() action?: string; @Input() cData?: string; @@ -57,7 +58,9 @@ export class NgxTurnstileComponent implements AfterViewInit, OnDestroy { private elementRef: ElementRef, private zone: NgZone, @Inject(DOCUMENT) private document: Document, - ) {} + ) { + afterNextRender(() => this.createWidget()); + } private _getCloudflareTurnstileUrl(): string { if (this.version === '0') { @@ -67,7 +70,7 @@ export class NgxTurnstileComponent implements AfterViewInit, OnDestroy { throw 'Version not defined in ngx-turnstile component.'; } - public ngAfterViewInit(): void { + public createWidget(): void { let turnstileOptions: TurnstileOptions = { sitekey: this.siteKey, theme: this.theme,