Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mouseLeaveDelayInSeconds option for desktop #12

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/web/docs/settings/desktop.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ title: desktop

- **triggerOnIdle** - if true, the handlers will be triggered when the user is idle
- **triggerOnMouseLeave** - if true, the handlers will be triggered when the user is about to leave your website
- **delayInSecondsToTrigger** - delay in seconds to trigger the exit intent handlers. **triggerOnIdle** **must be true**!
- **delayInSecondsToTrigger** - delay in seconds to trigger the exit intent handlers on idle. **triggerOnIdle** **must be true**!
- **mouseLeaveDelayInSeconds** - delay in seconds to trigger the exit intent handlers on mouse leave. **triggerOnMouseLeave** **must be true**!
- **useBeforeUnload** - if true, the handlers will be triggered when the user is about to leave your website. **UI will be blocked until the user cancels the action**.

<br/>

## Default
```tsx
delayInSecondsToTrigger: 10
mouseLeaveDelayInSeconds: 5
triggerOnMouseLeave: true
triggerOnIdle: false
useBeforeUnload: false
Expand All @@ -30,6 +32,7 @@ useBeforeUnload: false
useExitIntent({
desktop: {
delayInSecondsToTrigger: 10,
mouseLeaveDelayInSeconds: 5,
triggerOnMouseLeave: true,
triggerOnIdle: false
useBeforeUnload: false
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/shared/constants/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export const codes = {
"triggerOnIdle": false,
"useBeforeUnload": false,
"triggerOnMouseLeave": true,
"delayInSecondsToTrigger": 10
"delayInSecondsToTrigger": 10,
"mouseLeaveDelayInSeconds": 5
},

"mobile": {
Expand Down
15 changes: 15 additions & 0 deletions apps/web/src/templates/Home/Playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ export function PlaygroundSection() {
/>
</Label>

<Label>
mouseLeaveDelayInSeconds:
<input
defaultValue={settings?.desktop?.mouseLeaveDelayInSeconds}
type="number"
onChange={({ target }) => {
updateSettings({
desktop: {
mouseLeaveDelayInSeconds: target.valueAsNumber,
},
})
}}
/>
</Label>

<Label>
useBeforeUnload:
<input
Expand Down
18 changes: 14 additions & 4 deletions packages/use-exit-intent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ export function useExitIntent(props: ExitIntentSettings | void = {}) {
}

if (isMobile() && mobile?.triggerOnIdle) {
removeIdleEvents(execute)
createIdleEvents(execute)
}

Expand All @@ -180,12 +179,21 @@ export function useExitIntent(props: ExitIntentSettings | void = {}) {
secondsToMiliseconds(desktop?.delayInSecondsToTrigger!)
)

let mouseLeaveTimer: ReturnType<typeof setTimeout>

const handleMouseLeave = () => {
if (shouldNotTrigger.current) return
handleExitIntent()
}

if (desktop?.triggerOnIdle) {
createIdleEvents(execute)
}

if (desktop?.triggerOnMouseLeave) {
document.body.addEventListener('mouseleave', handleExitIntent)
mouseLeaveTimer = setTimeout(() => {
document.body.addEventListener('mouseleave', handleMouseLeave)
}, secondsToMiliseconds(desktop.mouseLeaveDelayInSeconds!))
}

if (desktop?.useBeforeUnload) {
Expand All @@ -200,8 +208,10 @@ export function useExitIntent(props: ExitIntentSettings | void = {}) {

return () => {
abort()
removeIdleEvents(execute)
document.body.removeEventListener('mouseleave', handleExitIntent)
clearTimeout(mouseLeaveTimer)
document.body.removeEventListener('mouseleave', handleMouseLeave),
removeIdleEvents(execute),
(window.onbeforeunload = null)
}
}
})
Expand Down
1 change: 1 addition & 0 deletions packages/use-exit-intent/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface DesktopOptions {
triggerOnMouseLeave?: boolean
delayInSecondsToTrigger?: number
useBeforeUnload?: boolean
mouseLeaveDelayInSeconds?: number
}

export interface MobileOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/use-exit-intent/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export const defaultSettings: InternalExitIntentSettings = {
daysToExpire: 30,
key: 'exit-intent',
},

desktop: {
triggerOnIdle: false,
useBeforeUnload: false,
triggerOnMouseLeave: true,
delayInSecondsToTrigger: 10,
mouseLeaveDelayInSeconds: 5,
},

mobile: {
Expand Down