@homebound/beam 2.152.0 → 2.153.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.
@@ -11,8 +11,12 @@ const getInteractiveElement_1 = require("../utils/getInteractiveElement");
11
11
  const useTestIds_1 = require("../utils/useTestIds");
12
12
  function Button(props) {
13
13
  const { onClick: onPress, disabled, endAdornment, menuTriggerProps, tooltip, openInNew, download, contrast = false, ...otherProps } = props;
14
- const showExternalLinkIcon = (typeof onPress === "string" && (0, utils_1.isAbsoluteUrl)(onPress)) || openInNew;
15
- const isDisabled = !!disabled;
14
+ const asLink = typeof onPress === "string";
15
+ const showExternalLinkIcon = (asLink && (0, utils_1.isAbsoluteUrl)(onPress)) || openInNew;
16
+ const [isDisabled, setIsDisabled] = (0, react_1.useState)(!!disabled);
17
+ (0, react_1.useEffect)(() => {
18
+ setIsDisabled(!!disabled);
19
+ }, [disabled]);
16
20
  const ariaProps = { onPress, isDisabled, ...otherProps, ...menuTriggerProps };
17
21
  const { label,
18
22
  // Default the icon based on other properties.
@@ -21,8 +25,17 @@ function Button(props) {
21
25
  const tid = (0, useTestIds_1.useTestIds)(props, label);
22
26
  const { buttonProps, isPressed } = (0, react_aria_1.useButton)({
23
27
  ...ariaProps,
24
- onPress: typeof onPress === "string" ? utils_1.noop : onPress,
25
- elementType: typeof onPress === "string" ? "a" : "button",
28
+ onPress: asLink
29
+ ? utils_1.noop
30
+ : (e) => {
31
+ const result = onPress(e);
32
+ if (isPromise(result)) {
33
+ setIsDisabled(true);
34
+ result.finally(() => setIsDisabled(false));
35
+ }
36
+ return result;
37
+ },
38
+ elementType: asLink ? "a" : "button",
26
39
  }, ref);
27
40
  const { isFocusVisible, focusProps } = (0, react_aria_1.useFocusRing)(ariaProps);
28
41
  const { hoverProps, isHovered } = (0, react_aria_1.useHover)(ariaProps);
@@ -38,7 +51,7 @@ function Button(props) {
38
51
  ...buttonProps,
39
52
  ...focusProps,
40
53
  ...hoverProps,
41
- className: typeof onPress === "string" ? components_1.navLink : undefined,
54
+ className: asLink ? components_1.navLink : undefined,
42
55
  css: {
43
56
  ...Css_1.Css.buttonBase.tt("inherit").$,
44
57
  ...baseStyles,
@@ -110,3 +123,6 @@ const iconStyles = {
110
123
  md: Css_1.Css.mr1.$,
111
124
  lg: Css_1.Css.mrPx(10).$,
112
125
  };
126
+ function isPromise(obj) {
127
+ return typeof obj === "object" && "then" in obj && typeof obj.then === "function";
128
+ }
@@ -15,7 +15,7 @@ export interface BeamButtonProps {
15
15
  */
16
16
  disabled?: boolean | ReactNode;
17
17
  /** If function, then it is the handler that is called when the press is released over the target. Otherwise if string, it is the URL path for the link */
18
- onClick: ((e: PressEvent) => void) | string;
18
+ onClick: ((e: PressEvent) => void) | ((e: PressEvent) => Promise<void>) | string;
19
19
  /** Text to be shown via a tooltip when the user hovers over the button */
20
20
  tooltip?: ReactNode;
21
21
  /** Whether to open link in a new tab. This only effects the element if the `onClick` is a `string`/URL. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homebound/beam",
3
- "version": "2.152.0",
3
+ "version": "2.153.0",
4
4
  "author": "Homebound",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",