@destyler/popover 0.2.10 → 0.2.12

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.
package/dist/index.d.mts CHANGED
@@ -1,12 +1,9 @@
1
- import * as _destyler_anatomy0 from "@destyler/anatomy";
2
1
  import { Placement, PositioningOptions } from "@destyler/popper";
3
2
  import { DismissableElementHandlers, FocusOutsideEvent, InteractOutsideEvent, PersistentElementOptions, PointerDownOutsideEvent } from "@destyler/dismissable";
4
- import * as _destyler_xstate0 from "@destyler/xstate";
5
3
  import { AnyEventObject, Machine, XSend, XState } from "@destyler/xstate";
6
4
  import { CommonProperties, DirectionProperty, NormalizeProps, PropTypes, RequiredBy } from "@destyler/types";
7
-
8
5
  //#region src/anatomy.d.ts
9
- declare const anatomy: _destyler_anatomy0.AnatomyInstance<"arrow" | "arrowTip" | "anchor" | "trigger" | "indicator" | "positioner" | "content" | "title" | "description" | "closeTrigger">;
6
+ export declare const anatomy: import("@destyler/anatomy").AnatomyInstance<"arrow" | "arrowTip" | "anchor" | "trigger" | "indicator" | "positioner" | "content" | "title" | "description" | "closeTrigger">;
10
7
  //#endregion
11
8
  //#region src/types.d.ts
