@swan-io/lake 15.4.0 → 15.5.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swan-io/lake",
3
- "version": "15.4.0",
3
+ "version": "15.5.0",
4
4
  "engines": {
5
5
  "node": ">22.12.0"
6
6
  },
@@ -126,7 +126,7 @@ export const BreadcrumbsRoot = ({ rootLevelCrumbs = emptyCrumbArray, children })
126
126
  }
127
127
  setRootCrumbs(rootLevelCrumbs.map(crumb => ({ id: uuid(), crumb, isRootCrumb: true })));
128
128
  }, [rootLevelCrumbs]);
129
- const orderedCrumbs = useMemo(() => [...crumbs].sort((a, b) => { var _a, _b; return (((_a = b.index) !== null && _a !== void 0 ? _a : -1) > ((_b = a.index) !== null && _b !== void 0 ? _b : -1) ? -1 : 1); }), [crumbs]);
129
+ const orderedCrumbs = useMemo(() => crumbs.toSorted((a, b) => { var _a, _b; return (((_a = b.index) !== null && _a !== void 0 ? _a : -1) > ((_b = a.index) !== null && _b !== void 0 ? _b : -1) ? -1 : 1); }), [crumbs]);
130
130
  const value = useMemo(() => [rootCrumbs, orderedCrumbs, setCrumbs, currentIndexRef], [rootCrumbs, orderedCrumbs]);
131
131
  return _jsx(BreadcrumbsContext.Provider, { value: value, children: children });
132
132
  };
@@ -20,8 +20,6 @@ const styles = StyleSheet.create({
20
20
  boxShadow: shadows.tile,
21
21
  },
22
22
  anchored: {
23
- borderTopLeftRadius: 0,
24
- borderTopRightRadius: 0,
25
23
  borderLeftWidth: 0.5,
26
24
  marginHorizontal: -1,
27
25
  marginBottom: -1,
@@ -87,8 +85,11 @@ export const LakeAlert = ({ anchored = false, variant, title, subtitle, children
87
85
  .exhaustive();
88
86
  return (_jsxs(View, { style: [
89
87
  styles.base,
90
- { backgroundColor: alertBackground[variant], borderColor: alertBorder[variant] },
88
+ {
89
+ backgroundColor: alertBackground[variant],
90
+ borderColor: alertBorder[variant],
91
+ },
91
92
  anchored ? styles.anchored : { borderLeftColor: alertLeftBorder[variant] },
92
93
  style,
93
- ], children: [_jsxs(Box, { direction: "row", alignItems: "center", children: [icon != null ? (_jsxs(_Fragment, { children: [_jsx(Icon, { name: icon, color: color, size: 20 }), _jsx(Space, { width: 20 })] })) : null, _jsxs(View, { style: commonStyles.fill, children: [_jsxs(Box, { direction: "row", justifyContent: "spaceBetween", children: [_jsx(LakeText, { color: color, variant: icon != null ? "regular" : "medium", children: title }), tag && _jsx(Tag, { color: translateColorTag(), children: tag })] }), isNotNullishOrEmpty(subtitle) && _jsx(LakeText, { color: color, children: subtitle })] }), isNotNullish(callToAction) && _jsx(View, { style: styles.callToAction, children: callToAction })] }), isNotNullish(children) && (_jsxs(View, { style: desktop && icon != null && styles.content, children: [_jsx(Space, { height: 12 }), isText(children) ? _jsx(LakeText, { color: color, children: children }) : children] }))] }));
94
+ ], children: [_jsxs(Box, { direction: "row", alignItems: "center", children: [icon != null ? (_jsxs(_Fragment, { children: [_jsx(Icon, { name: icon, color: color, size: 20 }), _jsx(Space, { width: 20 })] })) : null, _jsxs(View, { style: commonStyles.fill, children: [_jsxs(Box, { direction: "row", justifyContent: "spaceBetween", children: [_jsx(LakeText, { color: color, variant: "medium", children: title }), tag && _jsx(Tag, { color: translateColorTag(), children: tag })] }), isNotNullishOrEmpty(subtitle) && _jsx(LakeText, { color: color, children: subtitle })] }), isNotNullish(callToAction) && _jsx(View, { style: styles.callToAction, children: callToAction })] }), isNotNullish(children) && (_jsxs(View, { style: desktop && icon != null && styles.content, children: [_jsx(Space, { height: 12 }), isText(children) ? _jsx(LakeText, { color: color, children: children }) : children] }))] }));
94
95
  };
@@ -22,9 +22,10 @@ type Props = {
22
22
  disabled?: boolean;
23
23
  };
24
24
  export declare const LakeTooltip: ({ ref, content, children, ...rest }: Props) => string | number | bigint | boolean | import("react").JSX.Element | Iterable<ReactNode> | Promise<string | number | bigint | boolean | Iterable<ReactNode> | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | import("react").ReactPortal | null | undefined> | null | undefined;
