@zag-js/focus-visible 1.46.0 → 1.47.0

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,14 +1,13 @@
1
1
  type Modality = "keyboard" | "pointer" | "virtual";
2
2
  type RootNode = Document | ShadowRoot | Node;
3
3
  interface GlobalListenerData {
4
- /** Unset when the prototype's `focus` could not be read, so there is nothing to restore. */
4
+ /** Unset when the prototype's `focus` could not be read */
5
5
  focus: VoidFunction | undefined;
6
+ /** The unreadable accessor to restore */
7
+ descriptor?: PropertyDescriptor | undefined;
6
8
  }
7
9
  declare let listenerMap: Map<Window, GlobalListenerData>;
8
- /**
9
- * When true, the next focus event will be ignored. Used by preventFocus() to avoid
10
- * focus rings when programmatically reverting focus.
11
- */
10
+ /** Skip the next focus event, so preventFocus() doesn't show focus rings */
12
11
  declare let ignoreFocusEvent: boolean;
13
12
  declare function getInteractionModality(): Modality | null;
14
13
  declare function setInteractionModality(modality: Modality): void;
package/dist/index.d.ts CHANGED
@@ -1,14 +1,13 @@
1
1
  type Modality = "keyboard" | "pointer" | "virtual";
2
2
  type RootNode = Document | ShadowRoot | Node;
3
3
  interface GlobalListenerData {
4
- /** Unset when the prototype's `focus` could not be read, so there is nothing to restore. */
4
+ /** Unset when the prototype's `focus` could not be read */
5
5
  focus: VoidFunction | undefined;
6
+ /** The unreadable accessor to restore */
7
+ descriptor?: PropertyDescriptor | undefined;
6
8
  }
7
9
  declare let listenerMap: Map<Window, GlobalListenerData>;
8
- /**
9
- * When true, the next focus event will be ignored. Used by preventFocus() to avoid
10
- * focus rings when programmatically reverting focus.
11
- */
10
+ /** Skip the next focus event, so preventFocus() doesn't show focus rings */
12
11
  declare let ignoreFocusEvent: boolean;
13
12
  declare function getInteractionModality(): Modality | null;
14
13
  declare function setInteractionModality(modality: Modality): void;
package/dist/index.js CHANGED
@@ -128,22 +128,34 @@ function handleWindowBlur() {
128
128
  pendingLabelControl = null;
129
129
  hasBlurredWindowRecently = true;
130
130
  }
131
+ function readPrototypeFocus(proto) {
132
+ try {
133
+ return proto.focus;
134
+ } catch {
135
+ return void 0;
136
+ }
137
+ }
131
138
  function patchFocusMethod(win) {
139
+ const proto = win.HTMLElement.prototype;
132
140
  try {
133
141
  let patchedFocus2 = function() {
134
142
  hasEventBeforeFocus = true;
135
- nativeFocus.apply(this, arguments);
143
+ const focus = nativeFocus ?? getFocus.call(this);
144
+ focus.apply(this, arguments);
136
145
  };
137
146
  var patchedFocus = patchedFocus2;
138
- const nativeFocus = win.HTMLElement.prototype.focus;
147
+ const nativeFocus = readPrototypeFocus(proto);
148
+ const descriptor = nativeFocus ? void 0 : Object.getOwnPropertyDescriptor(proto, "focus");
149
+ const getFocus = descriptor?.get;
150
+ if (!nativeFocus && !getFocus) return { focus: void 0 };
139
151
  Object.defineProperty(win.HTMLElement.prototype, "focus", {
140
152
  configurable: true,
141
153
  writable: true,
142
154
  value: patchedFocus2
143
155
  });
144
- return nativeFocus;
156
+ return { focus: nativeFocus, descriptor };
145
157
  } catch {
146
- return void 0;
158
+ return { focus: void 0 };
147
159
  }
148
160
  }
149
161
  function setupGlobalFocusEvents(root) {
@@ -152,7 +164,7 @@ function setupGlobalFocusEvents(root) {
152
164
  }
153
165
  const win = (0, import_dom_query.getWindow)(root);
154
166
  const doc = (0, import_dom_query.getDocument)(root);
155
- const nativeFocus = patchFocusMethod(win);
167
+ const focusData = patchFocusMethod(win);
156
168
  doc.addEventListener("keydown", handleKeyboardEvent, true);
157
169
  doc.addEventListener("keyup", handleKeyboardEvent, true);
158
170
  doc.addEventListener("click", handleClickEvent, true);
@@ -174,7 +186,7 @@ function setupGlobalFocusEvents(root) {
174
186
  },
175
187
  { once: true }
176
188
  );
177
- listenerMap.set(win, { focus: nativeFocus });
189
+ listenerMap.set(win, focusData);
178
190
  }
