Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
fdc4e0d
(Icon): update icon types
msmx-mnakagawa Jun 6, 2025
189b004
(Icon): update markups and classnames
msmx-mnakagawa Jun 6, 2025
b05c36d
(Button): update markups and classnames
msmx-mnakagawa Jun 6, 2025
df1c712
(Button): add new button types
msmx-mnakagawa Jun 13, 2025
4e27579
(Button): reflect `iconSize` to `ButtonIcon`
msmx-mnakagawa Jun 13, 2025
096208e
(Button): remove unused classnames
msmx-mnakagawa Jun 13, 2025
90902c8
(Icon): update an interface about `container`
msmx-mnakagawa Jun 13, 2025
b78c6a6
(Icon): improve a category icon classname
msmx-mnakagawa Jun 13, 2025
73109a9
(Icon): add `title`, `currentColor`, and `flip` props
msmx-mnakagawa Jun 13, 2025
1d538cc
(Icon, Button): remove unnecessary `eslint-disable-` comments
msmx-mnakagawa Jul 2, 2025
9ea3e18
(Icon): update a naming convention
msmx-mnakagawa Jul 2, 2025
87ab6e1
Revert "(Icon): update icon types"
msmx-mnakagawa Jul 3, 2025
288d955
(Icon, Button): unify `SvgButtonIcon` with `SvgIcon`
msmx-mnakagawa Jul 3, 2025
ef99975
(Icon): reflect interface update to stories
msmx-mnakagawa Jul 3, 2025
5a141a3
Merge branch 'support-slds-2' into support-slds-2-icon-button
msmx-mnakagawa Jul 3, 2025
49a0628
(Button): change the order of properties
msmx-mnakagawa Jul 7, 2025
da113a2
(Icon): replace `circleContainer` with `container`
msmx-mnakagawa Jul 7, 2025
8235e6f
(Icon): make `textColor` and `currentColor` mutually exclusive
msmx-mnakagawa Jul 7, 2025
9bef3e3
(Icon): add the `Current Color` story
msmx-mnakagawa Jul 7, 2025
f57f21b
(Icon): unify the `label` into the `title`
msmx-mnakagawa Jul 7, 2025
893d631
remove duplicated type definitions
msmx-mnakagawa Jul 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions src/scripts/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import React, { FC, ReactNode, ButtonHTMLAttributes, Ref, useRef } from 'react';
import classnames from 'classnames';
import { Icon } from './Icon';
import { SvgButtonIcon, IconCategory } from './Icon';
import { Spinner } from './Spinner';
import { useEventCallback, useMergeRefs } from './hooks';

export type ButtonType =
| 'neutral'
| 'brand'
| 'outline-brand'
| 'destructive'
| 'text-destructive'
| 'success'
| 'inverse'
| 'icon'
| 'icon-bare'
Expand All @@ -31,6 +34,7 @@ export type ButtonIconMoreSize = 'x-small' | 'small' | 'medium' | 'large';
*/
export type ButtonIconProps = {
className?: string;
category?: IconCategory;
icon: string;
align?: ButtonIconAlign;
size?: ButtonIconSize;
Expand All @@ -42,12 +46,12 @@ export type ButtonIconProps = {
*
*/
export const ButtonIcon: FC<ButtonIconProps> = ({
icon,
align,
size,
inverse,
className,
style,
icon,
category = 'utility',
size,
...props
}) => {
const alignClassName =
Expand All @@ -56,19 +60,22 @@ export const ButtonIcon: FC<ButtonIconProps> = ({
: null;
const sizeClassName =
size && ICON_SIZES.indexOf(size) >= 0 ? `slds-button__icon_${size}` : null;
const inverseClassName = inverse ? 'slds-button__icon_inverse' : null;
const iconClassNames = classnames(
'slds-button__icon',
alignClassName,
sizeClassName,
inverseClassName,
className
);

if (icon.indexOf(':') > 0) {
[category, icon] = icon.split(':') as [IconCategory, string];
}

return (
<Icon
<SvgButtonIcon
className={iconClassNames}
icon={icon}
textColor={null}
category={category}
pointerEvents='none'
style={style}
{...props}
Expand Down Expand Up @@ -118,6 +125,7 @@ export const Button: FC<ButtonProps> = (props) => {
buttonRef: buttonRef_,
iconMoreSize: iconMoreSize_,
onClick: onClick_,
tabIndex,
...rprops
} = props;

Expand All @@ -135,19 +143,25 @@ export const Button: FC<ButtonProps> = (props) => {
onClick_?.(e);
});

const content = children || label;
const isIconOnly = type && /^icon-/.test(type) && icon && !content;

const typeClassName = type ? `slds-button_${type}` : null;
const btnClassNames = classnames(className, 'slds-button', typeClassName, {
'slds-is-selected': selected,
['slds-button_icon']: /^icon-/.test(type ?? ''),
[`slds-button_icon-${size ?? ''}`]:
/^(x-small|small)$/.test(size ?? '') && /^icon-/.test(type ?? ''),
});

const buttonContent = (
return (
// eslint-disable-next-line react/button-has-type
<button
ref={buttonRef}
className={btnClassNames}
type={htmlType}
title={isIconOnly || alt ? alt ?? icon : undefined}
tabIndex={tabIndex ?? -1}
{...rprops}
onClick={onClick}
>
Expand All @@ -159,7 +173,7 @@ export const Button: FC<ButtonProps> = (props) => {
inverse={inverse}
/>
) : undefined}
{children || label}
{content}
{icon && iconAlign === 'right' ? (
<ButtonIcon
icon={icon}
Expand All @@ -171,22 +185,10 @@ export const Button: FC<ButtonProps> = (props) => {
{iconMore ? (
<ButtonIcon icon={iconMore} align='right' size={iconMoreSize} />
) : undefined}
{alt ? <span className='slds-assistive-text'>{alt}</span> : undefined}
{isIconOnly || alt ? (
<span className='slds-assistive-text'>{alt ?? icon}</span>
) : undefined}
{loading ? <Spinner /> : undefined}
</button>
);

if (props.tabIndex != null) {
return (
<span
className='react-slds-button-focus-wrapper'
style={{ outline: 0 }}
tabIndex={-1}
>
{buttonContent}
</span>
);
}

return buttonContent;
};
Loading