@zag-js/focus-visible 1.45.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,21 +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,
153
+ writable: true,
141
154
  value: patchedFocus2
142
155
  });
143
- return nativeFocus;
156
+ return { focus: nativeFocus, descriptor };
144
157
  } catch {
145
- return void 0;
158
+ return { focus: void 0 };
146
159
  }
147
160
  }
148
161
  function setupGlobalFocusEvents(root) {
@@ -151,7 +164,7 @@ function setupGlobalFocusEvents(root) {
151
164
  }
152
165
  const win = (0, import_dom_query.getWindow)(root);
153
166
  const doc = (0, import_dom_query.getDocument)(root);
154
- const nativeFocus = patchFocusMethod(win);
167
+ const focusData = patchFocusMethod(win);
155
168
  doc.addEventListener("keydown", handleKeyboardEvent, true);
156
169
  doc.addEventListener("keyup", handleKeyboardEvent, true);
157
170
  doc.addEventListener("click", handleClickEvent, true);
@@ -173,7 +186,7 @@ function setupGlobalFocusEvents(root) {
173
186
  },
174
187
  { once: true }
175
188
  );
176
- listenerMap.set(win, { focus: nativeFocus });
189
+ listenerMap.set(win, focusData);
177
190
  }
178
191
  var tearDownWindowFocusTracking = (root, loadListener) => {
179
192
  const win = (0, import_dom_query.getWindow)(root);
@@ -185,12 +198,13 @@ var tearDownWindowFocusTracking = (root, loadListener) => {
185
198
  if (!listenerData) {
186
199
  return;
187
200
  }
188
- if (listenerData.focus) {
201
+ if (listenerData.focus || listenerData.descriptor) {
189
202
  try {
190
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
191
- configurable: true,
192
- value: listenerData.focus
193
- });
203
+ Object.defineProperty(
204
+ win.HTMLElement.prototype,
205
+ "focus",
206
+ listenerData.descriptor ?? { configurable: true, writable: true, value: listenerData.focus }
207
+ );
194
208
  } catch {
195
209
  }
196
210
  }
package/dist/index.mjs CHANGED
@@ -98,21 +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,
123
+ writable: true,
111
124
  value: patchedFocus2
112
125
  });
113
- return nativeFocus;
126
+ return { focus: nativeFocus, descriptor };
114
127
  } catch {
115
- return void 0;
128
+ return { focus: void 0 };
116
129
  }
117
130
  }
118
131
  function setupGlobalFocusEvents(root) {
@@ -121,7 +134,7 @@ function setupGlobalFocusEvents(root) {
121
134
  }
122
135
  const win = getWindow(root);
123
136
  const doc = getDocument(root);
124
- const nativeFocus = patchFocusMethod(win);
137
+ const focusData = patchFocusMethod(win);
125
138
  doc.addEventListener("keydown", handleKeyboardEvent, true);
126
139
  doc.addEventListener("keyup", handleKeyboardEvent, true);
127
140
  doc.addEventListener("click", handleClickEvent, true);
@@ -143,7 +156,7 @@ function setupGlobalFocusEvents(root) {
143
156
  },
144
157
  { once: true }
145
158
  );
146
- listenerMap.set(win, { focus: nativeFocus });
159
+ listenerMap.set(win, focusData);
147
160
  }
148
161
  var tearDownWindowFocusTracking = (root, loadListener) => {
149
162
  const win = getWindow(root);
@@ -155,12 +168,13 @@ var tearDownWindowFocusTracking = (root, loadListener) => {
155
168
  if (!listenerData) {
156
169
  return;
157
170
  }
158
- if (listenerData.focus) {
171
+ if (listenerData.focus || listenerData.descriptor) {
159
172
  try {
160
- Object.defineProperty(win.HTMLElement.prototype, "focus", {
161
- configurable: true,
162
- value: listenerData.focus
163
- });
173
+ Object.defineProperty(
174
+ win.HTMLElement.prototype,
175
+ "focus",
176
+ listenerData.descriptor ?? { configurable: true, writable: true, value: listenerData.focus }
177
+ );
164
178
  } catch {
165
179
  }
166
180
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/focus-visible",
3
- "version": "1.45.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.45.0"
31
+ "@zag-js/dom-query": "1.47.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "clean-package": "2.2.0"