This repository was archived by the owner on Jun 21, 2022. It is now read-only.
forked from owncloud/owncloud-design-system
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOcDrop.vue
More file actions
328 lines (309 loc) · 7.22 KB
/
Copy pathOcDrop.vue
File metadata and controls
328 lines (309 loc) · 7.22 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<template>
<div
:id="dropId"
ref="drop"
class="oc-drop oc-box-shadow-medium oc-rounded"
@click="$_ocDrop_close"
>
<div
v-if="$slots.default"
:class="['oc-card oc-card-body oc-rounded oc-background-secondary', paddingClass]"
>
<slot />
</div>
<slot v-else name="special" />
</div>
</template>
<script>
import tippy, { hideAll } from "tippy.js"
import { destroy, hideOnEsc } from "../../../directives/OcTooltip"
import uniqueId from "../../../utils/uniqueId"
import { getSizeClass } from "../../../utils/sizeClasses"
/**
* Position any element in relation to another element.
*/
export default {
name: "OcDrop",
status: "ready",
release: "1.0.0",
props: {
/**
* Id of the drop.
*/
dropId: {
type: String,
required: false,
default: () => uniqueId("oc-drop-"),
},
/**
* Specifies custom Popper options
*/
popperOptions: {
type: Object,
required: false,
},
/**
* CSS selector for the element to be used as toggle. By default, the preceding element is used.
**/
toggle: {
type: String,
default: "",
},
/**
* The position of the drop: `(top|right|bottom|left|auto)|(top|right|bottom|left|auto)-(start|end)`.
**/
position: {
type: String,
default: "bottom-start",
validator: value => {
return value.match(
/((top|right|bottom|left|auto)|(top|right|bottom|left|auto)-(start|end))/
)
},
},
/**
* Events that cause the drop to show. Multiple event names are separated by spaces
*
* @values click, hover, manual
**/
mode: {
type: String,
default: "click",
validator: value => {
return value.match(/(click|hover|manual)/)
},
},
/**
* Defines if the drop should be closed after clicking on it. Needs to have defined dropId to work.
*/
closeOnClick: {
type: Boolean,
required: false,
},
/**
* Element selector used as a target of the drop
*/
target: {
type: String,
required: false,
default: null,
},
/**
* Defines the padding size around the drop content. Defaults to `medium`.
*
* @values xsmall, small, medium, large, xlarge, xxlarge, xxxlarge, remove
*/
paddingSize: {
type: String,
required: false,
default: "medium",
validator: value => {
return value.match(/(xsmall|small|medium|large|xlarge|xxlarge|xxxlarge|remove)/)
},
},
},
data() {
return { tippy: null }
},
computed: {
triggerMapping() {
return (
{
hover: "mouseenter focus",
}[this.mode] || this.mode
)
},
paddingClass() {
return `oc-p-${getSizeClass(this.paddingSize)}`
},
},
watch: {
position() {
this.tippy.setProps({ placement: this.position })
},
mode() {
this.tippy.setProps({ trigger: this.triggerMapping })
},
},
beforeDestroy() {
destroy(this.tippy)
},
mounted() {
destroy(this.tippy)
const to = this.target
? document.querySelector(this.target)
: this.toggle
? document.querySelector(this.toggle)
: this.$el.previousElementSibling
const content = this.$refs.drop
if (!to || !content) {
return
}
const config = {
trigger: this.triggerMapping,
placement: this.position,
arrow: false,
hideOnClick: true,
interactive: true,
plugins: [hideOnEsc],
theme: "none",
aria: {
content: "describedby",
},
onShow(instance) {
hideAll({ exclude: instance })
},
popperOptions: this.popperOptions,
content,
}
if (this.target) {
config.triggerTarget = this.toggle
? document.querySelector(this.toggle)
: this.$el.previousElementSibling
}
this.tippy = tippy(to, config)
},
methods: {
$_ocDrop_close() {
if (this.closeOnClick) {
this.tippy.hide()
}
},
/**
* Programatically show the drop
*
* @public
*/
show(duration) {
this.tippy.show(duration)
},
/**
* Programatically hide the drop
*
* @public
*/
hide(duration) {
this.tippy.hide(duration)
},
},
}
</script>
<style lang="scss">
.tippy-box[data-theme~="none"] {
background-color: transparent;
font-size: inherit;
line-height: inherit;
.tippy-content {
// note: needed so that the box shadow from `oc-box-shadow-medium` doesn't get suppressed
padding: 8px;
}
li.action-menu-item-hover {
a,
button:not([role="switch"]) {
box-sizing: border-box;
padding: var(--oc-space-small);
color: var(--oc-color-swatch-passive-default);
&:focus,
&:hover {
background-color: var(--oc-color-background-hover);
text-decoration: none !important;
border-radius: 5px;
}
&:hover span {
color: var(--oc-color-swatch-brand-hover) !important;
}
span {
text-decoration: none !important;
}
}
}
}
.oc-drop {
width: 300px;
li a:focus {
outline: auto 1px !important;
}
}
</style>
<docs>
```js
<template>
<div class="oc-button-group oc-mt-s">
<oc-button id="my_menu" class="oc-mr-s">Menu</oc-button>
<oc-drop toggle="#my_menu" mode="click">
<ul>
<li icon="create_new_folder" autofocus>Item with icon</li>
<li>Item without icon</li>
<li>Active item</li>
</ul>
</oc-drop>
<oc-button id="my_filter" class="oc-mr-s">Filter</oc-button>
<oc-drop toggle="#my_filter" mode="hover">
<p>
Lets filter:
</p>
<ul class="oc-list">
<li>
<oc-checkbox label="" />
<span class="oc-text-muted">Show Files</span>
</li>
<li>
<oc-checkbox label="" />
<span class="oc-text-muted">Show Folders</span>
</li>
<li>
<oc-search-bar small placeholder="Filter by name" :button="false" label="" />
</li>
</ul>
</oc-drop>
<oc-button id="my_advanced">Advanced</oc-button>
<oc-drop dropId="oc-drop" toggle="#my_advanced" mode="click" closeOnClick>
<div slot="special" class="oc-card">
<div class="oc-card-header">
<h3 class="oc-card-title">
Advanced
</h3>
</div>
<div class="oc-card-body">
<p>
I'm a slightly more advanced drop down and I'll be closed as soon as you click on me.
</p>
</div>
</div>
</oc-drop>
</div>
</template>
```
### Custom target
```js
<div>
<div>
<p id="target">This is the target of the drop</p>
</div>
<oc-button id="custom-target-toggle">Trigger drop</oc-button>
<oc-drop dropId="oc-drop-custom-target" toggle="#custom-target-toggle" target="#target" mode="click" closeOnClick>
I am attached to a custom element
</oc-drop>
</div>
```
### Open drop programatically
```js
<template>
<div>
<oc-button id="manual-target" @click="open">Open</oc-button>
<oc-drop ref="drop" mode="manual" target="#manual-target">
I am triggered manually
</oc-drop>
</div>
</template>
<script>
export default {
methods: {
open() {
this.$refs.drop.show()
}
}
}
</script>
```
</docs>