@torpor/ui 1.3.2 → 1.3.3

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.
@@ -43,6 +43,10 @@ export function createPopoutContent(options: PopoutContentOptions): {
43
43
  } {
44
44
  let shown = false;
45
45
  let closeOnClick = false;
46
+ // Whether focus ever moved into the popout while it was shown. A
47
+ // mouse-opened popover never takes focus, so it must not yank focus back to
48
+ // its anchor when it hides.
49
+ let hadFocus = false;
46
50
 
47
51
  function setPosition() {
48
52
  const anchor = options.getAnchor();
@@ -83,12 +87,21 @@ export function createPopoutContent(options: PopoutContentOptions): {
83
87
  options.close(e);
84
88
  }
85
89
 
90
+ function handleDocumentFocusIn(e: FocusEvent) {
91
+ const content = options.getContent();
92
+ if (content && content.contains(e.target as Node)) {
93
+ hadFocus = true;
94
+ }
95
+ }
96
+
86
97
  function show() {
87
98
  shown = true;
88
99
 
89
100
  closeOnClick = false;
101
+ hadFocus = false;
90
102
  addDocumentEvent("mousedown", handleDocumentMouseDown);
91
103
  addDocumentEvent("click", handleDocumentClick);
104
+ addDocumentEvent("focusin", handleDocumentFocusIn);
92
105
  if (options.closeOnEscape) {
93
106
  addDocumentEvent("keydown", handleDocumentKeyDown);
94
107
  }
@@ -110,6 +123,7 @@ export function createPopoutContent(options: PopoutContentOptions): {
110
123
 
111
124
  removeDocumentEvent("mousedown", handleDocumentMouseDown);
112
125
  removeDocumentEvent("click", handleDocumentClick);
126
+ removeDocumentEvent("focusin", handleDocumentFocusIn);
113
127
  removeDocumentEvent("keydown", handleDocumentKeyDown);
114
128
  removeEventListener("resize", setPosition);
115
129
  const anchor = options.getAnchor();
@@ -117,17 +131,18 @@ export function createPopoutContent(options: PopoutContentOptions): {
117
131
  getScrollParent(anchor).removeEventListener("scroll", setPosition);
118
132
  }
119
133
 
120
- // Focus the anchor element again, per the WAI guidelines, unless focus
121
- // has already moved elsewhere (e.g. to an adjacent menubar item's menu).
122
- // If the focused element was removed with the content, focus has fallen
123
- // back to the body, and the anchor is focused again
134
+ // Focus the anchor element again, per the WAI guidelines, when the
135
+ // popout itself had focus. A mouse-opened hover popover never does, so
136
+ // closing it must not steal focus (and show a focus ring). Focus that
137
+ // has already moved elsewhere (e.g. to an adjacent menubar item's
138
+ // menu) is left alone; focus that fell back to the body when the
139
+ // focused content was removed still returns to the anchor.
124
140
  if (options.refocusAnchorOnHide) {
125
141
  const content = options.getContent();
126
- if (
127
- !content ||
128
- content.contains(document.activeElement) ||
129
- document.activeElement === document.body
130
- ) {
142
+ const active = document.activeElement;
143
+ const focusInContent = content !== undefined && active !== null && content.contains(active);
144
+ const focusFellBack = hadFocus && (active === null || active === document.body);
145
+ if (focusInContent || focusFellBack) {
131
146
  const el = options.getAnchor();
132
147
  if (el) {
133
148
  // The refocus is script-driven, not user intent: arm the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torpor/ui",
3
- "version": "1.3.2",
3
+ "version": "1.3.3",
4
4
  "description": "Accessible, unstyled components for use in your Torpor front-end",
5
5
  "type": "module",
6
6
  "torpor": true,