12
9
  interface OpenChangeDetails {
@@ -94,7 +91,7 @@ type ComputedContext = Readonly<{
94
91
  interface PrivateContext {
95
92
  /**
96
93
  * @internal
97
- * The elements that are rendered on mount
94
+ * The elements that are rendered on startup and opening
98
95
  */
99
96
  renderedElements: {
100
97
  title: boolean;
@@ -143,13 +140,13 @@ interface MachineApi<T extends PropTypes = PropTypes> {
143
140
  }
144
141
  //#endregion
145
142
  //#region src/connect.d.ts
146
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
143
+ export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
147
144
  //#endregion
148
145
  //#region src/machine.d.ts
149
- declare function machine(userContext: UserDefinedContext): _destyler_xstate0.Machine<MachineContext, MachineState, _destyler_xstate0.AnyEventObject>;
146
+ export declare function machine(userContext: UserDefinedContext): import("@destyler/xstate").Machine<MachineContext, MachineState, import("@destyler/xstate").AnyEventObject>;
150
147
  //#endregion
151
148
  //#region src/props.d.ts
152
- declare const props: ("open" | "ids" | "modal" | "portalled" | "autoFocus" | "initialFocusEl" | "closeOnInteractOutside" | "closeOnEscape" | "onOpenChange" | "positioning" | "defaultOpen" | "id" | "getRootNode" | "dir" | "onEscapeKeyDown" | "onPointerDownOutside" | "onFocusOutside" | "onInteractOutside" | "persistentElements")[];
153
- declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "open" | "ids" | "modal" | "portalled" | "autoFocus" | "initialFocusEl" | "closeOnInteractOutside" | "closeOnEscape" | "onOpenChange" | "positioning" | "defaultOpen" | "id" | "getRootNode" | "dir" | "onEscapeKeyDown" | "onPointerDownOutside" | "onFocusOutside" | "onInteractOutside" | "persistentElements">];
149
+ export declare const props: ("open" | "ids" | "modal" | "portalled" | "autoFocus" | "initialFocusEl" | "closeOnInteractOutside" | "closeOnEscape" | "onOpenChange" | "positioning" | "defaultOpen" | "id" | "getRootNode" | "dir" | "onEscapeKeyDown" | "onPointerDownOutside" | "onFocusOutside" | "onInteractOutside" | "persistentElements")[];
150
+ export declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "open" | "ids" | "modal" | "portalled" | "autoFocus" | "initialFocusEl" | "closeOnInteractOutside" | "closeOnEscape" | "onOpenChange" | "positioning" | "defaultOpen" | "id" | "getRootNode" | "dir" | "onEscapeKeyDown" | "onPointerDownOutside" | "onFocusOutside" | "onInteractOutside" | "persistentElements">];
154
151
  //#endregion
155
- export { type MachineApi as Api, type UserDefinedContext as Context, type ElementIds, type FocusOutsideEvent, type InteractOutsideEvent, type OpenChangeDetails, type Placement, type PointerDownOutsideEvent, type PositioningOptions, type Service, anatomy, connect, machine, props, splitProps };
152
+ export type { MachineApi as Api, UserDefinedContext as Context, ElementIds, FocusOutsideEvent, InteractOutsideEvent, OpenChangeDetails, Placement, PointerDownOutsideEvent, PositioningOptions, Service };
package/dist/index.mjs CHANGED
@@ -6,13 +6,11 @@ import { trackDismissableElement } from "@destyler/dismissable";
6
6
  import { trapFocus } from "@destyler/focus-trap";
7
7
  import { preventBodyScroll } from "@destyler/remove-scroll";
8
8
  import { compact, createSplitProps, isControlled, resolveControllableOpen, withControllableProvided } from "@destyler/utils";
9
- import { createMachine } from "@destyler/xstate";
9
+ import { ActionTypes, createMachine } from "@destyler/xstate";
10
10
  import { createProps } from "@destyler/types";
11
-
12
11
  //#region src/anatomy.ts
13
12
  const anatomy = createAnatomy("popover").parts("arrow", "arrowTip", "anchor", "trigger", "indicator", "positioner", "content", "title", "description", "closeTrigger");
14
13
  const parts = anatomy.build();
15
-
16
14
  //#endregion
17
15
  //#region src/dom.ts
18
16
  const dom = createScope({
@@ -33,7 +31,6 @@ const dom = createScope({
33
31
  getFocusableEls: (ctx) => getFocusables(dom.getContentEl(ctx)),
34
32
  getFirstFocusableEl: (ctx) => dom.getFocusableEls(ctx)[0]
35
33
  });
36
-
37
34
  //#endregion
38
35
  //#region src/connect.ts
39
36
  function connect(state, send, normalize) {
@@ -165,12 +162,12 @@ function connect(state, send, normalize) {
165
162
  }
166
163
  };
167
164
  }
168
-
169
165
  //#endregion
170
166
  //#region src/machine.ts
171
167
  function machine(userContext) {
172
168
  const ctx = compact(withControllableProvided(userContext, ["open"]));
173
169
  const { initialOpen } = resolveControllableOpen(ctx);
170
+ const renderedElementChecks = /* @__PURE__ */ new WeakMap();
174
171
  return createMachine({
175
172
  id: "popover",
176
173
  initial: initialOpen ? "open" : "closed",
@@ -191,32 +188,37 @@ function machine(userContext) {
191
188
  description: true
192
189
  }
193
190
  },
194
- computed: { currentPortalled: (ctx$1) => !!ctx$1.modal || !!ctx$1.portalled },
191
+ computed: { currentPortalled: (ctx) => !!ctx.modal || !!ctx.portalled },
195
192
  watch: { open: ["toggleVisibility"] },
193
+ activities: ["trackRenderedElements"],
196
194
  entry: ["checkRenderedElements"],
197
195
  states: {
198
- closed: { on: {
199
- "CONTROLLED.OPEN": {
200
- target: "open",
201
- actions: ["setInitialFocus"]
202
- },
203
- "TOGGLE": [{
204
- guard: "isOpenControlled",
205
- actions: ["invokeOnOpen"]
206
- }, {
207
- target: "open",
208
- actions: ["invokeOnOpen", "setInitialFocus"]
209
- }],
210
- "OPEN": [{
211
- guard: "isOpenControlled",
212
- actions: ["invokeOnOpen"]
213
- }, {
214
- target: "open",
215
- actions: ["invokeOnOpen", "setInitialFocus"]
216
- }]
217
- } },
196
+ closed: {
197
+ activities: ["trackRenderedElements"],
198
+ on: {
199
+ "CONTROLLED.OPEN": {
200
+ target: "open",
201
+ actions: ["setInitialFocus"]
202
+ },
203
+ "TOGGLE": [{
204
+ guard: "isOpenControlled",
205
+ actions: ["invokeOnOpen"]
206
+ }, {
207
+ target: "open",
208
+ actions: ["invokeOnOpen", "setInitialFocus"]
209
+ }],
210
+ "OPEN": [{
211
+ guard: "isOpenControlled",
212
+ actions: ["invokeOnOpen"]
213
+ }, {
214
+ target: "open",
215
+ actions: ["invokeOnOpen", "setInitialFocus"]
216
+ }]
217
+ }
218
+ },
218
219
  open: {
219
220
  activities: [
221
+ "trackRenderedElements",
220
222
  "trapFocus",
221
223
  "preventScroll",
222
224
  "hideContentBelow",
@@ -248,41 +250,50 @@ function machine(userContext) {
248
250
  }
249
251
  }
250
252
  }, {
251
- guards: { isOpenControlled: (ctx$1) => isControlled(ctx$1, "open") },
253
+ guards: { isOpenControlled: (ctx) => isControlled(ctx, "open") },
252
254
  activities: {
253
- trackPositioning(ctx$1) {
254
- ctx$1.currentPlacement = ctx$1.positioning.placement;
255
- const anchorEl = dom.getAnchorEl(ctx$1) ?? dom.getTriggerEl(ctx$1);
256
- const getPositionerEl = () => dom.getPositionerEl(ctx$1);
255
+ trackRenderedElements(ctx, evt, meta) {
256
+ if (evt.type === ActionTypes.Start) renderedElementChecks.set(ctx, {});
257
+ const checks = renderedElementChecks.get(ctx);
258
+ if (meta.state.matches("open") && evt.type !== ActionTypes.Init) meta.getAction("checkRenderedElements")?.(ctx, evt, meta);
259
+ return () => {
260
+ checks.cancel?.();
261
+ delete checks.cancel;
262
+ };
263
+ },
264
+ trackPositioning(ctx) {
265
+ ctx.currentPlacement = ctx.positioning.placement;
266
+ const anchorEl = dom.getAnchorEl(ctx) ?? dom.getTriggerEl(ctx);
267
+ const getPositionerEl = () => dom.getPositionerEl(ctx);
257
268
  return getPlacement(anchorEl, getPositionerEl, {
258
- ...ctx$1.positioning,
269
+ ...ctx.positioning,
259
270
  defer: true,
260
271
  onComplete(data) {
261
- ctx$1.currentPlacement = data.placement;
272
+ ctx.currentPlacement = data.placement;
262
273
  }
263
274
  });
264
275
  },
265
- trackDismissableElement(ctx$1, _evt, { send }) {
266
- const getContentEl = () => dom.getContentEl(ctx$1);
276
+ trackDismissableElement(ctx, _evt, { send }) {
277
+ const getContentEl = () => dom.getContentEl(ctx);
267
278
  let restoreFocus = true;
268
279
  return trackDismissableElement(getContentEl, {
269
- pointerBlocking: ctx$1.modal,
270
- exclude: dom.getTriggerEl(ctx$1),
280
+ pointerBlocking: ctx.modal,
281
+ exclude: dom.getTriggerEl(ctx),
271
282
  defer: true,
272
283
  onEscapeKeyDown(event) {
273
- ctx$1.onEscapeKeyDown?.(event);
274
- if (ctx$1.closeOnEscape) return;
284
+ ctx.onEscapeKeyDown?.(event);
285
+ if (ctx.closeOnEscape) return;
275
286
  event.preventDefault();
276
287
  },
277
288
  onInteractOutside(event) {
278
- ctx$1.onInteractOutside?.(event);
289
+ ctx.onInteractOutside?.(event);
279
290
  if (event.defaultPrevented) return;
280
291
  restoreFocus = !(event.detail.focusable || event.detail.contextmenu);
281
- if (!ctx$1.closeOnInteractOutside) event.preventDefault();
292
+ if (!ctx.closeOnInteractOutside) event.preventDefault();
282
293
  },
283
- onPointerDownOutside: ctx$1.onPointerDownOutside,
284
- onFocusOutside: ctx$1.onFocusOutside,
285
- persistentElements: ctx$1.persistentElements,
294
+ onPointerDownOutside: ctx.onPointerDownOutside,
295
+ onFocusOutside: ctx.onFocusOutside,
296
+ persistentElements: ctx.persistentElements,
286
297
  onDismiss() {
287
298
  send({
288
299
  type: "CLOSE",
@@ -292,91 +303,94 @@ function machine(userContext) {
292
303
  }
293
304
  });
294
305
  },
295
- proxyTabFocus(ctx$1) {
296
- if (ctx$1.modal || !ctx$1.portalled) return;
297
- const getContentEl = () => dom.getContentEl(ctx$1);
306
+ proxyTabFocus(ctx) {
307
+ if (ctx.modal || !ctx.portalled) return;
308
+ const getContentEl = () => dom.getContentEl(ctx);
298
309
  return proxyTabFocus(getContentEl, {
299
- triggerElement: dom.getTriggerEl(ctx$1),
310
+ triggerElement: dom.getTriggerEl(ctx),
300
311
  defer: true,
301
312
  onFocus(el) {
302
313
  el.focus({ preventScroll: true });
303
314
  }
304
315
  });
305
316
  },
306
- hideContentBelow(ctx$1) {
307
- if (!ctx$1.modal) return;
308
- const getElements = () => [dom.getContentEl(ctx$1), dom.getTriggerEl(ctx$1)];
317
+ hideContentBelow(ctx) {
318
+ if (!ctx.modal) return;
319
+ const getElements = () => [dom.getContentEl(ctx), dom.getTriggerEl(ctx)];
309
320
  return ariaHidden(getElements, { defer: true });
310
321
  },
311
- preventScroll(ctx$1) {
312
- if (!ctx$1.modal) return;
313
- return preventBodyScroll(dom.getDoc(ctx$1));
322
+ preventScroll(ctx) {
323
+ if (!ctx.modal) return;
324
+ return preventBodyScroll(dom.getDoc(ctx));
314
325
  },
315
- trapFocus(ctx$1) {
316
- if (!ctx$1.modal) return;
317
- const contentEl = () => dom.getContentEl(ctx$1);
326
+ trapFocus(ctx) {
327
+ if (!ctx.modal) return;
328
+ const contentEl = () => dom.getContentEl(ctx);
318
329
  return trapFocus(contentEl, { initialFocus: () => getInitialFocus({
319
- root: dom.getContentEl(ctx$1),
320
- getInitialEl: ctx$1.initialFocusEl,
321
- enabled: ctx$1.autoFocus
330
+ root: dom.getContentEl(ctx),
331
+ getInitialEl: ctx.initialFocusEl,
332
+ enabled: ctx.autoFocus
322
333
  }) });
323
334
  }
324
335
  },
325
336
  actions: {
326
- reposition(ctx$1, evt) {
327
- const anchorEl = dom.getAnchorEl(ctx$1) ?? dom.getTriggerEl(ctx$1);
328
- const getPositionerEl = () => dom.getPositionerEl(ctx$1);
337
+ reposition(ctx, evt) {
338
+ const anchorEl = dom.getAnchorEl(ctx) ?? dom.getTriggerEl(ctx);
339
+ const getPositionerEl = () => dom.getPositionerEl(ctx);
329
340
  getPlacement(anchorEl, getPositionerEl, {
330
- ...ctx$1.positioning,
341
+ ...ctx.positioning,
331
342
  ...evt.options,
332
343
  defer: true,
333
344
  listeners: false,
334
345
  onComplete(data) {
335
- ctx$1.currentPlacement = data.placement;
346
+ ctx.currentPlacement = data.placement;
336
347
  }
337
348
  });
338
349
  },
339
- checkRenderedElements(ctx$1) {
340
- raf(() => {
341
- Object.assign(ctx$1.renderedElements, {
342
- title: !!dom.getTitleEl(ctx$1),
343
- description: !!dom.getDescriptionEl(ctx$1)
350
+ checkRenderedElements(ctx) {
351
+ const checks = renderedElementChecks.get(ctx) ?? {};
352
+ renderedElementChecks.set(ctx, checks);
353
+ checks.cancel?.();
354
+ checks.cancel = raf(() => {
355
+ delete checks.cancel;
356
+ Object.assign(ctx.renderedElements, {
357
+ title: !!dom.getTitleEl(ctx),
358
+ description: !!dom.getDescriptionEl(ctx)
344
359
  });
345
360
  });
346
361
  },
347
- setInitialFocus(ctx$1) {
348
- if (ctx$1.modal) return;
362
+ setInitialFocus(ctx) {
363
+ if (ctx.modal) return;
349
364
  raf(() => {
350
365
  getInitialFocus({
351
- root: dom.getContentEl(ctx$1),
352
- getInitialEl: ctx$1.initialFocusEl,
353
- enabled: ctx$1.autoFocus
366
+ root: dom.getContentEl(ctx),
367
+ getInitialEl: ctx.initialFocusEl,
368
+ enabled: ctx.autoFocus
354
369
  })?.focus({ preventScroll: true });
355
370
  });
356
371
  },
357
- setFinalFocus(ctx$1, evt) {
372
+ setFinalFocus(ctx, evt) {
358
373
  const restoreFocus = evt.restoreFocus ?? evt.previousEvent?.restoreFocus;
359
374
  if (restoreFocus != null && !restoreFocus) return;
360
375
  raf(() => {
361
- dom.getTriggerEl(ctx$1)?.focus({ preventScroll: true });
376
+ dom.getTriggerEl(ctx)?.focus({ preventScroll: true });
362
377
  });
363
378
  },
364
- invokeOnOpen(ctx$1) {
365
- ctx$1.onOpenChange?.({ open: true });
379
+ invokeOnOpen(ctx) {
380
+ ctx.onOpenChange?.({ open: true });
366
381
  },
367
- invokeOnClose(ctx$1) {
368
- ctx$1.onOpenChange?.({ open: false });
382
+ invokeOnClose(ctx) {
383
+ ctx.onOpenChange?.({ open: false });
369
384
  },
370
- toggleVisibility(ctx$1, evt, { send }) {
385
+ toggleVisibility(ctx, evt, { send }) {
371
386
  send({
372
- type: ctx$1.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE",
387
+ type: ctx.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE",
373
388
  previousEvent: evt
374
389
  });
375
390
  }
376
391
  }
377
392
  });
378
393
  }
379
-
380
394
  //#endregion
381
395
  //#region src/props.ts
382
396
  const props = createProps()([
@@ -401,6 +415,5 @@ const props = createProps()([
401
415
  "positioning"
402
416
  ]);
403
417
  const splitProps = createSplitProps(props);
404
-
405
418
  //#endregion
406
- export { anatomy, connect, machine, props, splitProps };
419
+ export { anatomy, connect, machine, props, splitProps };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@destyler/popover",
3
3
  "type": "module",
4
- "version": "0.2.10",
4
+ "version": "0.2.12",
5
5
  "description": "displays rich content in a portal, triggered by a button.",
6
6
  "author": "Elone Hoo <elonehoo@gmail.com>",
7
7
  "license": "MIT",
@@ -22,16 +22,16 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@destyler/anatomy": "0.2.10",
26
- "@destyler/aria-hidden": "0.2.10",
27
- "@destyler/dom": "0.2.10",
28
- "@destyler/dismissable": "0.2.10",
29
- "@destyler/popper": "0.2.10",
30
- "@destyler/remove-scroll": "0.2.10",
31
- "@destyler/types": "0.2.10",
32
- "@destyler/xstate": "0.2.10",
33
- "@destyler/utils": "0.2.10",
34
- "@destyler/focus-trap": "0.2.10"
25
+ "@destyler/anatomy": "0.2.12",
26
+ "@destyler/aria-hidden": "0.2.12",
27
+ "@destyler/dismissable": "0.2.12",
28
+ "@destyler/dom": "0.2.12",
29
+ "@destyler/focus-trap": "0.2.12",
30
+ "@destyler/popper": "0.2.12",
31
+ "@destyler/remove-scroll": "0.2.12",
32
+ "@destyler/types": "0.2.12",
33
+ "@destyler/utils": "0.2.12",
34
+ "@destyler/xstate": "0.2.12"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"