Skip to content

Commit 288d955

Browse files
(Icon, Button): unify SvgButtonIcon with SvgIcon
1 parent 87ab6e1 commit 288d955

File tree

2 files changed

+22
-46
lines changed

2 files changed

+22
-46
lines changed

src/scripts/Button.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { FC, ReactNode, ButtonHTMLAttributes, Ref, useRef } from 'react';
22
import classnames from 'classnames';
3-
import { SvgButtonIcon, IconCategory } from './Icon';
3+
import { SvgIcon, IconCategory } from './Icon';
44
import { Spinner } from './Spinner';
55
import { useEventCallback, useMergeRefs } from './hooks';
66

@@ -72,7 +72,7 @@ export const ButtonIcon: FC<ButtonIconProps> = ({
7272
}
7373

7474
return (
75-
<SvgButtonIcon
75+
<SvgIcon
7676
className={iconClassNames}
7777
icon={icon}
7878
category={category}

src/scripts/Icon.tsx

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -180,41 +180,43 @@ export type IconProps = {
180180
*
181181
*/
182182
type SvgIconProps = IconProps & {
183-
iconColor: string | null;
183+
iconColor?: string | null;
184184
};
185185

186186
/**
187187
*
188188
*/
189-
const SvgIcon = forwardRef(
189+
export const SvgIcon = forwardRef(
190190
(props: SvgIconProps, ref: ForwardedRef<SVGSVGElement | null>) => {
191191
const {
192192
className = '',
193193
category: category_ = 'utility',
194194
icon: icon_,
195-
iconColor,
195+
iconColor = null,
196196
size = '',
197197
align,
198198
textColor = 'default',
199199
style,
200200
...rprops
201201
} = props;
202202
const { assetRoot = getAssetRoot() } = useContext(ComponentSettingsContext);
203-
const iconClassNames = classnames(
204-
'react-slds-icon',
205-
{
206-
'slds-icon': !/slds-button__icon/.test(className),
207-
[`slds-icon_${size}`]: /^(xx-small|x-small|small|medium|large)$/.test(
208-
size
209-
),
210-
[`slds-icon-text-${textColor ?? 'default'}`]:
211-
/^(default|success|warning|error|light)$/.test(textColor ?? '') &&
212-
!iconColor,
213-
'slds-m-left_x-small': align === 'right',
214-
'slds-m-right_x-small': align === 'left',
215-
},
216-
className
217-
);
203+
204+
const inIcon = !/slds-button__icon/.test(className);
205+
const iconOnlyClassNames = classnames('react-slds-icon', 'slds-icon', {
206+
[`slds-icon_${size}`]: /^(xx-small|x-small|small|medium|large)$/.test(
207+
size
208+
),
209+
[`slds-icon-text-${textColor ?? 'default'}`]:
210+
/^(default|success|warning|error|light)$/.test(textColor ?? '') &&
211+
!iconColor,
212+
'slds-m-left_x-small': align === 'right',
213+
'slds-m-right_x-small': align === 'left',
214+
});
215+
216+
const iconClassNames = classnames(className, {
217+
[iconOnlyClassNames]: inIcon,
218+
});
219+
218220
// icon and category prop should not include chars other than alphanumerics, underscore, and hyphen
219221
const icon = (icon_ ?? '').replace(/[^\w-]/g, '');
220222
const category = (category_ ?? '').replace(/[^\w-]/g, '');
@@ -233,32 +235,6 @@ const SvgIcon = forwardRef(
233235
}
234236
);
235237

236-
export const SvgButtonIcon = (
237-
props: {
238-
className?: string;
239-
category?: IconCategory;
240-
icon: string;
241-
} & SVGAttributes<SVGElement>
242-
) => {
243-
const {
244-
className = '',
245-
category: category_ = 'utility',
246-
icon: icon_,
247-
style,
248-
...rprops
249-
} = props;
250-
const { assetRoot = getAssetRoot() } = useContext(ComponentSettingsContext);
251-
252-
// icon and category prop should not include chars other than alphanumerics, underscore, and hyphen
253-
const icon = (icon_ ?? '').replace(/[^\w-]/g, '');
254-
const category = (category_ ?? '').replace(/[^\w-]/g, '');
255-
const iconUrl = `${assetRoot}/icons/${category}-sprite/svg/symbols.svg#${icon}`;
256-
return (
257-
<svg className={className} aria-hidden style={style} {...rprops}>
258-
<use xlinkHref={iconUrl} />
259-
</svg>
260-
);
261-
};
262238
/**
263239
*
264240
*/

0 commit comments

Comments
 (0)