@samline/drawer 2.0.5 → 2.0.6

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.
@@ -1450,6 +1450,7 @@ function getDragPermission({ targetTagName, hasNoDragAttribute, direction, timeS
1450
1450
  };
1451
1451
  }
1452
1452
 
1453
+ const useSafeLayoutEffect = typeof window === 'undefined' ? React__default.useEffect : React__default.useLayoutEffect;
1453
1454
  function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRelease: onReleaseProp, snapPoints, shouldScaleBackground = false, setBackgroundColorOnScale = true, closeThreshold = CLOSE_THRESHOLD, scrollLockTimeout = SCROLL_LOCK_TIMEOUT, dismissible = true, handleOnly = false, fadeFromIndex = snapPoints && snapPoints.length - 1, activeSnapPoint: activeSnapPointProp, setActiveSnapPoint: setActiveSnapPointProp, fixed, modal = true, onClose, nested, noBodyStyles = false, direction = 'bottom', defaultOpen = false, disablePreventScroll = true, snapToSequentialPoint = false, preventScrollRestoration = false, repositionInputs = true, onAnimationEnd, container, autoFocus = false }) {
1454
1455
  var _drawerRef_current, _drawerRef_current1;
1455
1456
  const animationEndTimeoutRef = React__default.useRef(null);
@@ -1557,19 +1558,23 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1557
1558
  noBodyStyles,
1558
1559
  autoFocus
1559
1560
  });
