Skip to content

Commit

Permalink
Merge pull request #195 from element-hq/florianduros/icon-button
Browse files Browse the repository at this point in the history
Add label and destructive props to `IconButton`
  • Loading branch information
florianduros authored Jun 24, 2024
2 parents 3b82efe + 81c390c commit 2742ad1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/components/Button/IconButton/IconButton.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,14 @@ limitations under the License.
inset-block-start: 0;
inset-inline-end: 0;
}

@media (hover) {
.icon-button:not([aria-disabled="true"]).destructive:hover {
background: var(--cpd-color-bg-critical-subtle);
outline: 1px solid var(--cpd-color-border-critical-subtle);
}
}

.icon-button:not([aria-disabled="true"]).destructive > * {
color: var(--cpd-color-icon-critical-primary);
}
12 changes: 12 additions & 0 deletions src/components/Button/IconButton/IconButton.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,15 @@ export const WithCriticalIndicator = {
indicator: "critical",
},
};

export const WithLabel = {
args: {
label: "label",
},
};

export const Destructive = {
args: {
destructive: "true",
},
};
32 changes: 28 additions & 4 deletions src/components/Button/IconButton/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import styles from "./IconButton.module.css";
import { UnstyledButton } from "../UnstyledButton";
import { UnstyledButtonPropsFor } from "../UnstyledButton";
import { IndicatorIcon } from "../../Icon/IndicatorIcon/IndicatorIcon";
import { Tooltip } from "../../Tooltip/Tooltip";

type IconButtonProps = UnstyledButtonPropsFor<"button"> &
JSX.IntrinsicElements["button"] & {
Expand All @@ -39,11 +40,19 @@ type IconButtonProps = UnstyledButtonPropsFor<"button"> &
* As in IndicatorIcon
*/
indicator?: "default" | "success" | "critical";

/**
* Whether the button is interactable
*/
disabled?: boolean;
/**
* Whether this button triggers a destructive action.
* @default false
*/
destructive?: boolean;
/**
* Optional tooltip for the button
*/
tooltip?: string;
};

/**
Expand All @@ -53,11 +62,24 @@ export const IconButton = forwardRef<
HTMLButtonElement,
PropsWithChildren<IconButtonProps>
>(function IconButton(
{ children, className, indicator, size = "32px", style, disabled, ...props },
{
children,
className,
indicator,
size = "32px",
style,
disabled,
destructive,
tooltip,
...props
},
ref,
) {
const classes = classnames(styles["icon-button"], className);
return (
const classes = classnames(styles["icon-button"], className, {
[styles.destructive]: destructive,
});

const button = (
<UnstyledButton
as="button"
ref={ref}
Expand All @@ -80,4 +102,6 @@ export const IconButton = forwardRef<
</IndicatorIcon>
</UnstyledButton>
);

return tooltip ? <Tooltip label={tooltip}>{button}</Tooltip> : button;
});

0 comments on commit 2742ad1

Please sign in to comment.