-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathAbsolutePopup.svelte
156 lines (127 loc) · 3.88 KB
/
AbsolutePopup.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<svelte:options immutable={true} />
<script>
import createDebug from 'debug'
import { setContext, tick } from 'svelte'
import { isChildOf } from '$lib/utils/domUtils'
import { keyComboFromEvent } from '$lib/utils/keyBindings'
const debug = createDebug('jsoneditor:AbsolutePopup')
let popupComponent = null
let popupProps = null
let popupOptions = {}
let refRootPopup
let refHiddenInput
function openAbsolutePopup(Component, props, options) {
debug('open...', options)
popupComponent = Component
popupProps = props || {}
popupOptions = options || {}
tick().then(focus)
}
function closeAbsolutePopup() {
if (popupComponent) {
const onClose = popupOptions.onClose
popupComponent = null
popupProps = null
popupOptions = {}
if (onClose) {
onClose()
}
}
}
function closeWhenOutside(event) {
if (
popupOptions &&
popupOptions.closeOnOuterClick &&
!isChildOf(event.target, (e) => e === refRootPopup)
) {
closeAbsolutePopup()
}
}
function handleWindowMouseDown(event) {
closeWhenOutside(event)
}
function handleMouseDownInside(event) {
event.stopPropagation()
}
function handleKeyDown(event) {
const combo = keyComboFromEvent(event)
if (combo === 'Escape') {
closeAbsolutePopup()
}
}
function handleScrollWheel(event) {
closeWhenOutside(event)
}
function calculateStyle() {
function calculatePosition() {
if (popupOptions.anchor) {
const {
anchor,
width = 0,
height = 0,
offsetTop = 0,
offsetLeft = 0,
position
} = popupOptions
const { left, top, bottom, right } = anchor.getBoundingClientRect()
const positionAbove =
position === 'top' || (top + height > window.innerHeight && top > height)
const positionLeft =
position === 'left' || (left + width > window.innerWidth && left > width)
return {
left: positionLeft ? right - offsetLeft : left + offsetLeft,
top: positionAbove ? top - offsetTop : bottom + offsetTop,
positionAbove,
positionLeft
}
} else if (typeof popupOptions.left === 'number' && typeof popupOptions.top === 'number') {
const { left, top, width = 0, height = 0 } = popupOptions
const positionAbove = top + height > window.innerHeight && top > height
const positionLeft = left + width > window.innerWidth && left > width
return {
left,
top,
positionAbove,
positionLeft
}
} else {
throw new Error('Invalid config: pass either "left" and "top", or pass "anchor"')
}
}
const rootRect = refRootPopup.getBoundingClientRect()
const { left, top, positionAbove, positionLeft } = calculatePosition()
const verticalStyling = positionAbove
? `bottom: ${rootRect.top - top}px;`
: `top: ${top - rootRect.top}px;`
const horizontalStyling = positionLeft
? `right: ${rootRect.left - left}px;`
: `left: ${left - rootRect.left}px;`
return verticalStyling + horizontalStyling
}
function focus() {
if (refHiddenInput) {
refHiddenInput.focus()
}
}
setContext('absolute-popup', { openAbsolutePopup, closeAbsolutePopup })
</script>
<svelte:window
on:mousedown|capture={handleWindowMouseDown}
on:keydown|capture={handleKeyDown}
on:wheel|capture={handleScrollWheel}
/>
<div
bind:this={refRootPopup}
class="absolute-popup"
on:mousedown={handleMouseDownInside}
on:keydown={handleKeyDown}
>
{#if popupComponent && popupProps}
<div class="absolute-popup-content" style={calculateStyle()}>
<input bind:this={refHiddenInput} class="hidden-input" />
<svelte:component this={popupComponent} {...popupProps} />
</div>
{/if}
</div>
<slot />
<style src="./AbsolutePopup.scss"></style>