1560
- React__default.useEffect(()=>{
1561
+ useSafeLayoutEffect(()=>{
1561
1562
  var _drawerRef_current;
1562
1563
  if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1563
1564
  return;
1564
1565
  }
1565
1566
  const activeElement = document.activeElement;
1566
- if (!(activeElement instanceof HTMLElement)) {
1567
+ if (!activeElement || activeElement === document.body) {
1568
+ return;
1569
+ }
1570
+ const activeElementNode = activeElement;
1571
+ if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElementNode)) || (activeElementNode.closest == null ? void 0 : activeElementNode.closest.call(activeElementNode, '[data-drawer]'))) {
1567
1572
  return;
1568
1573
  }
1569
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElement)) || activeElement.closest('[data-drawer]')) {
1574
+ if (typeof activeElementNode.blur !== 'function') {
1570
1575
  return;
1571
1576
  }
1572
- activeElement.blur();
1577
+ activeElementNode.blur();
1573
1578
  }, [
1574
1579
  autoFocus,
1575
1580
  isOpen,
package/dist/vue/index.js CHANGED
@@ -27991,6 +27991,7 @@ function getDragPermission({
27991
27991
  }
27992
27992
 
27993
27993
  // src/react/components.tsx
27994
+ var useSafeLayoutEffect = typeof window === "undefined" ? import_react9.default.useEffect : import_react9.default.useLayoutEffect;
27994
27995
  function Root2({
27995
27996
  open: openProp,
27996
27997
  onOpenChange,
@@ -28134,18 +28135,22 @@ function Root2({
28134
28135
  noBodyStyles,
28135
28136
  autoFocus
28136
28137
  });
28137
- import_react9.default.useEffect(() => {
28138
+ useSafeLayoutEffect(() => {
28138
28139
  if (!isOpen || !modal || autoFocus || typeof document === "undefined") {
28139
28140
  return;
28140
28141
  }
28141
28142
  const activeElement = document.activeElement;
28142
- if (!(activeElement instanceof HTMLElement)) {
28143
+ if (!activeElement || activeElement === document.body) {
28144
+ return;
28145
+ }
28146
+ const activeElementNode = activeElement;
28147
+ if (drawerRef.current?.contains(activeElementNode) || activeElementNode.closest?.("[data-drawer]")) {
28143
28148
  return;
28144
28149
  }
28145
- if (drawerRef.current?.contains(activeElement) || activeElement.closest("[data-drawer]")) {
28150
+ if (typeof activeElementNode.blur !== "function") {
28146
28151
  return;
28147
28152
  }
28148
- activeElement.blur();
28153
+ activeElementNode.blur();
28149
28154
  }, [autoFocus, isOpen, modal]);
28150
28155
  function getScale() {
28151
28156
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -28883,7 +28888,7 @@ var VISUALLY_HIDDEN_STYLE = {
28883
28888
  whiteSpace: "nowrap",
28884
28889
  border: 0
28885
28890
  };
28886
- var useSafeLayoutEffect = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28891
+ var useSafeLayoutEffect2 = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28887
28892
  function toReactDrawerProps(options, open, onOpenChange, internalOnDragChange, internalOnReleaseChange) {
28888
28893
  const { id: _id, parentId: _parentId, onDragChange, onReleaseChange, ...drawerOptions } = options;
28889
28894
  const baseProps = {
@@ -28917,7 +28922,7 @@ function VanillaNode({
28917
28922
  dataAttribute
28918
28923
  }) {
28919
28924
  const ref = import_react10.default.useRef(null);
28920
- useSafeLayoutEffect(() => {
28925
+ useSafeLayoutEffect2(() => {
28921
28926
  const element = ref.current;
28922
28927
  if (!element) {
28923
28928
  return;
@@ -29123,6 +29128,7 @@ function openAncestorChain(parentId) {
29123
29128
  openAncestorChain(nextParentId);
29124
29129
  }
29125
29130
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29131
+ releaseHiddenFocusBeforeOpen(parentRuntime.options);
29126
29132
  parentRuntime.controller.setOpen(true);
29127
29133
  notifyOpenStateChange(parentRuntime, true);
29128
29134
  renderVanillaDrawer(parentRuntime.id);
@@ -29142,6 +29148,7 @@ function bindTriggerElement(runtime) {
29142
29148
  }
29143
29149
  const triggerElement = runtime.options.triggerElement;
29144
29150
  const handleClick = () => {
29151
+ releaseHiddenFocusBeforeOpen(runtime.options);
29145
29152
  runtime.controller.setOpen(true);
29146
29153
  renderVanillaDrawer(runtime.id);
29147
29154
  };
@@ -29177,6 +29184,16 @@ function notifyOpenStateChange(runtime, open) {
29177
29184
  function canUseDOM3() {
29178
29185
  return typeof window !== "undefined" && typeof document !== "undefined";
29179
29186
  }
29187
+ function releaseHiddenFocusBeforeOpen(options) {
29188
+ if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29189
+ return;
29190
+ }
29191
+ const activeElement = document.activeElement;
29192
+ if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29193
+ return;
29194
+ }
29195
+ activeElement.blur();
29196
+ }
29180
29197
  function getRuntimeDrawerElement(runtime) {
29181
29198
  if (!runtime.element) {
29182
29199
  return null;
@@ -29230,6 +29247,9 @@ function renderVanillaDrawer(id) {
29230
29247
  open: snapshot.state.isOpen,
29231
29248
  onOpenChange: (open) => {
29232
29249
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29250
+ if (open) {
29251
+ releaseHiddenFocusBeforeOpen(runtime.options);
29252
+ }
29233
29253
  runtime.controller.setOpen(open);
29234
29254
  if (previousOpen !== open) {
29235
29255
  notifyOpenStateChange(runtime, open);
@@ -29276,6 +29296,9 @@ function buildVanillaController(id) {
29276
29296
  if (!runtime) {
29277
29297
  return createDrawer({ id, open }).getSnapshot();
29278
29298
  }
29299
+ if (open) {
29300
+ releaseHiddenFocusBeforeOpen(runtime.options);
29301
+ }
29279
29302
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29280
29303
  const snapshot = runtime.controller.setOpen(open);
29281
29304
  if (previousOpen !== open) {
@@ -29346,6 +29369,9 @@ function createDrawer(options = {}) {
29346
29369
  notifyOpenStateChange(existing, snapshot.state.isOpen);
29347
29370
  }
29348
29371
  }
29372
+ if (nextOptions.open && !previousOpen) {
29373
+ releaseHiddenFocusBeforeOpen(nextOptions);
29374
+ }
29349
29375
  renderVanillaDrawer(id);
29350
29376
  if (nextOptions.parentId && nextOptions.open) {
29351
29377
  openAncestorChain(nextOptions.parentId);
@@ -27966,6 +27966,7 @@ function getDragPermission({
27966
27966
  }
27967
27967
 
27968
27968
  // src/react/components.tsx
27969
+ var useSafeLayoutEffect = typeof window === "undefined" ? import_react9.default.useEffect : import_react9.default.useLayoutEffect;
27969
27970
  function Root2({
27970
27971
  open: openProp,
27971
27972
  onOpenChange,
@@ -28109,18 +28110,22 @@ function Root2({
28109
28110
  noBodyStyles,
28110
28111
  autoFocus
28111
28112
  });
28112
- import_react9.default.useEffect(() => {
28113
+ useSafeLayoutEffect(() => {
28113
28114
  if (!isOpen || !modal || autoFocus || typeof document === "undefined") {
28114
28115
  return;
28115
28116
  }
28116
28117
  const activeElement = document.activeElement;
28117
- if (!(activeElement instanceof HTMLElement)) {
28118
+ if (!activeElement || activeElement === document.body) {
28119
+ return;
28120
+ }
28121
+ const activeElementNode = activeElement;
28122
+ if (drawerRef.current?.contains(activeElementNode) || activeElementNode.closest?.("[data-drawer]")) {
28118
28123
  return;
28119
28124
  }
28120
- if (drawerRef.current?.contains(activeElement) || activeElement.closest("[data-drawer]")) {
28125
+ if (typeof activeElementNode.blur !== "function") {
28121
28126
  return;
28122
28127
  }
28123
- activeElement.blur();
28128
+ activeElementNode.blur();
28124
28129
  }, [autoFocus, isOpen, modal]);
28125
28130
  function getScale() {
28126
28131
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -28858,7 +28863,7 @@ var VISUALLY_HIDDEN_STYLE = {
28858
28863
  whiteSpace: "nowrap",
28859
28864
  border: 0
28860
28865
  };
28861
- var useSafeLayoutEffect = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28866
+ var useSafeLayoutEffect2 = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28862
28867
  function toReactDrawerProps(options, open, onOpenChange, internalOnDragChange, internalOnReleaseChange) {
28863
28868
  const { id: _id, parentId: _parentId, onDragChange, onReleaseChange, ...drawerOptions } = options;
28864
28869
  const baseProps = {
@@ -28892,7 +28897,7 @@ function VanillaNode({
28892
28897
  dataAttribute
28893
28898
  }) {
28894
28899
  const ref = import_react10.default.useRef(null);
28895
- useSafeLayoutEffect(() => {
28900
+ useSafeLayoutEffect2(() => {
28896
28901
  const element = ref.current;
28897
28902
  if (!element) {
28898
28903
  return;
@@ -29098,6 +29103,7 @@ function openAncestorChain(parentId) {
29098
29103
  openAncestorChain(nextParentId);
29099
29104
  }
29100
29105
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29106
+ releaseHiddenFocusBeforeOpen(parentRuntime.options);
29101
29107
  parentRuntime.controller.setOpen(true);
29102
29108
  notifyOpenStateChange(parentRuntime, true);
29103
29109
  renderVanillaDrawer(parentRuntime.id);
@@ -29117,6 +29123,7 @@ function bindTriggerElement(runtime) {
29117
29123
  }
29118
29124
  const triggerElement = runtime.options.triggerElement;
29119
29125
  const handleClick = () => {
29126
+ releaseHiddenFocusBeforeOpen(runtime.options);
29120
29127
  runtime.controller.setOpen(true);
29121
29128
  renderVanillaDrawer(runtime.id);
29122
29129
  };
@@ -29152,6 +29159,16 @@ function notifyOpenStateChange(runtime, open) {
29152
29159
  function canUseDOM3() {
29153
29160
  return typeof window !== "undefined" && typeof document !== "undefined";
29154
29161
  }
29162
+ function releaseHiddenFocusBeforeOpen(options) {
29163
+ if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29164
+ return;
29165
+ }
29166
+ const activeElement = document.activeElement;
29167
+ if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29168
+ return;
29169
+ }
29170
+ activeElement.blur();
29171
+ }
29155
29172
  function getRuntimeDrawerElement(runtime) {
29156
29173
  if (!runtime.element) {
29157
29174
  return null;
@@ -29205,6 +29222,9 @@ function renderVanillaDrawer(id) {
29205
29222
  open: snapshot.state.isOpen,
29206
29223
  onOpenChange: (open) => {
29207
29224
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29225
+ if (open) {
29226
+ releaseHiddenFocusBeforeOpen(runtime.options);
29227
+ }
29208
29228
  runtime.controller.setOpen(open);
29209
29229
  if (previousOpen !== open) {
29210
29230
  notifyOpenStateChange(runtime, open);
@@ -29251,6 +29271,9 @@ function buildVanillaController(id) {
29251
29271
  if (!runtime) {
29252
29272
  return createDrawer({ id, open }).getSnapshot();
29253
29273
  }
29274
+ if (open) {
29275
+ releaseHiddenFocusBeforeOpen(runtime.options);
29276
+ }
29254
29277
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29255
29278
  const snapshot = runtime.controller.setOpen(open);
29256
29279
  if (previousOpen !== open) {
@@ -29321,6 +29344,9 @@ function createDrawer(options = {}) {
29321
29344
  notifyOpenStateChange(existing, snapshot.state.isOpen);
29322
29345
  }
29323
29346
  }
29347
+ if (nextOptions.open && !previousOpen) {
29348
+ releaseHiddenFocusBeforeOpen(nextOptions);
29349
+ }
29324
29350
  renderVanillaDrawer(id);
29325
29351
  if (nextOptions.parentId && nextOptions.open) {
29326
29352
  openAncestorChain(nextOptions.parentId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samline/drawer",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
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",