@lumx/react 3.0.5 → 3.0.6-alpha.2

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
@@ -7,8 +7,8 @@
7
7
  },
8
8
  "dependencies": {
9
9
  "@juggle/resize-observer": "^3.2.0",
10
- "@lumx/core": "^3.0.5",
11
- "@lumx/icons": "^3.0.5",
10
+ "@lumx/core": "^3.0.6-alpha.2",
11
+ "@lumx/icons": "^3.0.6-alpha.2",
12
12
  "@popperjs/core": "^2.5.4",
13
13
  "body-scroll-lock": "^3.1.5",
14
14
  "classnames": "^2.2.6",
@@ -114,6 +114,6 @@
114
114
  "build:storybook": "cd storybook && ./build"
115
115
  },
116
116
  "sideEffects": false,
117
- "version": "3.0.5",
118
- "gitHead": "83eccabdc29dbf61fd519c88729002a5bb8d4790"
117
+ "version": "3.0.6-alpha.2",
118
+ "gitHead": "08b0d777dc906cf3a6816c9cb9e2d4b57ddaf8e1"
119
119
  }
@@ -2,7 +2,7 @@ import React, { forwardRef, ReactNode, SyntheticEvent, useRef } from 'react';
2
2
 
3
3
  import classNames from 'classnames';
4
4
 
5
- import { Dropdown, IconButtonProps, Offset, Placement, TextField, TextFieldProps } from '@lumx/react';
5
+ import { Dropdown, DropdownProps, IconButtonProps, Offset, Placement, TextField, TextFieldProps } from '@lumx/react';
6
6
 
7
7
  import { Comp, GenericProps, HasTheme } from '@lumx/react/utils/type';
8
8
  import { getRootClassName, handleBasicClasses } from '@lumx/react/utils/className';
@@ -41,10 +41,13 @@ export interface AutocompleteProps extends GenericProps, HasTheme {
41
41
  */
42
42
  placement?: Placement;
43
43
  /**
44
- * Whether the dropdown should fit to the anchor width or not.
44
+ * Manage dropdown width:
45
+ * - `maxWidth`: dropdown not bigger than anchor
46
+ * - `minWidth` or `true`: dropdown not smaller than anchor
47
+ * - `width`: dropdown equal to the anchor.
45
48
  * @see {@link DropdownProps#fitToAnchorWidth}
46
49
  */
47
- fitToAnchorWidth?: boolean;
50
+ fitToAnchorWidth?: DropdownProps['fitToAnchorWidth'];
48
51
  /**
49
52
  * The error related to the component.
50
53
  * @see {@link TextFieldProps#error}
@@ -160,7 +163,7 @@ export interface AutocompleteProps extends GenericProps, HasTheme {
160
163
  * Only the props not managed by the Autocomplete can be set.
161
164
  * @see {@link TextFieldProps}
162
165
  */
163
- textFieldProps?: TextFieldProps;
166
+ textFieldProps?: Partial<TextFieldProps>;
164
167
  }
165
168
 
166
169
  /**
@@ -34,10 +34,13 @@ export interface DropdownProps extends GenericProps {
34
34
  */
35
35
  closeOnEscape?: boolean;
36
36
  /**
37
- * Whether the dropdown should fit to the anchor width (if dropdown is smaller) or not.
37
+ * Manage dropdown width:
38
+ * - `maxWidth`: dropdown not bigger than anchor
39
+ * - `minWidth` or `true`: dropdown not smaller than anchor
40
+ * - `width`: dropdown equal to the anchor.
38
41
  * @see {@link PopoverProps#fitToAnchorWidth}
39
42
  */
