@samline/drawer 2.0.7 → 2.0.8

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.
@@ -29110,7 +29110,7 @@ function openAncestorChain(parentId) {
29110
29110
  openAncestorChain(nextParentId);
29111
29111
  }
29112
29112
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29113
- releaseHiddenFocusBeforeOpen(parentRuntime.options);
29113
+ releaseHiddenFocusBeforeOpen(parentRuntime.options, getRuntimeDrawerElement(parentRuntime));
29114
29114
  parentRuntime.controller.setOpen(true);
29115
29115
  notifyOpenStateChange(parentRuntime, true);
29116
29116
  renderVanillaDrawer(parentRuntime.id);
@@ -29125,18 +29125,36 @@ function cleanupRuntimeTrigger(runtime) {
29125
29125
  }
29126
29126
  function bindTriggerElement(runtime) {
29127
29127
  cleanupRuntimeTrigger(runtime);
29128
+ const drawerElement = getRuntimeDrawerElement(runtime);
29129
+ const builtInTriggerElement = getRuntimeTriggerElement(runtime);
29130
+ const cleanups = [];
29131
+ if (builtInTriggerElement) {
29132
+ const handleBuiltInTriggerClick = () => {
29133
+ releaseHiddenFocusBeforeOpen(runtime.options, drawerElement);
29134
+ };
29135
+ builtInTriggerElement.addEventListener("click", handleBuiltInTriggerClick);
29136
+ cleanups.push(() => {
29137
+ builtInTriggerElement.removeEventListener("click", handleBuiltInTriggerClick);
29138
+ });
29139
+ }
29128
29140
  if (!runtime.options.triggerElement) {
29141
+ runtime.cleanupTriggerElement = cleanups.length ? () => {
29142
+ cleanups.forEach((cleanup) => cleanup());
29143
+ } : null;
29129
29144
  return;
29130
29145
  }
29131
29146
  const triggerElement = runtime.options.triggerElement;
29132
29147
  const handleClick = () => {
29133
- releaseHiddenFocusBeforeOpen(runtime.options);
29148
+ releaseHiddenFocusBeforeOpen(runtime.options, drawerElement);
29134
29149
  runtime.controller.setOpen(true);
29135
29150
  renderVanillaDrawer(runtime.id);
29136
29151
  };
29137
29152
  triggerElement.addEventListener("click", handleClick);
29138
- runtime.cleanupTriggerElement = () => {
29153
+ cleanups.push(() => {
29139
29154
  triggerElement.removeEventListener("click", handleClick);
29155
+ });
29156
+ runtime.cleanupTriggerElement = () => {
29157
+ cleanups.forEach((cleanup) => cleanup());
29140
29158
  };
29141
29159
  }
29142
29160
  function notifyOpenStateChange(runtime, open) {
@@ -29166,15 +29184,32 @@ function notifyOpenStateChange(runtime, open) {
29166
29184
  function canUseDOM3() {
29167
29185
  return typeof window !== "undefined" && typeof document !== "undefined";
29168
29186
  }
29169
- function releaseHiddenFocusBeforeOpen(options) {
29187
+ function isElementInsideDrawer(element) {
29188
+ let currentElement = element;
29189
+ while (currentElement) {
29190
+ if (currentElement instanceof HTMLElement && currentElement.hasAttribute("data-drawer")) {
29191
+ return true;
29192
+ }
29193
+ currentElement = currentElement.parentElement;
29194
+ }
29195
+ return false;
29196
+ }
29197
+ function releaseHiddenFocusBeforeOpen(options, drawerElement) {
29170
29198
  if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29171
29199
  return;
29172
29200
  }
29173
29201
  const activeElement = document.activeElement;
29174
- if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29202
+ if (!activeElement || activeElement === document.body) {
29203
+ return;
29204
+ }
29205
+ const activeElementNode = activeElement;
29206
+ if (drawerElement?.contains(activeElementNode) || isElementInsideDrawer(activeElementNode)) {
29207
+ return;
29208
+ }
29209
+ if (typeof activeElementNode.blur !== "function") {
29175
29210
  return;
29176
29211
  }
29177
- activeElement.blur();
29212
+ activeElementNode.blur();
29178
29213
  }
29179
29214
  function getRuntimeDrawerElement(runtime) {
29180
29215
  if (!runtime.element) {
@@ -29182,6 +29217,12 @@ function getRuntimeDrawerElement(runtime) {
29182
29217
  }
29183
29218
  return runtime.element.querySelector("[data-drawer]");
29184
29219
  }
29220
+ function getRuntimeTriggerElement(runtime) {
29221
+ if (!runtime.element) {
29222
+ return null;
29223
+ }
29224
+ return runtime.element.querySelector("[data-drawer-vanilla-trigger]");
29225
+ }
29185
29226
  function getViewportSizeForDirection(direction) {
29186
29227
  if (!canUseDOM3()) {
29187
29228
  return 0;
@@ -29230,7 +29271,7 @@ function renderVanillaDrawer(id) {
29230
29271
  onOpenChange: (open) => {
29231
29272
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29232
29273
  if (open) {
29233
- releaseHiddenFocusBeforeOpen(runtime.options);
29274
+ releaseHiddenFocusBeforeOpen(runtime.options, getRuntimeDrawerElement(runtime));
29234
29275
  }
29235
29276
  runtime.controller.setOpen(open);
29236
29277
  if (previousOpen !== open) {
@@ -29279,7 +29320,7 @@ function buildVanillaController(id) {
29279
29320
  return createDrawer({ id, open }).getSnapshot();
29280
29321
  }
29281
29322
  if (open) {
29282
- releaseHiddenFocusBeforeOpen(runtime.options);
29323
+ releaseHiddenFocusBeforeOpen(runtime.options, getRuntimeDrawerElement(runtime));
29283
29324
  }
29284
29325
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29285
29326
  const snapshot = runtime.controller.setOpen(open);
@@ -29352,7 +29393,7 @@ function createDrawer(options = {}) {
29352
29393
  }
29353
29394
  }
29354
29395
  if (nextOptions.open && !previousOpen) {
29355
- releaseHiddenFocusBeforeOpen(nextOptions);
29396
+ releaseHiddenFocusBeforeOpen(nextOptions, existing ? getRuntimeDrawerElement(existing) : null);
29356
29397
  }
29357
29398
  renderVanillaDrawer(id);
29358
29399
  if (nextOptions.parentId && nextOptions.open) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samline/drawer",
3
- "version": "2.0.7",
3
+ "version": "2.0.8",
4
4
  "description": "A universal drawer package for React, Vue, Svelte, vanilla JS, and browser usage.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",