@destyler/popper 0.0.1-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ const index = require('./index.cjs');
4
+ require('vue');
5
+ require('@destyler/shared');
6
+ require('@destyler/primitive');
7
+ require('@destyler/composition');
8
+ require('@floating-ui/vue');
9
+ require('@destyler/arrow');
10
+
11
+
12
+
13
+ exports.DestylerPopperAnchor = index.DestylerPopperAnchor;
14
+ exports.DestylerPopperArrow = index.DestylerPopperArrow;
15
+ exports.DestylerPopperContent = index.DestylerPopperContent;
16
+ exports.DestylerPopperRoot = index.DestylerPopperRoot;
@@ -0,0 +1,5 @@
1
+ export { DestylerPopperAnchor, DestylerPopperAnchorProps, DestylerPopperArrow, DestylerPopperArrowProps, DestylerPopperContent, DestylerPopperContentProps, DestylerPopperRoot } from './index.cjs';
2
+ import 'vue';
3
+ import '@destyler/primitive';
4
+ import '@destyler/shared';
5
+ import '@floating-ui/vue';
@@ -0,0 +1,5 @@
1
+ export { DestylerPopperAnchor, DestylerPopperAnchorProps, DestylerPopperArrow, DestylerPopperArrowProps, DestylerPopperContent, DestylerPopperContentProps, DestylerPopperRoot } from './index.mjs';
2
+ import 'vue';
3
+ import '@destyler/primitive';
4
+ import '@destyler/shared';
5
+ import '@floating-ui/vue';
@@ -0,0 +1,5 @@
1
+ export { DestylerPopperAnchor, DestylerPopperAnchorProps, DestylerPopperArrow, DestylerPopperArrowProps, DestylerPopperContent, DestylerPopperContentProps, DestylerPopperRoot } from './index.js';
2
+ import 'vue';
3
+ import '@destyler/primitive';
4
+ import '@destyler/shared';
5
+ import '@floating-ui/vue';
@@ -0,0 +1,7 @@
1
+ export { DestylerPopperAnchor, DestylerPopperArrow, DestylerPopperContent, DestylerPopperRoot } from './index.mjs';
2
+ import 'vue';
3
+ import '@destyler/shared';
4
+ import '@destyler/primitive';
5
+ import '@destyler/composition';
6
+ import '@floating-ui/vue';
7
+ import '@destyler/arrow';
package/dist/index.cjs ADDED
@@ -0,0 +1,418 @@
1
+ 'use strict';
2
+
3
+ const vue = require('vue');
4
+ const shared = require('@destyler/shared');
5
+ const primitive = require('@destyler/primitive');
6
+ const composition = require('@destyler/composition');
7
+ const vue$1 = require('@floating-ui/vue');
8
+ const arrow = require('@destyler/arrow');
9
+
10
+ const [injectPopperRootContext, providePopperRootContext] = shared.createContext("DestylerPopperRoot");
11
+ const DestylerPopperRoot = vue.defineComponent({
12
+ name: "DestylerPopperRoot",
13
+ setup() {
14
+ const anchor = vue.ref();
15
+ providePopperRootContext({
16
+ anchor,
17
+ onAnchorChange: (element) => anchor.value = element
18
+ });
19
+ },
20
+ render() {
21
+ return this.$slots.default?.();
22
+ }
23
+ });
24
+
25
+ const destylerPopperAnchorProps = {
26
+ as: {
27
+ type: [String, Object],
28
+ required: false,
29
+ default: "div"
30
+ },
31
+ asChild: {
32
+ type: Boolean,
33
+ required: false,
34
+ default: false
35
+ },
36
+ element: {
37
+ type: Object
38
+ }
39
+ };
40
+ const DestylerPopperAnchor = vue.defineComponent({
41
+ name: "DestylerPopperAnchor",
42
+ props: destylerPopperAnchorProps,
43
+ setup(props) {
44
+ const { customElement, currentElement } = composition.useCustomElement();
45
+ const rootContext = injectPopperRootContext();
46
+ vue.watch(currentElement, () => {
47
+ rootContext.onAnchorChange(props.element ?? currentElement.value);
48
+ });
49
+ return {
50
+ customElement
51
+ };
52
+ },
53
+ render() {
54
+ return vue.h(primitive.DestylerPrimitive, {
55
+ as: this.$props.as,
56
+ asChild: this.$props.asChild,
57
+ ref: "customElement"
58
+ }, this.$slots.default?.());
59
+ }
60
+ });
61
+
62
+ function isNotNull(value) {
63
+ return value !== null;
64
+ }
65
+ function transformOrigin(options) {
66
+ return {
67
+ name: "transformOrigin",
68
+ options,
69
+ fn(data) {
70
+ const { placement, rects, middlewareData } = data;
71
+ const cannotCenterArrow = middlewareData.arrow?.centerOffset !== 0;
72
+ const isArrowHidden = cannotCenterArrow;
73
+ const arrowWidth = isArrowHidden ? 0 : options.arrowWidth;
74
+ const arrowHeight = isArrowHidden ? 0 : options.arrowHeight;
75
+ const [placedSide, placedAlign] = getSideAndAlignFromPlacement(placement);
76
+ const noArrowAlign = { start: "0%", center: "50%", end: "100%" }[placedAlign];
77
+ const arrowXCenter = (middlewareData.arrow?.x ?? 0) + arrowWidth / 2;
78
+ const arrowYCenter = (middlewareData.arrow?.y ?? 0) + arrowHeight / 2;
79
+ let x = "";
80
+ let y = "";
81
+ if (placedSide === "bottom") {
82
+ x = isArrowHidden ? noArrowAlign : `${arrowXCenter}px`;
83
+ y = `${-arrowHeight}px`;
84
+ } else if (placedSide === "top") {
85
+ x = isArrowHidden ? noArrowAlign : `${arrowXCenter}px`;
86
+ y = `${rects.floating.height + arrowHeight}px`;
87
+ } else if (placedSide === "right") {
88
+ x = `${-arrowHeight}px`;
89
+ y = isArrowHidden ? noArrowAlign : `${arrowYCenter}px`;
90
+ } else if (placedSide === "left") {
91
+ x = `${rects.floating.width + arrowHeight}px`;
92
+ y = isArrowHidden ? noArrowAlign : `${arrowYCenter}px`;
93
+ }
94
+ return { data: { x, y } };
95
+ }
96
+ };
97
+ }
98
+ function getSideAndAlignFromPlacement(placement) {
99
+ const [side, align = "center"] = placement.split("-");
100
+ return [side, align];
101
+ }
102
+
103
+ const [injectPopperContentContext, providePopperContentContext] = shared.createContext("DestylerPopperContent");
104
+ const destylerPopperContentProps = {
105
+ as: {
106
+ type: [String, Object],
107
+ required: false,
108
+ default: "div"
109
+ },
110
+ asChild: {
111
+ type: Boolean,
112
+ required: false,
113
+ default: false
114
+ },
115
+ side: {
116
+ type: String,
117
+ required: false,
118
+ default: "bottom"
119
+ },
120
+ sideOffset: {
121
+ type: Number,
122
+ required: false,
123
+ default: 0
124
+ },
125
+ align: {
126
+ type: String,
127
+ required: false,
128
+ default: "center"
129
+ },
130
+ alignOffset: {
131
+ type: Number,
132
+ required: false,
133
+ default: 0
134
+ },
135
+ avoidCollisions: {
136
+ type: Boolean,
137
+ required: false,
138
+ default: true
139
+ },
140
+ collisionBoundary: {
141
+ type: [Object, Array, null],
142
+ required: false,
143
+ default: () => []
144
+ },
145
+ collisionPadding: {
146
+ type: [Number, Object],
147
+ required: false,
148
+ default: 0
149
+ },
150
+ arrowPadding: {
151
+ type: Number,
152
+ required: false,
153
+ default: 0
154
+ },
155
+ sticky: {
156
+ type: String,
157
+ required: false,
158
+ default: "partial"
159
+ },
160
+ hideWhenDetached: {
161
+ type: Boolean,
162
+ required: false,
163
+ default: false
164
+ },
165
+ updatePositionStrategy: {
166
+ type: String,
167
+ required: false,
168
+ default: "optimized"
169
+ },
170
+ onPlaced: {
171
+ type: Function,
172
+ required: false
173
+ },
174
+ prioritizePosition: {
175
+ type: Boolean,
176
+ required: false,
177
+ default: false
178
+ }
179
+ };
180
+ const DestylerPopperContent = vue.defineComponent({
181
+ name: "DestylerPopperContent",
182
+ inheritAttrs: false,
183
+ props: destylerPopperContentProps,
184
+ setup(props, { attrs }) {
185
+ const rootContext = injectPopperRootContext();
186
+ const forwardRef = composition.useForwardRef();
187
+ const { customElement, currentElement: contentElement } = composition.useCustomElement();
188
+ const floatingRef = vue.ref();
189
+ const arrow = vue.ref();
190
+ const { width: arrowWidth, height: arrowHeight } = composition.useSize(arrow);
191
+ const desiredPlacement = vue.computed(() => props.side + (props.align !== "center" ? `-${props.align}` : ""));
192
+ const collisionPadding = vue.computed(() => {
193
+ return typeof props.collisionPadding === "number" ? props.collisionPadding : { top: 0, right: 0, bottom: 0, left: 0, ...props.collisionPadding };
194
+ });
195
+ const boundary = vue.computed(() => {
196
+ return Array.isArray(props.collisionBoundary) ? props.collisionBoundary : [props.collisionBoundary];
197
+ });
198
+ const detectOverflowOptions = vue.computed(() => {
199
+ return {
200
+ padding: collisionPadding.value,
201
+ boundary: boundary.value.filter(isNotNull),
202
+ // with `strategy: 'fixed'`, this is the only way to get it to respect boundaries
203
+ altBoundary: boundary.value.length > 0
204
+ };
205
+ });
206
+ const computedMiddleware = shared.computedEager(() => {
207
+ return [
208
+ vue$1.offset({
209
+ mainAxis: props.sideOffset + arrowHeight.value,
210
+ alignmentAxis: props.alignOffset
211
+ }),
212
+ props.avoidCollisions && vue$1.shift({
213
+ mainAxis: true,
214
+ crossAxis: !!props.prioritizePosition,
215
+ limiter: props.sticky === "partial" ? vue$1.limitShift() : void 0,
216
+ ...detectOverflowOptions.value
217
+ }),
218
+ !props.prioritizePosition && props.avoidCollisions && vue$1.flip({
219
+ ...detectOverflowOptions.value
220
+ }),
221
+ vue$1.size({
222
+ ...detectOverflowOptions.value,
223
+ apply: ({ elements, rects, availableWidth, availableHeight }) => {
224
+ const { width: anchorWidth, height: anchorHeight } = rects.reference;
225
+ const contentStyle = elements.floating.style;
226
+ Object.assign(elements.floating.style, {
227
+ maxWidth: `${availableWidth}px`,
228
+ maxHeight: `${availableHeight}px`
229
+ });
230
+ contentStyle.setProperty(
231
+ "--destyler_popper_available_width",
232
+ `${availableWidth}px`
233
+ );
234
+ contentStyle.setProperty(
235
+ "--destyler_popper_available_height",
236
+ `${availableHeight}px`
237
+ );
238
+ contentStyle.setProperty(
239
+ "--destyler_popper_anchor_width",
240
+ `${anchorWidth}px`
241
+ );
242
+ contentStyle.setProperty(
243
+ "--destyler_popper_anchor_height",
244
+ `${anchorHeight}px`
245
+ );
246
+ }
247
+ }),
248
+ arrow.value && vue$1.arrow({ element: arrow.value, padding: props.arrowPadding }),
249
+ transformOrigin({
250
+ arrowWidth: arrowWidth.value,
251
+ arrowHeight: arrowHeight.value
252
+ }),
253
+ props.hideWhenDetached && vue$1.hide({ strategy: "referenceHidden", ...detectOverflowOptions.value })
254
+ ];
255
+ });
256
+ const { floatingStyles, placement, isPositioned, middlewareData } = vue$1.useFloating(
257
+ rootContext.anchor,
258
+ floatingRef,
259
+ {
260
+ strategy: "fixed",
261
+ placement: desiredPlacement,
262
+ whileElementsMounted: (...args) => {
263
+ const cleanup = vue$1.autoUpdate(...args, {
264
+ animationFrame: props.updatePositionStrategy === "always"
265
+ });
266
+ return cleanup;
267
+ },
268
+ middleware: computedMiddleware
269
+ }
270
+ );
271
+ const placedSide = vue.computed(() => getSideAndAlignFromPlacement(placement.value)[0]);
272
+ const placedAlign = vue.computed(() => getSideAndAlignFromPlacement(placement.value)[1]);
273
+ vue.watchEffect(() => {
274
+ if (isPositioned.value)
275
+ props.onPlaced?.();
276
+ });
277
+ const cannotCenterArrow = vue.computed(
278
+ () => middlewareData.value.arrow?.centerOffset !== 0
279
+ );
280
+ const contentZIndex = vue.ref("");
281
+ vue.watchEffect(() => {
282
+ if (contentElement.value)
283
+ contentZIndex.value = window.getComputedStyle(contentElement.value).zIndex;
284
+ });
285
+ const arrowX = vue.computed(() => middlewareData.value.arrow?.x ?? 0);
286
+ const arrowY = vue.computed(() => middlewareData.value.arrow?.y ?? 0);
287
+ providePopperContentContext({
288
+ placedSide,
289
+ onArrowChange: (element) => arrow.value = element,
290
+ arrowX,
291
+ arrowY,
292
+ shouldHideArrow: cannotCenterArrow
293
+ });
294
+ return {
295
+ floatingRef,
296
+ floatingStyles,
297
+ isPositioned,
298
+ contentZIndex,
299
+ middlewareData,
300
+ forwardRef,
301
+ customElement,
302
+ placedSide,
303
+ placedAlign,
304
+ attrStyle: attrs.style
305
+ };
306
+ },
307
+ render() {
308
+ return vue.h("div", vue.mergeProps(this.$attrs, {
309
+ "data-destyler-popper-content-wrapper": "",
310
+ "ref": "floatingRef",
311
+ "style": {
312
+ ...this.floatingStyles,
313
+ transform: this.isPositioned ? this.floatingStyles.transform : "translate(0, -200%)",
314
+ minWidth: "max-content",
315
+ zIndex: this.contentZIndex,
316
+ ["--destyler_popper_transform_origin"]: [
317
+ this.middlewareData.transformOrigin?.x,
318
+ this.middlewareData.transformOrigin?.y
319
+ ].join(" ")
320
+ }
321
+ }), vue.h(primitive.DestylerPrimitive, vue.mergeProps(this.$attrs, {
322
+ "ref": (vnode) => {
323
+ this.forwardRef(vnode);
324
+ this.customElement = vnode;
325
+ },
326
+ "as": this.$props.as,
327
+ "asChild": this.$props.asChild,
328
+ "data-side": this.placedSide,
329
+ "data-align": this.placedAlign,
330
+ "style": {
331
+ animation: !this.isPositioned ? "none" : void 0,
332
+ opacity: this.middlewareData.hide?.referenceHidden ? 0 : void 0
333
+ }
334
+ }), this.$slots.default?.()));
335
+ }
336
+ });
337
+
338
+ const OPPOSITE_SIDE = {
339
+ top: "bottom",
340
+ right: "left",
341
+ bottom: "top",
342
+ left: "right"
343
+ };
344
+ const destylerPopperArrowProps = {
345
+ asChild: {
346
+ type: Boolean,
347
+ required: false,
348
+ default: false
349
+ },
350
+ width: {
351
+ type: Number,
352
+ required: false,
353
+ default: 5
354
+ },
355
+ height: {
356
+ type: Number,
357
+ required: false,
358
+ default: 10
359
+ },
360
+ as: {
361
+ type: [String, Object],
362
+ required: false,
363
+ default: "svg"
364
+ }
365
+ };
366
+ const DestylerPopperArrow = vue.defineComponent({
367
+ name: "DestylerPopperArrow",
368
+ inheritAttrs: false,
369
+ props: destylerPopperArrowProps,
370
+ setup() {
371
+ const contentContext = injectPopperContentContext();
372
+ const baseSide = vue.computed(() => OPPOSITE_SIDE[contentContext.placedSide.value]);
373
+ return {
374
+ contentContext,
375
+ baseSide
376
+ };
377
+ },
378
+ render() {
379
+ return vue.h("span", {
380
+ ref: (el) => {
381
+ this.contentContext.onArrowChange(el);
382
+ return void 0;
383
+ },
384
+ style: {
385
+ position: "absolute",
386
+ left: this.contentContext.arrowX?.value ? `${this.contentContext.arrowX?.value}px` : void 0,
387
+ top: this.contentContext.arrowY?.value ? `${this.contentContext.arrowY?.value}px` : void 0,
388
+ [this.baseSide]: 0,
389
+ transformOrigin: {
390
+ top: "",
391
+ right: "0 0",
392
+ bottom: "center 0",
393
+ left: "100% 0"
394
+ }[this.contentContext.placedSide.value],
395
+ transform: {
396
+ top: "translateY(100%)",
397
+ right: "translateY(50%) rotate(90deg) translateX(-50%)",
398
+ bottom: `rotate(180deg)`,
399
+ left: "translateY(50%) rotate(-90deg) translateX(50%)"
400
+ }[this.contentContext.placedSide.value],
401
+ visibility: this.contentContext.shouldHideArrow.value ? "hidden" : void 0
402
+ }
403
+ }, vue.h(arrow.DestylerArrow, vue.mergeProps(this.$attrs, {
404
+ as: this.$props.as,
405
+ asChild: this.$props.asChild,
406
+ width: this.$props.width,
407
+ height: this.$props.height,
408
+ style: {
409
+ display: "block"
410
+ }
411
+ }), this.$slots.default?.()));
412
+ }
413
+ });
414
+
415
+ exports.DestylerPopperAnchor = DestylerPopperAnchor;
416
+ exports.DestylerPopperArrow = DestylerPopperArrow;
417
+ exports.DestylerPopperContent = DestylerPopperContent;
418
+ exports.DestylerPopperRoot = DestylerPopperRoot;