40
- fitToAnchorWidth?: boolean;
43
+ fitToAnchorWidth?: PopoverProps['fitToAnchorWidth'];
41
44
  /**
42
45
  * Whether the dropdown should shrink to fit within the viewport height or not.
43
46
  * @see {@link PopoverProps#fitWithinViewportHeight}
@@ -314,3 +314,73 @@ export const NestedWithoutPortal = () => {
314
314
  </div>
315
315
  );
316
316
  };
317
+
318
+ export const FitToAnchorWidth = ({ theme }: any) => {
319
+ const demoPopperStyle = {
320
+ alignItems: 'center',
321
+ display: 'flex',
322
+ height: 100,
323
+ justifyContent: 'center',
324
+ width: 200,
325
+ };
326
+
327
+ const container = {
328
+ alignItems: 'center',
329
+ display: 'flex',
330
+ justifyContent: 'center',
331
+ flexDirection: 'column',
332
+ gap: 150,
333
+ marginTop: 150,
334
+ } as const;
335
+
336
+ const maxWidthAnchorRef = useRef(null);
337
+ const widthSmallAnchorRef = useRef(null);
338
+ const widthLargeAnchorRef = useRef(null);
339
+ const minWidthAnchorRef = useRef(null);
340
+ const defaultWidthAnchorRef = useRef(null);
341
+
342
+ return (
343
+ <div style={container}>
344
+ <div>
345
+ <Chip ref={maxWidthAnchorRef} theme={theme} size={Size.s}>
346
+ Anchor
347
+ </Chip>
348
+ </div>
349
+ <Popover theme={theme} anchorRef={maxWidthAnchorRef} fitToAnchorWidth="maxWidth" isOpen placement="top">
350
+ <div style={demoPopperStyle}>Popover maxWidth</div>
351
+ </Popover>
352
+ <div>
353
+ <Chip ref={widthSmallAnchorRef} theme={theme} size={Size.s}>
354
+ Anchor
355
+ </Chip>
356
+ </div>
357
+ <Popover theme={theme} anchorRef={widthSmallAnchorRef} fitToAnchorWidth="width" isOpen placement="top">
358
+ <div style={demoPopperStyle}>Popover width small anchor</div>
359
+ </Popover>
360
+ <div>
361
+ <Chip ref={widthLargeAnchorRef} theme={theme} size={Size.s}>
362
+ VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLargeAnchor
363
+ </Chip>
364
+ </div>
365
+ <Popover theme={theme} anchorRef={widthLargeAnchorRef} fitToAnchorWidth="width" isOpen placement="top">
366
+ <div style={demoPopperStyle}>Popover width large anchor</div>
367
+ </Popover>
368
+ <div>
369
+ <Chip ref={minWidthAnchorRef} theme={theme} size={Size.s}>
370
+ VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLargeAnchor
371
+ </Chip>
372
+ </div>
373
+ <Popover theme={theme} anchorRef={minWidthAnchorRef} fitToAnchorWidth="minWidth" isOpen placement="top">
374
+ <div style={demoPopperStyle}>Popover minWidth</div>
375
+ </Popover>
376
+ <div>
377
+ <Chip ref={defaultWidthAnchorRef} theme={theme} size={Size.s}>
378
+ VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLargeAnchor
379
+ </Chip>
380
+ </div>
381
+ <Popover theme={theme} anchorRef={defaultWidthAnchorRef} isOpen placement="top">
382
+ <div style={demoPopperStyle}>Popover default</div>
383
+ </Popover>
384
+ </div>
385
+ );
386
+ };
@@ -4,6 +4,7 @@ import { createPortal } from 'react-dom';
4
4
  import { usePopper } from 'react-popper';
5
5
 
6
6
  import classNames from 'classnames';
7
+ import memoize from 'lodash/memoize';
7
8
 
8
9
  import { DOCUMENT, WINDOW } from '@lumx/react/constants';
9
10
  import { useCallbackOnEscape } from '@lumx/react/hooks/useCallbackOnEscape';
@@ -62,6 +63,13 @@ export type Elevation = 1 | 2 | 3 | 4 | 5;
62
63
  */
63
64
  const ARROW_SIZE = 8;
64
65
 
66
+ const AnchorWidthOptions = {
67
+ MAX_WIDTH: 'maxWidth',
68
+ MIN_WIDTH: 'minWidth',
69
+ WIDTH: 'width',
70
+ } as const;
71
+ type AnchorWidthOption = ValueOf<typeof AnchorWidthOptions>;
72
+
65
73
  /**
66
74
  * Defines the props of the component.
67
75
  */
@@ -78,8 +86,13 @@ export interface PopoverProps extends GenericProps {
78
86
  closeOnEscape?: boolean;
79
87
  /** Shadow elevation. */
80
88
  elevation?: Elevation;
81
- /** Whether the dropdown should fit to the anchor width (if dropdown is smaller). */
82
- fitToAnchorWidth?: boolean;
89
+ /**
90
+ * Manage popover width:
91
+ * - `maxWidth`: popover not bigger than anchor
92
+ * - `minWidth` or `true`: popover not smaller than anchor
93
+ * - `width`: popover equal to the anchor.
94
+ */
95
+ fitToAnchorWidth?: AnchorWidthOption | boolean;
83
96
  /** Shrink popover if even after flipping there is not enough space. */
84
97
  fitWithinViewportHeight?: boolean;
85
98
  /** Element to focus when opening the popover. */
@@ -127,20 +140,20 @@ const DEFAULT_PROPS: Partial<PopoverProps> = {
127
140
  /**
128
141
  * Popper js modifier to fit popover min width to the anchor width.
129
142
  */
130
- const sameWidth = {
143
+ const sameWidth = memoize((anchorWidthOption: AnchorWidthOption) => ({
131
144
  name: 'sameWidth',
132
145
  enabled: true,
133
146
  phase: 'beforeWrite',
134
147
  requires: ['computeStyles'],
135
148
  fn({ state }: any) {
136
149
  // eslint-disable-next-line no-param-reassign
137
- state.styles.popper.minWidth = `${state.rects.reference.width}px`;
150
+ state.styles.popper[anchorWidthOption] = `${state.rects.reference.width}px`;
138
151
  },
139
152
  effect({ state }: any) {
140
153
  // eslint-disable-next-line no-param-reassign
141
- state.elements.popper.style.minWidth = `${state.elements.reference.offsetWidth}px`;
154
+ state.elements.popper.style[anchorWidthOption] = `${state.elements.reference.offsetWidth}px`;
142
155
  },
143
- };
156
+ }));
144
157
 
145
158
  /**
146
159
  * Popper js modifier to compute max size of the popover.
@@ -290,7 +303,9 @@ export const Popover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, re
290
303
  }
291
304
 
292
305
  if (fitToAnchorWidth) {
293
- modifiers.push(sameWidth);
306
+ const anchorWidthOption =
307
+ typeof fitToAnchorWidth === 'string' ? fitToAnchorWidth : AnchorWidthOptions.MIN_WIDTH;
308
+ modifiers.push(sameWidth(anchorWidthOption));
294
309
  }
295
310
  if (fitWithinViewportHeight) {
296
311
  modifiers.push({ ...maxSize, options: { boundary: boundaryRef?.current } }, applyMaxHeight);