@lumx/react 3.0.5 → 3.0.6-alpha.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
@@ -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.1",
11
+ "@lumx/icons": "^3.0.6-alpha.1",
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.1",
118
+ "gitHead": "e230447f554272d8cd29c103842f961ec424c5f4"
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';
@@ -44,7 +44,7 @@ export interface AutocompleteProps extends GenericProps, HasTheme {
44
44
  * Whether the dropdown should fit to the anchor width or not.
45
45
  * @see {@link DropdownProps#fitToAnchorWidth}
46
46
  */
47
- fitToAnchorWidth?: boolean;
47
+ fitToAnchorWidth?: DropdownProps['fitToAnchorWidth'];
48
48
  /**
49
49
  * The error related to the component.
50
50
  * @see {@link TextFieldProps#error}
@@ -160,7 +160,7 @@ export interface AutocompleteProps extends GenericProps, HasTheme {
160
160
  * Only the props not managed by the Autocomplete can be set.
161
161
  * @see {@link TextFieldProps}
162
162
  */
163
- textFieldProps?: TextFieldProps;
163
+ textFieldProps?: Partial<TextFieldProps>;
164
164
  }
165
165
 
166
166
  /**
@@ -37,7 +37,7 @@ export interface DropdownProps extends GenericProps {
37
37
  * Whether the dropdown should fit to the anchor width (if dropdown is smaller) or not.
38
38
  * @see {@link PopoverProps#fitToAnchorWidth}
39
39
  */
40
- fitToAnchorWidth?: boolean;
40
+ fitToAnchorWidth?: PopoverProps['fitToAnchorWidth'];
41
41
  /**
42
42
  * Whether the dropdown should shrink to fit within the viewport height or not.
43
43
  * @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,8 @@ 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
+ /** manage popover size to not be bigger (`width` & `maxWidth`) or smaller (`width`, `minWidth`, `true`) than the anchor. */
90
+ fitToAnchorWidth?: AnchorWidthOption | boolean;
83
91
  /** Shrink popover if even after flipping there is not enough space. */
84
92
  fitWithinViewportHeight?: boolean;
85
93
  /** Element to focus when opening the popover. */
@@ -127,20 +135,20 @@ const DEFAULT_PROPS: Partial<PopoverProps> = {
127
135
  /**
128
136
  * Popper js modifier to fit popover min width to the anchor width.
129
137
  */
130
- const sameWidth = {
138
+ const sameWidth = memoize((anchorWidthOption: AnchorWidthOption) => ({
131
139
  name: 'sameWidth',
132
140
  enabled: true,
133
141
  phase: 'beforeWrite',
134
142
  requires: ['computeStyles'],
135
143
  fn({ state }: any) {
136
144
  // eslint-disable-next-line no-param-reassign
137
- state.styles.popper.minWidth = `${state.rects.reference.width}px`;
145
+ state.styles.popper[anchorWidthOption] = `${state.rects.reference.width}px`;
138
146
  },
139
147
  effect({ state }: any) {
140
148
  // eslint-disable-next-line no-param-reassign
141
- state.elements.popper.style.minWidth = `${state.elements.reference.offsetWidth}px`;
149
+ state.elements.popper.style[anchorWidthOption] = `${state.elements.reference.offsetWidth}px`;
142
150
  },
143
- };
151
+ }));
144
152
 
145
153
  /**
146
154
  * Popper js modifier to compute max size of the popover.
@@ -290,7 +298,9 @@ export const Popover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, re
290
298
  }
291
299
 
292
300
  if (fitToAnchorWidth) {
293
- modifiers.push(sameWidth);
301
+ const anchorWidthOption =
302
+ typeof fitToAnchorWidth === 'string' ? fitToAnchorWidth : AnchorWidthOptions.MIN_WIDTH;
303
+ modifiers.push(sameWidth(anchorWidthOption));
294
304
  }
295
305
  if (fitWithinViewportHeight) {
296
306
  modifiers.push({ ...maxSize, options: { boundary: boundaryRef?.current } }, applyMaxHeight);