@sproutsocial/seeds-react-menu 1.2.0 → 1.2.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sproutsocial/seeds-react-menu",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Seeds React Menu",
5
5
  "author": "Sprout Social, Inc.",
6
6
  "license": "MIT",
@@ -108,7 +108,7 @@ export const AutocompleteTextWithButtonExample: Story = {
108
108
  Choose a book
109
109
  </MenuLabel>
110
110
  <Box display="flex" alignItems="center" gap={200}>
111
- <AutocompleteInput />
111
+ <AutocompleteInput style={{ flex: 2 }} />
112
112
  <MenuToggleButton>
113
113
  <Icon name="chevron-down" aria-label="Open Menu" />
114
114
  </MenuToggleButton>
@@ -125,7 +125,9 @@ export interface TypeMenuProviderProps {
125
125
  }
126
126
 
127
127
  export interface TypeMenuToggleContext extends TypeMenuContext {
128
- ref?: (arg0: React.ElementRef<any> | HTMLElement) => void;
128
+ ref?:
129
+ | React.RefObject<any>
130
+ | ((arg0: React.ElementRef<any> | HTMLElement) => void);
129
131
  toggle?: () => void;
130
132
  show?: () => void;
131
133
  hide?: () => void;
@@ -1,6 +1,12 @@
1
- import React, { useCallback, useEffect } from "react";
1
+ import React, {
2
+ useCallback,
3
+ useEffect,
4
+ useLayoutEffect,
5
+ type RefObject,
6
+ } from "react";
2
7
  import styled from "styled-components";
3
8
  import { Popout, type TypePopoutProps } from "@sproutsocial/seeds-react-popout";
9
+ import { mergeRefs } from "@sproutsocial/seeds-react-utilities";
4
10
 
5
11
  import {
6
12
  MenuContentProvider,
@@ -111,7 +117,7 @@ export const MenuRoot = ({
111
117
  children,
112
118
  menuToggleElement,
113
119
  popoutProps: { placement = "bottom", focusLockProps, ...popoutProps } = {},
114
- width = "300px",
120
+ width,
115
121
  ...contextProps
116
122
  }: TypeMenuRootProps) => {
117
123
  const {
@@ -121,17 +127,27 @@ export const MenuRoot = ({
121
127
  inputValue,
122
128
  hiddenItemsCount = 0,
123
129
  items,
130
+ getMenuProps,
124
131
  } = contextProps;
125
- const setIsOpen = useCallback(
126
- (newIsOpen: boolean) => {
127
- newIsOpen ? openMenu?.() : closeMenu?.();
128
- },
129
- [openMenu, closeMenu]
130
- );
132
+
133
+ const toggleElementRef = React.useRef<HTMLElement>(null);
134
+ const [finalWidth, setFinalWidth] = React.useState<typeof width>();
135
+
136
+ const min_width = 200;
137
+
138
+ useLayoutEffect(() => {
139
+ if (width) {
140
+ setFinalWidth(width);
141
+ } else if (toggleElementRef.current?.offsetWidth) {
142
+ const toggleWidth = toggleElementRef.current.offsetWidth;
143
+ setFinalWidth(Math.max(toggleWidth, min_width));
144
+ } else {
145
+ setFinalWidth(min_width);
146
+ }
147
+ }, [width, toggleElementRef.current?.offsetWidth]);
131
148
 
132
149
  useEffect(() => {
133
150
  if (!inputValue) return;
134
-
135
151
  if (items && hiddenItemsCount >= items.length && isOpen) {
136
152
  closeMenu?.();
137
153
  }
@@ -139,6 +155,9 @@ export const MenuRoot = ({
139
155
 
140
156
  const menuContext = useMenu(contextProps);
141
157
 
158
+ // Hack to suppress error: "downshift: You forgot to call the getMenuProps getter function on your component / element."
159
+ getMenuProps?.({}, { suppressRefError: true });
160
+
142
161
  return (
143
162
  <MenuPopout
144
163
  placement={placement}
@@ -150,10 +169,20 @@ export const MenuRoot = ({
150
169
  {children}
151
170
  </MenuContentProvider>
152
171
  }
172
+ width={finalWidth}
153
173
  {...popoutProps}
154
174
  >
155
175
  {(toggleProps) => (
156
- <MenuToggleProvider menuContext={{ ...menuContext, ...toggleProps }}>
176
+ <MenuToggleProvider
177
+ menuContext={{
178
+ ...menuContext,
179
+ ...toggleProps,
180
+ ref: mergeRefs<HTMLElement>(
181
+ toggleProps.ref,
182
+ toggleElementRef
183
+ ) as RefObject<HTMLElement>,
184
+ }}
185
+ >
157
186
  {menuToggleElement}
158
187
  </MenuToggleProvider>
159
188
  )}
@@ -85,6 +85,9 @@ export const NestedMenu = <
85
85
  onIsOpenChange: onNestedIsOpenChange,
86
86
  };
87
87
 
88
+ // Hack to suppress error: "downshift: You forgot to call the getMenuProps getter function on your component / element."
89
+ rootMenuContextProps.getMenuProps?.({}, { suppressRefError: true });
90
+
88
91
  /**
89
92
  * Handling the state of the NestedMenu content
90
93
  */