179
191
  var tearDownWindowFocusTracking = (root, loadListener) => {
180
192
  const win = (0, import_dom_query.getWindow)(root);
@@ -186,13 +198,13 @@ var tearDownWindowFocusTracking = (root, loadListener) => {
186
198
  if (!listenerData) {
187
199
  return;
188
200
  }
189
- if (listenerData.focus) {
201
+ if (listenerData.focus || listenerData.descriptor) {
190
202
  try {
191
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
192
- configurable: true,
193
- writable: true,
194
- value: listenerData.focus
195
- });
203
+ Object.defineProperty(
204
+ win.HTMLElement.prototype,
205
+ "focus",
206
+ listenerData.descriptor ?? { configurable: true, writable: true, value: listenerData.focus }
207
+ );
196
208
  } catch {
197
209
  }
198
210
  }
package/dist/index.mjs CHANGED
@@ -98,22 +98,34 @@ function handleWindowBlur() {
98
98
  pendingLabelControl = null;
99
99
  hasBlurredWindowRecently = true;
100
100
  }
101
+ function readPrototypeFocus(proto) {
102
+ try {
103
+ return proto.focus;
104
+ } catch {
105
+ return void 0;
106
+ }
107
+ }
101
108
  function patchFocusMethod(win) {
109
+ const proto = win.HTMLElement.prototype;
102
110
  try {
103
111
  let patchedFocus2 = function() {
104
112
  hasEventBeforeFocus = true;
105
- nativeFocus.apply(this, arguments);
113
+ const focus = nativeFocus ?? getFocus.call(this);
114
+ focus.apply(this, arguments);
106
115
  };
107
116
  var patchedFocus = patchedFocus2;
108
- const nativeFocus = win.HTMLElement.prototype.focus;
117
+ const nativeFocus = readPrototypeFocus(proto);
118
+ const descriptor = nativeFocus ? void 0 : Object.getOwnPropertyDescriptor(proto, "focus");
119
+ const getFocus = descriptor?.get;
120
+ if (!nativeFocus && !getFocus) return { focus: void 0 };
109
121
  Object.defineProperty(win.HTMLElement.prototype, "focus", {
110
122
  configurable: true,
111
123
  writable: true,
112
124
  value: patchedFocus2
113
125
  });
114
- return nativeFocus;
126
+ return { focus: nativeFocus, descriptor };
115
127
  } catch {
116
- return void 0;
128
+ return { focus: void 0 };
117
129
  }
118
130
  }
119
131
  function setupGlobalFocusEvents(root) {
@@ -122,7 +134,7 @@ function setupGlobalFocusEvents(root) {
122
134
  }
123
135
  const win = getWindow(root);
124
136
  const doc = getDocument(root);
125
- const nativeFocus = patchFocusMethod(win);
137
+ const focusData = patchFocusMethod(win);
126
138
  doc.addEventListener("keydown", handleKeyboardEvent, true);
127
139
  doc.addEventListener("keyup", handleKeyboardEvent, true);
128
140
  doc.addEventListener("click", handleClickEvent, true);
@@ -144,7 +156,7 @@ function setupGlobalFocusEvents(root) {
144
156
  },
145
157
  { once: true }
146
158
  );
147
- listenerMap.set(win, { focus: nativeFocus });
159
+ listenerMap.set(win, focusData);
148
160
  }
149
161
  var tearDownWindowFocusTracking = (root, loadListener) => {
150
162
  const win = getWindow(root);
@@ -156,13 +168,13 @@ var tearDownWindowFocusTracking = (root, loadListener) => {
156
168
  if (!listenerData) {
157
169
  return;
158
170
  }
159
- if (listenerData.focus) {
171
+ if (listenerData.focus || listenerData.descriptor) {
160
172
  try {
161
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
162
- configurable: true,
163
- writable: true,
164
- value: listenerData.focus
165
- });
173
+ Object.defineProperty(
174
+ win.HTMLElement.prototype,
175
+ "focus",
176
+ listenerData.descriptor ?? { configurable: true, writable: true, value: listenerData.focus }
177
+ );
166
178
  } catch {
167
179
  }
168
180
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/focus-visible",
3
- "version": "1.46.0",
3
+ "version": "1.47.0",
4
4
  "description": "Focus visible polyfill utility based on WICG",
5
5
  "keywords": [
6
6
  "js",
@@ -28,7 +28,7 @@
28
28
  "clean-package": "../../../clean-package.config.json",
29
29
  "main": "dist/index.js",
30
30
  "dependencies": {
31
- "@zag-js/dom-query": "1.46.0"
31
+ "@zag-js/dom-query": "1.47.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "clean-package": "2.2.0"