@nicolassancho/storybook-core 4.6.0 → 4.7.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.
@@ -15,6 +15,7 @@ export interface PopoverProps {
15
15
  * Where the panel appears. `bottom-start` and `bottom-end` open a dropdown under the trigger,
16
16
  * aligned to its left or right edge. `drawer` slides a full-height panel in from the left edge of
17
17
  * the screen, with a dimmed backdrop. The drawer is not modal: keyboard focus can still leave it.
18
+ * Like a dropdown, it closes when focus reaches an element outside it and the trigger.
18
19
  */
19
20
  placement?: "bottom-start" | "bottom-end" | "drawer";
20
21
  /**
@@ -36,7 +37,7 @@ export interface PopoverProps {
36
37
  * an outside click and on Esc, returning focus to the button, and drawing the panel above the page.
37
38
  *
38
39
  * This is a client component. It adds what the native popover lacks: `aria-expanded` on the button,
39
- * closing when a link inside the panel is clicked or a form inside it is submitted, and placing a
40
- * dropdown under its button. Render it from a Server Component; every prop is serializable.
40
+ * closing when a link inside the panel is clicked, a form inside it is submitted or focus reaches
41
+ * an element outside the panel and the button, and placing a dropdown under its button. Render it from a Server Component; every prop is serializable.
41
42
  */
42
43
  export declare const Popover: React.FC<PopoverProps>;
@@ -21,8 +21,8 @@ const GAP = 4;
21
21
  * an outside click and on Esc, returning focus to the button, and drawing the panel above the page.
22
22
  *
23
23
  * This is a client component. It adds what the native popover lacks: `aria-expanded` on the button,
24
- * closing when a link inside the panel is clicked or a form inside it is submitted, and placing a
25
- * dropdown under its button. Render it from a Server Component; every prop is serializable.
24
+ * closing when a link inside the panel is clicked, a form inside it is submitted or focus reaches
25
+ * an element outside the panel and the button, and placing a dropdown under its button. Render it from a Server Component; every prop is serializable.
26
26
  */
27
27
  export const Popover = ({ trigger, triggerLabel, variant = "outline", size = "medium", placement = "bottom-start", closeLabel = "Close menu", children, className, }) => {
28
28
  const panelId = useId();
@@ -57,6 +57,25 @@ export const Popover = ({ trigger, triggerLabel, variant = "outline", size = "me
57
57
  window.removeEventListener("scroll", place, true);
58
58
  };
59
59
  }, [open, place]);
60
+ // A panel left open can hide the next element the keyboard reaches. This listens for `focusin`
61
+ // because it fires only when an element receives focus. Safari does not focus a clicked button, so
62
+ // a `focusout` check on `relatedTarget` could close the panel while focus is still inside it. The
63
+ // trigger counts as inside: it toggles the panel, so Shift+Tab onto it keeps the panel open.
64
+ useEffect(() => {
65
+ if (!open)
66
+ return;
67
+ const closeOnFocusOutside = (event) => {
68
+ var _a, _b, _c;
69
+ const target = event.target;
70
+ if (!(target instanceof Node))
71
+ return;
72
+ if (((_a = panelRef.current) === null || _a === void 0 ? void 0 : _a.contains(target)) || ((_b = triggerRef.current) === null || _b === void 0 ? void 0 : _b.contains(target)))
73
+ return;
74
+ (_c = panelRef.current) === null || _c === void 0 ? void 0 : _c.hidePopover();
75
+ };
76
+ document.addEventListener("focusin", closeOnFocusOutside);
77
+ return () => document.removeEventListener("focusin", closeOnFocusOutside);
78
+ }, [open]);
60
79
  const closeOnNavigation = (event) => {
61
80
  var _a;
62
81
  if (event.target instanceof Element && event.target.closest("a[href]")) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nicolassancho/storybook-core",
3
- "version": "4.6.0",
3
+ "version": "4.7.0",
4
4
  "description": "Accessible React + Tailwind component library built with tailwind-variants and documented in Storybook.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",