@lumx/react 2.2.11 → 2.2.12

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.
@@ -1,10 +1,10 @@
1
- import React from 'react';
1
+ import React, { Fragment } from 'react';
2
2
 
3
3
  import { mdiEmail } from '@lumx/icons';
4
- import { ColorPalette, FlexBox, Icon, IconSizes, Size } from '@lumx/react';
4
+ import { ColorPalette, ColorVariant, FlexBox, Icon, IconSizes, Size } from '@lumx/react';
5
5
  import { Orientation } from '..';
6
6
 
7
- export default { title: 'LumX components/icon/Icon' };
7
+ export default { title: 'LumX components/icon/Icon', parameters: { hasGreyBackground: true } };
8
8
 
9
9
  const iconSizes: Array<IconSizes | undefined> = [
10
10
  undefined,
@@ -16,42 +16,61 @@ const iconSizes: Array<IconSizes | undefined> = [
16
16
  Size.xl,
17
17
  Size.xxl,
18
18
  ];
19
- const iconColors = [undefined, ...Object.values(ColorPalette)];
20
- const iconShapes = [false, true];
21
19
 
22
- export const AllIcon = ({ theme }: any) => {
20
+ const TemplateSizeVariants = ({ hasShape, theme }: any) => (
21
+ <FlexBox orientation={Orientation.horizontal}>
22
+ {iconSizes.map((size) => (
23
+ <FlexBox fillSpace key={`${size}`}>
24
+ <h2>Size: {size || 'undefined'}</h2>
25
+ <Icon icon={mdiEmail} size={size} hasShape={hasShape} theme={theme} />
26
+ </FlexBox>
27
+ ))}
28
+ </FlexBox>
29
+ );
30
+
31
+ export const AllSize = ({ theme }: any) => TemplateSizeVariants({ theme });
32
+
33
+ export const AllSizeWithShape = ({ theme }: any) => TemplateSizeVariants({ theme, hasShape: true });
34
+
35
+ const TemplateColorVariants = ({ hasShape, theme }: any) => {
36
+ const withUndefined = (a: any) => [undefined].concat(Object.values(a));
23
37
  return (
24
- <>
25
- {iconShapes.map((hasShape) => {
26
- return (
27
- <>
28
- <h1>{`With shape: ${hasShape}`}</h1>
29
- {iconSizes.map((size) => {
30
- return (
38
+ <FlexBox orientation={Orientation.horizontal}>
39
+ {withUndefined(ColorPalette).map((color) => (
40
+ <FlexBox fillSpace key={`${color}`}>
41
+ <small key={`${color}`}>Color: {color || 'undefined'}</small>
42
+ <br />
43
+ {withUndefined(Object.values(ColorVariant).reverse()).map((colorVariant) => (
44
+ <Fragment key={`${colorVariant}`}>
45
+ {!colorVariant && (
31
46
  <>
32
- <h2>{`Size: ${size}`}</h2>
33
- <FlexBox orientation={Orientation.horizontal}>
34
- {iconColors.map((color) => {
35
- return (
36
- <FlexBox fillSpace key={color}>
37
- {`Color: ${color}`}
38
- <Icon
39
- hasShape={hasShape}
40
- icon={mdiEmail}
41
- color={color}
42
- size={size}
43
- theme={theme}
44
- />
45
- </FlexBox>
46
- );
47
- })}
48
- </FlexBox>
47
+ <small>No theme</small>
48
+ <Icon
49
+ icon={mdiEmail}
50
+ color={color}
51
+ colorVariant={colorVariant}
52
+ size="m"
53
+ hasShape={hasShape}
54
+ />
49
55
  </>
50
- );
51
- })}
52
- </>
53
- );
54
- })}
55
- </>
56
+ )}
57
+ <small>Variant: {colorVariant || 'undefined'}</small>
58
+ <Icon
59
+ icon={mdiEmail}
60
+ color={color}
61
+ colorVariant={colorVariant}
62
+ size="m"
63
+ theme={theme}
64
+ hasShape={hasShape}
65
+ />
66
+ </Fragment>
67
+ ))}
68
+ </FlexBox>
69
+ ))}
70
+ </FlexBox>
56
71
  );
57
72
  };
73
+
74
+ export const AllColors = ({ theme }: any) => TemplateColorVariants({ theme });
75
+
76
+ export const AllColorsWithShape = ({ theme }: any) => TemplateColorVariants({ theme, hasShape: true });
@@ -19,8 +19,8 @@ export interface IconProps extends GenericProps {
19
19
  /** Whether the icon has a shape. */
20
20
  hasShape?: boolean;
21
21
  /**
22
- * Icon (SVG path).draw code (`d` property of the `<path>` SVG element).
23
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths}
22
+ * Icon (SVG path) draw code (`d` property of the `<path>` SVG element).
23
+ * See https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths
24
24
  */
25
25
  icon: string;
26
26
  /** Size variant. */
@@ -56,35 +56,25 @@ const DEFAULT_PROPS: Partial<IconProps> = {};
56
56
  export const Icon: Comp<IconProps, HTMLElement> = forwardRef((props, ref) => {
57
57
  const { className, color, colorVariant, hasShape, icon, size, theme, alt, ...forwardedProps } = props;
58
58
 
59
- let iconColor;
60
- let iconColorVariant;
61
- if (color) {
62
- iconColor = color;
63
- iconColorVariant = colorVariant;
64
- } else if (theme) {
65
- iconColor = theme === Theme.light ? ColorPalette.dark : ColorPalette.light;
59
+ // Color
60
+ let iconColor = color;
61
+ if (!iconColor && (hasShape || theme)) {
62
+ iconColor = theme === Theme.dark ? ColorPalette.light : ColorPalette.dark;
63
+ }
66
64
 
67
- if (colorVariant) {
68
- iconColorVariant = colorVariant;
69
- } else {
70
- iconColorVariant = Theme.light ? 'L1' : 'N';
71
- }
72
- } else if (hasShape) {
73
- iconColor = ColorPalette.dark;
65
+ // Color variant
66
+ let iconColorVariant = colorVariant;
67
+ if (!iconColorVariant && hasShape && iconColor === ColorPalette.dark) {
68
+ iconColorVariant = 'L2';
74
69
  }
75
70
 
76
- let iconSize;
77
- if (size) {
78
- if (hasShape) {
79
- if (size === Size.xxs || size === Size.xs) {
80
- iconSize = Size.s;
81
- } else if (size === Size.xxl) {
82
- iconSize = Size.xl;
83
- } else {
84
- iconSize = size;
85
- }
86
- } else {
87
- iconSize = size;
71
+ // Size
72
+ let iconSize = size;
73
+ if (size && hasShape) {
74
+ if (size === Size.xxs || size === Size.xs) {
75
+ iconSize = Size.s;
76
+ } else if (size === Size.xxl) {
77
+ iconSize = Size.xl;
88
78
  }
89
79
  } else if (hasShape) {
90
80
  iconSize = Size.m;
@@ -101,6 +91,7 @@ export const Icon: Comp<IconProps, HTMLElement> = forwardRef((props, ref) => {
101
91
  colorVariant: iconColorVariant,
102
92
  hasShape,
103
93
  prefix: CLASSNAME,
94
+ theme,
104
95
  size: iconSize,
105
96
  }),
106
97
  !hasShape && `${CLASSNAME}--no-shape`,
@@ -21,6 +21,8 @@ export interface ListProps extends GenericProps {
21
21
  isClickable?: boolean;
22
22
  /** Item padding size. */
23
23
  itemPadding?: Extract<Size, 'big' | 'huge'>;
24
+ /** Tab index of the list. Default to -1 */
25
+ tabIndex?: number;
24
26
  /**
25
27
  * On list item selected callback.
26
28
  *
@@ -41,6 +43,13 @@ const COMPONENT_NAME = 'List';
41
43
  */
42
44
  const CLASSNAME = getRootClassName(COMPONENT_NAME);
43
45
 
46
+ /**
47
+ * Component default props.
48
+ */
49
+ const DEFAULT_PROPS: Partial<ListProps> = {
50
+ tabIndex: -1,
51
+ };
52
+
44
53
  /* eslint-disable jsx-a11y/no-noninteractive-tabindex */
45
54
  /**
46
55
  * List component.
@@ -50,7 +59,7 @@ const CLASSNAME = getRootClassName(COMPONENT_NAME);
50
59
  * @return React element.
51
60
  */
52
61
  const InternalList: Comp<ListProps, HTMLUListElement> = forwardRef((props, ref) => {
53
- const { children, className, isClickable, itemPadding, onListItemSelected, ...forwardedProps } = props;
62
+ const { children, className, isClickable, itemPadding, onListItemSelected, tabIndex, ...forwardedProps } = props;
54
63
  const listElementRef = useRef<HTMLUListElement>(null);
55
64
 
56
65
  const { items, hasClickableItem } = useInteractiveList({
@@ -70,7 +79,7 @@ const InternalList: Comp<ListProps, HTMLUListElement> = forwardRef((props, ref)
70
79
  itemPadding: itemPadding ?? (clickable ? Size.big : undefined),
71
80
  }),
72
81
  )}
73
- tabIndex={clickable ? 0 : -1}
82
+ tabIndex={tabIndex}
74
83
  ref={mergeRefs(ref, listElementRef)}
75
84
  >
76
85
  {items}
@@ -79,5 +88,6 @@ const InternalList: Comp<ListProps, HTMLUListElement> = forwardRef((props, ref)
79
88
  });
80
89
  InternalList.displayName = COMPONENT_NAME;
81
90
  InternalList.className = CLASSNAME;
91
+ InternalList.defaultProps = DEFAULT_PROPS;
82
92
 
83
93
  export const List = Object.assign(InternalList, { useKeyboardListNavigation });
@@ -3,7 +3,7 @@
3
3
  exports[`<List> Snapshots and structure should render story 'AsLink' 1`] = `
4
4
  <ul
5
5
  className="lumx-list lumx-list--item-padding-big"
6
- tabIndex={0}
6
+ tabIndex={-1}
7
7
  >
8
8
  <ListItem
9
9
  before={
@@ -62,7 +62,7 @@ exports[`<List> Snapshots and structure should render story 'AsLink' 1`] = `
62
62
  exports[`<List> Snapshots and structure should render story 'KeyboardNavigation' 1`] = `
63
63
  <ul
64
64
  className="lumx-list lumx-list--item-padding-big"
65
- tabIndex={0}
65
+ tabIndex={-1}
66
66
  >
67
67
  <ListItem
68
68
  isHighlighted={false}
@@ -193,7 +193,7 @@ exports[`<List> Snapshots and structure should render story 'WithItemPadding' 1`
193
193
  Array [
194
194
  <ul
195
195
  className="lumx-list lumx-list--item-padding-big"
196
- tabIndex={0}
196
+ tabIndex={-1}
197
197
  >
198
198
  <ListItem
199
199
  className="lumx-color-background-dark-L6"
@@ -248,7 +248,7 @@ Array [
248
248
  </ul>,
249
249
  <ul
250
250
  className="lumx-list lumx-list--item-padding-big"
251
- tabIndex={0}
251
+ tabIndex={-1}
252
252
  >
253
253
  <ListItem
254
254
  className="lumx-color-background-dark-L6"
@@ -303,7 +303,7 @@ Array [
303
303
  </ul>,
304
304
  <ul
305
305
  className="lumx-list lumx-list--item-padding-huge"
306
- tabIndex={0}
306
+ tabIndex={-1}
307
307
  >
308
308
  <ListItem
309
309
  className="lumx-color-background-dark-L6"
@@ -12,6 +12,7 @@ import { ClickAwayProvider } from '@lumx/react/utils/ClickAwayProvider';
12
12
 
13
13
  import { Comp, GenericProps, getRootClassName, handleBasicClasses, ValueOf } from '@lumx/react/utils';
14
14
  import { mergeRefs } from '@lumx/react/utils/mergeRefs';
15
+ import { useFocusWithin } from '@lumx/react/hooks/useFocusWithin';
15
16
 
16
17
  /**
17
18
  * Different possible placements for the popover.
@@ -81,6 +82,8 @@ export interface PopoverProps extends GenericProps {
81
82
  fitWithinViewportHeight?: boolean;
82
83
  /** Element to focus when opening the popover. */
83
84
  focusElement?: RefObject<HTMLElement>;
85
+ /** Whether the focus should go back on the anchor when popover closes and focus is within. */
86
+ focusAnchorOnClose?: boolean;
84
87
  /** Whether we put an arrow or not. */
85
88
  hasArrow?: boolean;
86
89
  /** Whether the popover is open or not. */
@@ -213,6 +216,7 @@ export const Popover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, re
213
216
  style,
214
217
  usePortal,
215
218
  zIndex,
219
+ focusAnchorOnClose = true,
216
220
  ...forwardedProps
217
221
  } = props;
218
222
  // eslint-disable-next-line react-hooks/rules-of-hooks
@@ -222,6 +226,42 @@ export const Popover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, re
222
226
  // eslint-disable-next-line react-hooks/rules-of-hooks
223
227
  const clickAwayRef = useRef<HTMLDivElement>(null);
224
228
 
229
+ /**
230
+ * Track whether the focus is currently set in the
231
+ * popover.
232
+ * */
233
+ // eslint-disable-next-line react-hooks/rules-of-hooks
234
+ const isFocusedWithin = useRef(false);
235
+
236
+ // eslint-disable-next-line react-hooks/rules-of-hooks
237
+ useFocusWithin({
238
+ element: popperElement || null,
239
+ onFocusIn: () => {
240
+ isFocusedWithin.current = true;
241
+ },
242
+ onFocusOut: () => {
243
+ isFocusedWithin.current = false;
244
+ },
245
+ });
246
+
247
+ /** Action on close */
248
+ const handleClose = () => {
249
+ if (!onClose) {
250
+ return;
251
+ }
252
+
253
+ /**
254
+ * If the focus is currently within the popover
255
+ * when the popover closes, reset the focus back to the anchor element
256
+ * unless specifically requested not to.
257
+ */
258
+ if (isFocusedWithin.current && focusAnchorOnClose) {
259
+ anchorRef.current?.focus();
260
+ }
261
+
262
+ onClose();
263
+ };
264
+
225
265
  const modifiers: any = [];
226
266
  const actualOffset: [number, number] = [offset?.along ?? 0, (offset?.away ?? 0) + (hasArrow ? ARROW_SIZE : 0)];
227
267
  modifiers.push({
@@ -267,7 +307,7 @@ export const Popover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, re
267
307
  }, [style, styles.popper, zIndex, fitWithinViewportHeight]);
268
308
 
269
309
  // eslint-disable-next-line react-hooks/rules-of-hooks
270
- useCallbackOnEscape(onClose, isOpen && closeOnEscape);
310
+ useCallbackOnEscape(handleClose, isOpen && closeOnEscape);
271
311
  // eslint-disable-next-line react-hooks/rules-of-hooks
272
312
  useFocus(focusElement?.current, isOpen && (state?.rects?.popper?.y ?? -1) >= 0);
273
313
 
@@ -286,7 +326,7 @@ export const Popover: Comp<PopoverProps, HTMLDivElement> = forwardRef((props, re
286
326
  style={popoverStyle}
287
327
  {...attributes.popper}
288
328
  >
289
- <ClickAwayProvider callback={closeOnClickAway && onClose} refs={clickAwayRefs}>
329
+ <ClickAwayProvider callback={closeOnClickAway && handleClose} refs={clickAwayRefs}>
290
330
  {hasArrow && <div ref={setArrowElement} className={`${CLASSNAME}__arrow`} style={styles.arrow} />}
291
331
  {children}
292
332
  </ClickAwayProvider>
@@ -29,6 +29,7 @@ exports[`<Select> Snapshots and structure should render correctly 1`] = `
29
29
  closeOnEscape={true}
30
30
  fitToAnchorWidth={true}
31
31
  fitWithinViewportHeight={true}
32
+ focusAnchorOnClose={true}
32
33
  isOpen={false}
33
34
  onClose={[Function]}
34
35
  placement="bottom-start"
@@ -30,6 +30,7 @@ exports[`<SelectMultiple> Snapshots and structure should render chips 1`] = `
30
30
  closeOnEscape={true}
31
31
  fitToAnchorWidth={true}
32
32
  fitWithinViewportHeight={true}
33
+ focusAnchorOnClose={true}
33
34
  isOpen={false}
34
35
  onClose={[Function]}
35
36
  placement="bottom-start"
@@ -72,6 +73,7 @@ exports[`<SelectMultiple> Snapshots and structure should render defaults 1`] = `
72
73
  closeOnEscape={true}
73
74
  fitToAnchorWidth={true}
74
75
  fitWithinViewportHeight={true}
76
+ focusAnchorOnClose={true}
75
77
  isOpen={false}
76
78
  onClose={[Function]}
77
79
  placement="bottom-start"
@@ -103,7 +103,6 @@ export function useTooltipOpen(delay: number | undefined, anchorElement: HTMLEle
103
103
  for (const [node, eventType, evenHandler] of events) {
104
104
  node.removeEventListener(eventType, evenHandler);
105
105
  }
106
- closeImmediately();
107
106
  };
108
107
  }, [anchorElement, delay]);
109
108
 
package/types.d.ts CHANGED
@@ -938,6 +938,8 @@ export interface PopoverProps extends GenericProps {
938
938
  fitWithinViewportHeight?: boolean;
939
939
  /** Element to focus when opening the popover. */
940
940
  focusElement?: RefObject<HTMLElement>;
941
+ /** Whether the focus should go back on the anchor when popover closes and focus is within. */
942
+ focusAnchorOnClose?: boolean;
941
943
  /** Whether we put an arrow or not. */
942
944
  hasArrow?: boolean;
943
945
  /** Whether the popover is open or not. */
@@ -1012,6 +1014,8 @@ export interface DropdownProps extends GenericProps {
1012
1014
  placement?: Placement;
1013
1015
  /** Whether the focus should be set on the list when the dropdown is open or not. */
1014
1016
  shouldFocusOnOpen?: boolean;
1017
+ /** Whether the focus should go back on the anchor when dropdown closes and focus is within. */
1018
+ focusAnchorOnClose?: boolean;
1015
1019
  /**
1016
1020
  * Z-axis position.
1017
1021
  * @see {@link PopoverProps#zIndex}
@@ -1171,8 +1175,8 @@ export interface IconProps extends GenericProps {
1171
1175
  /** Whether the icon has a shape. */
1172
1176
  hasShape?: boolean;
1173
1177
  /**
1174
- * Icon (SVG path).draw code (`d` property of the `<path>` SVG element).
1175
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths}
1178
+ * Icon (SVG path) draw code (`d` property of the `<path>` SVG element).
1179
+ * See https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths
1176
1180
  */
1177
1181
  icon: string;
1178
1182
  /** Size variant. */
@@ -1474,6 +1478,8 @@ export interface ListProps extends GenericProps {
1474
1478
  isClickable?: boolean;
1475
1479
  /** Item padding size. */
1476
1480
  itemPadding?: Extract<Size, "big" | "huge">;
1481
+ /** Tab index of the list. Default to -1 */
1482
+ tabIndex?: number;
1477
1483
  /**
1478
1484
  * On list item selected callback.
1479
1485
  *