25
- export declare const InformationTooltip: ({ ref, text, icon, iconSize, }: {
25
+ export declare const InformationTooltip: ({ ref, text, placement, icon, iconSize, }: {
26
26
  ref?: Ref<TooltipRef>;
27
27
  text: string;
28
+ placement?: Props["placement"];
28
29
  icon?: IconName;
29
30
  iconSize?: number;
30
31
  }) => import("react").JSX.Element;
@@ -53,7 +53,7 @@ export const LakeTooltip = ({ ref, content, children, ...rest }) => {
53
53
  const Tooltip = memo(({ ref, children, content, describedBy, matchReferenceWidth = false, hideArrow = false, onHide, onShow, placement, width, maxWidth, togglableOnFocus = false, containerStyle, disabled = false, }) => {
54
54
  const timeoutRef = useRef(0);
55
55
  const [visible, setVisible] = useState(false);
56
- const { referenceRef, position } = useContextualLayer({
56
+ const { referenceRef, contentRef, position } = useContextualLayer({
57
57
  placement,
58
58
  visible,
59
59
  matchReferenceWidth,
@@ -113,7 +113,7 @@ const Tooltip = memo(({ ref, children, content, describedBy, matchReferenceWidth
113
113
  useEffect(() => {
114
114
  return () => clearTimeout(timeoutRef.current);
115
115
  }, []);
116
- return (_jsxs(_Fragment, { children: [_jsx(View, { ref: referenceRef, style: containerStyle, children: children }), isNotNullish(rootElement) && position.isSome() && !disabled && (_jsx(Portal, { container: rootElement, children: _jsx(View, { style: [position.get().rootStyle, styles.root], children: _jsxs(View, { role: "tooltip", "aria-describedby": describedBy, style: [styles.base, position.get().style], children: [position.get().verticalPosition === "bottom" ? (_jsx(Box, { direction: "row", justifyContent: match(position.get().horizontalPosition)
116
+ return (_jsxs(_Fragment, { children: [_jsx(View, { ref: referenceRef, style: containerStyle, children: children }), isNotNullish(rootElement) && position.isSome() && !disabled && (_jsx(Portal, { container: rootElement, children: _jsx(View, { style: [position.get().rootStyle, styles.root], children: _jsxs(View, { ref: contentRef, role: "tooltip", "aria-describedby": describedBy, style: [styles.base, position.get().style], children: [position.get().verticalPosition === "bottom" ? (_jsx(Box, { direction: "row", justifyContent: match(position.get().horizontalPosition)
117
117
  .with("left", () => "start")
118
118
  .with("center", () => "center")
119
119
  .with("right", () => "end")
@@ -123,4 +123,4 @@ const Tooltip = memo(({ ref, children, content, describedBy, matchReferenceWidth
123
123
  .with("right", () => "end")
124
124
  .exhaustive(), style: [styles.arrowBar, styles.arrowBarBottom], children: _jsx(Svg, { width: 16, height: 8, viewBox: "0 0 16 8", children: _jsx(Polygon, { points: "8 8 16 0 0 0", fill: hideArrow ? "transparent" : colors.gray[900] }) }) })) : null] }) }) }))] }));
125
125
  });
126
- export const InformationTooltip = ({ ref, text, icon = "info-regular", iconSize = 24, }) => (_jsx(LakeTooltip, { ref: ref, describedBy: "copy", placement: "center", togglableOnFocus: true, width: 300, content: text, children: _jsx(View, { style: styles.info, children: _jsx(Icon, { name: icon, size: iconSize, color: colors.gray[900] }) }) }));
126
+ export const InformationTooltip = ({ ref, text, placement = "center", icon = "info-regular", iconSize = 24, }) => (_jsx(LakeTooltip, { ref: ref, describedBy: "copy", placement: placement, togglableOnFocus: true, width: 300, content: text, children: _jsx(View, { style: styles.info, children: _jsx(Icon, { name: icon, size: iconSize, color: colors.gray[900] }) }) }));
@@ -3,8 +3,9 @@ import { View } from "react-native";
3
3
  type Props = {
4
4
  ref?: Ref<View>;
5
5
  value: boolean;
6
+ labelledBy?: string;
6
7
  onValueChange?: (value: boolean) => void;
7
8
  disabled?: boolean;
8
9
  };
9
- export declare const Switch: import("react").MemoExoticComponent<({ ref, value, disabled, onValueChange }: Props) => import("react").JSX.Element>;
10
+ export declare const Switch: import("react").MemoExoticComponent<({ ref, value, labelledBy, disabled, onValueChange }: Props) => import("react").JSX.Element>;
10
11
  export {};
@@ -61,4 +61,4 @@ const styles = StyleSheet.create({
61
61
  transitionDuration: "250ms",
62
62
  },
63
63
  });
64
- export const Switch = memo(({ ref, value, disabled = false, onValueChange }) => (_jsx(Pressable, { ref: ref, role: "switch", "aria-checked": value, disabled: disabled, onPress: () => onValueChange === null || onValueChange === void 0 ? void 0 : onValueChange(!value), children: ({ hovered }) => (_jsxs(_Fragment, { children: [_jsx(View, { style: [styles.shadow, hovered && styles.opaque] }), _jsx(View, { style: [styles.base, value && styles.active, disabled && styles.disabled], children: _jsx(View, { style: [styles.button, value && styles.buttonActive], children: _jsx(Icon, { color: colors.positive[400], name: "checkmark-filled", size: 10, style: [styles.icon, value && styles.opaque] }) }) })] })) })));
64
+ export const Switch = memo(({ ref, value, labelledBy, disabled = false, onValueChange }) => (_jsx(Pressable, { ref: ref, role: "switch", "aria-checked": value, "aria-labelledby": labelledBy, disabled: disabled, onPress: () => onValueChange === null || onValueChange === void 0 ? void 0 : onValueChange(!value), children: ({ hovered }) => (_jsxs(_Fragment, { children: [_jsx(View, { style: [styles.shadow, hovered && styles.opaque] }), _jsx(View, { style: [styles.base, value && styles.active, disabled && styles.disabled], children: _jsx(View, { style: [styles.button, value && styles.buttonActive], children: _jsx(Icon, { color: colors.positive[400], name: "checkmark-filled", size: 10, style: [styles.icon, value && styles.opaque] }) }) })] })) })));
@@ -33,6 +33,7 @@ export type ContextualLayerPosition = {
33
33
  };
34
34
  type ContextualLayerConfig = {
35
35
  referenceRef: RefObject<View | Text | null>;
36
+ contentRef: RefObject<View | Text | null>;
36
37
  position: Option<ContextualLayerPosition>;
37
38
  };
38
39
  export declare const useContextualLayer: ({ placement, verticalPlacement, visible, matchReferenceWidth, matchReferenceMinWidth, referenceRef: externalReferenceRef, }: Config) => ContextualLayerConfig;
@@ -1,10 +1,10 @@
1
1
  import { Option } from "@swan-io/boxed";
2
2
  import { useCallback, useEffect, useRef, useState } from "react";
3
3
  import { match } from "ts-pattern";
4
- const MAX_OFFSET_FOR_CENTER_PLACEMENT = 100;
5
4
  const HORIZONTAL_SAFETY_MARGIN = 16;
6
- export const useContextualLayer = ({ placement, verticalPlacement, visible, matchReferenceWidth = false, matchReferenceMinWidth = false, referenceRef: externalReferenceRef, }) => {
5
+ export const useContextualLayer = ({ placement = "left", verticalPlacement, visible, matchReferenceWidth = false, matchReferenceMinWidth = false, referenceRef: externalReferenceRef, }) => {
7
6
  const referenceRef = useRef(null);
7
+ const contentRef = useRef(null);
8
8
  const usedRef = externalReferenceRef !== null && externalReferenceRef !== void 0 ? externalReferenceRef : referenceRef;
9
9
  const [position, setPosition] = useState(Option.None());
10
10
  const getPosition = useCallback(() => {
@@ -21,12 +21,19 @@ export const useContextualLayer = ({ placement, verticalPlacement, visible, matc
21
21
  const availableSpaceAfter = viewportWidth - rect.right;
22
22
  const height = rect.bottom - rect.top;
23
23
  const width = rect.right - rect.left;
24
- const isCenteredEnough = Math.abs(availableSpaceBefore - availableSpaceAfter) < MAX_OFFSET_FOR_CENTER_PLACEMENT;
25
- const inferedPlacement = placement !== null && placement !== void 0 ? placement : (isCenteredEnough
26
- ? "center"
27
- : availableSpaceBefore > availableSpaceAfter
28
- ? "right"
29
- : "left");
24
+ const contentElement = contentRef.current;
25
+ // if contentRef isn't used in the component, we don't infer the placement depending on content size
26
+ const contentRect = contentElement != null ? contentElement.getBoundingClientRect() : { width: 0, height: 0 };
27
+ const contentWidth = contentRect.width;
28
+ const contentMidWidth = contentWidth / 2;
29
+ const isCenteredEnough = availableSpaceBefore > contentMidWidth && availableSpaceAfter > contentMidWidth;
30
+ const bestPlacement = availableSpaceBefore > availableSpaceAfter ? "right" : "left";
31
+ const inferedPlacement = match(placement)
32
+ .returnType()
33
+ .with("center", () => (isCenteredEnough ? "center" : bestPlacement))
34
+ .with("left", () => (availableSpaceBefore > contentWidth ? "left" : bestPlacement))
35
+ .with("right", () => (availableSpaceAfter > contentWidth ? "right" : bestPlacement))
36
+ .exhaustive();
30
37
  const openAbove = verticalPlacement === "above"
31
38
  ? true
32
39
  : verticalPlacement === "below"
@@ -93,6 +100,7 @@ export const useContextualLayer = ({ placement, verticalPlacement, visible, matc
93
100
  }, [visible, getPosition]);
94
101
  return {
95
102
  referenceRef: usedRef,
103
+ contentRef,
96
104
  position,
97
105
  };
98
106
  };
@@ -1,5 +1,5 @@
1
1
  export const sortBy = (array, keys) => {
2
- return [...array].sort((a, b) => {
2
+ return array.toSorted((a, b) => {
3
3
  let index = -1;
4
4
  while (++index < keys.length) {
5
5
  const key = keys[index];