From 2faa0277238d30df94b6d4a8e548fa52cca05625 Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Tue, 3 Sep 2024 19:57:03 +1000 Subject: [PATCH] feat(useScriptTriggerElement): pre-hydration event triggers (#237) --- .../3.api/3.use-script-trigger-element.md | 39 ++++++++++++++++- src/runtime/components/ScriptCarbonAds.vue | 15 +++++-- src/runtime/components/ScriptCrisp.vue | 9 +++- .../components/ScriptGoogleAdsense.vue | 10 ++++- src/runtime/components/ScriptGoogleMaps.vue | 4 +- src/runtime/components/ScriptIntercom.vue | 19 ++++++--- src/runtime/components/ScriptLemonSqueezy.vue | 13 ++++-- .../components/ScriptStripePricingTable.vue | 13 ++++-- src/runtime/components/ScriptVimeoPlayer.vue | 1 + .../components/ScriptYouTubePlayer.vue | 1 + .../composables/useScriptTriggerElement.ts | 42 +++++++++++++++---- 11 files changed, 136 insertions(+), 30 deletions(-) diff --git a/docs/content/docs/3.api/3.use-script-trigger-element.md b/docs/content/docs/3.api/3.use-script-trigger-element.md index c1674d4f..1cba1793 100644 --- a/docs/content/docs/3.api/3.use-script-trigger-element.md +++ b/docs/content/docs/3.api/3.use-script-trigger-element.md @@ -13,7 +13,7 @@ Create a trigger for an element to load a script based on specific element event ## Signature ```ts -function useScriptTriggerElement(options: ElementScriptTriggerOptions): Promise {} +function useScriptTriggerElement(options: ElementScriptTriggerOptions): Promise & { ssrAttrs?: Record } | 'onNuxtReady' {} ``` ## Arguments @@ -22,8 +22,10 @@ function useScriptTriggerElement(options: ElementScriptTriggerOptions): Promise< export interface ElementScriptTriggerOptions { /** * The event to trigger the script load. + * + * For example we can bind events that we'd normally use addEventListener for: `mousedown`, `mouseenter`, `scroll`, etc. */ - trigger?: ElementScriptTrigger | undefined + trigger?: 'immediate' | 'visible' | string | string[] | false | undefined /** * The element to watch for the trigger event. * @default document.body @@ -36,6 +38,39 @@ export interface ElementScriptTriggerOptions { A promise that resolves when the script is loaded. +## Handling Pre-Hydration Events + +When registering a trigger that depends on user input, such as `mousedown`, it's possible that the user will interact with the element before the hydration process is complete. + +In this case, the event listener will not be attached to the element, and the script will not be loaded. + +To ensure this is handled correctly you should bind the `ssrAttrs` value to the element you're attaching events to. Note that you should verify +that a promise is returned from the function before using the `ssrAttrs` value. + +```vue + + +``` + + ## Examples - Load a script when an element is visible. diff --git a/src/runtime/components/ScriptCarbonAds.vue b/src/runtime/components/ScriptCarbonAds.vue index f8c38d37..5a8bb1c9 100644 --- a/src/runtime/components/ScriptCarbonAds.vue +++ b/src/runtime/components/ScriptCarbonAds.vue @@ -1,7 +1,7 @@